Общий форумDoes any one have a faster code of python than this p=int(input()) plist=set() for i in range (p): a=int(input()) plist.add(a) s=int(input()) slist=[] count=0 for i in range (s): a=int(input()) if a in plist: count+=1 print (count) I've no idea how to make it faster. Got stuck myself. I TLE on test 1. What is the test 1 Thank you. Oh, there are no such cases. There is at least one healer and one attacker. Fixed in statement now. алгоритм сделал и работает, но не проходит проверку, говрит что напечатал слишком много букафф,,, мне кажется я неправильно читаю К, не знаю как перепрыгнуть через те решетки :( int K; char ch; scanf("%s%d",&ch,&K); Не могу понять что не так с 19-ым тестом... Даёт "Wrong anser" хотя я проверил каждую мелочь... кто может помочь? I had this problem too, because of a logic problem... Test: 9 2 0 0 0 0 2 0 2 0 6 0 6 0 8 0 10 6 1 2 3 4 5 6 11 3 7 8 9 Ans: 11 right 11 left 12 right 15 left 14 left 12 left 13 right 13 left 14 right Is it guaranteed that a1+a2+...+ak >= n? #include <iostream> #include <string> using namespace std; int main() { int a[1000]; int n,i,k; char Name; cin>>n; string B; k=0; for (i=0;i<n;i++){ cin>>B; Name=B[0]; if(Name=='A' or Name=='P' or Name=='O' or Name=='R') {a[i]=1;} else {if(Name=='B' or Name=='M' or Name=='S') {a[i]=2;} else {a[i]=3;}; } } k=a[0]-1;
for (i=1;i<n;i++){ if (a[i-i]<a[i]){ k=k+a[i]-a[i-1];} else { k=k+a[i-1]-a[i];}
} cout<<k; return 0;
} Thanks for the site problem, a very interesting and exciting)) Passed with PD + Segment tree. Edited by author 16.06.2012 05:41 Edited by author 16.06.2012 05:42 Yes, thanks for the problem. My solution is one sort, DP without recursion and with binary search. AC since 19th try :) So my algo is to have a vector of pairs(value and index), sort them(used usual <algorithm> sort), and then binary searched each request - first by value, and if value was correct, by index. The program ACed, on G++11 compiler with 0.921 time. when I removed the inclusion of <cstdlib>, it ACed with 0.859(and same on G++14). But funny thing is, on Visual C++ 2010, it ACed with 0.593s time, which is astounding(to me at least) because the difference is quite noticeable(25% drop). So I guess if you are getting TLE, it's worth a try to run it with Visual C++ - you might get a pass this way. Edited by author 17.01.2015 15:10 My Accepted solution #6069000 will fail if you add all permutations of test 32 as separate tests. Can any one see what's the issue? static void Main(string[] args) { string input = Console.In.ReadToEnd(); var valid = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; var sb = new StringBuilder(); var numbers = new Stack<string>(); for (int i = 0; i < input.Length; i++ ) { if(valid.Contains(input[i])) { sb.Append(input[i]); } else if(sb.Length > 0) { numbers.Push(sb.ToString()); sb.Clear(); } } NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo; foreach(var n in numbers) { double sqrt = Math.Sqrt(double.Parse(n, nfi)); Console.WriteLine(string.Format(nfi, "{0:F4}", sqrt)); } } . Edited by author 16.01.2015 20:50 If the input stream ends with a digit, your code will lose it. For debugging - replace Console.In.ReadToEnd with Console.ReadLine and try to read string with one symbol "1" Hello everyone. I always receive time limit on Test №21. Could you give any hints? Maybe this task requires special data structure (I try to represent tree as list of edges or adjacency list) or there is interesting algorythm? Thanks for reading. Not update connected vertexes for vertexes with amount of connections more than Sqrt(N). In this case sum for vertex will be current sum + delta for connected vertexes with size more than Sqrt(N). I have the same problem with TLE #21 and I can't understand your hint: what's that 'delta', how can I track/calculate it? I realize that the bottleneck is in vertices with high amount of linked edges but I can't understand how to avoid them - I still need update all vertices, otherwise I loose data for next steps. yes, you're right about data structure. I used binary indexed tree to solve the problem. Overall complexity is O(n + m * logn). So, you actually preprocess data in O(n) time and for each query you spent O(logn) time, which perfectly fits time constraints of the problem * Clarification in problem statements: For any pair of crossroads no two segments of the same category connect these crossroads. * Tests not satisfing this condition are corrected. * The first test is changed to the sample from the problem statements. * New time limit is 2.5 seconds (old time limit was 3 seconds). * New memory limit is 64 MB (old memory limit was 16 MB). * New tests are added All solutions are rejudged. More than 100 authors lost AC during this rejudge. * Clarification in problem statements: For any pair of crossroads no two segments of the same category connect these crossroads. Sample test 0 2 0 0 2 1 is this true? * Clarification in problem statements: For any pair of crossroads no two segments of the same category connect these crossroads. Sample test 0 2 0 0 2 1 is this true? Yes, of course! Give me some test. Or give me test 10! Edited by author 14.02.2007 18:59 Is there a class or sth that can both read integers and single chars? Somebody please give me some example code. Thanks! I write this: import java.io.*; public class Main { static BufferedReader in; //........ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(in.readLine()); //..... for(int i=1; i<n+1; i++){ for(int j=1; j<n+1; j++) { lab[i][j] = (char)in.read(); while(lab[i][j]!='#' && lab[i][j]!='.') lab[i][j] = (char)in.read(); //in win used two bytes end of line, in *NIX 1 byte } in.read(); } //..... import java.util.*; import java.io.*; public class Main{ public static void main(String[] argv) throws IOException{ new Main().run(); } PrintWriter pw; Scanner sc;
public void run() throws IOException{ boolean oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); sc = new Scanner(reader); pw = new PrintWriter(writer); int n = sc.nextInt(); int arr[][] = new int[n+2][n+2]; for(int i=1;i<=n;i++){ String str = sc.next(); for(int j=0;j<n;j++) if(str.charAt(j)=='.') arr[i][j+1] = 1; } //... pw.close(); }
} The best template for Java: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer tok = null; String readToken() throws IOException { // reads the token; to read a full line use in.readLine() while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); // sometimes it's better to use nextToken(String) method here } int readInt() throws IOException { // write readDouble(), readLong(), readBigInteger() methods if necessary return Integer.parseInt(readToken()); } In addition to previouse answer char[] arr = readString().toCharArray(); //reads string and creates an array of characters. It works not so fast, but for small input, like this, it would be the quickest way to write a code. If you want to read for 1 char manual try this: char readChar() throws IOException{ int x = 0; while ((x = in.read()) == '\n' || x == '\r' || x == ' '){ } return (char)x; } Please help me .this code failed at test case 5 #include <stdio.h> int main() { int n,k; scanf ("%d\n%d" ,&n,&k); int l, pos; l = n % 2 ;
if ( l != 0 || n < 1 || k < 1 || k > n || n > 50 ) { printf("invalid input"); } else { if (n != 2 ) { if ( k <= n/2 ) { pos = n/2 - k ; printf("%d", pos); } else { pos = k - ((n/2) + 1 ); printf("%d", pos); } } } if ( n == 2 ) { pos = 0; printf("%d", pos); } return 0; }
used: ... for(i=1;i<=S;i++) for(j=i;j<=S;j++) ... But the result is 'Time limit exceeded'. Please help, why 'Time limit exceeded'? don't use brute force... O(n^2) n=30000 ===>>> time limit) hint: you can don't use N in your solution brute force (with some optimizations) works fine Hi, guys! Can't move throw #11 test. Need help. I just have build tree, and in every step removing smallest leaf. At last, I count sum of left leaf's weight and print it. But, algorithm don't pass at #11 test. Thanks 7 3 1 2 12 1 3 1 2 4 30 2 5 30 3 6 30 3 7 30 Correct answer is 72, your answer 61 what is test #3; 3 or what? don't you think that when n>0 answer is (n*(n+1))/2? when n<0 answer is (-((n*(n-1))/2-1))? when n=0 or n=1 answer is 1 ? Edited by author 14.01.2015 14:52 please change the wording of the statement: "there should be at most k Rocks of pairwise different colors among the Rocks taken from the Cave" would be much better read if it is changed to "there should be at most k Rocks of different colors among the Rocks taken from the Cave." in other words, the word 'pairwise' should be removed to make the statement clear. Edited by author 14.01.2015 05:20 Edited by author 14.01.2015 05:21 |
|