### adedalic's blog

By adedalic, history, 4 years ago, First, I apologize for problems with round and serious problem with Div1A/Div2C. It was very important round for me and, as always, something goes wrong. KAN have already wrote about this.

Anyway, there are problems, so editorial must exists. Due some circumstances, Editorial will be upload in parts. Also, most of tutorials will contain main ideas how to solve task, not ready algorithm.

Tutorial is loading...
code
Tutorial is loading...
code
Tutorial is loading...
code
Tutorial is loading...
code
Tutorial is loading...
code
Tutorial is loading...
code
Tutorial is loading...
code Tutorial of Codeforces Round #421 (Div. 1) Tutorial of Codeforces Round #421 (Div. 2) Comments (36)
 » Auto comment: topic has been updated by adedalic (previous revision, new revision, compare).
 » But in both cases we must add 3 arithmetic progression to the segment of array d. Its well known task, which can be done by adding/subtracting values in start and end of segment offline.Can you please elaborate a bit(or point to some reference) on this well known task?
•  » » 4 years ago, # ^ | ← Rev. 2 →   I a general case when you need to add progressions and answer querys you can use a Lazy Fenwick tree, see this tutorial. Maybe someone knows how yo solve it whitout this structure, I don't.But in this specific case, you made updates but you have to answer the query just after all modifications are made. So you can made the updates using just 2 arrays
•  » » Yes, i will elaborate this with next update
•  » » Reference for offline addition(407C)
 » 4 years ago, # | ← Rev. 4 →   A"if...else if''solution for div2 C... 28115691
•  » » can you please explain your code??
•  » » » 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
•  » » A loop come brute force solution must work I guess. Basically logic I tried out is checking all strings where the repeating character introduced by B is any of the last max(1, a-b) characters. http://codeforces.com/contest/820/submission/28126243
 » 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.
 » 4 years ago, # | ← Rev. 2 →   Can someone please help me understand how Div2 Problem B can be solved in O(1) ?
•  » » 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.
 » can anyone explain div2d div1 b more thoroughly . i am not getting it
 » 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 .
 » Can someone elaborate more accurately on Div2 D?
 » For Div2D / Div1B, one could also keep the amount of elements which holds a[i] >= i to update the difference in O(1) time.Code with comments : http://codeforces.com/contest/819/submission/28113390
•  » » Can you explain your solution a bit more?
•  » » » 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.
•  » » Can you please tell me what does upd[i] hold? Actually I don't undersatand what this operation "upd[a[i]+i]++" is doing.
•  » » » 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 did it more stupid using segment tree, but calculating indexes carefully is a bit difficult, and each update in log time :Phttp://codeforces.com/contest/820/submission/28132944
•  » » Awesome solution! Thanks. I understood it and coded myslef but it's giving TLE! I dont understand how a O(n) solution gets TLE?! Code Can you please check it once why its happening
•  » » » Editted versionI 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
 » 4 years ago, # | ← Rev. 2 →   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
•  » » I did something similar: http://codeforces.com/contest/820/submission/28127444
 » Auto comment: topic has been updated by adedalic (previous revision, new revision, compare).
 » 4 years ago, # | ← Rev. 2 →   Abstract.Another (and probably more simple) approach to Div1E.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.Continue with n = n - 2 here.
•  » » 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.
 » 4 years ago, # | ← Rev. 2 →   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
•  » » The description said: "From multiple variants of t lexicographically minimal is chosen." Why each segment a is descending?
•  » » Your solution inspire me.This is my solution.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].
 » 4 years ago, # | ← Rev. 2 →   Here is my O(1) solution of 820B : 28500099
»
3 years ago, # |
Rev. 6

Could someone please tell me why my test's 6 output differs from my output in my consola ? I have correct output (that is 2 1 558 ) even though in my consola i see this ---> 2 1 584

That's the code

# include

using namespace std;

float n, a;

float kw, ks;

int main() {

ios_base::sync_with_stdio(false);

cin >> n >> a;

kw = ((n &mdash; 2) * 180) / n;

ks = kw / (n &mdash; 2);

int l = 0;

float mini = 180;

float p = 0;

int i = 0;

while (kw >= ks)
{
p = kw- a;

if (p < 0)p = -p;
if (mini > p)
{
mini = p;
l = i;
}
i++;
kw -= ks;
}
if (l == 0)
{
cout << "1" << " " << "2" << " " << "3";
return 0;
}
if (p == mini)
{
cout << "2" << " " << "1" << " " << "3";
return 0;
}
else
{
cout << "2" << " " << "1" << " " << n-l;
return 0;
}
return 0;


}

It was my first time posting and i did not know that after pasting the code will get deformed. Sorry.

 » 6 months ago, # | ← Rev. 2 →   I just wanted to share the way I solved 820B. For any polygon, we can easily find the sum of interior angles using formula. Since we are dealing with a regular polygon, each interior angle is equal. Now, considering that a regular polygon with n sides can be divided into n — 2 equal triangles, let us fix an interior angle and divide it into n-2 parts. We do this because, in a polygon, there are only n-2 different possible angles in increasing order of angle values as: {2 1 3}, {2 1 4},..., {2 1 n}. Other vertices combination yield the same set of angles due to inherent symmetry. So, by greedily checking through each multiple of the small angle (interior_angle/n-2) upto the full interior angle itself, we can find the one angle which is closer to the given angle 'a'. With regards to finding the vertices, this can again be done by simply adding our closer multiple's factor to '2' and altering the last value in our vertices set. This is because we have a fixed vertices set: {2 1 x}. Time complexity: O(n) submission Happy Coding! :)