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

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

Thank you for participation and we hope you enjoy this round :)

How did you find the contest?
Which problem is your most favourite?
Which problem you hate the most?

D2A Submission is All You Need

Fun
Hint 1
Hint 2
Tutorial
solution
Rate the Problem

D2B Pathless

Hint1
Hint2
Hint3
Hint4
Tutorial
solution
Rate the Problem

D1A Double Perspective

Hint1
Hint2
Tutorial
solution
Rate the Problem

D1B Stay or Mirror

Hint1
Hint2
Tutorial
solution
Rate the Problem

D1C1 Interactive RBS (Easy Version), D1C2 Interactive RBS (Medium Version) and D1C3 Interactive RBS (Hard Version)

Hint1
Hint2
Hint3
Hint4
Hint5
Hint6
Tutorial (easy version)
Tutorial (medium version)
Tutorial (hard version)
solution (easy version)
solution (medium version)
solution (hard version)
Rate the Problem

D1D Permutation Blackhole

Hint1
Hint2
Hint3
Tutorial
solution
Rate the Problem

D1E Induced Graph Queries

Fun
Hint1
Hint2
Hint3
Hint4
Hint5
Tutorial
solution
Rate the Problem

D1F1 Top-K Tracker (Easy Version) and D1F2 Top-K Tracker (Hard Version)

Fun
Hint1
Hint2
Hint3
Hint4
Hint5
Hint6
Hint7
Hint8
Hint9
Hint10
Tutorial (easy version)
Tutorial (hard version)
solution (easy version)
solution (hard version)
Rate the Problem
Разбор задач Codeforces Round 1040 (Div. 1)
Разбор задач Codeforces Round 1040 (Div. 2)
  • Проголосовать: нравится
  • +118
  • Проголосовать: не нравится

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

fast editorial!!

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

why the most favourite + most hated problem share the same vote

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

congrats me for becoming newb again

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

it's quite interesting that the constraints are just enough for all inequalities for the solution to work,

i.e. $$$30+435\times 2 + 380\times 3 = 60\times 29+300$$$,

and the $$$435-30=405$$$ subsets of size $$$2$$$ of $$$\{1,\dots,29\}$$$ we need is only $$$1$$$ less than $$$\binom{29}{2}$$$

nice design

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

For Div2C, can you not just order all segments, and for each iteration, take, among all the segments that start before the end of the latest segment taken, the one that ends the latest possible. If no possible segments, just take the segment with the closest start after end and among all those that start at this closest start, take the one that ends the latest possible. There will never be cycle that way and by design you're maximising the length of the union of all segments. And it's nlogn (just a sorting has to be done). Or maybe it's possible to hack me? https://mirror.codeforces.com/contest/2130/submission/331785344

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

the most favourite + most hated problem share the same vote

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

I’m pretty sure the complexity for div1D can be made n^3, if you keep your dp states from 0 to s[i] if s[i] exists and otherwise keep only one general state not depending on the value contributed to this side, so the complexity will be (sum(s[i]) + n)^3, but sum(s[i]) < n for the answer not to be 0.

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

thank you for superfast editorial.

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

can someone explain the editorial solution for 2D/1B?

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

    You can just check greedily that is it better to convert this number into 2*n-(num) or keep as it is. You want to minimise the score so just take lower one. You go from left to right and check for every number and count the numbers that are bigger than it from left and right side. If you keep number as it is then the numbers that are greater on left side will give inversion or if you choose to increase the number to 2*n-(num) than number on right will become smaller and will give inversion, even in future if you decide to increase that numbers which are on right side then also they will be smaller than this number so they will give inversion.

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

      I need the proof that they are independent

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

        I don't know how to prove but here is the simple method I used.

        The numbers on left have already calculated their share in the answer. So if the number on the left is smaller than their is no effect whether this number is increased or not, but if it is bigger than that will give inversion so if we increase this number then inversions on left will be solved. And for the number smaller on right side, they will get calculated on their turn.

        For the the numbers on right side, suppose 1 < 5 but 2*n-1 = 11 if n = 6 , so 11 > 5 , even if we increase 5 then also 11 > 7 so we can calculate inversions by this number on right side.

        I don't know if you understand what I am saying, if anyone can drop the proper proof here, will be helpful.

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

        I don't know if this counts as a proof but here it is, in any case of a < b or a > b, changing the greater value won't affect the relation, the change is only done when the smaller value is changed to 2 * n — val, so if you change some value p_i to 2 * n — p_i, it affects its relation with values > than it to its left and right, so u can just take the better choice.

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

    It's very simple. Please dry run on paper. U will observe if we process from element 1-N in P, Any element greater than 1 we will change in future that won't be change inversions for 1. Same for 2, 3 and so on.

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

    i had a doubt ,how do we know that a n^2 solution would get accepted ?

    I was trying this ques , and could come up with n^2 solution , or by using ordered set, i didnt have practise of ordered set so wasted a lot of time thinking of an alternative

    Thanks in advanced!

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

