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

Автор vovuh, история, 6 лет назад, перевод, По-русски

Я очень благодарен всем тестерам, особенно Gassa и Rox за их неоценимую помощь!

1409A - Ещё одна задача о двух числах

Идея: vovuh

Разбор
Решение

1409B - Минимальное произведение

Идея: vovuh

Разбор
Решение

1409C - Ещё одно восстановление массива

Идея: vovuh

Разбор
Решение (Gassa)
Решение (vovuh)
Решение (Rox)

1409D - Уменьшение суммы цифр

Идея: MikeMirzayanov

Разбор
Решение

1409E - Две платформы

Идея: vovuh

Разбор
Решение

1409F - Подпоследовательности длины два

Идея: vovuh

Разбор
Решение
Решение (Gassa, жадность, O(n^4))
Разбор задач Codeforces Round 667 (Div. 3)
  • Проголосовать: нравится
  • +107
  • Проголосовать: не нравится

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

fastest tutorial <3

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

Runtime Error C++ Exit code is -1073741571

Submission : https://codeforces.me/contest/1409/submission/91879266

Can anyone explain what is going wrong in my code that is giving me runtime error?

Any help would be appreciated

Thanks :)

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

nice problemset

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

My screencast + live commentary, enjoy watching :)

https://youtu.be/nloGFTpdTJo

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

Wonderful problems, tutorials and solutions!

Thanks to writers!

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

.

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

Why does pow(10,18) return 10^18-1

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

Why B solution is optimal? I did it, but didn't understand why it worked

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

    Suppose you have 2 numbers a and b and you can decrement them by 1, 2 times.

    Then you have 3 options
    - a-1,b-1
    - a-2,b
    - a,b-2

    In first case product is ab+1-a-b.
    In second case product is ab-2b.
    In third case product is ab-2a.

    Now let b>a

    Then ab+1-a-b > ab-a-b > ab-2b (because a<b)
    So you now saw first case gives bigger product than 2nd case.

    Similarly you can prove for other pair of cases too.

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

    If we decrease a 2 times, then you decreased the product by 2*b. If we decrease b 2 times, then we decreased the product by 2*a. If we decrease a once and b once, then we decreased the product by a+b.

    If a>b, 2*a>=a+b>=2*b, whereas, if b>a, 2*b>=a+b>=2*a

    I guess you can see why decreasing one as much as possible is optimal.

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

Can someone tell me why my submission for E doesn't work? It's coordinate compression + greedy. https://codeforces.me/contest/1409/submission/91881485

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

Well, with a little bit of maths, C can even be solved in O(n)

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

E can also be solved with a simple DP. First, sort the $$$x$$$ array. Then for each $$$i$$$ from $$$0$$$ up to $$$n - 1$$$:

  • Find first such $$$j$$$ that either $$$j = n$$$ or $$$x_i + k \lt x_j$$$. Notice that $$$j$$$ never decreases so there is no additional complexity here.
  • Let $$$dp^0_i = max(dp^0_i, dp^0_{i - 1})$$$ and $$$dp^1_i = max(dp^1_i, dp^1_{i - 1})$$$ for $$$i \ne 0$$$ and $$$dp^0_i = dp^1_i = 0$$$ for $$$i = 0$$$.
  • Let $$$dp^0_j = max(dp^0_j, j - i)$$$ and $$$dp^1_j = max(dp^1_j, dp^0_i + j - i)$$$.

$$$dp^0_i$$$ contains best answer up to $$$x = x_i$$$ with one platform, and $$$dp^1_i$$$ with two platforms. If you are lazy (like me), just take the maximum of all values. Note that $$$j$$$ can be $$$n$$$ in the above loop, so the DP array will have $$$n + 1$$$ items.

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

Very nice problems!

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

Thanks vovuh! Nice contest to learn the basics for noobs like me

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

How to solve E if we have K platforms? $$$O(KNlogN)$$$ is fine but better than this??

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

Yeah, F can be solved in O(n^3).

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

Hey could someone please take a look. I did the same thing as the editorial for Problem E but still getting WA.

