awoo's blog

By awoo, history, 8 months ago, translation, In English

1860A - Not a Substring

Idea: BledDest

Tutorial
Solution (Neon)

1860B - Fancy Coins

Idea: BledDest

Tutorial
Solution 1 (BledDest)
Solution 2 (BledDest)

1860C - Game on Permutation

Idea: BledDest

Tutorial
Solution (Neon)

1860D - Balanced String

Idea: BledDest

Tutorial
Solution (Neon)

1860E - Fast Travel Text Editor

Idea: BledDest

Tutorial
Solution (awoo)

1860F - Evaluate RBS

Idea: BledDest

Tutorial
Solution (awoo)
  • Vote: I like it
  • +125
  • Vote: I do not like it

»
8 months ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

Pardon me if this is a stupid question, In problem D, after dp is used, the editorial says the answer is in the dp[n][cnt0][need], why can't "need" also be equal to just cnt0*cnt1 instead of (cnt0*cnt1/2) because in 00011 its balanced and the number of 01 subsequences is cnt0*cnt1 so should'nt we also check dp[n][cnt0][cnt0*cnt1].Thanks

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

    00011 is not balanced there are 6 subsequences of 01 but 0 subsequence of 10

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    00011 is not balanced unless you mean one of the resulting string after swap(s) is balanced. For example: 01010. In that case, the count of 01 & 10 is 3 which is equal to 3*2/2.

»
8 months ago, # |
  Vote: I like it +6 Vote: I do not like it

In problem C, I used the approach of maximum increasing subsequence. If Alice puts the chip on ith number, BOB, in the next turn, will put it to the second smallest number in maximum increasing subsequence, and then Alice has to move to the smallest number in the next turn which makes BOB the winner.

so Alice can only win if the size of the maximum increasing subsequence is 1 till the ith number. so that bob has to move the chip to the smallest number and Alice wins.

I got the wrong answer on test case 2. Please help me to know why this approach did not work. Thanks in advance.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Alice will not win if the length is 1, She'll only put the coin in first move on the i th element. Now Bob won't have anywhere to move that coin to, So Bob wins. Alice only wins if the length of the maximum increasing subsequence ending at ith element has the length 2 : she puts the coin on ith element, then Bob has to move it to the remaining 1 element, then Alice won't have any moves to make further.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Lets take an example:

    arr = [1, 2, 3, 4]
    Expected Answer: 1
    Your Answer: 2
    

    According to your solution position with value 2 and 4 will be lucky.

    But if you see for the value 4, Bob will make a move to 2, Then Alice is forced to make a move to 1, and Bob can't make a move now. So Bob wins.

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

It is now obviuos that this round had a problem with tests for task D. They were weak, tons of wrong greedy solutions got accepted and then hacked. Only an idiot would consider tests of such a quality normal. This is not okay, and I think the least authors owe to contestants is an apology for their sloppy work. Things like this should not be allowed by the community.

P.S.: Dear BledDest, I'm asking, I'm begging on my knees: please, don't make other posts about how wrong being toxic is. Because, as we've seen yesterday, sometimes if the author spends too much time fighting with toxicity in the community, he may not have enough time left to develop good tests for his problems.

P.P.S.: Don't mean to offend anybody. Make love, not crappy tests.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it -32 Vote: I do not like it

    “Greedy makes man blind and foolish, and makes him an easy prey for death.”
    -Rumi

  • »
    »
    8 months ago, # ^ |
    Rev. 2   Vote: I like it -38 Vote: I do not like it

    If the test were strong, you can just spamming greedy solution without proving it.

    P/S: FST==Skill issue

    P/s: BledDest don't listen to the idiot who can't prove his solution because of his skill issue and then blamming you for FST. Your contest is awesome.

    • »
      »
      »
      8 months ago, # ^ |
      Rev. 2   Vote: I like it -12 Vote: I do not like it

      Maybe then we should remove all the tests? To assure nobody will send a solution without proving? Moron, submitting without a strong prove is a common thing in competitive programming, exactly because there are tests to tell if solution is right or wrong! And btw remind me, what harm is in someone spamming wrong greedy solution, getting a WA verdict and receiving extra penalty?

      • »
        »
        »
        »
        8 months ago, # ^ |
        Rev. 3   Vote: I like it -12 Vote: I do not like it

        FST==skill issue

        hahahahahahahaha

        Anyway don't blame BledDest for your FST. Blame your skill issue.

        If you blame him for FST then maybe there should be no hacking phrase and every solution passes pretest should pass all the test????????????

        Actually, I think it is hard to make the test that break the solution without knowing them first.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    meme

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +32 Vote: I do not like it

    This man speaks facts.

    What is the point of tests if they accpet ideologically wrong solutions? More than 20% of solutions were hacked right after the contest. Need to pretend that everything is okay and not pay attention to it?