the problems were really nice! very impressive. thank you!

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

The votes in the which problem you like thing are broken. You can't have different votes for both.

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +32 Проголосовать: не нравится
  • Hint 1
  • Hint 2
  • Hint 3
  • Hint 4
  • Hint 5
  • Hint 6
  • Hint 7
  • Hint 8
  • Hint 9
  • Hint 10

Ruler Of The Zoo II after 4 years?!

qwq

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

Anyone else mess up on C by proving that if theres a cycle, there wouldnt be any addition in the union? (instead of proving the reverse, that if theres no addition, there wont be a cycle)

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

Hi, according to the editorial's code for D2B, the input 1 4 8 1 1 2 2 does not have a solution, but doesn't 1 2 1 2 work?

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

use this string can get $$$\frac{n}{14} + O(\log)$$$ querys in div1C, better than offical solution $$$\frac{n}{13} + O(\log)$$$

Spoiler

after some optimization, can reach 80 queries limit.

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

A very simple $$$O(n)$$$ solution for C is to remove all edges u -> v except for the maximum edge. It's easy to see that this doesn't affect $$$F(s)$$$, and it also ensures $$$G(s)$$$ is zero since if there is a cycle of length $$$ \gt = 3$$$, then there must exist a node with more than one edge.

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

The bracket trick for E1 is very clever, I failed to differentiate '((' and '))' during the contest.

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

Good problems. I enjoy them very much.

But I did very badly. Too slow and too many wa atts. Bye GM.

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

    For the 3rd version, $$$\left\lceil\frac n{11}\right\rceil+\left\lceil\log n\right\rceil=101$$$. I generated it through a silly idea. But it's soooooo close to the constraint. Is it intended to stop it from accepted?

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

As a Professional Newbie™️ I have to say B felt REALLY hard to solve and took me about 2 hours. Awesome problem though and probably a skill issue on my end but did anyone else find it hard?

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

For Div2E1, my pattern is s1[]s2]] which has a value of 1,2,3 or 4 depending on s1 and s2.

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

In the solution of D1C3, there is a function $$$g(i)$$$ mentioned, which should have the property that $$$g(i) \gt g(1) + g(2) + ... + g(i-1)$$$. How is that different from the medium version, where $$$g(i) = 2^i$$$?

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

    Nvm, I figured it out. The condition $$$g(i) \gt g(1) + g(2) + ... + g(i-1)$$$ is wrong, it should rather be $$$f(g(i)) \gt f(g(1)) + f(g(2)) + ... + f(g(i-1))$$$ where $$$f(n) = \frac{n(n+1)}{2}$$$ (in English: the number of regular substrings that we can build using $$$g(i)$$$ pairs has to be greater than the number of regular substrings using $$$g(1)$$$ pairs plus the number of substrings using $$$g(2)$$$ pairs and so on).

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

      Hi, can you explain why does it have to be f(g(i)) > f(g(1)) + ... + f(g(2)) ? The medium version was quite easy for me to understand but the hard one isn't. Thank you

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

        The brackets can either be opening brackets or closing brackets. If you decide to include some bracket $$$n$$$ times, there are two cases: Either, the construction from the editorial contributes $$$\frac{n(n+1)}{2}$$$ to the total number of regular substrings (which means we have an opening bracket), or 0 (meaning we have a closing bracket). Since we want to be able to determine the bracket configuration using just the total number of regular bracket substrings, we need the condition $$$f(g(i)) \gt f(g(1)) + ... + f(g(i-1))$$$.

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

wuhudsm Proof for D1B is incomplete. You proved it for '1', and then said, we have solved the problem..!!! how ?

You should have proved it for 'x', and then build transition to x+1 and then announce it to be proven.


Here is my doubt ( which I couldn't prove during the contest... )...

"replacement while moving from left to right doesn't affect the answer" hasn't been proven. as part of editorial.

  • »
    »
    10 месяцев назад, скрыть # ^ |
    Rev. 13  
    Проголосовать: нравится 0 Проголосовать: не нравится
    WORKS, Can't prove why
    DOESN'T WORK initially, Figure out bug, and Fix it and gets ACCEPTED
    One more approach.
  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I think the editorial proof works. Can you elaborate on what part you think remains to be proven?

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

But for E3, if g(i)> sum(g(1),g(2),... ,g(i-1)) that means g(i+1) > g(i) + sum(g(1),g(2),... ,g(i-1)) > 2*sum(g(1),g(2),... ,g(i-1)) So wouldn't g have to increase based on powers of 2, not letting us have 12 different values in one query?

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

For div2 B is the sequence 0 0 0 .. 2 1 2 1 .. not possible ?

I wrote this sequence but it got me incorrect answer

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

In div1D, am I missing something, or is the actual complexity $$$\mathcal{O}(n^3\log^4n)$$$? To recalculate dp[l][r][x][y] we need to iterate over i, j, k, and I spent some time on constant optimizations, though in the end it worked in about a second, so some of them were probably unnecessary.

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

Has anyone solved Div1B using dynamic programming? I was trying to solve, but was getting Memory Limit Exceeded. 331823637 How to optimize the memory here?

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

funny white text on 1B :)

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

