9

Codeforces Round 942 (Div. 1, Div. 2) Editorial

 4 months ago
source link: https://codeforces.com/blog/entry/129027
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
By rui_er, history, 5 hours ago, In English

1972A - Contest Proposal

Hint 1
Tutorial
Solution

1972B - Coin Games

Hint 1
Hint 2
Tutorial
Solution

1967A - Permutation Counting

Hint 1
Hint 2
Hint 3
Tutorial
Solution

1967B1 - Reverse Card (Easy Version)

Hint 1
Hint 2
Tutorial
Solution

1967B2 - Reverse Card (Hard Version)

Hint 1
Hint 2
Hint 3
Tutorial
Solution

1967C - Fenwick Tree

Hint 1
Hint 2
Tutorial
Solution

1967D - Long Way to be Non-decreasing

Hint 1
Hint 2
Hint 3
Tutorial
Solution

1967E1 - Again Counting Arrays (Easy Version)

Hint 1
Hint 2
Hint 3
Tutorial
Solution

1967E2 - Again Counting Arrays (Hard Version)

Thanks A_G for discovering that E2 is possible!

Hint 1
Hint 2
Tutorial
Solution

1967F - Next and Prev

Hint 1
Hint 2
Hint 3
Tutorial
Solution

5 hours ago, # |

Rev. 2  

+48

too math, downvoted...

  • 5 hours ago, # ^ |

    I thought the problem was cool

  • 5 hours ago, # ^ |

    not enough persistent segment tree...

  • 4 hours ago, # ^ |

    Rev. 2  

    0

    it is mid night here and i had to do mAtHs

4 hours ago, # |

I don't think that this round deserves hate. div2A-D2 were all good. I say that as someone stuck on D2 for almost 2 hours. My final submission was rejected because of strict limits. However, the trick can compensate for it. I find it fascinating, and I am glad to have seen it. The model code is much simpler than mine.

The downside is that all problems (the ones I attempted) had the same (math) tag. But, it is a common trend nowadays. We cannot do anything about it, so cope.

  • 4 hours ago, # ^ |

    it had some very nice problems, definitely

4 hours ago, # |

Rev. 2  

+10

Well, here is the main contribution list of the writers.

Please feel free to tell us which is you favourite problem and why, and even the problems you dislike too. Meanwhile, I would also like to thank our hard-working coordinator and testers. They together made it possible for us to hold this round.

If there is a chance, we hope to meet again with better problems.

  • 4 hours ago, # ^ |

    D1 is not mine, it was accidentally invented by the tester newb_programmer while he was solving D2.

  • 4 hours ago, # ^ |

    I liked 2F/1D. Too hard for me to solve during round but solution nicely unfolds in several steps

  • 4 hours ago, # ^ |

    Rev. 2  

    +3

    2F/1D is really cool. I would have been annoyed if hint 2 was the intended solution.

    Btw typo in editorial

4 hours ago, # |

Rev. 2  

0

can anyone explain, why my code is WA on test 7 (problem B)

#include "bits/stdc++.h"
#define int long long

signed main() {
    int tt; scanf("%lld", &tt);
    while (tt--) {
        int n; scanf("%lld", &n);
        char c[n]; scanf("%s", &c);
        std::string s(c);
        int cntU = std::count(s.begin(), s.end(), 'U');
        if (cntU % 2 == 1) printf("YES\n");
        else printf("NO\n");
    }
}
  • 4 hours ago, # ^ |

    Inadequate char array length.

    • 4 hours ago, # ^ |

      Rev. 3  

      0

      Thanks!

    • 4 hours ago, # ^ |

      but why? its size is n

      • 4 hours ago, # ^ |

        You need a \0 (which is if you convert it to int) to tell the language that a C-type string ends here. So you'll need extra space.

        • 4 hours ago, # ^ |

          ok thanks! i will never use char[], scanf, printf

          • 3 hours ago, # ^ |

            That's a bit of an overreaction, isn't it?

4 hours ago, # |

Whoa did not expect the solution to B would be so simple

4 hours ago, # |