[Submission here](https://codeforces.me/contest/1409/submission/91884363)

any help is appreciated, thanks!

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

can someone help me the tutorial of Problem B ? .. I didnt get it ... or any better soln will be appreciated !

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

    Well the tutorial says that if we decide decrease a by 1 in the step then it's better to continue to decrease a until we can and then start with b. Similarly if we start with b then keep on decreasing it until we can then start with a.

    consider both of the above cases and print the smallest product. Hope this helps:)

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

Problem D — Can be solved in constant time (O(18))

Approach
Example
Python Implementation
»
6 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

1409E - Two Platforms I do not get the point why the y[] are in the statement. If it is obvious that we do not need them, then they are obviously unnecessary. If it is not so obvious then it is a misleading statement. So, why?

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

    spookywooky I do not get the point why this comment is under the editorial. If it is obvious that we do not need it, then it is obviously unnecessary. If it is not so obvious then it is a misleading comment. So, why?

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

      I mean, I don't think this makes sense. Clearly spookywooky thought we do need the comment, so your clever response isn't actually valid.

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

        I thought this legend needs $$$y$$$-coordinates as well as he thought we need this comment.

        Partially, your answer below is right, this part teaches to notice some probably useless things in the problem and to make some transitions from one problem to another. And yeah, this is essentially not the worst way to allow repeated $$$x$$$-coordinates.

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

    The y coordinates are of no use I agree but they are necessary as a part the author to chose to describe the original problem, sometimes some variables are just to help understand the actual statement clearly , but here these y coordinates are not misleading!

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

    One benefit of $$$y$$$-coordinates is that it makes it clear and unambiguous that we can have repeated $$$x$$$-coordinates, as well as overlapping platforms. However, the statement said platforms could overlap anyway, so yeah I'm not sure.

    Also, maybe it was intentional that they're unnecessary because the 2D to 1D transformation is something worth testing/teaching, especially in Division 3. In general, part of the difficulty of Codeforces problems does stem from transforming the problem into an equivalent but simpler one (in a way, this is all problem-solving).

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

For Problem D: Can anyone tell me what's wrong in the following approach: For example 217871987498122 10, traverse from starting digits, 2,1,7,8 and add them, when sum become greater than or equal to 10, that is on 7. increment one digit before 7 from 1 to 2 and rest all digits after it to 0, the number will become 220000000000000. if it violates on first digit then make 1000... this number. please help?

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

My solution to C was as follows. I "stuffed" as many elements as I could betweeen x and y. Then I calculated this minimum difference and added some elements less than x. I would add as many as I could until I was done or these values became negative. In the case they became negative, I would add the remaining values above y. I believe this should be O(N)

My solution: https://codeforces.me/contest/1409/submission/91831887

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

https://codeforces.me/contest/1409/submission/91894057

can somebody please help me with this problem why it is giving the wrong answer? any help would be highly appreciated

Thanks

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

