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

Автор DatVu, история, 4 года назад, По-английски

We hoped you find our problems interesting. We apologize for the late editorial. Hopefully you were still able to enjoy our contest.

Anyway, here are the tutorials for each of the problems:

1397A - Juggling Letters

If the total number of occurrences of some character $$$c$$$ is not a multiple of $$$n$$$, then it is impossible to make all $$$n$$$ strings equal — because then it is impossible for all $$$n$$$ strings to have the same number of $$$c$$$.

On the other hand, if the total number of occurrences of every character $$$c$$$ is a multiple of $$$n$$$, then it is always possible to make all $$$n$$$ strings equal. To achieve this, for every character $$$c$$$ we move exactly ((the total number of occurrences of $$$c$$$) $$$/$$$ $$$n$$$) characters $$$c$$$ to the end of each string, and by the end we will have all $$$n$$$ strings equal each other.

We can easily check if the condition satisfies by counting the total number of occurrences of each character $$$c$$$ and check its divisibility by $$$n$$$. The final complexity is $$$O(S \cdot 26)$$$ or $$$O(S)$$$ where $$$S$$$ is the sum of lengths of all strings.

C++ solution
Python solution

1397B - Power Sequence

First of all, the optimal way to reorder is to sort $$$a$$$ in non-decreasing order.

Proof

From now on, we assume $$$a$$$ is sorted in non-decreasing order.

Denote $$$a_{max} = a_{n - 1}$$$ as the maximum value in $$$a$$$, $$$f(x) = \sum{\lvert a_i - x^i \rvert}$$$ as the minimum cost to transform $$$a$$$ into $$${x^0, x^1, \cdots, x^{n-1}}$$$, and $$$c$$$ as the value where $$$f(c)$$$ is minimum.

Note that $$$c^{n - 1} - a_{max} \le f(c) \le f(1)$$$, which implies $$$c^{n - 1} \le f(1) + a_{max}$$$.

We enumerate $$$x$$$ from $$$1, 2, 3, \dots$$$ until $$$x^{n - 1}$$$ exceeds $$$f(1) + a_{max}$$$, calculate $$$f(x)$$$ in $$$O(n)$$$, and the final answer is the minimum among all calculated values. The final complexity is $$$O(n \cdot max(x))$$$.

But why doesn't this get TLE? Because $$$f(1) = \sum{(a_i - 1)} < a_{max} \cdot n \le 10^9 \cdot n$$$, thus $$$x^{n - 1} \le f(1) + a_{max} \le 10^9 \cdot (n + 1)$$$. When $$$n = 3, 4, 5, 6$$$, $$$max(x)$$$ does not exceed $$$63245, 1709, 278, 93$$$ respectively; so we can see that $$$O(n \cdot max(x))$$$ comfortably fits in the time limit.

C++ solution
Python solution

1396A - Multiples of Length

In this problem, the answer is rather simple. Here is one possible solution to this task.

Solution for n = 1
Solution for n != 1
C++ solution
Python solution

1396B - Stoned Game

Let us denote $$$S$$$ as the current total number of stones.

Consider the following cases:

Case A: There is a pile that has more than $$$\lfloor \frac{S}{2} \rfloor$$$ stones.

The first player (T) can always choose from this pile, thus he (T) is the winner.

Case B: Every pile has at most $$$\lfloor \frac{S}{2} \rfloor$$$ stones, and $$$S$$$ is even.

It can be proven that the second player (HL) always wins.

Proof 1
Proof 2

Case C: Every pile has at most $$$\lfloor \frac{S}{2} \rfloor$$$ stones, and $$$S$$$ is odd.

The first player (T) can choose from any pile, and we arrive back at case B where the next player to move loses.

So the first player (T) wins if and only if there is a pile that has more than $$$\lfloor \frac{S}{2} \rfloor$$$ stones or $$$S$$$ is odd. This can be easily checked in $$$O(n)$$$.

