### pikmike's blog

By pikmike, history, 21 month(s) ago, , 1027A - Palindromic Twist

Tutorial
Solution (PikMike)

1027B - Numbers on the Chessboard

Tutorial
Solution (Vovuh)

1027C - Minimum Value Rectangle

Tutorial
Solution (PikMike)

1027D - Mouse Hunt

Tutorial

1027E - Inverse Coloring

Tutorial
Solution (PikMike) O(n^3)
Solution (BledDest) O(n^2)

1027F - Session in BSU

Tutorial
Solution (Vovuh)
Solution (Vovuh) Kuhn's Algorithm

1027G - X-mouse in the Campus

Tutorial  Tutorial of 2019-2020 BUAA Autumn Training 9 Comments (58)
 » You don't need a binary search in problem F. The complexity is O(nlogn) for coordinate compression. Build the graph, then apply DSU or SCC(Tarjan or Korasaju) with do the job. If there exists a connected component with number of edges greater than the number of vertices, then the answer is -1. If the number of edges is equal to the number of vertices, record the largest value, If the number of edges is equal to the number of vertices minus one, record the second largest value, the answer is the maximum over all connected components.
•  » » I was trying the same way but got tle in 45th test case. Any optimization you would suggest? I have used path compression and union by rank.
•  » » » You were using map in your program, which adds an additional log factor together with large constant.
 » Can you please elaborate on the part of partial sums in problem E to solve the problem in N^2 time. I am unable to get it.
•  » » 21 month(s) ago, # ^ | ← Rev. 2 →   let lte[i] be the n.of binary strings of length n such that maximum segment of a single color has length <= i. (lte[i]-lte[i-1]) will be the n.of binary strings of length n such that maximum segment of a single color has length = i. Hence if we have lte array we can solve the problem. We can calculate lte[i] in O(n) as follows. let dp[j] be the n.of binary strings of length j such that maximum segment of a single color has length <= i.If j>i dp[j]=dp[j-1]+dp[j-2]....dp[j-i] else dp[j]=dp[j-1]+dp[j-2]..+dp+1. This is because in a string of length j at the beginning we can have at most i bits with the same color and after that, it's just dp[remaining length]. Now, lte[i]=dp[n]. Clearly, we can calculate the dp array by using partial sums in O(n). Since each element of the lte array takes O(n) time to calculate overall complexity is O(n^2). P.S: Don't forget to multiply by 2 at the end since all this is for a fixed color of the top left tile and it has 2 options (white or black).AC: O(n^2)
•  » » » Thanks a lot. Can you just tell me why are we adding 1 in case of j <= i.
•  » » » » I think I got it. You are assuming that 1st element is fixed and 1 is for assuming all the j elements are same. Thanks.
•  » » » » » yes, glad that I was of some help :D
•  » » » 21 month(s) ago, # ^ | ← Rev. 2 →   Can you please explain the relation for j>idp[j] = dp[j-1]+...dp[j-i]How ?
•  » » » 21 month(s) ago, # ^ | ← Rev. 2 →   This is because in a string of length j at the beginning we can have at most i bits with the same color and after that, it's just dp[remaining length].I don't see why the reasoning is correct.If you put X bit of same color in the beginning and fill the rest with dp[j — X] then it is possible to end up with a segment of same color that has length more than i. isn't it?I guess you meant something else by beginning.
 » Can someone find out what is time complexity on my code? (problem C) Here is my code 41811657 I think it should be O(T*n*log(n)) . But why it got TLE in case 6. Thank you!!
•  » » Maybe because ?In fact, for each test you create arrays a and cnt istead of making it global and clearing after each test.
•  » » » » 21 month(s) ago, # ^ | ← Rev. 4 →   Try using ios_base::sync_with_stdio(false). I optimized my solution from TLE to AC.TLE on test 6: 41767762AC: 41767854
•  » » » » » Thanks _Kuroni_
•  » » » » » Wow, that helped me too. Thank you!
•  » » » » Try scanf and printf rather than cin and cout it worked for me
•  » » » » » but ,will i be able to use sort() function , if i am taking input through scanf() ,because i have coded in c++
•  » » » » » » Ya Of course
•  » » » » » » » This code is not working , dude can you figure out the error #include main() { int i,arr; for( i=0;i<5;i++) { arr[i]=5-i; } sort(arr,arr+5); for(i=0;i<5;i++) { printf("%d ",arr[i]); } } 
•  » » » » » » » » 21 month(s) ago, # ^ | ← Rev. 2 →   Add using namespace std;
•  » » » » » » » » » Yeah , Got it !!! Thanks.
•  » » » » Why it's getting WA?https://codeforces.com/contest/1027/submission/77138815
 » 21 month(s) ago, # | ← Rev. 2 →   What does this line do in the code for Problem E? ans = (ans * (long long)((MOD + 1) / 2)) % MOD; Edit : Understood. For anyone having trouble it is modular inverse
