sorry that I failed to prove it when b < a,seems complex...I just think it will be a loop; and when b >= a or the length is short,the answer is sure.there are some mistakes before,now corrected and some comments added

An alternative to handle the even case of 1E is to split the graph into 2 equal disjoint sets A and B. Label the elements a_1 to a_m, b_1 to b_m.

Use induction to construct for A and B separately. What remains is the complete bipartite graph, which can be done using cycles of length 4, all of form a_i — b_(i+j) — a_(i+1) — b_(i+j+1), where i and j run from 1 to m, and indices are taken modulo m.

Here's my submission Well, if you want to find closest angle to a, you just need to find an arc with angle most close to 2*a which its vertices are vertices of polygon. For doing this, you make your ideal arc (with angle 2*a) and find the vertex in polygon which is closest to end of arc.

The solution for Div 2 B is really nice. My solution was to construct the polygon then use dot products and a binary search to find the best end point with two other points fixed. It was really tedious and time consuming to code and had a worse complexity of .

To calculate the weighted sum of the (i+1)-th shift from the i-th shift, the elements which holds i >= a[i] (named as gt in the code) contributes +1 to the sum and i < ai contributes -1. That being said, we could solve the problem by simply maintaining the amount of elements which holds true on the above cases.

For non-tail elements, we could easily tell the moment of i < a[i] becomes i >= a[i] is a[i] — i, meaning that we need to account for this moments later.

For the tail element at the moment, we shall recalculate its contribution and placing a new update for it. Note that as a[i] <= n, it is guaranteed that n > a[tail] holds true, so we shall remove one from gt before the update. Same for updating lt for the tail.

As upd[time] stores the pended update for moment "time", upd[time]++ means that there will be one extra element will switch from contributing -1 to +1. As we are placing a[tail] back to the front, after a[tail] iterations it will hit a[index] == index, therefore we shall place an update on time = a[tail] + current_time = a[tail] + i.

I interchanged line 10 & 11 of the original code and it passed all cases, the TLE is most likely caused by initializing the array with size n+2 before n is properly initialized.

For the sake of competitve programming, I would recommend you to use array that has fixed size instead of referring their sizes to variables -- This increases memory usage in practical cases but we only care about the worse case scenario here.

Yea. I figured it out just after submitting the solution in java. In c++ uninitialized variables contain garbage values and no compilation error, however in java you need initialise it. BTW thanks for giving this nice concept to solve the problem

I think my solution of div2D/div1B is a bit easier. We can see that after a cyclic shift our array is always divided into two parts: shifted part and not shifted part. So i just simulated the process. All wee need is two maintain two values for each of two parts: number of such elements that |p[i] — i| will increase after shifting, and number of elements for which this value will decrease. Complexity is O(n). My explanation isn't very good, but code may help you 28121452

Here I will suggest a way of constructing a solution for n, given any solution for n - 2. No properties are requested and required to be preserved by such induction.

You still need to solve cases for n = 3 and n = 4 to get the full solution.

The method:

Let's take two nodes, let them be s = n - 1 and t = n for simplicity of numbering.

Consider following paths:

...

(this one is special from above).

Taking any two of such pathes you form a valid cycle of length 3 or 4. Match listed paths into cycles the following way: first with second, second with third, ..., pre-last with last, and last with first (in other words, match by neighbourhood).

Add such cycles to the answer. Note, that each listed path was used twice by construction above, this way all edges connected to s or t were used twice too.

This solution is absolutely brilliant! Thanks for sharing it with us. It's unexpectedly elegant and easy to understand. I've looked for an induction approach but failed to think about it for long because it was hard for me to imagine that I might be able to keep the old construction in its exact form, without some strange alterations.

why editorial link is not there on contest page . I know there is issue with div2-c question but rest of the question are good , i learned and solved div2-d and e(almost done) . good work in editorial and contest adedalic . thumbs up for good work.

There is my solution to 819A - Mister B and Boring Game First of all, I guess the color for each segment a is ascending or descending. And for each segment b its color is the minimum or maximum value of the previous paragraph a. We can easily make the interval between l and r no more then 5*(a+b). Thus, you can enumerate the status of each segment and calculate the current answer. Maybe I can't describe it clearly.This is my submission:28097355

For each segment a is increasing then as you said: "for each segment b its color is the minimum or maximum value of the previous paragraph a.", so I separate it to two situation: If a<=b then get the minimum/maximum value in section[lst+1,lst+a] else get it in section[lst+b+1,lst+a].