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

Автор vaaven, история, 15 месяцев назад, По-английски

2113A - Shashliks

Tutorial
Code

2113B - Good Start

Tutorial
Code

2113C - Smilo and Minecraft

Tutorial
Code

2113D - Cheater

Tutorial
Code

2113E - From Kazan with Love

Tutorial
Code

2113F - Two Arrays

Tutorial
Code
Разбор задач Codeforces Round 1031 (Div. 2)
  • Проголосовать: нравится
  • +60
  • Проголосовать: не нравится

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +42 Проголосовать: не нравится

IT WAS TOO HARD FOR ME :(

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +35 Проголосовать: не нравится

I love these fast editorials :D

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +36 Проголосовать: не нравится

I think that $$$A,B$$$ were a bit too tough. I liked the contest, though.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

As a newbie, I was cooked!!

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

guessforces -> got back to purple

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

someone can explain me: i didn't understand the D question 3rd test case

5
8 6 3 10 1 7 9 5 2 4

why i as a player can't swap 10 to 8 instead of 10 to 3 which is explained in ts.

if i swap 10 to 8, i collect more than 3 points.

swapping the max element to first card win you more game no ?

  • »
    »
    15 месяцев назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится
    8 6 3 10 1
    7 9 5 2 4
    -> (swap)
    10 6 3 8 1
    7 9 5 2 4
    -> (run)
    6 3 8 1
    7 9 5 2 4
    -> (run)
    6 3 8 1
    9 5 2 4
    -> (run)
    6 3 8 1
    5 2 4
    -> (run)
    3 8 1
    5 2 4
    -> (run)
    3 8 1
    2 4
    -> (done)
    

    Score: 2

    the game always ends after N rounds, not after one player ran out of cards.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Completely stuck in C for so long, was trapped thinking that it involved some graphs. It was really a tough contest.

»
15 месяцев назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +4 Проголосовать: не нравится

Had the ideas for D (BS on Ans, minimum on pr fix + max in suffix) independently. But couldn't even understand how to implement it all. Interesting contest nonetheless, a little hard after a while.

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

    Please,can you explain why this strategy is the most optimal?I thought about this problem and explanation for hours and didn't understand much

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

      Ok When we play n turns, what happens is us choosing x from a, and n-x from b. We can binary search on answer for x. Since every value in a after xth index (1-based) is unused for x selections from a, we can greedily select the max value amongst them and replace it.

      Now since we want it to reach x, we want the smallest value before it to be replaced with this new large value. Greedy choice

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

As a non-participate, I wonder why E seems harder than F.

Is F classic?

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

I upvote for superfast editorial

I enjoyed the problems.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

The ideas of A and B are not difficult, but they require a lot of case-by-case discussions, which makes writing them rather laborious. I don't particularly like encountering such problems on Codeforces. On the contrary, I enjoy problem D, as the thinking process accounts for the majority of the time, and the code implementation part is very simple.

  • »
    »
    15 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +29 Проголосовать: не нравится

    Wait really? B was basically a one-liner for me; I don't think there was any casework. Just cout << ((x1 != x2 && !((x1-x2) % a)) || (y1 != y2 && !((y1-y2) % b)) ? "YES" : "NO") << '\n';. Even if you want to split it into multiple lines so it's easier to read, it's still at most three lines. I think A was also more or less three lines.

    I agree with you that I enjoyed problem D, though.

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

pretty tough contest, D trapped me making me think i might get it, though couldn't think of anything except bruteforce in c.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

much respect for having n ^ 3 solutions pass on C , 2d prefix sum with out of bound stuff is pain

also you should add spoilers to the editorial got E spoiled for me

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

    how to do in n^3 ??

    .. I think brute force can be n^4 and I think my prefix sum approach is n^2 so not sure about n^3 technique

  • »
    »
    15 месяцев назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    For me, it wasn't an issue.

    I just used Min() and Max() functions appropriately.

    Spoiler
»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +25 Проголосовать: не нравится

Fast editorial, without spoiler. So what? trash mathforces + gridforces + guessingforces round.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can someone explain the problem C more clearly

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

It was really hard for me (-_-)

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

I can't believe D was binary search, I spent an hour during the contest trying to verify a greedy strategy.

  • »
    »
    15 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +11 Проголосовать: не нравится

    Actually you don't need binary search if you calculate prefix min, prefix 2nd smallest, and suffix max

  • »
    »
    15 месяцев назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    you don't need binary search. for all i from 1 to n just store the smallest and second smallest numbers in the subarray a1,a2,a3...ai. also store the maximums for the subarray ai,a(i+1),...an. and store the minimum for the subarray b1,b2,...bi. I think this much is enough.

    basically you need to take the first i cards from a, the first n-i+1 cards from b, check if you can make one swap such that the lowest number on these n+1 cards is one of the cards in b. if it is, it means that you can win at least i points if you play optimally, if not then you can't.

    I think this solution works, but I haven't coded it yet, so there is a possibility that I am wrong

    Edit: Coded this and the code got accepted

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

Задачи, начиная с Cшки интересные, не спорю, но вторая — просто самое ужасное, что можно было ожидать. Писать Келдыша 5часов и поторатить на B 40мин +- норм, но на двухчасовой див потратить 40+- минут не очень

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

Can anyone explain why this submission: 324507382 is giving TLE? I think the time complexity is $$$O(nlogn)$$$.

My check function uses dyanmic programming as follows: $$$dp[i][0]$$$ represents the minimum index with which $$$a_i$$$ can match if I have done no swaps in the first $$$i$$$ indices, and $$$dp[i][1]$$$ represents the same thing but considering that I have done one swap in the first $$$i$$$ indices, and swapped it with the maximum value outside the range I'm checking for.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

Can someone give me proof intution for D's correctness? I don't understand how the property mentioned in the first sentence of the solution helps.

  • »
    »
    15 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +38 Проголосовать: не нравится

    Here is my thought process:

    Suppose, you were able to get k points. That means that at least first k cards from your deck and at most first (n-k) cards from dealer's deck were removed. It is also necessary that there was some card in dealers' deck that "lost" to your minimal card that "won". Thus:

    minimum of your first k cards must be at least minimum of first (n-k+1) cards in dealer's deck. (+1 because the first card in dealer's hand to be untouched should be counted too)

    Two important points:

    1. this condition is sufficient to score at least k points.

    2. you can manipulate minimum of your first k cards by swapping two cards. in fact, the optimal way is to switch minimum of your first k cards with maximum of the rest.

    Thus, you can just find by binary search largest k for which:

    second minimum of $$$a_1,a_2,\ldots a_k, \max(a_{k+1},a_{k+2},\ldots a_n)$$$ is greater than minimum of $$$b_1,b_2,\ldots b_{n-k+1}$$$

    I, personally, handled edge cases (k=0 and k=n) separately. You can also just find corresponding prefix/suffix mins and maxs beforehand instead of doing binary search.

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

    Let's say our goal is just to win at least $$$t$$$ turns (Ignore that we are allowed to swap for the moment).
    Since we use a card only when we win a turn, only the top $$$t$$$ cards will ever be used, so we can discard the rest of our $$$n-t$$$ cards. Similarly, the dealer wants to win just $$$n-t+1$$$ turns here, so the dealer only brings their top $$$n-t+1$$$ cards.
    Now the new rules are: there are 2 piles with $$$t$$$ and $$$n-t+1$$$ cards, we play the game until one of piles is empty, and the empty pile wins. The pile with the minimum card will lose, because that card cannot ever win a turn. So its enough to check if $$$\min(a_1,\ldots,a_{t})$$$ > $$$\min(b_1,\ldots,b_{n-t+1})$$$. .

    If we are allowed to swap, the best move is to swap out our minimum card with the maximum from the rest of our discarded cards (if its better). Note that we don't really need to binary search here, as we can check this for each $$$t$$$ in $$$O(1)$$$ by maintaining prefix and suffix arrays for min, max, and 2nd min (or a std::set).

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +26 Проголосовать: не нравится