C++ solution
Python solution

1396C - Monster Invaders

In this problem, it is useful to note that when the boss only has $$$1$$$ hp left, just use the pistol because it has the least reloading time. So there are 3 strategies we will use when playing at stage $$$i$$$ $$$(1 \le i \le n)$$$:

  • Take $$$a_i$$$ pistol shots to kill first $$$a_i$$$ monsters and shoot the boss with the AWP.
  • Take $$$a_i + 1$$$ pistol shots and move back to this stage later to take another pistol shot to finish the boss.
  • Use the laser gun and move back to this stage later to kill the boss with a pistol shot.

Observation: We will always finish the game at stage $$$n$$$ or $$$n - 1$$$. Considering we are at stage $$$i$$$ $$$(i \le n - 1)$$$ and the boss at both stage $$$i$$$ stage $$$i - 1$$$ has $$$1$$$ hp left, we can spend $$$2 * d$$$ time to finish both these stages instead of going back later, which costs us exactly the same.

Therefore, we will calculate $$$dp(i,0/1)$$$ as the minimum time to finish first $$$i - 1$$$ stages and 0/1 is the remaining hp of the boss at stage $$$i$$$. The transitions are easy to figure out by using 3 strategies as above. The only thing we should note is that we can actually finish the game at stage $$$n - 1$$$ by instantly kill the boss at stage $$$n$$$ with the AWP so we don't have to go back to this level later.

Answer to the problem is $$$dp(n, 0)$$$. Time complexity: $$$O(n)$$$.

C++ solution
Tutorial is loading...
C++ solution
Tutorial is loading...
C++ solution
Разбор задач Codeforces Round 666 (Div. 1)
Разбор задач Codeforces Round 666 (Div. 2)
  • Проголосовать: нравится
  • +112
  • Проголосовать: не нравится

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

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

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

Awesome contest and nice problems! The pretests on Div 2 B were a bit weak.

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

    Good or bad thing? I think it's interesting because some people prefer weak pretests for hacking potential, while others prefer strong pretests to have the peace of mind that their submissions are right or wrong.

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

    Sorry for that. I tried to create strong pretests, but couldn't do much with only 5 pretests allowed.

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

      atoiz Can you The cost to transform ai to ci is |ai−ci|, and |ai−ci|+|aj−cj|≤|aj−ci|+|ai−cj| when i<j and ai≤aj, thus it is optimal to have ai≤aj for each 0≤i<j<n. elaborate this more?

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

About time

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

What happens if in Stoned Game we are allowed to remove non-empty pile of any size?

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

    Whatever i said was incorrect. The rule that a person cannot choose from the same pile as the previous turn invalidates whatever i said

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

    It will be equivalent to nim because in nim game, strategy to win always doesn't contain any move that remove stones from the piles in the immediate previous turn.

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

Thank you for lightning fast editorial

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

Stoned game problem is very much similar to CF round 577 div 2 B. If you submit that problem solution just changing YES->HL and NO->T, you will get AC on this problem which is D type of Div2.

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

    In that problem the solution might be same but there we are the ones who select both i and j right but in this case both persons try to counter each other. So if you are able to observe that it does not matter much here. Then yeah both are same problems

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

      Yeah right, Both are exactly same problems for those who can observe the intuition behind the problem.

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

      One more thing that I found different, was that the player can't choose piles from the immediate previous turn. Why this fact doesn't affect the solutions to the problems?

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

        One turn of selecting i and j in that problem is equivalent to two moves in the game where one person selecting pile i and the other pile j.

        I hope this is what you were trying to ask

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

    Great observation ! It shows that how you think of a problem drastically changes the way you try to solve it..

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

Why is the code for Div 1. C so complicated? There are 2 dp recurrences, one from dp[i] to dp[i+1], and one from dp[i] to dp[i+2].

$$$upd(dp[i+1],dp[i]+d+r1*arr[i]+r3);$$$