»
8 months ago, # |
Rev. 2   Vote: I like it +28 Vote: I do not like it

There is also a $$$O(n^4/\omega)$$$ approach for D:

Let's say the imbalance score of a string is the difference between the number of 01 and 10 sequences. Then the desired string should have a imbalance score of 0. There are two observations:

  1. You will never operate on a position twice, and you will never operate on two zeros or ones.
  2. By swapping a pair of 0 and 1 at position $$$(p, q)$$$, the imbalance score increases/decrease by $$$2 \times(p-q)$$$, no matter what substring is between the pair.

So you can calculate the imbalance score of the initial string and then do a backpack with bitset. In detail, let $$$S_0$$$ denote the set of position where there are 0s initially, and $$$S_1$$$ the set of position where there are 1s initially, and $$$d$$$ to be imbalance score of the initial string.

By observation 2, the task is now transformed into this: find the minimal $$$k$$$ such that you can select exactly $$$k$$$ numbers from each of $$$S_0$$$ and $$$S_1$$$, so that the sum of the $$$k$$$ numbers selected from $$$S_0$$$ is exactly $$$d/2$$$ greater than the sum of the $$$k$$$ numbers selected from $$$S_1$$$.

https://codeforces.com/contest/1860/submission/219317671

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    but, why is it always mimimum cnt of exchanges that 0 and 1 in order if (cnt of 01) > (cnt of 10)?

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think it's $$$O(n^4/\omega)$$$ though much faster than std

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      You are correct, I've just updated the comment.

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey, can you elaborate more about "backpack with bitset"? Is it some technique ? Any useful link will be appreciated !!

»
8 months ago, # |
  Vote: I like it +8 Vote: I do not like it

it is stored in dp(n,cnt0,need) , where cnt0 is equal to the number of characters 0 in the string s , and need=cnt0⋅cnt12 (because the number of subsequences 01 should be equal to the number of subsequences 10). But our dynamic programming stores the number of changes in the string, and the problems asks for the minimum number of swaps. However, we can easily get it from the dp value. Since the amounts of zeroes and ones are fixed in the string, then the number of changes from 0 to 1 equals to the number of changes from 1 to 0 and we can pair them up. So, the answer to the problem is the half of the dp value.

Can anyone explaine me how need is equal to c0*c1 also how storing string changes helps in calculating answer?Please

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Take for example, n = 5 c0 = 3 and c1 = 2. To maximize the number of 01s, we can take the string 00011. Here the maximum number of 01s is 3*2 = 6, and the minimum number of 10s is 0. Our objective is to reach the middle where the number of 01s = number of 10s = 0+c0*c1/2 = c0*c1/2.

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      THanks for reply ,i got half but still confused like why we need to create string of form 00001111 like how does it gaurnatees minimlaity for swaps.This is the only correlation I am finding hard to understand.

      • »
        »
        »
        »
        8 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        We dont need to create a string of the form 00001111. The maximum value that the number of 01s and 10s can take is when it is of this form. The thing that guarantees the minimality of swaps is the dynamic programming algorithm he wrote where we have a subproblem of the form (i,cnt0,cnt01). The minimum value is at (n,number of zeros in string , c0*c1/2) which is when the number of 01s in the string is of value c1*c0/2 and so is the number of 10s.

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

In problem C, How is it solved using Binary Indexed Tree or Segment tree? What is the logic?

»
8 months ago, # |
  Vote: I like it -35 Vote: I do not like it

Very late Editorial.

»
8 months ago, # |
  Vote: I like it +1 Vote: I do not like it

What can be the best complexity in problem C, if we would allow repetitions in the array?

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

Can anyone explain the problem D solution?

»
8 months ago, # |
  Vote: I like it +8 Vote: I do not like it

In problem D, Is there any way to solve it with recursive dp?

  • »
    »
    8 months ago, # ^ |
      Vote: I like it +11 Vote: I do not like it
  • »
    »
    8 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    There is alternative solution with recursive dp. In my opinion, it is harder (but possible) to proof that it is not TL (Time Limit) exceeding solution. 219620926

    • »
      »
      »
      8 months ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      The complexity of recursive DP is O(n^4). But there won't be more than 100*100*5000(c0*c1*d) states. Actually it is even smaller than that, because c0+c1<=100. Plus the time limit was 2 seconds and there was no multiple test cases.

»
8 months ago, # |
Rev. 2   Vote: I like it -7 Vote: I do not like it

I solved two problems during the contest and upsolved the third one. Here I would like to share my approach in them.

Problem A — Not a Substring Just one simple fact is enough that in the two cases of a valid bracket sequence (((...))) and ()()()... there is only one common substring "()". Otherwise having s in one of them surely makes the other situation our answer t.