shashlik was not the only one who got cooked in this contest.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Someone please help me, my submissions for question-2 give error after error. Last submission[submission:324523587]

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

Why did D come up with the idea of binary search? Who can prove the continuity of the answer? I feel that I cannot understand the correctness of binary search.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Hi, I don't quite understand the thought process in D: How do we know the function is monotonous (and what function i.e. whats the argument)? Why is that obvious from the data given by the tutorial that we need to switch the minimum? Also, shouldn't the last index that is less than a_{k_{j}} be a_{k_{j+1}-1} and not a_{k_{j+1}} by its definition? Would appreciate the help.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

IMO F should be before D and E

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Although the solutions are simple, they're still hard to think during the contest...

»
15 месяцев назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I think my solution for E is similar to the editorial's but I approached it with a slightly different thought-process. I think it makes my solution a bit smaller than others' – still not sure of its time-complexity.

I initialise a flood-fill from x and repeated the following 2 steps until the flood reaches vertex y (or the flood dies down completely).

  • Simulate one step of flooding – the existing flooded vertices flood the immediately adjacent un-flooded vertices.
  • Dry-up the flood from the vertices where enemies would be at that time.

We can also think of this as Marat constantly spawning his clones and moving into every vertex he can to simulate the process of survival through brute-force.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

