mainyutin's blog

By mainyutin, history, 2 years ago, translation, In English

1955A - Yogurt Sale

Idea: mainyutin, prepared: mainyutin

Tutorial
Solution

1955B - Progressive Square

Idea: ssor96, mainyutin, prepared: Vladosiya

Tutorial
Solution

1955C - Inhabitant of the Deep Sea

Idea: ssor96, Vladosiya, prepared: mainyutin

Tutorial
Solution

1955D - Inaccurate Subsequence Search

Idea: mainyutin, Vladosiya, prepared: mainyutin

Tutorial
Solution

1955E - Long Inversions

Idea: ssor96, prepared: Vladosiya

Tutorial
Solution

1955F - Unfair Game

Idea: mainyutin, prepared: mainyutin

Tutorial
Solution

1955G - GCD on a grid

Idea: ZergTricky, prepared: mainyutin

Tutorial
Solution

1955H - The Most Reckless Defense

Idea: VManoshin, prepared: mainyutin

Tutorial
Solution
  • Vote: I like it
  • +92
  • Vote: I do not like it

| Write comment?
»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Thanks for the editorial.

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

Too dumb to understand H

»
2 years ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

Hacks in problem G were especially too much in this contest Right?

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

    Sorry but I'm a newbie here. How it can be hacked? I do not see any using of hashing

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

      Newbie? Anyways, hacking is when someone finds a counter-example/test which the code fails to answer correctly, on time or exceeds memory limit. Basically, when someone hacks someone's solution/code which passed all tests with "Accepted", they find a testcase following the constraints, however returns wrong verdict when processed by the "solution".

»
2 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Can anyone explain this swap preparation

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

    It often is a good idea to let someone else prepare the problem so that in case mistakes happen in terms of the correctness of the solutions, more people are aware of what is going on.

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

      What does preparation mean in this context?

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

        Create strong test data, alternative solutions and checkers/validators. In this contest, one validator was messed up but it was fixed later on.

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

Can somebody please tell me why my submission for B 255683892 got TLE in system testing?

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

    Unordered map's worst case time complexity is $$$O(N^2)$$$ due to collisions. Use ordered map for the same.

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

      Thank you.

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

      So should we always use ordered map?

      • »
        »
        »
        »
        2 years ago, hide # ^ |
        Rev. 4  
        Vote: I like it +5 Vote: I do not like it

        A rule of thumb is to avoid using unordered map whenever you can.

        1. Verify if you can avoid the unordered map by using an array/matrix as a table to index the elements directly. If you can't use directly, sometimes you can normalize the values to make it possible. If the problem has multiple test cases, remember to analyze the complexity to clean up the table between the cases.

        2. If you can't use an array by any reason (memory, hard to code, complexity to clean/reuse...), analyze the worst case complexity with ordered map. If it's clearly enough to pass within the time, then use it. The complexity is more predictable and stable. There is no reason to exchange the "not so fast ordered map, but guaranteed AC" by a "MAYBE faster unordered map, but also MAYBE TLE" during the contest.

        3. If you really need/want to use the unordered map, keep in mind that the test cases can blow up the average unordered map solution, getting TLE sometimes. Hopefully, you can try to avoid this by making some changes in the hash function. You can read more about here: Blowing up unordered_map, and how to stop getting hacked on it

        EDIT: Changed the order and added some explanations.

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it -11 Vote: I do not like it

    Just for TLE issue, there are two things you can do to make it run faster:

    1. cin / cout for large amount of data is slow, check here https://mirror.codeforces.com/blog/entry/6251 and use std::ios::sync_with_stdio(false); in your code. You can check samples of other user's submissions to see how they use this to speed up input efficiency.

    2. Don't dynamically allocate arrays in runtime as in int a[n*n], allocate a static array in advance, something like int a[510 * 510] at the beginning.

    I don't believe unordered_map is the issue.

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

      Thank you. But it seems like unordered_map was the issue because I submitted the same solution but with a map instead of an unordered_map and it got accepted.

»
2 years ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

Thank you for fast editorial

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

Can anyone help me with this ? 255764657 getting TLE.. thanks a lot

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

Has anyone solved the problem G with bfs?

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

For C there’s an easier way to think of it: instead of one Kraken, we have two krakens attacking from both left and right, with the left one dealing $$$ceil(\frac{k}{2})$$$ damage and the right one dealing $$$floor(\frac{k}{2})$$$ damage. We can then simulate their attacks independently and count number of ships sunken afterwards.

Code: my solution

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