If you forgot to deal with the nodes of degree $$$0$$$ in D1E,

you are not alone. $$$\Large\unicode{x1F62D}$$$

Also, my screencast (in Chinese)

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

Given an array A of length n, define a transformation on each element as B[i] = 2n — A[i]. Then construct a new array by applying the following rules: **** Initialization: **** Set A[0] = min(A[0], B[0]). **** For each index j from 1 to n — 1: **** Compare the previous value A[j — 1] with both A[j] and B[j]. **** Case 1: If A[j — 1] ≤ A[j] and A[j — 1] ≤ B[j], → choose min(A[j], B[j]) as the next value. **** Case 2: If A[j — 1] > A[j] and A[j — 1] > B[j], → again choose min(A[j], B[j]) . **** Case 3: If A[j — 1] > A[j] and A[j — 1] ≤ B[j], → choose B[j] **** Case 4: If A[j — 1] ≤ A[j] and A[j — 1] > B[j], → choose A[j] **** **** **** will it work...??

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

For problem Div2E1, the editorial says

For example, if $$$f_0−f_1 = (10000101)_2$$$, we can get $$$s_1 = s_3 = s_8 = '('$$$.

shouldn't it be $$$s_1 = s_3 = s_8 = ')'$$$? Or am I understanding it wrong?

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

can anyone provide me questions similar to "D2A Submission is All You Need"

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

Good

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

Is it just me, or the constraint for C confusing af?..

It's basically sort all segments increasing L and decreasing R for same Ls, then

ans = array(s[0].id)
r = s[0].r
for i in SZ:
  if s[i].r > r:
    r = s[i].r
    ans.push_back(s[i].id)

print(ans)

Works NlogN.

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

Could anybody explain a little bit clearer on how does DP in D1D work? Can't get it from editorial :/

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

Div1C can be done in 100 or less queries by observing we can ask for the answer of a string consisting of "$$$s_i)$$$" concatenated $$$x$$$ times and get $$$\frac{x(x+1)}{2}$$$ as the answer if $$$s_i$$$ is '$$$($$$' and $$$0$$$ otherwise.

With that in mind we can try to construct every power of $$$2$$$ up to $$$2^x$$$ as a sum of triangular numbers and query for it after. Turns out $$$x$$$ can go up to $$$13$$$ if our max for characters in a query is $$$1000$$$, so we can solve even the hard version this way.

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

    Nice solution! May I know the proof that each power of 2 can be represented as the sum of triangular numbers? Thanks

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

      $$$1$$$ is a triangular number, so actually any positive value can be represented by triangular numbers. You can construct a number $$$x$$$ greedly always picking the greatest triangular number less than or equal to $$$x$$$ and subtracting $$$x$$$ by it. I don't know if that's the best way, but it's good enough for this problem.

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

        Ok, I see. It turns out that according to Gauss's Eureka Theorem, it states that all positive integers can be represented as the sum of 3 triangular numbers. The optimal configuration also cannot be greedly found. However, finding this configuration is not needed for this problem as seen in 332146124.

        My code utilizes the fact that a similar system to replace binary encoding can be created by just using triangular numbers. I brute force each triangular number in a seperate piece of code to find the indexes of a series of triangular numbers where a_i_j > \sum_{1}{i_j-1}a_i_k to preserve uniqueness of each possible combination. The maximum number of locations per query using this solution is 13. If anyone finds a better solution in terms of locations per query, please let me know with how you did it!

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