$$$upd(dp[i+2],dp[i]+4*d+min(r1*(arr[i]+1),r2)+r1+min({r1*(arr[i+1]+2),r1*(arr[i+1])+r3,r2+r1}));$$$

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

    They store the remaining HP of the boss of the previous stage. The recursion is still overcomplicated, as this suffices:

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

      I cannot understand why we need the case with odd-length sequence. Since when we have the odd length sequence we should convert it to an even length sequence based on the fact that the cost of one attack >= 2 attack and if we have an odd sequence then we are going to spend 2*d anyway so why not just make it an even length sequence?

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

        Good observation, though you still need to handle the case where you attack once at $$$n$$$ and turn around. 91777714

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

          Thanks for the help! Actually I was missing the case

          (dp[n][1] + d) + d*(n-1))
          

          when outputting the final answer which I think is required if we have an odd number of levels and we end up taking attack 2 on all of them. This was giving me wrong answer.

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

Div1C: 'We will always finish the game at stage n or n−1.'

Why is that so? Why is something like, let's say (for n = 5)

1 -> 2 -> 3 -> 4 -> 5 -> 4 -> 3 -> 2, not possible?

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

    It is possible, but isn't optimal.

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

      It can be optimal. What we know is that there always exists an optimal solution that finishes at $$$n$$$ or $$$n-1$$$.

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

    It's just that there is an equivalent solution that finishes at n or n-1

    In your case it would be

    1-->2-->3-->2-->3-->4-->5-->4

    And this comes from the obersvation that if you have unfinished business at position X (so you have to come back at X), then you don't face the same issue for X+1 (because from X+1 you go back to X then to X+1)...

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

The editorial doesn't include intuition for Div2C/Div1A.

Here is my intuition: First I started thinking about how many operations an element needs.

(1) With one operation it is possible if the len divides the element.

(2) With two coprime operations it is always possible.

Ok, so (2) seems like the most viable method. Now how do we do two coprime operations on each element? We can do two operations of length n, but we need them to be coprime. Okay, so how about one operation with length n and one with length n-1. Now we can use the third operation with length 1 on the remaining element.

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

i solved B using ternary search

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

Good tutorial,but the div2E's solution is really vague,"why We will always finish the game at stage n or n−1"? The explaination can never be understood,let's think we have n = 5,1->2->3->4->5->4->3->2->1 must be some data set's optimal solution.I am silly,and you guys can downvote me for my rudeness, but I really hope the offical can explain this clearly!!!

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

    Your strategy can also be optimal, but actually you can finish the game at stage n or n — 1 with exactly same moving cost. In your example, instead of following the path 1 -> 2 -> 3 -> 4 -> 5 -> 4 -> 3 -> 2 -> 1, you can move like this: 1 -> 2 -> 1 -> 2 -> 3 -> 4 -> 3 -> 4 -> 5. By moving like this, it's so much easier for us to transit between DP states.

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

      Firstly,really thank you for your forgiveness on my rudeness and your deeper explaination. I somehow understand what you have said,and I will have a try.Thanks for your explaintion and patientenss again!(sorry for my poor English....)

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

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

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

How to solve Div 1B/2D if the player can't choose a pile that he/she has chosen in his/her previous turn.

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

In Power Sequence, I tried to find the sum of first n terms of GP such that the first term is always 1 and the common difference keeps incrementing in a loop until it exceeds the limit as specified in the editorial above and inside that loop I'm storing the minimum of the difference of the sum of n terms of GP and the total sum of the array but this logic is not passing the test cases, can someone help me find fault in this logic.

Sn = 1*(pow(i, n) / (i — n)), res = min(res, Sn — arr_sum)

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

    The difference between the sum of n terms and the total sum of the array isn't necessarily the sum of absolute difference between $$$a_j$$$ and $$$i^j$$$ for all $$$0 \le j \le n - 1$$$. You can check it for yourself.

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

