adedalic's blog

By adedalic, history, 7 years ago, In English

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
  • Vote: I like it
  • +200
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it +12 Vote: I do not like it

Auto comment: topic has been updated by adedalic (previous revision, new revision, compare).

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

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?

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Yes, i will elaborate this with next update

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Reference for offline addition(407C)

»
7 years ago, # |
Rev. 4   Vote: I like it +6 Vote: I do not like it

A"if...else if''solution for div2 C... 28115691

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    can you please explain your code??

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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://mirror.codeforces.com/contest/820/submission/28126243

»
7 years ago, # |
  Vote: I like it +5 Vote: I do not like it

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.

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can someone please help me understand how Div2 Problem B can be solved in O(1) ?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
7 years ago, # |
  Vote: I like it +4 Vote: I do not like it

can anyone explain div2d div1 b more thoroughly . i am not getting it

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like 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 .

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone elaborate more accurately on Div2 D?

»
7 years ago, # |
  Vote: I like it +18 Vote: I do not like it

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://mirror.codeforces.com/contest/819/submission/28113390

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you explain your solution a bit more?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +7 Vote: I do not like it

      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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you please tell me what does upd[i] hold? Actually I don't undersatand what this operation "upd[a[i]+i]++" is doing.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I did it more stupid using segment tree, but calculating indexes carefully is a bit difficult, and each update in log time :P

    http://mirror.codeforces.com/contest/820/submission/28132944

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Editted version

      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.

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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

»
7 years ago, # |
Rev. 2   Vote: I like it +11 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by adedalic (previous revision, new revision, compare).

»
7 years ago, # |
Rev. 2   Vote: I like it +48 Vote: I do not like it

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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The description said: "From multiple variants of t lexicographically minimal is chosen." Why each segment a is descending?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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].