Can somebody explain me the greedy approach in problem F please....

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

    Let the string $$$t$$$ be be (begin and end). Sure, there is also a case where the letters of $$$t$$$ are equal, but it can be solved separately, or the solution could be carefully implemented to account for that.

    Now, suppose that we are going to make exactly $$$k$$$ moves. We have two kinds of useful moves: put b somewhere and put e somewhere. Let the number of b-moves be $$$b$$$, and the number of e-moves be $$$e = k - b$$$.

    Of all the ways to put exactly $$$b$$$ letters b, the most impactful way is to put them into $$$b$$$ leftmost places in the string... unless we change some e into b in the process. Let the number of e -> b moves we are going to make be $$$x$$$ ($$$0 \le x \le b$$$). So, with our b-moves, we pick $$$x$$$ leftmost letters e and change them into b, then pick $$$b - x$$$ leftmost letters which are not b and not e and change them into b.

    Similarly, when we are going to put exactly $$$e$$$ letters e, the most impactful way to do it is to put them into $$$e$$$ rightmost places in the string... unless we change some b into e. Let the number of b -> e moves we are going to make be $$$y$$$ ($$$0 \le y \le e$$$). So, with our e-moves, we change $$$y$$$ leftmost letters b into e, and also change $$$e - y$$$ leftmost letters which are not b and not e into e.

    What remains is to look over all possible tuples of $$$(b, e, x, y)$$$. Remember that $$$e = k - b$$$, so there are only $$$O (n^3)$$$ such tuples. For each of them, simulate the above greedy process (in linear time) and then find the answer (in linear time too). The total complexity is $$$O (n^4)$$$. A possible speedup is to precompute the required stuff for prefixes and suffixes, bringing it down to $$$O (n^3)$$$ or $$$O (n^2)$$$ (didn't actually try).

    Why we can assume $$$e = k - b$$$? If we actually do less than $$$k$$$ moves in the optimal solution, it means all the letters become b or e. So we can cheat: for the optimal $$$e$$$, we make unnecessary moves from the left, and then overwrite their results by exactly $$$e$$$ moves from the right.


    All in all, this greedy solution requires more observations than the dynamic programming solution, but requires less proficiency to come up with it.

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

1409C - Yet Another Array Restoration can be solved greedily in $$$O(n)$$$ instead of $$$O(n+\sqrt{y})$$$.

Hint
Code (Python 3)
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

In 1409D - Decrease the Sum of Digits, if we maintain the digit sum, time complexity will be reduced to $$$O(\log n)$$$.

Code (Python 3)
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi guys, I'm a python coder and I was wondering if someone could explain to me what's slowing my code down?

Submission: https://codeforces.me/contest/1409/submission/91900467

It should work quite fast since each loop can only go 18 times, and it seems to work fast when I tested it. However, I keep running out of time on test 3.

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

    Number of operations is $$$2 \cdot 10^4 \cdot 18 \cdot 18 \approx 6.5 \cdot 10^6$$$ operations, and when you include the powers 10**(i+1) and 10**i, as well as the fact that pypy bigint's are pretty slow, it's too much for python to handle I guess.

    I tried submitting it in Python3 instead, to solve the bigint problem, but that didn't work.

    It could've been easily fixed by having $$$100$$$ or $$$1000$$$ test cases maximum, but ¯\_(ツ)_/¯

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

Can someone tell me why my code is giving the wrong answer, please... Submission: https://codeforces.me/contest/1409/submission/91873431 Thank you.. big help

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

My doubt is pretty naive. Still I want to know why wrapper class Integer/ Long array sorting is faster than primitive data types like int / long ? I know that primitive data type use quicksort internally in Arrays.sort().

Also, when to use primitive for sorting and when to use wrapper for the same.

Also look at both of my submissions for E in this contest.

  • Submission 1 getting TLE because of using Arrays.sort() in long[] array. My submission 91904739
  • Submission 2 passing system tests because of using Arrays.sort() in Long[] array. My submission: 91904938
  • »
    »
    6 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    In a nutshell, you can be hacked with an adversarial case that makes quicksort degrade to quadratic. Object sort uses merge sort instead which doesn't have this issue, but wrapper objects are slow and inconvenient.

    I think the best solution is to randomly shuffle before sorting (I have a prewritten safeSort method, you can see it here: 91830757).

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

Then let's build suffix maximum array on r and prefix maximum array on l. For l, just iterate over all i from 2 to n and do li:=max(li,li−1). For r, just iterate over all i from n−1 to 1 and do ri:=max(ri,ri+1).

In problem E — the editorialist tells to build suffix maximum array on r and prefix maximum array on l. But Why?? I understand the use of the prefix array and suffix array. Why to find the maximum prefix array and suffix array?

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

    Let's assume the answer is segL and segR (which do not overlap of course). Assume that there is a point midp which lies between two segments but do not overlap. We know segL stands on the left side of the midp and segR stands on the right side of the midp. Note that segR is the best platform possible on the right side of the midp otherwise the answer would contain the better platform (which means segR is not a valid solution). Greedly you can see the score of the best platform that is on the right side of the midp can be found using maximum suffix array. Also the the score of the longest platform that is on the left side of the midp can be found using maximum prefix array. This operation takes O(1) time if you have the prefix and suffix arrays. Now, because we don't know the midp, our goal is to iterate over all possible midps in O(n) time and store the best possible score.

    Please don't hesitate asking any questions I'm trying my best to make things clearer.

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

Can someone share some thoughts about how $$$F$$$ can be solved when length of $$$t$$$ is arbitrary. During contest i didn't read the part that length of $$$t$$$ is fixed and is 2. I kept trying to solve it for t of arbitrary length and when after the contest i read that length of t is fixed i realised it was trivial (: for length t = 2.

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

A slightly different and a bit easier to understand (maybe?) implementation of E. 91876338. :)

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