•  » » It divides ans by 2 modulo MOD, as is the modular inverse of 2, because » 21 month(s) ago, # | ← Rev. 3 →   Kuhn's algo for F works only with this particular modification found in your solution -- you first check all the edges and if you find a suitable one, then you use it. Only if there are no suitable edges found, then you do a second loop and recurse into them (DFS). This versions passes tests. I don't know why.However, a "vanilla Kuhn" that just uses a regular DFS (tries every edge to a non-visited vertex recursively) receives TLE, which is kind of expected, since it has complexity O(N*M) -- too big for this problem.
 » I've sent a solution for C (after the end of the contest) which sounds precisely like the given here (41817149), but it still didn't fit into given time :( What did I do wrong?
 » In problem D, Why it is better to put trap on cycle, although there may be cheaper way to put trap on each path leads to this cycle like that. https://drive.google.com/open?id=19ff-GgeGhbhGXh5MykElhYXB3EErpq_9
•  » » 21 month(s) ago, # ^ | ← Rev. 2 →   Because we don't know where the mouse starts (it may start in any vertex). Mouse can be on a cycle at the beginning and if the mouse is on a cycle at the beginning, it will be on this cycle forever. Thus it is necessary to put a trap on cycle, otherwise mouse beginning on cycle will never be caught. And according to your example, if we put a trap in vertex with cost 100, mouse will go through this vertex regardless of its beginning vertex, so it is enough to put there a trap.
 » As far I know Complexity of Kuhn is O(n * n). How F is Solved with Kuhn? What's the trick or am I wrong? Thanks in advance.
•  » » Kuhn works O(n*m). But tests for this assume prix is rough estimate. In real kuhn works very fast. There are also different heuristics.
 » can anyone explain this line in tutorial for problem EThat way, you can also guess that the area of maximum rectangle of a single color you will get in you coloring is the product of maximum lengths of segments of a single color in both of the strings.I cant understand why maximum rectangle is product of maximum lengths of segments of only 2 binary strings
 » 21 month(s) ago, # | ← Rev. 2 →   Problem G: How to prove there is exactly u that u * x = v?Help me. Thanks.
•  » » Because u = v·x  - 1
•  » » » Thanks so much.
 » can someone help me in visualization of problem E dp.
 » with regard to Palindromic twist what about a and z . These two works only for +1 for a and -1 for z. But your solution says "verify that the distance between the corresponding letters is either 0 or 2. Should'nt this be between 0 and 2.
•  » » If the difference is 1, you won't be able to match both of the letters because you will have to apply +1 or -1 to both of them
 » A little help on problem C! I don't know what I am doing wrong. I have a code in Python similar to the above solution, and I got TLE on test 5. Here is my code : http://codeforces.com/contest/1027/submission/41941233 Can someone find out?
•  » » I don't know why it gets TLE, but it also seems incorrect. Test: 1 7 1 1 999 999 7 7 7 Your output: 7 7 7 7 Trace: 1) c == 1, a[i] == 7 -> c := 72) c == 7, a[i] == 7 -> push3) c == 7, a[i] == 7 -> pushBut in fact you don't have 2 pairs
 » In problem D, the editorial says that the mouse will stuck in a cycle. But my question is that, should the vertex in the cycle be reachable from all other nodes which are not part of the vertex's cycle because the girls are not aware of the position of the mouse? If it is required the how can we find it?
•  » » It will always be stuck in a cycle, once it reached that cycle. So all you have to do is find all cycles, take the minimum number from each cycle and add to the answer :)
 » 21 month(s) ago, # | ← Rev. 3 →   Can anyone help me find the complexity of my code..?for Problem D...THis is my code in java.. http://codeforces.com/contest/1027/submission/42027693 Thanks
 » sorry ignoring my algorithm (i mean whether is it correct or not) why i am getting TLE on test 2 ty 42082971
•  » » Try to convert the input/output from cin/cout to scanf/printf and don't forget to extend the size of array ar to not get RTE.
•  » » » Sorry i tryed to change them however i dont know why it is printing wrong answers! 42083838 amazing thing about it is that when i compiled it in my computer it printed right answers!!
•  » » » » 21 month(s) ago, # ^ | ← Rev. 2 →   You should use the identifier %lld with the type long long. And the size of the array is still not enough ;).
•  » » » » » sorry still TLE 42085192
•  » » » » » » sorry i find the problem XD
•  » » » » » » » XD the siiizzze
 » Hi Do anyone knows what is it? 42086511 Ty
 » This is my code for Problem A, It is not passing test 2, I cant see the input for test 2.. can any1 help me find the mistake? - if(a[i]==a[j]) - continue; - else if((a[i]++)==(a[j]--)) - continue; - else if((a[i]--)==(a[j]++)) - continue; - else - { - System.out.println("NO"); - f=1; - break; - } 
 » Problem D:Why does the mouse jump on a cycle at some point, no matter the starting vertex ?
•  » » Because it cant jump infinite times. In some number of jumps it will return to where it was
 » 15 months ago, # | ← Rev. 2 →   .
 » https://codeforces.com/contest/1027/submission/41811657 can someone tell me why I am getting tle my method is almost same as that it tutotrial
 » 3 weeks ago, # | ← Rev. 3 →   Why do I get WA on test 3 (problem E)? https://codeforces.com/contest/1027/submission/78959890 UPD: fixed, a -1 was missing (AC: https://codeforces.com/contest/1027/submission/78974251)