Show all threads Hide all threads Show all messages Hide all messages |
What is algo??? | IgorKoval(from Pskov) | 1863. Peaceful Atom | 16 Aug 2022 12:17 | 7 |
Head-on solution have complexity O(2^40)=O(10^12). It's very slow. How do it faster? I used 40=30+20 First 20- memorization second 20 as 2^20=10000000- that normaly Could you explain your solution more in detail? "First 20- memorization" What it mean in Russian? I understood that what we do with second 20. But can not understood that what we do with first 20. =) I meant that first 20 also as 2^20 or with brute force aproach, but result of first 20 we place in arr[1..1000000](memorization) sort arr than in second 20 use log-bin search in arr. Thank you very much. You is good man! I got AC. =) It's a bit more complicated than that because you should care about rod not getting off the rails during the process as well, so you can't just pick arbitrary memorized sequence which gives suitable end-result, it also must stay within limits while it performs memorized shiftings. Looks like segment tree algo, not just plain binary search (or tests are very weak). P.S: Got AC with 0.5 sec and 44Mb, probably could've done better. For every 2^20 endings I track range of start values where it is suitable (i.e. fits for its entire internal history), then for every 2^20 beginnings (sorted) I track min/max of currently active segments via two heaps. Edited by author 16.08.2022 11:51 P.P.S: Ah, I'm dumb! Memorizing first 20 leads to much easier algo when last 20 are brute-forced (not vice versa). 0.265 sec, 7 Mb. Yet, coding that monster from previous "P.S" was fun :) |
the conditions of the problem | _JackDaniel's | 1461. Christmas Garland | 16 Aug 2022 07:49 | 2 |
Why is the correct answer in the example "uhhdud" and not "uhdudh"? in terms of lexicographic order, this combination is closer to the original data. It's not next in the book, it's before. d < h < u |
Solution | Alikhan Zimanov | 1003. Parity | 16 Aug 2022 01:26 | 4 |
Solution Alikhan Zimanov 12 Jul 2020 13:56 Let a[1], ... , a[n] be the sequence written by the friend and pref[i] be the xor of the prefix a[1...i]. Then a question (l, r, t) (which means the parity of the number of ones on the segment from l to r is t) can be represented as (pref[r] xor pref[l - 1] = t). Let's build a graph with two vertices for each prefix (thus, there will be 2 * (n + 1) vertices). Using compression, we actually need only at most 2 * m vertices (where m is the number of questions). Let the vertices corresponding to the prefix i be f0(i) and f1(i). Now, let's add all edges f0(i) ~ f1(i). For a question (pref[r] xor pref[l - 1] = t) if t = 1, we add two edges f0(l - 1) ~ f0(r) and f1(l - 1) ~ f1(r), else if t = 0, we add edges f0(l - 1) ~ f1(r) and f1(l - 1) ~ f0(r). To check whether a sequence of questions from 1 to x is achievable we just need to check whether our current graph is bipartite. This can be done by simply doing dfs after each question. Can you please elaborate in a few words? I actually didn't get the significance of having two vertices for each prefix and how it's solving the problem. |
A good problem to train O(1) priority queue madskillz | Harkonnen | 1959. GOV-internship 3 | 15 Aug 2022 10:17 | 1 |
Though O(N*log(N)) and O(N) both gave me 0.068 timing, it's a good testdata to write such code, would matter if N could be up to 10^6. Also this solution with O(1) stepping may additionally report number of ways to achieve minimal result. The structure is doubly-linked list of available amounts (always strictly ascending), each of which is non-empty doubly-linked list of characters with that amount. This structure allows to query for max and to change amount of single character +-1 at O(1). |
What is the proper solution? (Got AC with a hack, my solution inside) | araifr | 1569. Networking the “Iset” | 15 Aug 2022 06:52 | 3 |
Could anyone who solved it share the idea? My solution was: iterate over all vertices i. Fix each i start bfs, so you get a tree (rooted in i). Find it's diameter (from the farthest node v from root i start another BFS and find the farthest node u from v. The distance from u to v would be the diameter). Across all the diameters find the minimum, that would be the answer. This solution got WA 8. After random_shuffle() on adjacency lists I've got WA 17. Now repeat this solution 100 times and you get AC. So my question is, what is the solution? 1. Find shortest paths from all vertices with Floyd-Warshall or BFSes 2. Brute force all possible tree centers. Note, that a diameter center can be either a vertex or an edge. Basically, my solution is exactly yours while I believe you've forgotten to check edges that can form a center. My solution is completely deterministic. Edited by author 07.11.2018 17:03 Edited by author 07.11.2018 17:03 I just tried all vertices and all edges (meaning pushing both neighbors in priority before BFS starts), then checked tree diameter across all vertices, not just to these roots (there is O(N) algo for that). I used no Floyd-Warshall for candidates, just tried all pairs v1 <= v2, besides I am not sure Floyd will help here because original graph diameter can be shorter than that of resulting tree (e.g. see WA17 topic). |
What is test 17? | Felix_Mate | 1569. Networking the “Iset” | 15 Aug 2022 06:47 | 3 |
Use this test 6 9 5 6 3 4 2 4 1 5 3 6 1 2 1 6 4 5 Thanks, helped me to get AC, but first line in your test should be 6 8. Correct answer is diameter 3. |
Is the edge direct or undirect? | RoBa | 1569. Networking the “Iset” | 15 Aug 2022 06:36 | 2 |
Graph is not oriented. Word "direct connection" in the statement means connection w/o intermediate nodes. |
Any tips on solving this problem? | Sap | 1237. Evacuation Plan | 14 Aug 2022 22:22 | 4 |
hi, im trying to solve this problem and would like to get tips in the right direction of solving this problem. would a grapha matching solution work? or perhaps a minimum cost flow solution? or anything else? thanks in advance. My solution is to find really optimal matching with min-cost max-flow. Then compare it to one from input. I solved it with negative cycle searching. My algo have following complexity: (N+M)*(2*N*M + M*M) It can also be a chain ending up in shelter with residue capacity. It is also possible to make it N*M*M + M*M*M by jumping from shelter to shelter through one preselected building which grants maximum yield for that pair of shelters (hence the first N*M*M step), M*M*M is Bellman-Ford. |
Hint | pmartynov | 1965. Pear Trees | 14 Aug 2022 08:18 | 2 |
Hint pmartynov 17 Jan 2014 17:06 I spent a lot of time on this problem. For those who want to fit the time limit I recommend reading A. Brandstƒadt, D. Kratsch "On partitions of permutations into increasing and decreasing subsequences". Also M.D. Atkinson "Permutations which are the union of an increasing and a decreasing subsequence" is very interesting to read but not so helpful for this particular problem. My algo is O(N) DP which does not rely on input array being a permutation. Input can be anything - even floats or strings and may have repeating elements. Moreover, it's "online" DP, so if you don't need detailed result (just 'Possible' or 'Fail') it can be coded in O(1) memory. |
Can someone explain me why's that test case is impossible? | Ivan | 1133. Fibonacci Sequence | 14 Aug 2022 02:33 | 1 |
-1000 0 0 1 1 My solution do not deal with testcases like this, yet still was accepted. Am I missing something? For those who wonder, my program gives 9079565065540428013 on this testcase lol |
hint | ironchat(ITMO) | 2123. Knapsack | 13 Aug 2022 10:27 | 2 |
hint ironchat(ITMO) 9 Aug 2019 16:39 Simple solution, but try to optimize it! If you want better explanation (myironmistake@gmail.com) Simple recursion from highest weight down to lowest with proper cut-off on maximal reachable weight and caching of results gives AC in 0.015 sec and 1Mb RAM. I write that spoiler here because I suspects that tests are very weak because I have no proof of why this straightforward thing works so fast when problem has 4-sec/256Mb limits. |
I think GCC and Clang can be tuned for better FPU performance | Harkonnen | | 12 Aug 2022 11:38 | 1 |
Example - my submission 9959592 (problem 1990 - Podracing). It's not about input or sorting (checked everything), but GCC gives 0.5 sec where VC++ gives 0.171 sec (it was 0.9 sec on the verge of time limit when everything was double). FPU is just a guess because I faced situations before in real projects when GCC used FPU and VC++ used SSE or something else and was way faster, there was some command-line switch like -fdisable-fpu or -fdisable-387 or something like that for gcc (can't remember now), probably it will help. |
Problem 1364 “LaraKiller”. The statement was updated. | Sandro (USU) | 1364. LaraKiller | 12 Aug 2022 10:38 | 2 |
The original version of the statement was unclear, and not all the limitations were described in the input data format. Both English and Russian version of the statement were updated. It's still unclear what to output if Lara has enough time to escape (i.e. there is no reason to hide) - empty output? My AC solution ignores that case like if entrance is locked. |
What happened with my program on Test #24? | 198808xc | 1990. Podracing | 12 Aug 2022 10:04 | 2 |
My algorithm is linear, except the quicksort procedure. It gets TLE at Test #24. What happened? I have checked the program multiple times. Thanks. It's very weird - almost happened to me too (0.934 sec, dunno which test). After that I switched to 'int' and kept 'double' only for boundaries - it became 0.5 sec. Then I precalculated 1.0/(y[i+1]-y[i]) - timing didn't change. Then I suspected it was some anti-qsort case, tried to random-shuffle, even tried heapsort - no matter, still 0.5 sec. Then I've noticed that I am submitting it as GCC instead of VisualC (was testing another problem before on GCC/clang) - got 0.171 sec with same code :) GCC suxx. Dunno if it's input or sorting or what. I was using scanf/printf (cin/cout are slower in VisualC even with iostream::sync_with_stdio(false)). |
HINT | Felix_Mate | 1926. The Tournament of Intelligences | 12 Aug 2022 08:46 | 5 |
HINT Felix_Mate 30 Dec 2016 20:18 When you count invM*rj*M mod M*pj you can get number > int64. But you can use this: a*b mod a*c=a*(b mod c). In our situation M*(invM*rj mod p[j]). Re: HINT http://xoptutorials.com/index.php/category/timus/ 1 Jan 2017 23:44 WA5 is about this case (overflow test, WA9 and WA15 too), but formula I used still gets int64 overflow (chinese remainders), so I had to devise "short long arithmetics" (4 qwords to store result with base-2^32 digits, then fetching remainder via base-2^4 interpretation - that fits unsigned int64). Timing is still bad - like 0.14 sec, and was like 0.5 sec without bitwise optimizations. Your hint though allowed to fetch remainder via base-2^8 interpretation which shortened timing down to 0.062s sec. Edited by author 12.08.2022 05:27 Finally AC 0.031 without long arithmetics. Another hint - do not process primes one after another because in the end you will get a*b%c where both 'a' and 'b' are close to 2^63. Instead process first 9 primes individually and last 6 primes individually (product of both sequences is on the verge of 2^32). Then you may combine result within 2^64 using "dirty hack" mentioned by Felix_Mate :) P.S: I finally understood why you had 'inv' there, but in this case no mod operations are necessary at all (except during calculating inverse which is mod 47 at most), and this way of getting the answer (POW instead of GCD) works fine with sequential primes processing, no need to split them in two parts and pray for no overlow. |
WA test 2 | andrei parvu | 1713. Key Substrings | 11 Aug 2022 17:25 | 8 |
Can somebody tell me how they passed test nr. 2 (or give me some good test cases)? I have a solution in O(N*M*log(N*M)) using suffix array and I can't find the bug. Thanks Edited by author 16.09.2010 22:39 I've got absolutely the same problem. I use suffix and lcp arrays. I use the following algo: for i = 1 to LENGTH_OF_ALL_WORDS l = the closest preceding suffix that belong to another word r = the closest succeeding suffix that belong to another word v = max(lcp(l, i), lcp(i, r)) + 1; w = the word that contains suffix i; if (v < ans[w]) ans[w] = v; I've tried lots of tests but can't find what's wrong. There is something strange with this problem. I hasn't written suffix array building on my own, I used a reference implementation. Looking for a bug I replaced it with code that builds suffix array in the naive way (i.e. literally sorting suffixes). I got AC! So there are 2 results: - The reference implementation I used is wrong :) - The test data seems to be weak because naive suffix and LCP arrays building gets AC. TO VC15 (Orel STU) max(lcp(l, i), lcp(i, r)) + 1 may longer than the word‘s length Edited by author 29.06.2012 13:49 Yes, it could be. In this case I don't change the answer for the word. 2 qwertyuiopasdfghjklzxcvbnm qwezrtyuiopasdfghjklzxcvbnmer My answers are: ert ez Or ert me Edited by author 11.08.2022 18:28 Try this 2 mississipi misisspi |
Author could use L, r, /, \ for corners instead of pseudographics :) | Harkonnen | 1006. Square Frames | 11 Aug 2022 11:56 | 1 |
|
How to solve it in O(N) | Victor Barinov (TNU) | 1130. Nikifor's Walk | 11 Aug 2022 04:22 | 11 |
Hi everybody! I solved this problem, but my algo is O(n log n) and it works 0.14 seconds. Idea of my algorithm is to find two vectors witch sum is not greater than L and change them on their sum. It it possible to prove that such two vectors exists if there are more than two vectors in set. Do this procedure while we can find such two vectors. But many people solved it much better... I want to know this solution. Consider that for any vectors a,b,c, |a|,|b|,|c| <= L exists integers u, v, w, |u|,|v|,|w|=1, |ua+vb+wc|<=L It is not right: what are u, v, w for a = (5,0) b = (0,0) c = (0,5)??? L = 5, of course Many people above have solved this problem, but they didn't express their thought well. The key is this: if there are AT LEAST 3 vectors in the set with module not exceeed L, we can ALWAYS choose 2 of them and, with choosing the direction, make the module of their sum not exceed L. Thus, we can reduce the amount of vectors by this until there are only TWO. At last, for any two vectors which has the module not longer than L, we can make the module of their sum not exceed L*sqrt(2). And that's all the key. What's left is to find a way to programme. Maybe it will help you, maybe not for my poor English. Thank you for well-expressed idea. It really helps to understand the idea of solution to the problem. 2 Victor Barinov (TNU): complexity of my solution is not O(NlogN), but even O(N^2)! And it's AC 0.015 sec. Just use the same heuristics as in disjoint sets. Edited by author 04.07.2009 18:52 Edited by author 04.07.2009 18:56 Can somebody, please, prove the idea described by 198808xc? P.S. I've solved this problem using lot of cheating (brute force+random sort+heuristic) and would like to know, what is right solution. P.P.S. To prove it we should just realize the fact, that there is always 2 vectors, such, that angle between them is >=120 degrees. So their sum is <=L. Edited by author 14.07.2009 19:24 If you only two vectors, you're out of luck of getting |X+Y| and |X-Y| <= L when angle between them is more than 60 degrees (so together with their sum/diff they form an equilateral triangle). Now if you have 3 vectors, these 3 vectors and their reflections form a lotus (or a hexagon) which covers everything, so there will always be a pair with |X+Y| or |X-Y| <= L. When you're down to just 2 vectors, the worst situation is 90 degree angle which yields sqrt(2)*L. Your algo should be O(n) ;) I don't quite understand how write it with complexity of O(n log n) |
Can anyone explain sample test? | Sq1 | 1805. Chapaev and a Cipher Grille | 10 Aug 2022 11:59 | 2 |
Can anyone explain sample test? I have smth like this: Case n = 4, k = 1: 0000 0000 0000 1111 Case n = 4, k = 2: 0000 0000 0001 0111 Case n = 4, k = 3: 0000 0000 0001 1011 Case n = 4, k = 4: 0000 0000 0001 1101 Case n = 4, k = 15: 0000 0000 0011 1100 Matrix should not overlap itself after 90-degree rotations (no two '1' at same position) and should cover all NxN elements after 4 such rotations. |
any algo | Ibragim Atadjanov (Tashkent U of IT) | 1805. Chapaev and a Cipher Grille | 10 Aug 2022 11:56 | 8 |
any algo Ibragim Atadjanov (Tashkent U of IT) 12 Nov 2010 10:57 Who know how to solve it. I think all the varians will be 4^(9 + 7 + 5 + 3 + 1) = 4^25 when n = 10. So i cannot count from 1st to kth variant Re: any algo Vedernikoff 'Goryinyich' Sergey (HSE: АОП) 12 Nov 2010 13:56 O(N^4) simple algo exists. Re: any algo Ibragim Atadjanov (Tashkent U of IT) 13 Nov 2010 18:52 Can you tell anything else? I mean is the algo search(Binary Search or smth else). Please give me a clue Re: any algo Vedernikoff 'Goryinyich' Sergey (HSE: АОП) 13 Nov 2010 22:49 Suppose you have already built a part of a grille and is thinking what symbol is to put in the following cell. Can you count the number of grilles which can be built with such a beginning and symbol? I suppose that O(n^2) algorithm is even more simple. Both in inventing and coding. That's right. I track amount of remaining variations for every cycle and their total product. This information is enough at each of N^2 steps to decide if it should be '0' or '1' Given advices too weak to be helpfull. For me key idea was forming n*n/4 classes with 4 cell in each and working with level, where 1<=level<=n*n; We counting all combinations under level in each classes exept whose are used already. Re: any algo Strekalovsky Oleg [Vologda SPU #1] 8 Nov 2011 00:01 Given advices too weak to be helpfull. For me key idea was forming n*n/4 classes with 4 cell in each and working with level, where 1<=level<=n*n; We counting all combinations under level in each classes exept whose are used already. My algo was same as Vedernikoff 'Goryinyich' Sergey (HSE: АОП) said. Algo is very simple and wasn't too hard to implement. Edited by author 08.11.2011 00:01 |