435.md (3719B)
1 Source: https://codeforces.com/blog/entry/12479 2 3 ### [435A - Queue on Bus Stop](/contest/435/problem/A "Codeforces Round 249 (Div. 2)") 4 5 The problem could be solved in one cycle by groups. The solution could be implemented in this way: 6 7 ``` 8 int result = 1; 9 int people = 0; 10 11 for(int i = 0; i < n; i++) 12 { 13 if (people + a[i] <= m) 14 people += a[i]; 15 else 16 { 17 result++; 18 people = a[i]; 19 } 20 } 21 22 cout << result << endl; 23 ``` 24 25 ### [435B - Pasha Maximizes](/contest/435/problem/B "Codeforces Round 249 (Div. 2)") 26 27 The problem could solved by greedy algorithm. We will try to pull maximum digits to the beginning. The algorithm could be described in this way: 28 29 1) Consider every position in the number from the first, assume that the current position is *i* 30 2) Find the nearest maximum digit from the next *k* digits of the number, assume that this digit is on position *j* 31 3) If this maximum digit is greater than current digit on position *i*, then make series of swaps, push this digit to position *i*, also decrease *k*, that is do *k* = *k* - (*j* - *i*) 32 33 ### [435C - Cardiogram](/contest/435/problem/C "Codeforces Round 249 (Div. 2)") 34 35 This problem is technical, you should implement what is written in the statement. First, you need to calc coordinates of all points of the polyline. Also create matrix for the answer. Coordinate *y* could become negative, so it is useful to double the sizes of the matrix and move the picture up to 1000. In the end you should print the answer without unnecessary empty rows. 36 37 To paint the cardiogram you should consider every consecutive pair of points of the polyline and set characters in the answer matrix between them. If the polyline goes up then set slash, otherwise set backslash. To understand the solution better please paint the first test case on the paper, mark coordinates of the points and find what values to set in cycles in your program. 38 39 ### [435D - Special Grid](/contest/435/problem/D "Codeforces Round 249 (Div. 2)") 40 41 Values *n* and *m* are not so large, so the solution with complexity *O*(*max*(*n*, *m*)3) should pass. It means that you should consider all triangles and check all conditions in *O*(1). 42 43 To make this check you should precalc arrays of partial sums on all diagonals, rows and columns. After that you could check, that there is no black nodes on the side using one sum-query. 44 45 Some hints about this problem and the implementation: 46 47 * all correct triangles are isosceles right triangles; 48 * either all legs or hypotenuse of the triangle is parallel to the sides of the grid; 49 * if you know how to solve the problem for two types of the triangles, you can get the whole solution making 4 rotates of the matrix. 50 51 ### [435E - Special Graph](/contest/435/problem/E "Codeforces Round 249 (Div. 2)") 52 53 To solve this problem you have to paint different correct colorings on the paper. After it you could observe that there are two types of them: vertical and horizontal. 54 55 Vertical colorings looks like this: 56 57 acbcbd... 58 bdadac... 59 acbcbd... 60 bdadac... 61 acbcbd... 62 bdadac... 63 ...... 64 65 In other words, each vertical has only two colors, odd verticals have equal colors, even verticals have two others. The order of the colors on every vertical could be arbitrary. 66 67 Horizontal colorings are the same, they are rotated by 90 degrees. Of course, there are both vertical and horizontal colorings, but it doesn't change the solution. 68 69 So, you should consider every type of described colorings and check them. That is, you could choose what colors are on the verticals or what colors are on horizontals and check that obtained coloring matches the given matrix. 70 71 The solution's complexity is *O*(*n* × *m*).