is it possible to solve 2C/1A using Priority Queue? Not in . I din't think of binary search and now my Pupil is on the line :sob:

  • 4 hours ago, # ^ |

    I've tried, but its making solution too much complex bcz of such input: 1 1 1 2 2 2 3 3 3

    • 3 hours ago, # ^ |

      can you explain please?

      • 3 hours ago, # ^ |

        Here curr is storing the minimum value all the elements can reach and rem stores the remaining operations after that minimum value is reached. Every element in priority queue I am comparing with previously reached value and calculating whether it possible to reach next top of priority queue value with remaining k.

4 hours ago, # |

Rev. 2  

0

Too much time wasted for proving the solution of B

4 hours ago, # |

weak pt. I just wrote the limit as the correct limit -1.

  • 4 hours ago, # ^ |

    then I fst at 1A

4 hours ago, # |

Could solve all the way to the problem D2. My math knowledge was useful in both task D1 & D2, however, mathforces.

4 hours ago, # |

Editorial is Little bit confusing for me.

Please can anyone verify that following approach was correct?

1)sort array use binary search, to make all all element atleast equal to X

This x will have range from min of array to max+k of array..

2)then ..make arrangement,1->n in this cycle and calculate the answer...using this x

  • 4 hours ago, # ^ |

    You don't need binary search if you already sorted array.

    1. Find = number full permutations (bin search or sort + prefix sum)
    2. = remaining k
    3. add (and purchase distinct ) at any end of full permutations to form additional permutations of .

    258906139

    • 4 hours ago, # ^ |

      I used binary search on k, I sorted array just ,

4 hours ago, # |

Rev. 2  

0

in div2D1 you do not have to count using math, you can naively run a for loop. The time complexity will be still for each test case. This is because, for each value of the loop will take time, and converges to .

  • 4 hours ago, # ^ |

    We can also notice that must be a multiple of and iterate from and from , then it turns into .

    • 4 hours ago, # ^ |

      Unfortunately couldn't make the same trick work for Div2D2, do you have any idea how?

      • 3 hours ago, # ^ |

        Rev. 2  

        0

        My solution for D2 uses same trick 258920365. Though it is O(n log^3(n))

        • 3 hours ago, # ^ |

          Cool that it fits in time

          • 2 hours ago, # ^ |

            Well, i am sure that it is less than O( nlog^3 n), but i don't know what it is exactly.

4 hours ago, # |

My O(n log^3 n) solution has passed the Div1B2, can anyone hack it or give a tighter complexity?

258888693

  • 2 hours ago, # ^ |

    your complexity is :) max answer is

    • 2 hours ago, # ^ |

      Thanks.

      Is this explaination correct? "For most cases, gcd(u,i-u)=1, so we can assume the enumeration quantities approximate to the answer."

4 hours ago, # |

Nice maths training contest

4 hours ago, # |

Rev. 2  

0

Can anyone give me the reason why this got signed integer overflow 258927405. I have checked the code it and it is correct for other ranges,but fails here.

  • 4 hours ago, # ^ |

    mid=1e17, and k-=mid*n.

    • 4 hours ago, # ^ |

      Thanks i got it ,k can get too much negative to handle more subtraction

    • 4 hours ago, # ^ |

      but how can i fix this?

      • 4 hours ago, # ^ |

        break when k<0

4 hours ago, # |

As far as I'm concerned, E is a bit classical and similar to CF837F.

By the way, thanks anyway to this round that made me GM.

4 hours ago, # |

A different solution to div1. A, div2. C using binary search.

The answer is completely binary searchable and there isn't anything wrong with it, but it doesn't match my intuition.

Let binary search on the number of Full permutations you create, we can handle the remaining part manually with an O(n), in binary search, let's check if we can have full permutations created, to check it we can just calculate the need for that much permutation for each item and see if have more or less if we had less we just add the mid - a[i] to the want variable, it's just the matter to check if want <= k.
Obviously, l is the answer, so you know that you can create n*(l-1) + 1 full permutations, (for x permutations, we need elements).
Now let's handle the remaining value. we manually loop through all the elements and if any element had anything less than k, we just add that to the want variable, otherwise the number of permutations you can create gets increased by one(reader's exercise: Why? ).

the code: 258927507

4 hours ago, # |

4 hours ago, # |

OMG D2...

