221.md (6653B)
1 Source: https://codeforces.com/blog/entry/5177 2 3 ### [221A - Little Elephant and Function](/contest/221/problem/A "Codeforces Round 136 (Div. 2)") 4 5 In this problems you should notice that the answer for the problem is always of the following form: *n*, 1, 2, 3, ..., *n*-1. In such case array will be always sorted after the end of the algorithm. 6 7 ### [221B - Little Elephant and Numbers](/contest/221/problem/B "Codeforces Round 136 (Div. 2)") 8 9 Here you just need to find all divisors of *n*. This can be done using standart algorithm with iterating from 1 to *sqrt*(*n*). After that you need to write some function that checks whether two numbers has same digits. This also can be done using simple loops. 10 11 ### [220A - Little Elephant and Problem](/contest/220/problem/A "Codeforces Round 136 (Div. 1)") 12 13 There are multiple possible solutions for this problem. For example, the following. Find the last index *x* such that there exists some *y* (*y* < *x*) (minimal possible) that *A**x* < *A**y*. Then you just need to try two possibilities — either swap *A**x* and *A**y*, or don't change anything. 14 15 ### [220B - Little Elephant and Array](/contest/220/problem/B "Codeforces Round 136 (Div. 1)") 16 17 This problem can be solve in simpler *O*(*NsqrtN*) solution, but I will describe *O*(*NlogN*) one. 18 19 We will solve this problem in offline. For each *x* (0 ≤ *x* < *n*) we should keep all the queries that end in *x*. Iterate that *x* from 0 to *n* - 1. Also we need to keep some array *D* such that for current *x* *D**l* + *D**l* + 1 + ... + *D**x* will be the answer for query [*l*;*x*]. To keep *D* correct, before the processing all queries that end in *x*, we need to update *D*. Let *t* be the current integer in *A*, i. e. *A**x*, and vector *P* be the list of indices of previous occurences of *t* (0-based numeration of vector). Then, if |*P*| ≥ *t*, you need to add 1 to *D**P*[|*P*| - *t*], because this position is now the first (from right) that contains exactly *t* occurences of *t*. After that, if |*P*| > *t*, you need to subtract 2 from *D**P*[|*P*| - *t* - 1], in order to close current interval and cancel previous. Finally, if |*P*| > *t* + 1, then you need additionally add 1 to *D**P*[|*P*| - *t* - 2] to cancel previous close of the interval. 20 21 ### [220C - Little Elephant and Shifts](/contest/220/problem/C "Codeforces Round 136 (Div. 1)") 22 23 Each of the shifts can be divided into two parts — the right (the one that starts from occurrence 1) and the left (the rest of the elements). If we could keep minimal distance for each part, the minimal of these numbers will be the answers for the corresponding shift. Lets solve the problems of the right part, the left will be almost the same. 24 25 Let we have some shift, for example 34567[12] and the permutation *A* is 4312765 and *B* is 2145673, then shifted *B* is 4567321. Let we keep two sets (*S*1 and *S*2). The first will keep all the distances from integers in current left part to the corresponding positions in *A* (for the example above, it is \texttt{2, 4}). When you come to the next shift, all integers in *S*1 should be decreased by 1 (that is because all distances are also decreased by 1). But now some integers in set may be negative, when any negative integer occures (it always will be -1) you need to delete it from *S*1 and put 1 to the *S*2. Also after shifting to the next shifts, all integers in *S*2 must be increase by 1. After that, for any shift, the answer will be minimum from the smallest numbers in *S*1 and *S*2. 26 27 It was very useful to use standart "set" in C++. 28 29 ### [220D - Little Elephant and Triangle](/contest/220/problem/D "Codeforces Round 136 (Div. 1)") 30 31 Let iterate all possible points that, as we consider, must be the first point. Let it be (*x*;*y*). Let the second and the third points be (*x*1;*y*1) and (*x*2;*y*2). Then the doubled area is |(*x*1 - *x*)(*y*2 - *y*) - (*x*2 - *x*)(*y*1 - *y*)|. We need this number to be even and nonzero. For first we will find the number of groups of points that are even, after that just subtract the number of groups with area equal to zero. 32 33 For the first subproblem, we need to rewrite our formula. It is equal to |*x*(*y*1 - *y*2) + *y*(*x*2 - *x*1)|. Since we know *x* and *y* and we just need to check parity, we can try all possible 24 values of parity of *x*1, *y*1, *x*2 and *y*2 (let it be *d*0, *d*1, *d*2 and *d*3, respectively). And check whether they will form a 0 after multiplications and taking modulo 2. If it froms a 0, then add to the answer value *cx**d*0*cy**d*1*cx**d*2*cy**d*3, where *cx**d* is equal to the number of integers between 0 and *n*, inclusve, that modulo 2 are equal *d*. *cy**d* is the same but in range [0..*m*]. 34 35 Now we need to subtract bad groups — the ones that has the area equal to zero. This means that they will either form a dot or a segment. If it is segment, we can just iterate *dx* = |*x*1 - *x*2| and *dy* = |*y*1 - *y*2| instead of all 4 coordinates. Then the number of such segments on the plane will be (*n* - *dx* + 1)(*m* - *dy* + 1). Also for counting the number of triples of points on the segment you need to find the number of integer coordinates on the segment. It is well-know problem, and the answer is *gcd*(*dx*, *dy*) + 1. 36 37 This gives us, with some simple optimizations, and *O*(*nm*) solution. 38 39 ### [220E - Little Elephant and Inversions](/contest/220/problem/E "Codeforces Round 136 (Div. 1)") 40 41 In this problems you can use a method of two pointers. Also some RMQ are required. If you do not know about RMQ, please, read about it in the Internet before solving this problem. 42 43 Firstly, map all the elements in the input array. After that all of them will be in range [0..*n* - 1]. We need to keep two RMQs, both of size *n*. Let the first RMQ be *Q*1 and the second *Q*2. *Q*1*i* will contain the number of numbers *i* in current left subarray. *Q*2*i* will contain the number of numbers *i* in the left subarray. Firstly, add all *n* number to the *Q*2. After that iterate some pointer *r* from *n* - 1 downto 1, by the way keeping point *l* (which, at the beggining, is equal to *n* - 1) Using RMQs, you can keep the number of inversions when you decrease *r* or *l* (using "sum on the range" operation). While the current number of inversions is more then *k* and *l* ≥ 0, decrease *l*. Then for each *r* the answer of correct *l* will be *l* + 1 (considering 0-based numeration). 44 45 This makes the algorithm working in *O*(*NlogN*) time with correct realisation.