Problem F has a simple $$$O(1)$$$ mathematical solution: 255716134.

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

    can u elaborate how u come up with this formula

    • »
      »
      »
      2 years ago, hide # ^ |
      Rev. 3  
      Vote: I like it +3 Vote: I do not like it

      It can be simplified to $$$\sum_{i=1}^{4}\lfloor\frac{p_i}{2}\rfloor + (p_1,\ p_2\ and\ p_3\ are\ all\ odd)$$$. You just have to count states that have zero xor sum, which can be expressed as $$$\vec p = (p_1, p_2, p_3, p_4) = (2n_1, 2n_2, 2n_3, 2n_4) + (n_5, n_5, n_5, 0)$$$ where $$$n_i$$$ 's are arbitary integers

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

      Greedy, first consider $$$n_4$$$, the number of $$$(100)_2$$$, $$$n_4$$$ should be even to make xor = 0, after erasing all number expect $$$(100)_2$$$, the last $$$\lfloor n_4 / 2 \rfloor$$$ sequences have 0 xor.

      Then consider $$$n_1, n_2, n_3$$$, it is evident that $$$n_1, n_3$$$ and $$$n_2, n_3$$$ should have the same parity to make xor = 0. Then you need to try to achieve that by erasing some numbers as shown in the code.

      The optimal solution is: first make $$$n_4$$$ even, then make $$$n_1, n_3$$$ and $$$n_2, n_3$$$ have the same parity, thus one zero-xor is obtained for every two erasing.

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

      I come up with similar formula base on the fact that: 1. The result of two XOR elements is 0 if and only if the two elements are equal; 2. The result of three XOR elements is 0 if and only if they are exactly (1, 2, 3); 3. The result of more than three XOR elements is 0 if and only if it can be divided into the sum of the several pairs and triple above. Finally, add a little greedy thought)

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Could someone explain why BFS TLEs with same logic for G?

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

For problem E, I wonder if there exists some other method to choose&reverse which is better? Or, why always choosing&reversing the first zero from left to right(or right to left) can examine whether a 01-string can be converted to all-1-string. Is there any provements? Thanks a lot.

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

    yaa same doubt

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

      i though something just now, lets take 110.... for example. so now since leftmost one is part of just 1 segment we cant do any operation on this, come to next 1, so due to previous segment there is no change on this 1 and segment starting from this 1 itself is the last one involving this 1 so cant do any operation again, now next 0, so previous segments have no effect. Segment starting from this element is the last one involving it so we have to do operation, no choice and then so on, we will have to take into consideration the operation applied on this zero for next k-1 elements.

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

F — Greedy Approach 255903652

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

What is the rate of problem E could it be below 1400 ?

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

This contest really was not made for python users, most of the solutions were giving tle for python answers and not for the exact same cpp solution

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

why is BFS getting TLE for problem G ?

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

what is the time complexity of this code?? I am. confused about how many factors will be there in a particular state dp[i][j] ?? 255662591

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

Hi, I try to use the logic of dijktra algorithm on porblem G. GCD on a grid.

But, I get wrong answer 2136th numbers differ - expected: '8', found: '4'

I can't figure out the bug: 255917561

Please, help

Thanks

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

    Creating vectors of size $$$10^6$$$ in $$$10^4$$$ test cases.

    $$$10^6 * 10^4 = 10^{10}$$$

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

Why O(N) solution of problem D is giving TLE in python? 255903393

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

I dont understand why A* and DFS (with early stopping) give TLE for G. Surely the number of nodes visited is similar to that in the tutorial (effectively doing a search from start to end for each divisor of g)

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

Can someone explain to me how the third example of question F works? I think the answer is 4.

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

    Why do you think the answer is 4? The best case scenario is having the same ones adjacent: 1 1 2 2 3 3

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

how to prove the num of divisor of N is N^(1/3)

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

    Not exactly what the tutorial said, but you can have a look at the function's growth rate.

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

    I don't think so they are trying to say that the number of divisors is N^(1/3), but they are trying to say that the number of divisors is not greater than that. Although for the past 15 mins I was also wondering how is the possible and where is the proof. If someone can explain me in simple terms I would be very thankful.

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

    There were problems about finding the numbers with maximal number of divisors till N, they solved through the backtracking typo generate p_{1}^q_{1}*p_{2}^q_{2}*...*p_{k}*q_{k}, where p_{i} is the i-th smallest number, and the number of divisors is a product of q_{i}+1

    So in generation the most non-trivial thing is to find out that q_{i-1} >= q_{i} because if several numbers with equal number of divisors we are interested in smallest ones, by it you can find that number with maximal number of divisors till 10^9 has 1344 divisors, and till 10^18 has ~1.03e5 divisors, but estimation is clearly rough because 1344 > 1e3

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