I got: a = dp >= (p + q)p b = dq >= (p + q)q

So, I decided to sum up them and get: a + b >= (p + q)^2

Nice contest though

4 hours ago, # |

Loved the round, thank you! The best moment of the contest for me was when I started coding Persistent Segment Tree in Div.1 D (it wouldn't have worked probably) and realized that there's a cute solution with much less code! Though E1 looks like a pretty standart problem to be honest...

4 hours ago, # |

For 1972C - Permutation Counting, I first misread the problem as to find the maximum possible answer for any , where the subarrays form permutations of . 258892957

I'm not sure if that was the right approach for the misread verison, but after realizing my mistake, simply disabling the outer loop to handle only case made it AC. 258896138

Would've been great for me if the problem was actually about the misread one :)

4 hours ago, # |

Very good math problems and fast editorial. Thanks!

4 hours ago, # |

I am a newbie and I intend to reach to pupil quickly.What resources should I follow to reach it and I really feel annoyed with questions like B that was asked today.I tend to spend a lot of time on such problems and many a times contest gets ended and I am unable to come up with solution.Please guide me

  • 3 hours ago, # ^ |

    Take a look at the codeforces catalog. There people much smarter than me have posted the exact thing you are looking for.

    From resources to guides on how to improve and tutorials on specific topics and even stuff on mindset and attitude.

    https://codeforces.com/catalog

3 hours ago, # |

Rev. 3  

+21

I like Div1E, thanks, though I couldn't make it on time...(QAQ) Here is my solution.

We can rephrase the problem as a random walk:

  • The states are .
  • Start at .
  • States and are absorbing.
  • From states , decrement with probability , increment otherwise.
  • Let be the probability that it does not end up at after steps. The answer is .

If we had infinite number of steps instead of , the problem would be classical. Let be the probability that, starting from , it is eventually absorbed at after infinite number of steps. We have . It is known that for and for . (Caveat: What if divides the denominator? It turns out no such case exists within the constraints, luckily.)

To compute of for , we can reduce it to certain path counting and use reflection method as in the editorial. It takes time for each , thus overall. (Caveat: The sum of is not bounded! We should do this only for .)

2 hours ago, # |

Rev. 2  

+6

Lol, I thought about the model solution to E1, but also thought "Huh? quite weird tradeoff solution with a ton of detailed math? I'm not coding that for 1750 points! There must be some much slicker way to do this!" and that turned out to be exactly the intended solution xD... But I managed to get D instead and I wouldn't be able to do both anyway

2 hours ago, # |

Could anyone explain the output of this test case for the div2C problem?

1 9 1 2 1 5 7 5 3 3

The answer is 27 but I am only getting 26 as max.

[6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]

  • 78 minutes ago, # ^ |

    Here is an optimal solution for that test case

    • Buy 3 cards of the first and third ones
    • Buy one card for the fourth one
    • Buy two cards for the fifth one

    then you can arrange them as follows and get 27 as the maximum score:

116 minutes ago, # |

I have made detailed video explanations for C and D1 Question Here are the links for anyone who wishes to watch

C : https://www.youtube.com/watch?v=ZP4HPYTWtZQ D1 : https://www.youtube.com/watch?v=cSKooXv7FKA

  • 15 minutes ago, # ^ |

    Why didn't you mention in the comment that it's not in English? To gain some unintended view?

43 minutes ago, # |

can anyone help me finding what's wrong with this approach in 2/C

  • 6 minutes ago, # ^ |

    Inside the binary search, you can break when tmp < 0 (clearly). Turns out, you "must" break otherwise some negative integer overflow will occur and unintentionally make tmp positive even though it should DEFINITELY be negative once tmp < 0 occurred. I got AC on your submission with this change. 258943287

    • 4 minutes ago, # ^ |

      yeah i just found the problem with my code

  • 5 minutes ago, # ^ |

    NVM it was actually signed integer overflow! :(

3 minutes ago, # |

We can also solve Div1C in by maintaining the generating function for every position , where denotes the value of where is the array we are trying to find and is the "fenwick function" from the statement. We maintain it by processing 's in the ascending order of their lowest bit and summing up the generating functions. The result can be extracted as .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK