### Polichka's blog

By Polichka, 7 years ago, translation, ,
Problem A

It's clear that the leftmost soldier with the maximum height should be the first and the rightmost soldier with the minimum height should be the last. Thus we will minimize the number of  swaps. And the answer is number of leftmost soldier with the maximum height - 1 + n - number of rightmost soldier with the minimum height. And if the leftmost soldier with the maximum height is more right then the rightmost soldier with the minimum height we should subtract one from the answer.

Problem B

Let's try to check all integer points of the table perimeter and add to the answer such of them that don't cover by circles of radiators. Let xa < xb and ya < yb, and if it's not true then swap xa and xb, ya and yb. So generals sit in the next integer points: (xa, y), (xb, y), (x, ya), (x, yb), where  xa ≤ x ≤ xb и ya ≤ y ≤ yb. We should be attentive when we count the generals who sits in points: (xa, ya), (xa, yb), (xb, ya), (xb, yb),  that don't count them twice.

Problem C

Let's count number of each letter in the second string and save it, for example, in array a[1..26]. For the first strings' prefix of length n, where n is the length of second string, (it's the first substring) we count number of each letter in array b[1..26]. We don't count characters \texttt{?}''. If there are b[i] ≤ a[i] for all i, then it's good substring. Then go to the second substring: subtract from the array b the first character:  b[s[1] - 'a' + 1] –  and add n + 1 character: b[s[n + 1] - 'a' + 1] +  + . If some of these characters is \texttt{?}'' then we shouldn't do for it the subtraction or addition. Then repeat the showed check and go to the next substring. Let's repeat this procedure for all substrings of length n.

Problem D

d[i] --- the minimum distance from vertex s to vertex i, that counted by algorithm of Dijkstra. "et's count the number of points on each edge of the graph that are on the distance l form the vertex s (and l --- the minimum distance from these points to s).

For edge (u, v):

if d[u] < l and l - d[u] < w(u, v) and w(u, v) - (l - d[u]) + d[v] > l then add to the answer the point on this edge, the distance of which to the vertex u is l - d[u];

if d[v] < l and l - d[v] < w(u, v) and w(u, v) - (l - d[v]) + d[u] > l then add to the answer the point on this edge, the distance of which to the vertex v is l - d[v];

if d[v] < l and d[u] < l and d[u] + d[v] + w(u, v) = 2 * l then add to the answer the point on this edge, the distance of which to the vertex v is l - d[v] and to the vertex u is l - d[u].

And if d[i] = l, then let's add to the answer this point.

Problem E

It's clear that the nearest squares of the secondary diagonal to some sportsman form the "segment" of the squares of the secondary diagonal. Let's write these segments for each sportsman.

Let's consider sportsmen so that we should compare to each sportsman excactly one square of the secondary diagonal from his "segment" and to each square of the secondary diagonal no more then one sportsman. It's clear that sportsmen can reach theirs squares without occupying the same square simultaneously with another sportsman. We should maximize the number of choosen sportsmen. And solution of this reformulated problem is greedy.

•
• +51
•

 » 7 years ago, # |   +3 Thank you for the contest and fast published tutorial :-)
 » 7 years ago, # |   +1 Actually I'm confused by how to greedy in Pro E... I have worked out the convertion, but didn't know how to solve the "segment" problem. THX
•  » » 7 years ago, # ^ | ← Rev. 2 →   0 After sorting the left endpoints of the segments, you loop through them, and use a priority queue (heap data structure) to repeatedly select the next non-intersecting segment with the leftmost possible right endpoint.By the way, the editorial's question marks are not displaying properly.
•  » » » 7 years ago, # ^ |   0 Got it，THX very much
 » 7 years ago, # |   0 I still don't know about the solution to the Pro-E "segment" problem.Could you explain it more clearly?Thanks a lot.
 » 7 years ago, # | ← Rev. 2 →   +3 Very sad that its impossible to get Accepted on problem B with Ruby.Here is my code, may be some optimizations?
•  » » 7 years ago, # ^ |   0 Seems like you are iterating the whole grid... of course it'll get TLE.Try to iterate only cells of the frame of the rectangle.
•  » » » 7 years ago, # ^ | ← Rev. 2 →   0 Already tried... Here is my submission.TLE, I think its because input is too large.
 » 7 years ago, # |   0 How to prove the correctness of problem A.I simulate 2 times.
•  » » 7 years ago, # ^ |   0 The solution is a greedy approach which is proved by contradiction. Consider the best case and assume it's not the one that your algorithm produces and then come up with a contradiction to prove its correctness.
 » 7 years ago, # | ← Rev. 3 →   0 Can somebody help me with this (code) problem (C) ?Something strange starts from test #13 (look at input params).From problem statement:The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters.PS.Got Accepted, but the question remains.
•  » » 2 years ago, # ^ |   0 Are you talking about the cases that only have one line?I think maybe because the string in the first line is so long that the system can only show a small prefix of it. then the whole input file remained is ignored.eg.xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzzzzaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbwill looks likexxxxxxx...
•  » » 20 months ago, # ^ |   0 In Codeforces, large test case displays first 256 characters only, after which they show ellipsis (...)
•  » » » 20 months ago, # ^ | ← Rev. 2 →   0 Thank you,after 5 years SyFy can at last live happily.
 » 7 years ago, # |   0 Hello! I want to know why the Pro-D could use Dijkstra to solve? That the  complexity of Dijkstra is O(v^2), isn't it ? And this time , |V| <= 10^5. thanks, Happy new year ~!~
•  » » 7 years ago, # ^ |   0 We can improve complexity to O(V × logE) using data structures to extract minimum in O(logn), for example, prority queue based on either binary heap or sorted set.
 » 4 years ago, # |   0 I am not able to understand in problem C Anagram Search that while moving to next substring why we are doing this in b[s[n + 1] - 'a' + 1]+ +  in the editorial
 » 3 years ago, # |   0 Why are we doing this — if the leftmost soldier with the maximum height is more right then the rightmost soldier with the minimum height we should subtract one from the answer ?
•  » » 3 years ago, # ^ |   0 Because when this situation occurs, then while moving maximum-height soldier to the start of the line by swapping we also move minimum-height soldier one step towards the end of the line. But this step is already counted by the formula given in the problem editorial, so we have to subtract one.
 » 12 months ago, # |   0 Is there any proof of the solution to problem d?