Can the twoer located in the enemie's path in the problem H? If it is posiible, why the solution of dp is right?(just one tower has the range of 0?)

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

    By default, all towers have a zero range, but the player can set a tower's range to an integer r (r>0), in which case the health of all enemies will increase by 3r. However, each r can only be used for at most one tower.

    it is mentioned r>0

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

      I know that, but when I use dp, in the final answer, no more than one tower will have the radius of 0. However, when some towers located in the enemy's path, as the promblem says(0 is default), those towers will damage the enemy with zero radius. It seems that the dp can not count this part of damage. Because my dp is weak, I want to konw that whether I misunderstand the problem or the dp can count all zero part in fact.

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

    Towers are located on empty cells

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

Hi guys currently stuck on problem H can't find the problem for this code(wa on test 4). Can anyone help me take a look? Thanks in advance!

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

    Great News guys,got accepted already.

    What was wrong:
  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    which topic to study for solving this problem

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

      If we're only talking about "TOPICS" then this is a pretty standard bitmask DP problem. However, There's an important observation that the maximum radius of a tower doesn't exceed 12, which LEADS to the method of bitmaskDP.

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

For G, why is the number of divisor the cube root of A

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

Problem G can be solved using brute force: store the list of gcds at each element and choose the maximum element at point (m,n) https://mirror.codeforces.com/contest/1955/submission/255975752

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

Hey anyone have solved Problem E using Segment Tree??

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

256004789

can someone please explain why am I getting time limit exceeding on test 3? in question C. Inhabitant of the Deep Sea and also tell where I went wrong please ;-;

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

very easy code for F 256008944

if both cnt[1] and cnt[2] are odds, just think them as one for cnt[3]

(01 XOR 10 = 11)

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

Can someone explain to me why my code for G, in which I use DP having a complexity of O(m.n.n) is not working. What I am doing is, I stored dp[i][j] as max gcd from (i,j) to (n,m).

For this, I update the dp as follows:

dp[i][j] = max(dp[i][j], gcd("gcd from (i,j) to (k,j)",dp[k][j+1]))

dp[i][j] = max(dp[i][j], gcd("gcd from (i,j) to (k,j)",dp[k+1][j]))

here is the code https://mirror.codeforces.com/contest/1955/submission/256013095

  • »
    »
    2 years ago, hide # ^ |
    Rev. 3  
    Vote: I like it +1 Vote: I do not like it

    It's my first approach in the problem but found an easy counterexample for it

    2 3 
    6 2 1
    3 6 4
    

    here ,by maximum $$$GCD$$$ logic, it will outputs $$$1$$$.

    (Since dp[2][2] = max(dp[1][2] = 2, dp[2][1] = 3) = 3 and dp[2][3] = max(1,1) = 1 )

    However, the correct answer here is $$$2$$$ by considering the $$$path$$$

    $$$(1,1)= \gt (1,2)= \gt (2,2)= \gt (2,3)$$$

    P.S:

    The above example is correct in you code, since you start from end

    consider the inverted version of the same example

    2 3
    4 6 3
    1 2 6
    

    your code outputs $$$1$$$, but answer is $$$2$$$ by the same $$$path$$$ described above

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

      Ohhh ok got it. Thanks. How do I know if a DP approach won't work in a program like any tips?

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

        Well, make sure the solution checks all the possibilities. It's convenient to try on small cases first and try to validate your solution on them.

        When you validate your logic, write the slow solution for transition and try to optimize until it fits the time limit.

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

Can anyone help me figure out why my solution to C got TLE? Seems O(n) to me.

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

    your code is O(k), dont subtract a[i] by 1 at a time, you can subtract it by batch

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

In question G we don't have to iterate through all divisors of g
We can maintain a list of factors
In each loop:
- Choose a search candidate in the list
- If exists a path, discard all values smaller than the candidate
- If not exists a path, discard all values which is multiplier of the candidate
https://mirror.codeforces.com/contest/1955/submission/256059578

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

