Общий форумI walked through all topics in discussion, grabbed every test, but still got WA7. I had several mistakes in my solution. First of all, in some versions I forgot to make fixed format of output and forgot to cast my answer to integer type, therefore the solution printed the answer in scientific notation. Test: 25 7217 6651 7358 2764 7034 5638 2431 6831 6550 2019 2334 1524 3375 4802 5960 4579 7078 1798 3176 7335 5872 3950 179 7351 9800 3982 2080 897 9345 2389 6470 6556 8420 9316 1671 7557 922 2613 9464 6735 1865 8925 6701 3365 6519 6665 9458 1535 8792 4383 Answer: 1495422 In some solutions there were rounding issues, I rounded down while should have rounded up. Still I disagree that sometimes one should use floor(ans) instead of int(round(ans)), the latter always works fine in this problem. Test: 3 4826 9656 137 7424 8340 2686 Answer: 22472 In some attempts I accidentally used = instead of ==, this test detected this perfectly. Test: 4 0 0 2 0 2 2 0 2 Answer: 14 Edited by author 01.09.2022 22:29 ITS REALLY HARD I WORK SO HARD FOR THIS PROBLEM TO DO ITS REALLY FAST, U HAVE 1MB MEMORY input THIS IS ABOUT 600-700k symbols, U NEED TO DO ITS Very VERY VERY VERYVERY VERy fAST AND THIS VERYV VEryV eVEYR Very HARD U NEED TO READ THIS NEVER NEVER NEVER USE CIN USE c=getchar() where c - char. And IF U WANT TO STOP INPUT JUST int WHILE in the conditions of the while loop, put restrictions on the symbol, that is, there are all charms from A to Z and something else. AND THEN YOU NEED A REALLY FAST ALGORITHM I rewrote mine six times, I can't understand it myself, it's very difficult, I only remember that char is int, and int is a deadline, and diffirintly THE ALGORITHM is SLOW Please remember that input can contain numbers with more than one digit. My WA was due to this, and I don't think anybody will be this silly. But still posting! :) Edited by author 29.08.2022 21:23 Edited by author 29.08.2022 21:23 delete iostream and using namespace std. all cin cout replace on scanf and pritf write in start of code: #define CRT_SECURE_NO_WARNINGS and #include<cstdio> and behind your vectors,pair and queue write std:: do vector WITHOUT ANY INTS, ALL BOOL DONT USE PUSH_BACK its really slow, just write size of vector Example: >std::vector<std::vector<bool>> a(n); >std::vector<bool> b(m); and call with a[i][j] or something else; If you look at other aspects of the task, then EASY BFS String can have size 1000, max size is not 999. I don't know, what is the maximum possible sum of digits, but in these tests it is 31. Simple, badly optimized, recursion passes quickly. Как узнать, когда игрок сделал страйк или спэр, если количество бросков во фрейме не задано? My code: #include <stdio.h> #include <stdlib.h> int main() { int n,i,len,max=0,min=0; int ball[10]; len=sizeof(ball)/sizeof(int); for(i=0;i<len;i++){ scanf("%d",&ball[i]); min+=ball[i]; } for(i=0;i<len;i++){ n=ball[i]; max+=n; if((n==10)&& (ball[i+1]==10) && (i+1<len)){ max+=ball[i+1]; max+=ball[i+2]; } else if((n==10) && (ball[i+1]<10) && (i+1<len)) max+=ball[i+1]; else if((i+1==len) && (n==10))max+=20; } printf("%d %d",min,max); return 0; } Wrong answer in test №17 нужной найти максимум и минимум I'm tried to solve it many times and this problem dont need strong. There is no need to count a lot, compare numbers, check so that all the money can be divided, all you need is to take all the powers of three and check for each that in sum with n it also gives the sum of the powers of three. That is, t is the sum of degrees, if t+n too, then output the answer immediately! Be careful, the input numbers are float, though sample and all tests here have only integer numbers. It appears first at <= 6th test. wa4 have test n=1 and some m if u use dp and u write in your massive some int, when u write, u delete int and create new ints. But if u have n=1, u now answer for every for n=1 and when u use dp its write 0, it enters zero instead of the answer you wrote down, although there should be one in some cells
Just curious did anyone managed to pass the time limit using binary search + rolling hash or the test data are specially designed to be solved in time only by an efficient suffix tree/array implementation? I tried an O(N.log N) suffix array (although finding the length of a match was O(N.log N.log N)) and it was too slow, although it ran in about 1.3s on my machine. Test 10 was very hard for binary+hash solutions,but double or triple hash would pass it. These solutions are correct, because the test set was really hard. The solutions of the authors are based on suffix trees (Nikita Rybak, Pascal) and suffix arrays (Ilya Grebnov, Java) and pass the TL easily. Thank you Dmitry! It seems that pretty soon Ukkonen and/or efficient suffix array implementations will become a must in real time contests. I would like to congratulate the autors for the excellent problem set! Single hash-based solution got AC too I got AC with optimized single hash-based solution too. But if I wrote double hash based solution I wouldn't have +9. I got TLE on #28, with single hash solution. The simplest solution with single hash gets AC in 0.468 Some statistics: 0. Suffix Automaton get AC in C++ 0.125 time and 22 320 KB Memory, author is [SPbSU ITMO] WiNGeR 1. Suffix Automaton get AC in C++ 0.14c with 22220КБ Memory, author is Burunduk1 3. Suffix Tree gets AC in 0.14 time and 23 888 KB Memory, author is [SPbSU ITMO] WiNGeR 4. Suffix Array with O(N) implementation get AC in C++ 0.343c with 5756 КБ Memory, author is me 5. Single Hash get AC in C++ 0.39c with 4024 КБ Memory, author is me 6. Single Hash get AC in Pascal 0.5c with 6191 КБ Memory, author is me 7. Suffix Tree get AC in Pascal 0.656c with 16416 КБ Memory, author is Kit(Vologda SPU) 8. Suffix Array with O(NLogN) implementation get AC in C++ 0.765c with 2756КБ Memory, author is me 9. Suffix Array with O(NLogN) implementation get AC in Java 1.406c with 5503КБ Memory, author is me Edited by author 22.12.2006 15:35 Edited by author 25.12.2006 11:24 Edited by author 11.02.2007 13:44 Edited by author 11.02.2007 13:45 Edited by author 11.02.2007 13:45 I write Suffix Tree(Ukkonen Algorithm). AC - 1 sec. But I used 30mb memory. I tried to solve this problem using Suffix Tree (ukkonen95) and got MLE on test #9, what trick did you use to reduce the memory? Then I saw this discussion about Suffix Array, I tried Larsson-Sadakane algorithm and got WA on test #8. Then I tried Karkkainen-Sanders algorithm and got TLE on test #9. I haven't try to use Suffix Automaton yet. This problem is really hard. Anyone can give hints about reducing memory for the Suffix Tree? I tried to solve this problem using Suffix Tree (ukkonen95) and got MLE on test #9, what trick did you use to reduce the memory? No tricks, I have almost double reserve in memory. But I use hash working with edges, may be that's the matter. Test #8 is extremally small, so you can find similiar manually. #9 is just the first large test. Good luck! No tricks, I have almost double reserve in memory. But I use hash working with edges, may be that's the matter. Yes, I got it Accepted at last :) The trick is to use your own hash implementation rather than hash_map from STL! Did anybody solve this problem using STL? I just got AC with Suffix Array + LCP in 0.3xx secs. The implementation is cleaner than Suffix Tree and requires less space yet still run in O( N ) time. I'm curious with Burunduk1's Suffix Automaton, is there any online resources available to learn about Suffix Automaton? I really want to solve this problem using Suffix Automaton :) >This problem is really hard. Anyone can give hints about >reducing memory for the Suffix Tree? I also had MLE#9, after that I saved at node of suffix tree only used links to child And got AC. It reduced memory, but I my prog eat 30mb. Those who solved it using Suffix Trees: Did you built 2 Suffix Trees or just 1? What I did is to create 1 Suffix Tree with string: s1 + "$" + s2 + "#" where s1 and s2 is the first and second string in the input. After I built the Suffix Tree, I traversed it and find the deepest node which has child "$" and "#". Or is it better to create 2 Suffix Trees and compare the two trees? Or there is a way to build a Suffix Tree only for the first string and try to find the LCS with the second string using that Tree. Did you get AC using the first algorithm? second? or the last? Thanks I use hashing but my algorithm is O(n(logn)^2), is it a better way? Here is my algorithm: m = the result l = length(m) - Do a binary search on l Check whether there is a common substring length l: + Calculate and sort hash values of string 1 (into array f) + For each value in string 2, search it in f, check whether there is a match! What is double hash??? My single hash O(N*logN) prog. got AC within 0.671 sec. and 6,... MB memory. What I did is to create 1 Suffix Tree with string: s1 + "$" + s2 + "#" where s1 and s2 is the first and second string in the input. After I built the Suffix Tree, I traversed it and find the deepest node which has child "$" and "#". this is exactly what iam trying to do but i got stack overflow in test case 9, obviously the stach overflows when i traverse the tree wishing to find the deepest node which has the two terminator characters as children (or in the subtree below). I guess the tree is too deep, so how could u solve this part? Don't traverse the tree using recursion ;) Try using explicit array[100000] Don't traverse the tree using recursion ;) Try using explicit array[100000] Do u mean to use that array to bottom up traverse the tree? or u mean something else? Don't traverse the tree using recursion ;) Try using explicit array[100000] Do u mean to use that array to bottom up traverse the tree? or u mean something else? A recursion is using stack implicitly (stack memory) and it has limitation on the depth of the recursion for some compiler (or maybe by the stack memory configuration of the compiler). So, instead of using stack memory we can use an explicit stack array to simulate the same behavior. However, the code gets uglier and harder to read :p Suffix Tree gets AC in 0.14 time and 23 888 KB Suffix Automation gets AC in 0.125 time and 22 320 KB autor is me P.S. Who can tell me, how to implement Suffix Array in O(n)? Suffix array can be constructed in O(n) time by Karkkainen-Sanders algorithm. Hi WiNGeR, You might be interested to see this demo about creating Suffix Array in linear time by Juha Karkkainen and Peter Sanders: http://felix-halim.net/pg/suffix-array/index.php Do you have any reference on how to build Suffix Automaton? Which one is simpler? Suffix Tree or Automaton? Thanks i relalize double hashing, but i get tle on test 9. Can anybody get me hash-functions used in solution on this problem? try 64bit hashing I can't use single hash to solve the problem... Would you please tell me how to design the hash? And what is the 64bit hashing meaning? As far as I am concerned, Suffix Automata are the best tool for solving this problem. At least, it holds for me. I got AC three hours after I started reading "Automata for Matching Patterns" ( http://www-igm.univ-mlv.fr/~mac/REC/B4.html ). Earlier I spent much more time on Suffix Trees and Arrays - and I failed to solve this problem by their means. Would be grateful for any hints concerning Trees and Arrays (just curious). I used the standard ukkonen implementation. However, I use sibling lists for edges representation (watch Wikipedia "Suffix tree"), and I make leaves of tree explicit, that means that in the worst case you have 2*N nodes totally, N of them are leaves, where N is the total size of the text. This works fine. Also, for the traversal of the tree, I used simple recursion. I tried to use stack modelling, as someone proposed in the posts earlier, but failed with TLE on test 9. In order not to get stack overflow set up the size of the stack about 10 megabytes (watch FAQ in order to learn, how to do it). I just did it with hash and my own hash table for 0.265 sec and 7mb memory. I used the standard ukkonen implementation. However, I use sibling lists for edges representation (watch Wikipedia "Suffix tree"), and I make leaves of tree explicit, that means that in the worst case you have 2*N nodes totally, N of them are leaves, where N is the total size of the text. This works fine. Also, for the traversal of the tree, I used simple recursion. I tried to use stack modelling, as someone proposed in the posts earlier, but failed with TLE on test 9. In order not to get stack overflow set up the size of the stack about 10 megabytes (watch FAQ in order to learn, how to do it). Totaly agree. Sibling lists save memory well, and work pretty fast. I also needed to increase stack size. http://acm.timus.ru/status.aspx?author=48362&from=3761647&count=1 Edited by author 27.10.2011 02:46 Edited by author 27.10.2011 02:46 Hi! I have tried so many interpretations of the main idea for finding suffix array in O(NlogN), but all of them (except 1, because I optimize it) ran for more than 2 second . Would you share the secret, I mean is there something special you do? There is no any secret. Try to find original article "Faster Suffix sorting" by N.Jasper Larson and Kunihico Sadakane. Also very useful article for this problem is "Two Space Saving Tricks for Linear Time LCP array Computation" by Giovanni Manzini. Just got 0.96s and 28M memory with suffix automaton. How do i make it fast? String hashes + binary search: 0.468 s String hashes + hashset: 0.234 s Suffix automation AC C++ MSV2013 0.093 sec 22 MB, yeah Actually, not any hash is suitable here. Why not suffix automaton? It is much easier to implement and, of course, gets AC =) please answer to me. 6 years have passed. But anyway, maybe this will help someone, the test number 3 is N = 1 5 years passed. anyway thanks. I has helped me. :) #include<iostream> #include<cmath> #include<cstring> long long int n; int main(){ const int k = 256; std::string g = "GOOD"; std::string b = "BAD"; std::string str; std::cin>>str; std::cin>>n; if( n == 0){ std::cout<<"0"<<"\n"; return 0; } int arr2[4]; int bit = 1; int arr[4]; long long int l = 0; memset(arr,0,sizeof(arr)); memset(arr2,0,sizeof(arr2)); for(int i = 3;i >=0 ; i--){ long long int j = pow(k,i); for(int C = 1;C<=255;C++){ long long int h = C*j; if( n >= h){ l = n % h; if(l==0 && i!=0){ arr[i] = 1; bit = 1; n = n-bit*h; break; } else if( i == 0 && n <255){ arr[i] = n; break; } else { arr[i] = n/h; bit = n/h; n = n-bit*h; break; }
} else break; } } if(str == g){ long long sum = 0; for(int i = 0,m = 3; i<=3,m>=0;i++,m--){ sum+=pow(k,m)*arr[i]; } std::cout<<sum<<"\n"; } else if( str == b){ long long int sum = 0; for(int i = 0,m = 3; i<=3,m >= 0;i++,m--) sum+=pow(k,m)*arr[i]; std::cout<<sum<<"\n"; } else; return 0; } dude, 14 lines is enough to solve this problem if u have wa 44 just remove one in the cyclic factorization, if there is one. <=sqrt() + 1 -- bad <=sqrt() -- good Can anybody help? Easy solution without optimization efforts. Use Matrix A= [0 1 0 0 0 0] [0 0 1 0 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [c1 c2 c3 c4 c5 c6] Answer=[[A^(X-N)]*K][N] Thus we must calculate pow A^(X-N) Use famous O(lg) pow in groop of matrix __int64 in inner calculations and int (rez)%Y as result My such solution ran for 2.9 seconds with 3 for TL. So it's still not enogh for stable solution. I had to differentiate between M^2 and M*A matrix multiplications (the latter can be implemeted via int, +, - and >=), then it ran for 1.5 sec :) My such realisation works in 0.609. This time even without optimizations on +- for fast modulus, and not using fact that matrix is sparse in those cases (so multiplication can be done at O(N^2)) - it's 0.078 sec. With these optimizations - 0.046 sec. Edited by author 17.08.2022 11:56 This was overall a great problems set! Can anyone give a hint on the solution for "J. Turning Turtles"? What special property (beside that it's a tree) must one use? What is the complexity of the intended solution? Thanks in advance! This problem can be solved in O(N + Q*logN), where N is the size of the field. Am I right that we build slightly another tree and then use LCA on that? =) And when the problems will be added to the problemset? Actually it is possible to use the given tree. But calculating the answer is not very trivial. I was glad to use successfully LCA with forgotten implementation. It is enough to know what LCA do. It is mean that LCA very adequate tool for tree and cannot be omitted in learning. It is possible to use given tree, but my complexity is O(N*log(N)+Q*log(N)) - 0.25 sec and quite a bunch of RAM. For every node store its 1st, 2nd, 4th, 8th, 16th, ... 2^16th parent. Also store number of turns on that path and final orientation after last move (horizontal/vertical). This information is enough to calculate answers at log(N). 1. Use integer calculation for judging if the answer is -1 or 0. 2. Use integer calculation until the last step for the area (an integer-division). 3. Use 'long long' instead of 'long' or 'int'. Also, here are some cases which might help. 0 0 -1000 0 0 0 1000 1 1000001000.000000 -1000 0 0 1000 0 -1000 1000 -999 2002004004.004004 0 400 1000 1000 -1000 -1000 355 -187 -1 0 400 1000 1000 -1000 -1000 350 -190 0 0 400 1000 1000 -1000 -1000 353 -188 16931680400.000000 -1000 -1000 1000 1000 -1000 -1000 1000 999 31984004000.000000 -1000 -1000 1000 1000 -472 -851 379 1 5800420000.000000 More tricky cases: 0 0 3 0 1 0 1 0 0 The following testcase helps me to beat WA#33. 0 0 3 0 0 1 3 1 -1 Edited by author 27.11.2012 17:54 Edited by author 27.11.2012 17:54 Edited by author 27.11.2012 17:54 This test shouldn't be applied: 0 0 3 0 1 0 1 0 0 because it is said that window lengths are positive. And that's true, I've checked. Or maybe a misprint? -1000 0 0 1000 0 -1000 1000 -999 2002004004.004004 i get 2002004.0040040, but accepted. Same here, also another test is incorrect. All others are fine. -1000 0 0 1000 0 -1000 1000 -999 2002004.00400400 0 400 1000 1000 -1000 -1000 353 -188 16931680400.00000000 Since input numbers are integers, pseudovector product absolute value will be at least 1 if they aren't parallel, so you can compare it to 0.5 to have bigger margin for double precision error. Just get cross-product (dx1*dy2-dx2*dy1). If it's zero - they are parallel. When n>10 is a prime, i am writing n=k*3+l*2, where l=1,2 or 3 depending whether n mod 3 = 1 2 or 0. Then I get lcm=3^k*2^l, which is the maximal with respect to all possible divisions of n. However, my program gives WA6. Am I correct? Thanks! Clearly the lcm of k 3's and l 2's is at most 6 -- you are asked the lcm of them, not the product of them. |
|