in D2E1 & E2 & E3, i can only find a '(' to solve all the problem : (x ((yy (x ((yy ((((zzzz ((((((((wwwwwwww ...... (x ( (y(y ( (z(z(z .......

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

C3 too hard for me :(

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

Is there any knowledge to know How to find out the ways to construct the string on E2 / C2, E3 / C3 ?

On C1, I just write it on my notebook. But on C2, I have to run backtracking but I can't :(

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

Alternate solution for 1C2:

  • Find a "(" and a ")" in $$$O(\log{n})$$$ queries through any method of your choice.
  • Let there be three constants $$$c_1, c_2, c_3$$$.
  • We'll find the characters at six positions in one query. Let them be $$$i_1, i_2, \dots, i_6$$$.
  • Our queried string is $$$\sum_{j = 1}^{3} ((c_j - 1) \cdot \text{"("}) + (c_j \cdot (s_{i_{2 \cdot j - 1}} s_{i_{2 \cdot j}})) + ((3 \cdot c_j) \cdot \text{")"})$$$.
  • If we choose the constants carefully, then every possible combination of values for characters $$$s_{i_1}, s_{i_2}, \dots, s_{i_6}$$$ yields a different number of valid substrings in its corresponding string. One such set of constants is $$$c_1 = 36, c_2 = 12, c_3 = 5$$$.
  • The total number of queries we use is $$$O(\log{n}) + n/6$$$ which stays below 200 even if you have a 2x constant factor for the $$$O(\log{n})$$$ queries.

331884800

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

My $$$O(n^3\log^3n)$$$ solution to div1D got TLE until I optimized it to $$$O(n^3\log^2n)$$$. Maybe the constraints are too narrow.

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

Actually, Problem C/E can determine 13 locations in one query. It needs about 87 queries.

The main idea is using $$$\underbrace{()()()()...()}_{c\ times}$$$ structure, and $$$f(s)=\binom{c}{2}+c$$$. Just using knapsack and you can see that $$$2^0, 2^1,\dots, 2^{12}$$$ only need about 700.

Code: 331838756

Sorry for my poor English.

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

Ok, so something strange that I noticed is that I had a couple of asserts placed throughout the code. Then, when I turned in C2, I got WA. I figured it was a flaw with my approach and spent a decent amount of time trying to figure out what was wrong. Then, through a moment of pure divine stupidity, I decided to turn in my code under the assumption that the WA was actually an RTE... and it worked.

After the contest, I checked the submissions and indeed, my RTE was turned into a WA. So, are interactive problems managed this way or is this something new that I should take into account? Also, does anybody know if the same behavior is present in ICPC tournaments (domJudge, etc)?

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

really good round

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

For div2B, if anyone is struggling to understand/prove "Why every number except 1 can be obtained be with just 2 & 3"

Why 2 and 3?

Chicken McNugget theorem

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

    This is also provable without Chicken McNugget Theorem; all even numbers can be represented as 2x, where x is an arbitrary integer, and all odd numbers can be represented as an even number + 1, and 2x+1 = 2(x-1) + 3, and since we can't be have x <= 0 (as then you would be subtracting 2), you can get all numbers greater than 1.

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

is it possible to solve div2D/div1B using merge sort? if yes, can you please give some ideas how to implement it because I tried so much and it always failed at 2 pretest

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

Attention is all you need , Famous paper on Transformer model.

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

my solution of C in O(nlogn) time complexity

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

Who can tell me the solve of div2D, I can't understand the Editorial

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

    I was confused by this problem too, but this is how I figured it out:

    The actual solution involves going through each element from smallest -> largest values. Start with 1 (no matter what index it may be). You can either keep it as 1 or flip it to the highest possible number (2N-1), resulting in a inversion count of either the number of items before that index (if you kept as 1 because all number before will be higher) or the number of items after (if you flipped to the highest possible number). Then, you REMOVE that index from the permutation p and you go to the next value, 2, and repeat. Notice that whether you flipped 1 or not, it WILL NOT affect any items after it! This is the part that I missed! Think about it:

    • If you keep 1 the way it is, then obviously nothing would change
    • BUT if you flipped it, then you're already counting all the inversions that it causes (by taking the number of items to the right of it considering that all numbers lower have been removed). Even if later on, there is a number that is flipped (and becomes higher than it was originally), it is guaranteed that the new flipped number will always be less than the previously flipped one, resulting in no new inversions! (Ex. 1 is processed before 2 and 1 flipped would be 2N-1 while 2 flipped is 2N-2. Clearly, 2N-1 is greater than 2N-2). So, once you process an item it's basically "irrelevant" when processing future items.

    Because of this logic, all you need to do is iterate through the numbers from the lowest -> highest original value! If you don't want the hassle of removing the numbers, you could do what the editorial did and just pre-calculate all the numbers to the left/right that are greater for each index considering that all the lower numbers would've already been processed and removed by the time you got to the new number. I hope that makes sense!

    You can see my solution here: 331985291

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

D1C3

Here's a different approach: Start by querying $$$s_1s_2s_1$$$. If the answer is $$$1$$$, then $$$s_1 \neq s_2$$$; otherwise, $$$s_1 = s_2$$$. Based on this, we can optimize the subsequent string construction: build a pattern like $$$s_1s_ks_1\dots s_ks_1$$$, where $$$s_k$$$ is repeated $$$x$$$ times. If $$$s_1 \neq s_k$$$, the answer will be $$$\frac{x \cdot (x + 1)}{2}$$$, and the query length will be $$$2x + 1$$$.

Next, we need to construct a set $$$A$$$ that is as large as possible, satisfying:

The subset sums of $$$\{\frac{x \cdot (x + 1)}{2} \mid x \in A \}$$$ are all distinct;

The total length constraint $$$\sum_{x \in A} (2x + 1) \le 1000$$$.

Each query can then determine $$$|A|$$$ positions' relation to $$$s_1$$$. Finally, by finding any position $$$s_k$$$ where $$$s_k \neq s_1$$$ and querying $$$s_1s_k$$$, we can deduce $$$s_1$$$ and thus the entire string. The total number of queries is at most $$$\lceil \frac{n - 1}{|A|} \rceil + 1$$$.

Constructing Set $$$A$$$: Start with $$$1$$$ and iteratively add numbers, ensuring each new element is larger than the sum of all previous elements. This gives $$$|A| = 13$$$, with a worst-case query count of just $$$78$$$.

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

Div.1-E, I noticed that it can be solved in O((N+M)*sqrt(Q) + Q*sqrt(N)) without thinking about degrees.

First, sort the queries by L, after that, sort them by R for each block of [0,D), [D,2D),...,[kD,Q) with a block size D ( = O(sqrt(Q)) ). Then, in that order, just expand and contract L and R like in Mo's algorithm.

During the expansion/contraction, the update at each R happens at most once in each block. The update for each L happens in at most 2 blocks. This is because if the same L were to span 3 or more blocks, L should be constant, except for the first and the last.

I think this technique is useful, and with this algorithm, I got the current fastest: https://mirror.codeforces.com/contest/2129/submission/331921365

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

Proof for the solution of D?

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

In div2C/div1A, why does the simple dfs search tree not work?

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

i think tutorial to div 2 D is not well written or you can it will be easier to understand if you have told us that in a relation a<b this relation will only change if you convert a to 2n -a not when you convert b to 2n-b

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

Is there any intuition for E3? I can understand how I can find 12 locations with one query and why it works, but getting that idea for optimizing the number of queries seems to be on a different level.

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

    The key intuition here for me was try to separate brackets in query. Here I tried describe my whole thoughts during contest:

    I thought to guess $$$12$$$ locations in one query, then in my query $$$f_{1} s_{1} f_{2} s_{2} \ldots, f_{12} s_{12} f_{13}$$$ I must divide values $$$s_i$$$ with some seperators $$$f$$$ so $$$s_1, s_2,\ldots,s_{12}$$$ will not intersect in RBS and then query will give me value $$$v_1 + v_2 + \ldots + v_{12}$$$. And now task is "in sum $$$s$$$ guess the numbers that formed that sum", it looked like greedy, for example, then we get number from binary notation. But we sorta can't make powers of two, but we can get values $$$1,3,6,10,15$$$ etc, then just I checked that if we choose numbers that differs at least $$$2$$$ times we will get exactly $$$12$$$ numbers.

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

For Div2B if n = 3 , s = 10 and a = {0,1,2} why the answer is -1? Why not 0 2 1?

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

Более простое и детальное объяснение Div.1B (Div.2D), если кто-то в нём нуждается

Мы хотим построить массив A, каждый элемент которого либо p[i] либо 2*n — p[i] Для удобства будем называть p[i] "меньшим", а 2*n — p[i] — "бОльшим" значением.

Итак, мы хотим расставить в каждую позицию массива либо больший либо меньший элемент так, чтобы количество инверсий в итоге было минимальным. Идея такая — для каждой позиции в массиве, мы перебираем оба варианта — сколько инверсий ГАРАНТИРОВАННО добавится после добавления меньшего элемента и сколько инверсий ГАРАНТИРОВАННО добавится после добавления бОльшего элемента. Выберем на текущую позицию тот, который даст в итоге меньше инверсий. Для позиции i мы хотим выбрать, какой элемент поставить:

Если мы выбираем меньший элемент
Если мы выбираем больший элемент

Итак, когда мы выяснили, какая версия элемента даёт меньше инверсий — левая или правая, пусть счётчик инверсий для меньшей версии элемента называется count1, а для большей count2. Вариантов решения есть несколько. Самый простой, где мы просто считаем кол-во инверсий — мы сразу на ходу, добавляем к результату минимальное количество инверсий — min(count1, count2).

Второй, немного с лишними действиями, но новичку может быть нагляднее. Раннее мы сказали, что выбирать элемент будем тот, у которого меньше всего инверсий. Так давайте и выберем. Если count1 < count2 — result[i] = p[i] Иначе, если count2 >= count1 — result[i] = 2*n — p[i]. Когда восстановили весь массив, просто за сложность O(N^2) в лоб посчитаем кол-во инверсий для каждого элемента двойным циклом и выведем ответ.

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

A simpler and more detailed explanation of Div.1B (Div.2D) if anyone needs it

We want to build an array A, each element of which is either p[i] or 2*n — p[i] For convenience, we will call p[i] the "smaller" value, and 2*n — p[i] the "larger" value.

So, we want to put either a larger or smaller element in each position of the array so that the number of inversions in the end is minimal. The idea is this — for each position in the array, we iterate over both options — how many inversions are GUARANTEED to be added after adding a smaller element, and how many inversions are GUARANTEED to be added after adding a larger element. We will choose the one that will give the least inversions in the current position. For position i we want to choose which element to put:

If we choose a smaller element
If we choose a larger element

So, when we figure out which version of the element gives fewer inversions — left or right, let the inversion counter for the smaller version of the element be called count1, and for the larger one — count2. There are several possible solutions. The simplest one, where we simply count the number of inversions — we immediately add the minimum number of inversions to the result — min(count1, count2).

The second one, with a little extra actions, but it may be clearer for a beginner. Earlier we said that we will choose the element with the least number of inversions. So let's choose it. If count1 < count2 — result[i] = p[i] Otherwise, if count2 >= count1 — result[i] = 2*n — p[i]. When we have restored the entire array, we will simply calculate the number of inversions for each element with a double cycle for O(N^2) complexity and output the answer.

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

I think there is a mistake in the explanation of problem C2. The last line should be s1=s3=s8=′)′ Am I right?

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

I really don't konw why D1D/D2F calculating dp[l][r][x][y] should multiply the combination ((r-l)(i-l)),sorry for poor english and don't konw how to express combination

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

So, what is the relationship between Problem C and the Disjoint Set Union?

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

In div2 C, is it a typo: "we can always find $$$x_{i-1} \lt x_i \gt x_{i+1}$$$ , where either $$$[x_{i-1},x_i] \subseteq [x_i,x_{i+1}]$$$ or $$$[x_i,x_{i+1}] \subseteq [x_{i-1},x_i]$$$" and should be "$$$[x_{i-1},x_i] \subseteq [x_{i+1},x_i]$$$ or $$$[x_{i+1},x_i] \subseteq [x_{i-1},x_i]$$$" instead ($$$x_{i+1}$$$ and $$$x_i$$$ swapped)?

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

why the solution of Div1.A cannot even solve the example ? solution's output is 1 1 2 3 4 but the correct output is 1 1 3 1 2 4

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

i really liked how d1A presents that n^2 solution is predicted when the actual solution was n

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

Can someone explain why d2b works

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

D2E3 / D1E3, and the problem as a whole, is one of my favourite problems I have encountered and solved on CF. For a long time I was stuck on trying to query ( i₁ i₂ ) but this doesn’t work because you have no way to distinguish whether i₁ is ) or i₂ is ( if the query returns 1.

After a while, a hint led me to the realization that we’ll have to query something like i₁ ) i₁ ) ) i₂ ) to distinguish:

  • if i₁ forms a valid RBS, result = 3
  • if i₂ forms one, result = 1
  • if both do, result = 4
  • if neither, result = 0

This is sufficient for E1.

Key observation: a longer RBS always has more valid substrings. Define f(x)=x(x+1)/2 as the total substrings formed by x consecutive pairs. We choose k₁,k₂,… such that f(kᵢ)>∑_{j< i}f(kⱼ) so each block’s contribution is larger than all previous combined.

Powers of 2 satisfy this, but only up to 8 fit under 1000 characters. Instead, brute‐force up to 1000 to find all kᵢ, yielding (in descending order): {123, 87, 61, 43, 30, 21, 15, 10, 7, 5, 3, 2, 1}.

We build the query as [ i₁ ) i₁ ) ]×123 + ) + [ i₂ ) ]×87 + ) + … + [ i₁₃ ) ]×1 + )

Total length of query = 829 characters.

Since each f(kᵢ) exceeds the sum of all earlier f’s, the total substrings returned >f(kₓ) iff iₓ is (, else it’s ).

This recovers 13 positions in 1 query (vs editorial’s 12), enabling a more lenient binary search.

Total queries: ⌈1000/13⌉ + 2·log₂(1000) + 2 ≈ 99

Amazing problem, thank you.

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

Tutorial of D1F2.

$$$2\times(1+29+\frac{29\times 30}{2})=872$$$

should be changed to

$$$2\times(1+29+\frac{29\times 28}{2})=872$$$

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

Div2D can be solved in $$$O(n \log n)$$$ by greedily choosing the best possible element from the back.. https://mirror.codeforces.com/contest/2130/submission/332358826

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

For questions like Div1C1, do you guys think a while to find a bracket pattern with 2 missing positions that give different # of valid bracket substrings for each of the 4 possibilities or write a script to brute force every bracket string of length x?

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

I think you can tighten the complexity analysis of D to around $$$O(n^3)$$$. Here's how:

Note the sum of the positive elements of $$$s$$$ is bounded by $$$n - 1$$$ (or else the answer is always $$$0$$$). Concretely, $$$\sum_{i = 1}^{i = n} \text{max}(s[i], 0) \leq n - 1$$$ going forward.

We take the same approach as the tutorial, except in our $$$dp[l][r][x][y]$$$ we only need to maintain $$$x \leq s[l - 1]$$$ and $$$y \leq s[r + 1]$$$. If either of these are $$$-1$$$ then we'll just code them specially (so shift everything up by $$$1$$$, per se).

Then, for some fixed $$$l, r$$$, we have the following scenarios:

$$$s[l - 1] \geq 0, s[r + 1] \geq 0 \Rightarrow (s[l - 1] + 1) \cdot (s[r + 1] + 1)$$$ states.

$$$s[l - 1] = -1, s[r + 1] \geq 0 \Rightarrow (s[r + 1] + 1)$$$ states.

$$$s[l - 1] \geq 0, s[r + 1] = -1 \Rightarrow (s[l - 1] + 1)$$$ states.

$$$s[l - 1] = -1 = s[r + 1] \Rightarrow 1$$$ state.

Hand-wavily we can say this is roughly bounded by $$$(s[l - 1] + 1) \cdot (s[r - 1] + 1)$$$ (if we pretend to "clip" $$$s$$$ to $$$0$$$).

The cost of a transition is to iterate over the entire range and iterate to fix some $$$r$$$ to the left and some $$$s[k] - r$$$ on the right (similar to tutorial). So the total cost to compute a state is approx. $$$s[l - 1] \cdot s[r + 1] \cdot ((s[l] + 1) + \ldots + (s[r] + 1))$$$. The latter term is bounded by $$$n - 1 + n = 2n$$$. So the total cost of everything is bounded by $$$\sum_{l = 1}^{l = n} \sum_{r = l}^{r = n} (s[l] + 1) \cdot (s[r] + 1) \cdot 2n \leq ((s[1] + 1) + \ldots (s[n] + 1))^2 \cdot 2n \leq 8n^3 = O(n^3)$$$.

Implementation is more of a pain witht his for sure because you need to make sure you're not iterating $$$x$$$ and $$$y$$$ too much and handle somewhat edge cases when $$$s[l - 1] = -1$$$ or $$$s[r + 1] = -1$$$ or $$$s[k] = -1$$$ when iterating between $$$l$$$ to $$$r$$$.

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

For the problem "Stay or mirror", Why the number of inversions containing i is only related to a[i]?

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

    When we say that the number of inversions involving $$$i$$$ depends only on $$$a_i$$$, we mean that it doesn’t matter what decisions we make for the other elements: if an element is greater than $$$p_i$$$, it will remain greater regardless of whether we keep it as $$$p_j$$$ or change it to $$$2n - p_j$$$; and if it is smaller, it will remain smaller in both cases.

    The key is to count the inversions involving $$$i$$$ without double-counting cases.

    If we set $$$a_i = p_i$$$ (small value), then all elements greater than $$$p_i$$$ that are to the left will form inversions with $$$i$$$. This remains true even if those elements are changed to $$$2n - p_j$$$, because they will still be greater than $$$p_i$$$.

    If we set $$$a_i = 2n - p_i$$$ (large value), then all elements greater than $$$p_i$$$ that are to the right will form inversions with $$$i$$$. This also remains true even if they are changed to their large form, because they will still be smaller than $$$a_i$$$.

    Thus, for each $$$i$$$, there are only two possible inversion counts involving it:

    $$$ \min(\#\ \text{greater to the left},\ \#\ \text{greater to the right}) $$$

    We choose the smaller of the two, then remove $$$i$$$ from the array. After this, the remaining problem is of exactly the same type, so the process repeats recursively until all positions are processed.

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

Loved the solution for D1B — Stay or Mirror!!

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

For Div1A, I ended up doing an $$$O(n)$$$ greedy. I made the observation that, for a range $$$[s_k,e_k]$$$, a cycle through $$$s_k$$$ and $$$e_k$$$ would require two paths from $$$s_k$$$ to $$$e_k$$$ (one of which is redundant for the score $$$f(S)$$$). Accordingly, if we treat $$$e_k$$$ as a unique representative and take the smallest corresponding $$$s_{k'}$$$, this will get rid of subintervals existing while keeping the answer optimal. (Note that, while I mention a representative, this solution does not use a DSU; a DSU is a touch overkill.)

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

can anyone please help me to find mistakes in this solution for problem D1A Double Perspective.

// author :- Vikas Kumar Vikrant, CSE @NITS
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define f(i, a, b) for (int i = a; i < b; i++)
#define fastIO                    \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define all(v) v.begin(), v.end()
#define cntbit(n) __builtin_popcountll(n)
#define get(a, n)     \
    vector<int> a(n); \
    f(i, 0, n) cin >> a[i];
#define put(a, n) f(i, 0, n) cout << a[i] << ' ';
#define cntbit(n) __builtin_popcountll(n)
long double PI = acos(-1.0);
const int mod = 1e9 + 7;
// x = x & ~(x-1); //give rightmost set bit no.

class DSU
{
public:
    vector<int> rank, parent, size;
    DSU(int n)
    {
        rank.resize(n + 1, 0);
        parent.resize(n + 1);
        size.resize(n + 1, 0);
        for (int i = 0; i <= n; i++)
            parent[i] = i;
    }
    int findPar(int node)
    {
        if (node == parent[node])
            return node;
        return parent[node] = findPar(parent[node]);
    }
    void unionByRank(int u, int v)
    {
        int ulp_u = findPar(u);
        int ulp_v = findPar(v);
        if (ulp_u == ulp_v)
            return;
        if (rank[ulp_u] < rank[ulp_v])
            parent[ulp_u] = ulp_v;
        else if (rank[ulp_u] > rank[ulp_v])
            parent[ulp_v] = ulp_u;
        else
        {
            parent[ulp_v] = ulp_u;
            rank[ulp_u]++;
        }
    }
    void unionBySize(int u, int v)
    {
        int ulp_u = findPar(u);
        int ulp_v = findPar(v);
        if (ulp_u == ulp_v)
            return;
        if (size[ulp_u] < size[ulp_v])
        {
            parent[ulp_u] = ulp_v;
            size[ulp_v] += size[ulp_u];
        }
        else
        {
            parent[ulp_v] = ulp_u;
            size[ulp_u] += size[ulp_v];
        }
    }
};

void solve()
{
    int n;
    cin >> n;
    vector<int> g[2 * n + 1];
    set<int> st;
    DSU dsu(2 * n + 1);
    vector<pair<int, int>> pairs;
    set<pair<int, int>> cp;
    for (int i = 0; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        st.insert(u);
        st.insert(v);
        pairs.push_back({u, v});
        g[u].push_back(v);
        if (dsu.findPar(u) != dsu.findPar(v))
        {
            dsu.unionByRank(u, v);
        }
        else
        {
            // part of cycle, needs to ignored
            cp.insert({u, v});
        }
    }

    map<int, vector<int>> mp;
    for (int i = 0; i < n; i++)
    {
        auto pr = pairs[i];
        int u = pr.first, v = pr.second;
        int u_p = dsu.findPar(u), v_p = dsu.findPar(v);
        if (!cp.count(pr))
        {
            mp[u_p].push_back(i);
        }
    }

    int result_parent_id = -1;
    int maxVal = INT_MIN;

    for (auto it : mp)
    {
        int min_curr_node = INT_MAX;
        int max_curr_node = INT_MIN;
        for (int index : it.second)
        {
            auto pr = pairs[index];
            int u = pr.first, v = pr.second;
            min_curr_node = min(min_curr_node, min(u, v));
            max_curr_node = max(max_curr_node, max(u, v));
        }
        int len = max_curr_node - min_curr_node + 1;
        if (len >= maxVal)
        {
            result_parent_id = it.first;
            maxVal = len;
        }
    }
    cout << mp[result_parent_id].size() << endl;
    for (auto index : mp[result_parent_id])
        cout << index + 1 << " ";
    cout << endl;
}
signed main()
{
    fastIO int t = 1;
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt","w", stdout);
    cin >> t;
    while (t--)
    {
        solve();
    }

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

If you are stuck on understanding what the editorial for Div1D is trying to do like I was, the DP state is dp[l][r][x][y] = the number of ways to fill in the subarray from l to r where we assume that l-1 and r+1 have already been chosen, and that the counts of l-1 and r+1 must increase by x and y respectively by filling in this subarray. Then we calculate by looking which of the elements from l to r we choose first.

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

Problem B, Div 1. Stay or Mirror Can anyone help me , why is me code failing on test case 4. ~~~~~

vector<int> inv(n,0);
for(int i =0;i<n;i++)
{
    for(int j = 0;j<i;j++)
    {
        if(p[j]>p[i])   inv[i]++;
    }
}
for(int i =n-1;i>=0;i--)
{
    int flipped_inv = 0;
    for(int j = i+1;j<n;j++)
    {
        if(2*n-p[i]>=p[j])    flipped_inv++;
    }
    if(inv[i]>=flipped_inv){
        int val = 2*n - p[i];
        p[i] = val;
    }
    else    continue;
}

int final_ans =0;
for(int i = 0;i<n;i++)
{
    for(int j = 0;j<i;j++)
    {
        if(p[j]>p[i])   final_ans++;
    }
}
cout << final_ans << endl;

~~~~~