For problem H, isnt that the enemy only visit certain cells in path, but cover() considers all cells in the range of radius R, right ???

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

    I forgot the fact that enemies will pass thorugh all # cells, i wias thinking in direction where enemies only pass through some # cells, and compilcated my problem..

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

    Thus, the maximum radius for the tower can be found from the inequality $$$500⋅π⋅r^2−3^r \gt 0$$$, assuming that the tower has a damage of pi=500. This estimate gives R=12

    Could you explain how come the Maximum radius is just 12?

    Inequality should be $$$500\times$$$(# of cells)$$$-3^r \gt 0$$$ => $$$500\times (50\times 50-1)-3^r \gt 0$$$ which gives $$$r=107$$$ right?

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

Can anyone tell why recursion is giving TLE for G? Even if I put this condition that dp[i][j]==true and visited[i][j]==true, return true it still gives tle This

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

Can someone help for the line below:

cout << ans + (dq.size() && dq.front() <= k) << '\n';

What does the (dq.size() && dq.front() <= k) do? What value does it add to ans?

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

    It’s for handling the last remaining element in the deque.

    Dry run the following test case for better clarity:
    5 9
    1 2 3 2 1

    If we didn’t had that line, answer computed by us would be 4 whereas the actual answer is 5.

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

why in the problem G the dp[i][j] = max(__gcd(dp[i][j+1] , curr_num) , __gcd(dp[i+1][j],curr_num))

doesnt work any test case

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

Does there exist any solution to problem G using some modification to dijkstra?

I tried submitting a similar approach to dijkstra but it got TLE (on test case 31) because unlike dijkstra, we can't greedily choose the best path with largest gcd for a given vertex and ignore other bad paths with less gcd for that vertex because those bad paths with less gcd further might become good paths. Here's the submission.

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

Damn, prob G we can check divisor of gcd(a11, anm) and check by dfs O(n * m * sqrt(MaxAij)). Enough to AC

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

I want to share an alternative solution for H, which can be solved nicely with a "by-the-book" MCMF implementation:

The key observation is indeed that there are not that many ranges feasible (at most 12). But the fact that two towers can't use the same range may point out the need for a matching kind of approach.

Further, as some choices bring cost (powers of 3) and some are reducing costs (more tiles being covered), then the problem can be modelled as flow graph with:

  • one source
  • K nodes representing the towers (linked to the source, with capacity 1 and no cost)
  • 12 nodes representing the powers of 3 (linked to the towers accordingly, with capacity 1 and the negative cost based on the power of the tower multiplied with the covered tiles using a specific radius)
  • one special node representing that we don't use a power of 3 (i.e. radius 0, linked to each tower, with capacity 1 and no cost)
  • a sink node connected to the powers of 3 (with the cost being the power of 3 and capacity 1) and to the special node (with no cost and an arbitrary large capacity)

Apply MCMF (min-cost, max-flow) algorithm and the answer is the cost straight-away.

https://mirror.codeforces.com/contest/1955/submission/257223979

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

Can someone elaborate why G has a time complexity of O(n*m*cubic_root(A))? I think it's O(n*m*sqrt(A)).

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

Can anyone tell why the following code for the problem G ~~~~~ Your code here... ~~~~~

1955G - GCD on a grid gives wrong answer 259895161

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

    The solution and the idea is wrong, you don't take gcd with dp values you take gcd of path (i.e the values given in the grid). Here you are taking $$$dp[i][j]$$$ = $$$max(dp[i][j], gcd(dp[i-1][j]/dp[i][j-1], a[i][j]))$$$ in this $$$dp[i-1][j]/dp[i][j-1]$$$ is storing max values instead of $$$(a[i-1][j] / a[i][j-1])$$$ you are taking gcd of max value and current value instead of $$$gcd(a[i-1][j]/a[i][j-1], a[i][j])$$$ which is wrong.

    Hint for correct solution:

    Hint-1

    Hint-2

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

D have a little strange

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

Hey, can anyone please help me understand why this submission 262094929 got TLE at tc35?

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

    hey did you understand the reason of TLE , I had same doubt

    263251053

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

      Vector initialization takes more time than creating a constant size array, so either create an array everytime. Or create a dp vector once, and then inside the for loop, reset it each time. This had solved the tle for me in my code.

»
19 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In problem H, I think the inequality $$$500\pi.r^2 - 3^r \gt 0$$$ means that the each tower should have positive contribution, why is this true? Can't some towers make up for others for examples?

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

All the solutions for the problem C that I've seen so far seem to be unnecessary difficult.

Kraken has k times to attack. If k is greater than sum of health of all ships, then no ships will left and the answer is n.

In other case, at least one ship will left. Front and back attacks alternate, so if k is even, Kraken can cause a k / 2 damage to both sides. And if k is odd, then make + 1 damage to the front, because Kraken starts attacking from the front. So we have ceil(k / 2) for the front and floor(k / 2) for the back.

Then just iterate from the front and from the back until Kraken can destroy a ship.

Solution

»
15 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

.

»
11 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

In Problem 1955D - Inaccurate Subsequence Search, why to remove element from Extra in the final loop from m to n, if Extra contains useless elements, so just keep it there. Don't check if old is present in Extra, simply check it for its presence in Done.

»
6 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

I solved H with the Hungarian algorithm but it took me a year and a half lol, but the complexity achieved is $$$O(R^3k)$$$.

Submission: https://mirror.codeforces.com/contest/1955/submission/349977128