Regarding problem E. Two Platforms, I used TreeMap to solve. Them time complexity is also O(N log N), but it is TLE. I got stuck on investigating the cause. Could anyone help me to have a look? thanks.

https://codeforces.me/contest/1409/submission/91911507

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

Why this greedy approache for F is wrong?Can you help me?

Detail:

Suppose we change i positions into t[0],and i2 into t[1].Let's greedy:The i positions which are changed into t[0] is as small as possible,and the i2 positions which are changed into t[1] is as big as possible. (If s[i]=t[0]/t[1] ,it doesn't need to be changed).

Code:

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

    i did a greedy approach too and it was like you said the t[0] position should be the least and the t[1] position should be the last but there is a problem maybe we have some character t[0] in the end of the string not exactly the last character but lets say the second half of the string lets say t[0] = a and t[1] = b

    there can be a situation like this : aaa .. b .... (a) .. bb in this case its better to change (a) to (b) to get more occurrences.

    in my solution i count if we change the i position to t[0] or t[1] how much we can get and how much we loss as we can see in the sample if we change (a) to (b) then we get 3 more occurrences and loss 2 occurrences and its good but some times its not however my solution got wrong answer in testcase 41 :))))

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

For problem D, why do we need to have(10−dig)%10 moves instead of (10−dig) moves? As dig=n%10, is that %10 really necessary?

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

Can someone help me with F.
My dp[ind][k][left] states that we are at index 'ind', we can do atmost k changes, and we have left occurances of t[0] from [0:ind-1]. Then my answer will be dp[0][k][0]. My transition are, either change current character to t[0], or t[1] or don't change at all.
I am getting wrong answer at testcase 20. My submission 91926014

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

Binary Search Solution for D

For some reason spoiler isn't working properly.

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

Dear, MikeMirzayanov Please Check Why all my Correct Solutions got Skipped. I had submitted 3 Solutions in Yesterday Div 3. Contest and they were correct but saw today all were skipped which dipped my rating a lot. Please Check why Did it happen.

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

In the solution of Two platforms problem,why are we considering l[i]+r[i+1] rather than l[i]+r[i]?

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

I don't understand where the author says in problem E that li:=max(li,li−1) will help. Why? How? Can somebody explain it more clearly?

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

    Don't forget that you have calculated l and r beforehand.

    where li is the number of points to the left from the point i (including i) that are not further than k from the i-th point

    After that you convert the same array to a prefix maximum array (ith element contains best possible value of elements in range(0, i)). By induction you can iterate from left to right and you can say that ith element of maximum array is either i-1th element or the new added element's value. Because the author didn't separate the two arrays it might get a little confusing. They just converted the array to aa prefix maximum array without assigning it to a new variable.

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

I think the explanation of Problem E is a little hard to understand. I think the key point of this question was "Dividing the number line into two segments.". The answer is the best platform that lies in the left segment + the best platform that lies in the right segment. Simply iterate over all possible divisions and the best result is your answer. I couldn't understand the solution when I read this explanation but when I watched SecondThread's video I really understood the problem.

I also really liked the difficulty levels of the first 4 questions.

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

What is the proof for the fact in problem B solution ?

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

    Assume you are going step by step (decrease 1 at a time) and a > b (you can switch them very easily in code).

    if you decrease a, the final product will be (a-1)b = ab — b if you decrease b, the final product will be a(b-1) = ab — a

    because a > b, decreasing a from ab will result in a smaller product then decreasing b from ab.

    The problem starts when a=x. After a=x, you start decreasing from b. I don't think you can have a solid proof of which is optimal without starting from a or starting from b after this point. Therefore try both possibilities.

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

What is 1ll in editorialist solution? ans = min(ans, (a — da) * 1ll * (b — db));

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

In the solution code of problem C (Rox); the condition diff/delta + 1 > n Can someone explain it for me?

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

How to solve F with bigger constraints? I was trying to solve for O(n^2) solution but couldn't make significant progress.

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

I felt soo dumb on myself when i did the correct thing but wasted 45 min and 5 WA submissions for erasing the vector for graph only upto n-1 and not n,on question D.

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

del