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

Автор djm03178, история, 5 лет назад, По-английски
Tutorial is loading...

Solutions

Tutorial is loading...

Solutions - C++ - Java - Python 3

Tutorial is loading...

Solutions - C++ - Java - Python 3

Tutorial is loading...

Solutions - C++ - Java

Tutorial is loading...

Solutions - C++ - Java

Tutorial is loading...

Solutions - C++ - Java - Python 3

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

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

Nice Editorial by the looks of it... I mean the explanation seems thorough

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

Well, B was harder than usual Div2 B.

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

Really the explanation is superb.

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

    In problem B=> Why it is optimal to equal two adjacent elements once? Cannot there be a case that we can get min operations by doing equal two elements but not adjacent? why not? what is the proof for that contradiction?

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

      You might want to think of it like this :
      No matter what, you have to fix the adjacent differences using the operations.
      So what's the optimal strategy to achieve that by changing one element?
      It's to make a[i] anywhere between a[i-1] and a[i+1] because no matter what you do, abs(a[i+1]-a[i-1]) will remain. but as for the edges, this is not the case because they have only one adjacent element, and we can completely erase the difference by making them equal.

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

      Mathematically, you can think of it like this. lets say you have three elements a b c. Initially total number of moves would be abs(c-b)+abs(b-a). Now without loss of generality lets assume that abs(c-b)<abs(b-a).

      Lets make two non-adjacent elements a, c equal : c b c ans=2*abs(b-c).

      Now, lets make two adjacent elements equal, b b c ans=abs(b-c)+0. As you can see, it is always optimal to make two adjacent elements equal.
      Hope this was useful.

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

Well, the working of D was googleable. You just need to connect and implement.

Link to the site: https://www.codechef.com/wiki/tutorial-expectation (Q3).

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

Thanks for your fast & detailed solution <3

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

Thank you for the great solutions. (I love the problemset)

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

Pls help. I did the problem C with almost exactly the same algorithm (which is O(n^2)), but got TL many times. Submission.

Is this a std::vector slow or what?

P.s. I even did an initialisation to vector before operating with digits

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

The observation for problem B was very critical relating to problem statement. But I think problem C was easier as it was greedy implementation problem.

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

I really liked problem D. Thanks for the contest :)

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

I would admit B is an interesting problem, but it's way harder than a typical Div.2 B problem. Well, at least it's a good lesson about my mentality adjustment. I still can't overcome the frustration about can't solve easy(at least in my mind) problems during the contest (:」∠)_

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

This contest was tough!!

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

Nice ProblemSet, Editorial !

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

Everyone solved problem C assuming that it is always optimal for us to choose the right triangle. But how do we prove it? The statement said at least one side of the triangle should be parallel with one of the sides of the board. So we can have other types of triangles as well. djm03178

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

In problem E, I failed this problem in test 2, 14th testcase. System said I responded 3, but it expected 2. However, I debug the same code in code::blocks, it printed 2. I think the Testing system has error.

E번 문제 두 번째 테스트 14번째 케이스에서 정답이 2이고, 코드블럭에서 똑같은 코드에 똑같은 테케를 넣어본 결과 2를 대답했는데, 3을 대답해서 틀렸다고 나옵니다. 시스템 오류가 있는 것 같습니다.

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

Problem B was not easy for us!

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

I did a very stupid mistake in B.
For n = 2 answer will be 0 but I don't know what came to my mind, I printed their difference.
Sadly that case wasn't in the pretests.

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

Here is an easier solution for E:

Just binary search the answer $$$K$$$ and let $$$DFS(Node)$$$ return the minimum difference in height between this node and the last node you ate a snack from.

If all the children return difference in height $$$\lt K - 1$$$ just return the minimum meaning the last child you will visit is the one with minimum difference in height.

If one of them has difference in height equal to $$$\ge K - 1$$$ return it.

If more than one of them has difference in height equal to $$$\ge K - 1$$$ then there is no answer.

In the end just check that $$$DFS(1) \le K$$$.

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