The problems are challenging. Great :)

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Thank You for div. But too many matrix problems

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Why are problems like A and B accepted to be at those positions :sob: ? I had to guess the solution for B and when the example was correct i just submited and hoped for acception hahaha

C was cool tho

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

Shorter solution for problem B

Code
»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

A different more complicated approach for D

Let's simulate the game on the original arrays

It is easy to observe that the only element of array A which could be swaped to improve the score is the last element of A which lost a game (Exercise for the reader). Let it's position be ls. So there are at most N-ls positions with which we could try swapping it with. (Actually there are even less because there is no need to swap it with an element that won in the original simulation)

Now the problem is how to calculate efficiently the score for every potential swap (There may be easier ways to do this, also I don't know if this is considered difficult to come up with and code or I just had skill issues during contest and debugging)

Let's split the obtainable score and cost (as in rounds of the game it takes to obtain it) to 3 parts

1) The score and cost till reaching position ls is the same for all swaps as it isn't affected by it and can be calculated during the original simulation

2) For every element i we swap with ls, until we find a smaller element after it in A the score and cost is only determined by how many extra rounds we needed to lose after ls to take i + the extra ammount of elements we take. We take as many as possible and if there are remaining rounds move to step 3

3) For the first remaining element we calculate the cost of taking it. Now for the remaining elements the added cost of taking each of them is irrelevant to the actual swap that happened and we can precalculate it if we simulate the game again after removing ls. We take the largest prefix we can

Time Complexity O(nlogn)

Sorry if my explanation isn't clear, you can find my ugly implementation here 324571068

Would be cm had I implemented this in contest (╥﹏╥)

  • »
    »
    11 месяцев назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Used a similar approach. Our logic is more to the point, but implementation is lengthy.

    Don't you think the idea of taking a maximum from suffix of "Is"( "Is" here is reference to coder3000's reply above) is a bit unexplained in the tutorial, or it seems intutitve??

    We can't swap from the previous segments or you can say prefix because it is always less than the previous minima(in "b" array), since it is less than the current minima(in "b" array")

»
15 месяцев назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

Can someone help with my TLE in F 324590873 . I thought I am visiting every edge exactly once in my dfs and thus code should have a tc of O(n).

Edit : I found the issue.

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

I solved B in more then hour, but solved C in 20 minutes after contest ;( B was kind of easy, but for me it was so hard to find right formula.

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

324503370 in this submission I just check to swap minimum in starting position result, but with all others maybe you can say any optimization to this algorithm or code

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +39 Проголосовать: не нравится

F can also be interpreted as euler path in each component, notice once u make a graph like edges from a[i] — b[i], we are sure every number can contribute to A or B, so we need to traverse every edge exactly once and everytime we enter through the edge give the first end point to A, and second end point to B, and to make euler tour possible, we need every degree even, which can be automatically handled by adding extra edges with 0.

324547251

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

About problem B: What if instead of two initial rectangles there were three or more rectangles and we are required to test if we can fill the grid with axb rectangles does anybody have insights or resources for a similar problem

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

    The tutorial shows that if there are two rectangles with either (x2 - x1) mod a != 0 or (y2 - y1) mod b != 0, then the pattern is determined uniquely.

    So if we have a set of placed rectangles {R1, ..., Rm} we either have two Ri, Rj with (xi - xj) mod a != 0 or (yi - yj) mod b != 0and they uniquely determine the tiling (then we need to check that all other rectangles fit to this pattern).

    Or second case all (xi - xj) mod a = 0 and (yi - yj) mod b = 0 then we can trivially tile everything by shifts of any of the rectangles by vectors (m*a, n*b), m,n are integral