Блог пользователя adedalic

Автор adedalic, история, 7 лет назад, перевод, По-русски

Для начала, извиняюсь за проблемы с раундом и задачей Div1A/Div2C в частности. Это был очень важный раунд для меня и, как это иногда бывает, что-то пошло не так. KAN уже написал по данному поводу.

В любом случае, есть задачи, а значит нужны решения. Разбор на русском запоздал, но все же. Также, большинство разборов будут концентрироваться на ключевых идеях решения.

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
Разбор задач Codeforces Round 421 (Div. 1)
Разбор задач Codeforces Round 421 (Div. 2)
  • Проголосовать: нравится
  • +200
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

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

»
7 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Yes, i will elaborate this with next update

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    Reference for offline addition(407C)

»
7 лет назад, # |
Rev. 4   Проголосовать: нравится +6 Проголосовать: не нравится

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

»
7 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

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

»
7 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone elaborate more accurately on Div2 D?

»
7 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

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

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Can you explain your solution a bit more?

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится +7 Проголосовать: не нравится

      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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 лет назад, # |
  Проголосовать: нравится +64 Проголосовать: не нравится

Объясните пожалуйста непонимающему человеку. В чём прикол в код авторского решения вставлять все эти ваши 100-строчные шаблоны? Это понты какие-то или что? Когда я смотрю код решения, я хочу видеть простые 50 строчек решения, а не 200 строк, в которых я ещё должен искать основной код. И уж тем более я не хочу видеть использование этих шаблонов в основном коде, потому что искать потом, что ваши форны означают, не доставляет никакого удовольствия. Никому ваши шаблоны не интересны, оставляйте их, пожалуйста, при себе.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится -18 Проголосовать: не нравится

    А теперь представьте себя на месте автора, который никогда не задумывался о том, что вы написали, а теперь видит ваш комментарий.

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится +13 Проголосовать: не нравится

      Это обращение скорее не конкретно к этому автору, а ко всей аудитории этого сайта. Я очень часто такое вижу в разборах. Я постарался максимально описать моё недовольство этим. Надеюсь, что авторы, прочитав это, будут всё-таки не лениться и не копировать чисто свой код из своей среды, а всё-таки немного его обрабатывать, чтобы сделать более читаемым. Эти 2 минуты, ушедшие у автора на удаление ненужного, могут сделать жизнь нескольких тысяч человек чуточку проще.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    Да это везде так, на остальных сайтах в эдиториалах код такой же нечитаемый. Традиция...

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +21 Проголосовать: не нравится

Автор россиянин, а разбор только на английском...

UPD. Так то лучше)

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Автокомментарий: текст был переведен пользователем adedalic (оригинальная версия, переведенная версия, сравнить).

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +48 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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