Can someone explain D with a bit more mathematical proof? For the sequence 1 0 1 what are the expected number of tries for each stage?

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

    Let $$$E(x)$$$ be the expected number of tries to complete a game with $$$x$$$ levels, i.e, if you lose you have start again from level $$$1$$$. Let us have $$$m$$$ checkpoints at $$$a_1=1$$$, $$$a_2$$$, $$$...$$$ $$$a_m$$$. Then answer required is $$$E(a_1)+E(a_2-a_1)+...E(a_n-a_{n-1})+E(n-a_n)$$$.

    For a game with a single level, game ends once you make a single win, and you can win either in $$$1^{st}$$$ try or $$${2^{nd}}$$$ try and so on. If you win at $$$i^{th}$$$ try, you would have made $$$i$$$ moves. Since the probabilities of winning and losing are same,

    $$$E(1)=\sum_{i=1}^{\infty}1/2^i*i=2$$$.

    To calculate $$$E(n)$$$, we can use the same method we used for calculating $$$E(1)$$$. The difference here is that, every time you lose, you have to make $$$E(n-1)+1$$$ moves. The main idea is that, every time you win level $$$n$$$, you have to win level $$$n-1$$$ as well.

    So, $$$E(n)=\sum_{i=1}^{\infty}1/2^i*(i*(E(n-1)+1))=2*(E(n-1)+1)$$$.

    Solving this recurrence yields, $$$E(n)=2(2^n-1)$$$.

    The "constructing the checkpoints" part is nicely explained in the editorial.

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

      Thanks a lot for this clear explanation!

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

        For me this is less hard to understand:

        Let $$$x_i$$$ be the expected number to finish i stages without any checkpoint.

        $$$x_1=\frac{1}{2} + \frac{1}{2} \cdot (1+x_1)=2$$$ (Because with half prob we finish the stage in first atempt, and other half prob we need that one atempt and again have the same expected value)

        $$$x_2=x_1 + \frac{1}{2} + \frac{1}{2} \cdot (1+x_2)=6$$$

        ...

        $$$x_i=(x_{i-1}+1)\cdot 2$$$

        We see that this value gets bigger quickly, 1e18 is reached with less than 60 steps. Lets write those numbers into an array.

        Then for a given $$$k$$$, we can search that array for the biggest number smaller than $$$k$$$, let that be $$$x_j$$$. To construct stages so that the expected number is $$$x_j$$$ we need to put $$$j-1$$$ times 0 then a 1.

        Put that to ans and decrement $$$k$$$ by $$$x_j$$$, then repeat.

        Note that for some reason we are forced to place a 1 in first stage, so start with $$$k-2$$$. If $$$k$$$ is odd there seems to be no solution.

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

      How to prove this: $$$E(1) = \sum\limits_{i = 1}^\infty 1/2^i * i = 2$$$ ?

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

I tried E but I dont know where its wrong.

can someone point out my error https://mirror.codeforces.com/contest/1453/submission/100387883

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

My O(n^2) C gives TLE -_-:

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

B was harder than usual...!

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

During the contest I had the same idea for E as the editorial, but I couldn't see why it is always optimal to choose the child that has the shortest 'moving back' distance. I'd like to know if there is an easy proof/intuition for this.

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

It should be forbidden to write an editorial for that C without drawing a picture.

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

Wow... all codes are short!

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

Thank you for B, I learnt something new.

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

Irrespective of how much I f***ed up in the contest , I want to say that the problems are beautiful. I learnt a lot. I will practice harder. Thank you for such a nice contest.

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

my this WA solution and this AC solution is almost same, just diff is i am using log2 operator instead of another loop, why its wrong then???????

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

Okay this is probably silly, but help me out a bit.

To my understanding, I derived the following,

Assertion : In a sequence of length $$$k+1$$$, where the 1st and last characters are 1 and rest are zero. The expected number of tries to reach (not beat) $$$(k+1)^{th}$$$ stage from $$$1^{st}$$$ stage is $$$2^{k}$$$

Proof

With this assertion, I did the following, for an input x, subtract 2 (Since we need this to exit the last stage) and now for $$$k^{th}$$$ set bit, append sequence $$$1(0)^{k-1}$$$. And finally append $$$1$$$.

However for $$$8$$$, my algorithm prints $$$1011$$$, $$$2^2$$$ (beating 1,2) to reach $$$3^{rd}$$$, then $$$2$$$ beating $$$3$$$ to reach $$$4$$$, and finally beating $$$4^{th}$$$ with $$$2$$$ itself to win. A total of $$$4+2+2=8$$$.

It would be very helpful if someone could point out where I am wrong.

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

.

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

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

Thanks for the great problems.

