### Polichka's blog

By Polichka, 10 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 - '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. Tutorial of Codeforces Round #103 (Div. 2)  Comments (23)
 » Thank you for the contest and fast published tutorial :-)
 » 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
•  » » 10 years ago, # ^ | ← Rev. 2 →   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.
•  » » » Got it，THX very much
 » I still don't know about the solution to the Pro-E "segment" problem.Could you explain it more clearly?Thanks a lot.
 » 10 years ago, # | ← Rev. 2 →   Very sad that its impossible to get Accepted on problem B with Ruby.Here is my code, may be some optimizations?
•  » » 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.
•  » » » 10 years ago, # ^ | ← Rev. 2 →   Already tried... Here is my submission.TLE, I think its because input is too large.
 » How to prove the correctness of problem A.I simulate 2 times.
•  » » 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.
 » 10 years ago, # | ← Rev. 3 →   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.
•  » » 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...
•  » » In Codeforces, large test case displays first 256 characters only, after which they show ellipsis (...)
•  » » » 5 years ago, # ^ | ← Rev. 2 →   Thank you,after 5 years SyFy can at last live happily.
 » 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 ~!~
•  » » 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.
 » 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
 » 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 ?
•  » » 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.
 » Is there any proof of the solution to problem d?
»
10 months ago, # |
Rev. 3

Can anyone please find the bug in my code for problem A. It outputs 11 for 7 10 10 58 31 63 40 76 whereas the correct answer is 10 ~~~~~ Your code here... ~~~~~

# include<bits/stdc++.h>

using namespace std; int main() { int n; cin >> n; vector v(n); for(int i = 0; i < n; i++) { cin >> v[i]; }

int maxele = INT_MIN; int minele = INT_MAX; for(int i = 0; i < n; i++) { maxele = max(maxele, v[i]); minele = min(minele, v[i]); }

int maxelepos; int minelepos; for(int i = 0; i < n; i++) { if(v[i] == maxele) maxelepos = i; } for(int i = n — 1; i >= 0; i--) { if(v[i] == minele) minelepos = i; }

int res = 0;; if(maxelepos < minelepos) { res = maxelepos + ((n — 1) — minelepos); } if(maxelepos == minelepos) { ; } if(maxelepos > minelepos) { maxelepos--; res = maxelepos + ((n — 1) — minelepos); }

cout << res; return 0; } ~~~~~

 » if the leftmost soldier with the maximum height is more right then the rightmost soldier with the minimum height why we have to subtract one from the answer ???
•  » » Take the case of the example 10,10,58,37,63,40,76. Here 76 is the leftmost element with maximum height and 10 at position 2 is the right most element with minimum height. So the number of swaps required to move 76 to position 1 is 7-1=6 and the number of swaps required to move 10 to position 7 is 7-2 = 5. But suppose we initially start swapping 76 to the end, then at a certain position 76 gets swapped with our 10 which moves by one place forward, more close to the end, so the number of swaps required while swapping the rightmost minimum element will be one less. so answer = 6 + 5-1=10.