Problem B — Fancy Coins This was a very interesting problem and I would like to call my approach a greedy+compromise method. First we act by spending all ak coins, then a1 coins. Then we find the number of fancy coins used as the first case. Another case we deal with is where we compromise by spending lesser a1 coins and more fancyk coins. The minimum of the two situations will be the answer.

Problem C — Game on Permutation The perfect strategy for Alice to win will be a condition a[j]>a[i] for every j<i. And it should occur only once in any subarray. That's the perfect winning strategy for Alice. For that to happen, we take two variables: firstmin(fm) and secondmin(sm). If our current element becomes something like: sm<curr && curr<fm If we find the above condition, then curr is a winning position for Alice, and we will update curr=sm and move forward.

If you want to read a much detailed solution, here's my in-depth written editorial on it — https://medium.com/@Harshit_Raj_14/educational-codeforces-round-153-rated-for-div-2-editorial-a286eda94f5c

Also the codes are available on my github repo — https://github.com/Harshit-Raj-14/My-Codeforces-Solution/tree/main/Educational%20Codeforces%20Round%20153%20(Rated%20for%20Div.%202)

Hope you enjoyed going through the article. You can follow me to get the latest update when I upload more such amazing articles.

»
8 months ago, # |
  Vote: I like it +32 Vote: I do not like it

The rotating sweep line intuition in F is cumbersome and not so easy to think about. I find the following way to be more easy and intuitive (Well, they are equivalent, but I don't like rotating sweeplines).

Sorting pairs $$$(a, b)$$$ by $$$ax + by$$$ is the same as sorting pairs by $$$a + b\cdot \frac{y}{x}$$$. Now you can replace $$$\frac{y}{x}$$$ with any positive real $$$t > 0$$$, and you sort pairs by $$$a + b\cdot t$$$. From now it's obvious, that initially you arrange the pairs lexicographically ($$$t = +\varepsilon$$$), and then gradually increase $$$t$$$, the pairs that are swapped are of kind $$$(a_1, b_1)$$$ and $$$(a_2, b_2)$$$, where $$$a_1 < a_2, b_1 > b_2$$$ (and the swap is performed for $$$t = \frac{a_2 - a_1}{b_1 - b_2}$$$).

»
8 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Finally new Color(CM).

Video Editorial for Problem D,E:-

https://youtu.be/khVG1JPdR1o

Video Editorial for Problem A,B,C:-

https://youtu.be/wZF5qfvBhuM

»
8 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Explanation for those who are confused in Problem E why the author has not constructed a transposed graph to find out the distance from f -> s :

Let say we are connecting an edge of weight 1 when we are going from dummy node to an index node and an edge of weight 0 from index node to dummy node. When we are finding shortest distance from dummy node to index node then bfs on the normal graph will work. It is obvious.

But when we are finding shortest distance from index node to dummy node then we should apply bfs from the index node. But we can't do that as it will result in O(n^2) complexity. Alternative is that we can apply bfs from dummy node in the transposed graph and find the shortest path for each index node. So the complexity is now reduced.

But it is not necessary to construct the transposed graph. Edges between index nodes are already bidirectional. In transposed graph, from dummy to index we will have an edge weight of 0 now . In the original graph, we are getting an extra edge weight as from dummy to index we are traversing via edge weight 1 and rest all edges in path have the same effect.

So we can use the original distance and reduce it by 1.

Hope it helps !!

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

Problem D. Let dp[i][z][b] = min number of swaps to make first i characters balanced given that we have seen z zeros so far and the number of 01s — 10s so far is b. Now let's assume that s[0...i] is balanced after performing some number of operations. Let's also assume that s[i + 1] = 1. Then the introduction of the character s[i + 1] increases the "balance" by z (the number of previously seen zeros). Presumably, this balance may be fixed in only 1 swap. However this isn't obvious to me at all. Someone help please!

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

BledDest Sorry for necroposting. For Problem F, I would like make a clarification to the problem statement by asking what the expected output is given the following test case:

1
4
4 1 )
3 2 (
2 3 (
1 4 )

According to the second paragraph of your problem statement, we may choose $$$(x, y) = (1, 1)$$$ so all the $$$a_i \cdot x + b_i \cdot y$$$ are $$$5$$$ and place them in ()() since it's a tie. However, the sample solution written by awoo above outputs NO instead.

Thanks a lot!

  • »
    »
    7 months ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    This test case is given in incorrect format. There are $$$2n$$$ points in the problem, not $$$n$$$, so the number in the second line should be $$$2$$$.

    If you change it to $$$2$$$, then the model solution says YES.

    • »
      »
      »
      7 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks for your replying and sorry for making such a stupid mistake...

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

In Problem B. It is nowhere written what is used for fancy coins.

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

1860C - Game on Permutation

Hint1
Hint2

Tutorial

Submission : 237228234

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Double ke jalwe har jagah dikh rahe h OoO