In B problem, if the input is suppose 1,13,4 is this not already power sequence but according to the solution provided the cost will come as 3, as first we will sort and then increment 13 to 16 so the array becomes 1,4,16. Can somebody please tell me, am i getting something wrong from the quetion is'nt the cost supposed to be zero?

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

    I think you should reread the statement for further information. Remember the common base c must be a positive integer.

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

In the div1 D editorial: "It can be proven that $$$f_x$$$ is non-decreasing, i.e. if $$$x<y$$$, then $$$f_x<f_y$$$." There is a minor typo — $$$f_x \le f_y$$$

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

I might be wrong, but the problems (B, C, D) should've been cyclically rotated. D felt easier than B and C to me.

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

in problem stones game , what happend if the stones are 2,2,2 . according editorial HL is winner but according to sample problem T is winner, so it is wrong solution

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

    I don't see [2,2,2] in the sample problem...You may have misread something...

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

Thank you for the fastest tutorial!

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

In div 1 C editorial there is a small typo: "Therefore, we will calculate $$$dp(i,0/1)$$$ as the minimum time to finish first $$$a_i−1$$$ stages.."

It should be "the minimum time to finish first $$$i-1$$$ stages.."

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

Can someone give a simpler proof for Stoned Game.I am not able to understand that when each pile has atmost sum/2 stones then how can we find the solution.Why it does not depends on the order of moves and only on the parity of total stones.

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

    It'll be helpful to break the question into smaller parts and analyse it. Firstly, it is easy to see that if there is an element greater than $$$\lfloor \frac{sum}{2} \rfloor$$$, T can keep picking from it and win. Secondly, if we ignore the rule that one cannot choose a pile that has been chosen in the previous turn, it is easy to see that the solution depends on the parity of $$$sum$$$.

    Now the interesting part — if initially there is no element greater than $$$\lfloor \frac{sum}{2} \rfloor$$$, either of the players can ensure that this condition will never hold on the other player's turn, i.e. there will never be an element greater than $$$\lfloor \frac{sum}{2} \rfloor$$$ on the other player's turn (think about how this can be done). Who will want to ensure this condition will not hold? Simple, it will be the player who would win if there was no restriction on the pile to be chosen, so if parity of $$$sum$$$ is $$$1$$$ it will be T else it would be HL.

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

I am sorry for asking late but I am not able to understand div1 C/div2. E. The boss can move to adjacent stages , does it mean the boss can move to adjacent only one time then return to its original state?

In editorial, it is said that return to this stage later to kill the boss. I didn't get how will we always find the boss in this stage and will it always be optimal.

Thanks in advance:)

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

in stone game, greedily choose the pile with most stones (for both players) seems to be able to AC.. 109435178

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

1C is bashable even if you don't notice the observation in the editorial and the fact that r1 <= r2 <= r3. submission: https://codeforces.com/contest/1396/submission/122626784

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

Base case: If Max Pile Size > TotSum/2 -> First Player Wins

If this is not the case, Max Pile <= TotSum/2 Then for First Player,its optimal to choose stone from the Max Pile, since it reduces LHS by one and RHS by 0.5 What is the best move for second Player ? Let say piles are sorted in non-increasing order s1>=s2>=s3>s4

Second Player has just make sure this inequality holds. He can make sure that inequality holds by reducing the next pile i where si> (TotSum/2) [the piles from 1 to i-1 has been removed] How can he do it ? By using all his moves which is (s1+s2+s3..s(i-1))/2 to remove from si we know si< RemSum(i+1..n)/2+ (s1+s2+...si-1)/2 si-(s1+s2+..si-1)/2 < remsum[i+1...n]/2

so second player always has a way not to break the inequality and more over second player has more moves than first as Max-Pile<rem Piles

This goes on.. and finally 2 piles are left

Answer basically depends on 2 piles size