For E, I keep getting runtime error for a pure dfs solution.

Looking at the submissions, it looks like only two solutions in PyPy3 were accepted, and each of these does dfs 'non-recursively' (by a bfs first), e.g. https://mirror.codeforces.com/contest/1453/submission/100385834

Could someone explain why a pure dfs solution gives a runtime error? Many thanks.

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

can someone please tell why this gives tle but this is Ac .

only difference between them is that the Ac code does not have the line #define int long long

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

Personally, I think that this is one of the most interesting contests in a long time. I enjoyed solving these problems a lot, especially problems B and E.

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

For E: it's too **boring** to find the sub-maximum in linear time, so just sort the candidates and it will be O(nlogn) for each test case.

More like: it's too **tedious** ...

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

Can anyone hack my O(n^3) solution for F? 100403925

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

Can anyone provide me the explanation of third case in editorial of B. abs(ai-ai+1)+abs(ai-ai-1)+abs(ai+1-ai-1).I am not able to understand why we are doing this.

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

    try to think if we have changed a2 with a1

    so now it will be a1,a1,a3,a4.... so final answer will look like this- final ans=abs(a1-a1)+abs(a3-a1)+abs(a4-a3)+......

    original answer with a1,a2,a3,a4.... orig ans=abs(a2-a1)+abs(a3-a2)+abs(a4-a3)+........

    subract final ans from orig ans so it will become abs(a2-a1)+abs(a3-a2)-abs(a3-a1) and that is exactly written in 3rd case

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

The problems were great. Thank you, djm03178. But I totally messed up this round. :(

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

if i am not hungry, i could solve F and become master

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

B was hard enough

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

Deteailed hindi video tutorial from A to E and thought process here

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

B problem was fun to solve! Thanks for such questions :)

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

For the triangles problem, I had the same approach and as n=2000 it should have worked for n^2. But it gave me TLE on the 5th pretest. Someone please tell me where I went wrong. Thanks in advance

100388093

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

Can someone explain how the max area*2 is 42 for digit-9 in this testcase 42987101 98289412 38949562 87599023 92834718 83917348 19823743 38947912 Any help is kindly required :(

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

As a matter of fact, the problem C is a little boring.
But overall, the contest is pretty good.
Thanks for the contest

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

Nice tutorial and problem set. Thank you.

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

This was by far the best editorial on codeforces according to me.

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

B was more tuff than usual but thanks for the explanation really helpful :)

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

can someone help me, i keep getting wrong ans on test case23 on test no 5. i have done something similiar to the first strategy in the editorial .

I find the sum of the gp of 2,4,8... which is just smaller than n and take those many zeroes and then subtract that sum from n and then the dif becomes the new n and i do this recursively till the dif is zero.

I even took the test case and found that the expected value was same as the number given .

Any help is much appreciated 100441237

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

Solution for prob B I found this video really helpful.

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

for the first time, I will say the tutorial is awesome. I am learning a lot.

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

F is cool, thanks

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

For problem B, if you know before the contest that the basic solution (in case of no replacement) is : $$$\sum_{i=2}^n abs(a_i - a_{i-1})$$$. Then it's probably easier to solve it as you can reason about this equation.

Is it part of a common knowledge that experienced competitive programmer have ?

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

Can anyone help me with this problem

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

For Problem F, 1453F — Even Harder, my O(n**3) DP solution passed. I didn't try to optimize the code, just wrote the simplest version for my DP expectig it will TLE and I will have to optimize my code to O(n**2). http://mirror.codeforces.com/contest/1453/submission/100570074 Although the solution is O(n**3), I am having difficulty in coming up with a test case which will trigger TLE on my solution.

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

Can anybody please explain the general equation derived in question D.I cannot get it please

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

can anybody please explain the main idea of solution used in problem E DOG'S SNACK

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

The problem D is very useful. I hope to conduct more value contests.

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

It took us one month to prepare a video solution for problem B :) If it is still interesting to anyone, please consider:

Video link

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

Alternate $$$O(N^2.\log{N})$$$ dp solution for problem F using a segment tree: link

The basic idea is to maintain n segment trees, and ith segment tree stores that if point i is the second point chosen, then all j > i, store the total answer if point i is chosen as the second point and j is chosen as the third point. This works because we only need to maintain information for the first two points, and rest of the chosen points will inductively be valid.

Note to self: Write explanation later.