vovuh's blog

By vovuh, history, 5 years ago, In English

1183A - Nearest Interesting Number

Idea: MikeMirzayanov

Tutorial
Solution

1183B - Equalize Prices

Idea: MikeMirzayanov

Tutorial
Solution

1183C - Computer Game

Idea: MikeMirzayanov and vovuh and BledDest

Tutorial
Solution

1183D - Candy Box (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1183E - Subsequences (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1183F - Topforces Strikes Back

Idea: vovuh

Tutorial
Solution

1183G - Candy Box (hard version)

Idea: MikeMirzayanov

Tutorial
Solution

1183H - Subsequences (hard version)

Idea: MikeMirzayanov

Tutorial
Solution
  • Vote: I like it
  • +64
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

Can somebody explain how we solve B with Binary Search?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    We can do binary search over the final price and then check whether it differs not more than k from the minimum and the maximum original price.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem B, initially I wrote binary search solution for answer. But, it seems it is giving $$$-1$$$ for some valid case too.

Can someone please help me identify the mistake in my submission ?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I wrote almost the same solution but I got WA on test 3 :(

    https://mirror.codeforces.com/contest/1183/submission/56084413

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Ok, I get it. Even if for a certain value, we cannot satisfy the conditions, It does not mean that the answer should be less than the value, the answer can be more than the current value too.

    For eg, Your array is 2 2 5 10 and let K be 4, the lower bound is 0 and the upper bound cannot be greater than (min+K). So you first check for (0+6)/2 = 3 (as mid). You see that condition is not satisfied as (10-3)>4, so you conclude that answer should be less than 3, which is not true, since the value 6 satisfies all the condtions.

    So, I think that Binary Searching the Answer cannot work for this problem.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    you should initialize lo to the least valid value, but you're initializing it wrong (0 isn't a valid answer). you should rather initialize to : max(a[n — 1] — k, 1) but only after checking if that value is indeed a valid candidate for an answer. (which is easy you can iterate through the array and check that) my solution

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

1183C - Computer Game Can somebody explain why we need to do (-c+1)/(a-b) instead of (-c)/(a-b)? Thanks

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    Because you have to add some differences to obtain the charge at least $$$1$$$. See the following example: $$$-c$$$ = 4, $$$diff = 2$$$. Then your formula will say $$$2$$$ and after two additions we will obtain $$$c=0$$$ and this is bad for us.

»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Vovuh's contests : Always fast and high quality

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem H, I wonder if we don't get min of 10^12, what will happen then ?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    Theoretically, the number of subsequences of the string of length $$$n$$$ is $$$2^n$$$. This value is the upper bound but there can be strings that contain more than $$$2^{63}-1$$$ subsequences and if you not take the minimum with $$$10^{12}$$$ then your dynamic programming will overflow the 64-bit datatype. I takes $$$10^{12}$$$ because you never need more subsequences than $$$10^{12}$$$.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      what is the time complexity of solution of problem E ?

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        $$$O(n^2k)$$$ since it's a BFS that visits $$$k$$$ vertices, and processing each vertex takes $$$O(n^2)$$$ time (generating/checking $$$n$$$ strings, taking $$$O(n)$$$ time each).

        (Edited to fix runtime, thanks Roach00.)

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it +4 Vote: I do not like it

          but what about unnecesary generated subsequences apart from visiting k vertices and O(n) in deleting character from string for each vertex string

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            You're right, it's $$$O(n^2 k)$$$ in the worst case where there are lots of duplicates and we only get one new string of each length.

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              we could reduce the time complexity of search,insertion and deletion to log(n),if we use suitable data structure.but that could be hell of implementation.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Can anyone explain how to come up with the upper_bound(1e12) for any given string?

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        it's in the problem. ~~~~ 1<= k <= 10e12 ~~~~

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

It's great to see these problems which have two versions! It helps me have a deeper understanding of the problem~

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

what is the mistake in my idea in problem D my code here

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Hey just check out this case:

    No. of type 1 candy = 5

    No. of type 2 candy = 5

    No. of type 3 candy = 5

    No. of type 4 candy = 5

    No. of type 5 candy = 4

    ans after soring would be 5,5,5,5,4. and just before you would be adding the last type of candy. State of map s would be : 5:4 4:1 3:1 2:1

    & now you should increment at 1 but instead, you increment at (4-2+1) i.e 3.

    A better approach would be to store the last number of candies added and then decide what to do in the next loop. If stuck you may check out: https://mirror.codeforces.com/contest/1183/submission/56115382

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

tnx for fast tutorial and good explanation but lots of questions was mikemirzayanov's

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Really liked the trick you applied to F . Nice contest and especially problem F.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hello, Could you help me with H (hard version)? It says wrong answer on test 43. I have no idea what is wrong with it. Thanks in advance. http://mirror.codeforces.com/contest/1183/submission/56165101

Edit: Anyways, I solved it after long hours of hard work.

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

I solved H with slightly different approach where dp[i][j]=#of subsequences of length j on prefix [1..i]. Let character on position i be A, then the transition looks like: dp[i][j]=dp[i-1][j]+dp[i-1][j-1]-dp[last[A]-1][j-1]. It is correct, however it may overflow sometimes. What I did was first adding dp[i][j]+=dp[i-1][j], and if now dp[i][j]>1e18 i skip the second part of addition. It passed the tests, but im almost sure that this is not correct. If we substract this value of dp somewhere later it is not preceise, so later dp states may be not exactly correct. So here come my questions: 1. Am i right that it is not correct? 2. Is this really hard to construct counter-test?

»
5 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

Can someone pls explain to me the solution of Problem H

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explains the 3. Computer tutorial formula for turns i.e. turns = ceil(-c+1/(diff)). How this formula is derived?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Alright. Let the number of just play turns be $$$x$$$, and let the number of play and charge turns be $$$y$$$.

    The problem statement suggests that total number of turns should be $$$n$$$, hence $$$x+y=n$$$.

    It also suggests that the battery that these moves consume altogether should be strictly less than the initial charge. Just play moves consume $$$a*x$$$ amount of battery, whereas play and charge moves consume $$$b*y$$$ amount of battery, hence $$$a*x+b*y<k$$$.

    Substituting $$$y$$$ from equation 1, you can obtain an upper bound on $$$x$$$, and hence you'll arrive at the aforementioned equation.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks for your explaination. I got expression as y<(-c)/(a-b) as per your explaination above. But in code why are we taking ceil operation and adding 1 int he numerator. How its giving the correct answer

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please explain why my submission won't work for Candy hard where submission id is https://mirror.codeforces.com/contest/1183/submission/56124846?

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Can someone please explain the validity of the solution given here for F?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My solution to F is: Sort the array (arr). Remove duplicate elements. Iterate for all i find maximum index j < i such that arr[i]%arr[j]!=0 and then find maximum index k < j such that arr[j]%arr[k]!=0 && arr[i]%arr[k]!=0, maximize the answer by arr[i]+arr[j]+arr[k].

    Let prove this solution by contradiction: Suppose, for index i there are indexs j0, k0 (k < j0 < k0 < i) can be selected such that arr[j0]+arr[k0] > arr[j]+arr[k] and i, j0, k0 satisfies all the conditions, if this is possible then our soluton is wrong. But if we look carefully this is not possible. j0 was not selected as k so we are sure arr[j]%arr[j0] = 0, same k0 was not selected as k because arr[j]%arr[k0] == 0, So arr[j0] and arr[k0] are divisors of arr[j] and obviously smaller than arr[j]. And we know than some smaller divisor of a number n is <= n/2 so arr[j0] + arr[k0] <= arr[j]. So above condition arr[j0]+arr[k0] > arr[j]+arr[k] is not possible.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Thanks, for the explanation. I was wondering what is the time complexity of this solution. I saw your submission, its execution time is 78ms, but from the approach above it should be O(n^2). Can you please explain what I am missing and how does the overall complexity is less than n^2.

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

        Since there are no duplicates and you only keep iterating in the second loop while you see a number thats a factor of either arr[i] or arr[j], that only happens at most $$$2\sqrt(n)$$$ times, because a number has at most $$$2\sqrt(n)$$$ factors. Therefore, you can bound the time complexity as $$$O(n\sqrt(n))$$$

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

someone explain where my solution fails for Problem C? MY Solution

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You are using ints, rather than longs. This overflows when you multiply n and b. I replaced your declarations of variables k, n, a, b with longs and it passes the first case you failed.

»
5 years ago, # |
  Vote: I like it +4 Vote: I do not like it

How to solve F by iterating over all possible triplets?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me with the problem C why are we adding diff to k (k + diff — 1) / diff

»
5 years ago, # |
  Vote: I like it +5 Vote: I do not like it

For 1183F - Topforces Strikes Back, I tried all of the possible options for 30 biggest unique elements and failed in system testing (56093272). After the contest, I changed 30 to 40 and got accepted (56189352). Can anybody come with a counter test for my solution? The main idea is if $$$x \neq y \land x | y \rightarrow x \times 2 \leq y$$$.

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it +8 Vote: I do not like it

    This solution will fail at the test case where a lot of divisors of maximum should be omitted in greedy solution. So we can take a number $$$x$$$ with maximum number of divisors, omit one from $$$\{x/2, x/3, x/5\}$$$ and add two minimum numbers that are not divisors of $$$x$$$. This gives us a test with $$$n = 149$$$, where the least number is used in optimal answer.

    Test
    Generator

    Probably this or something similar should be added to this problem testset, vovuh

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      Thank you, I will add this test now!

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain me the Problem D ?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In this problem, you are provided with total different types of candies (n)

    and then the type of a particular candy in a series

    for eg- 8 1 4 8 4 5 6 3 8

    8 is the total number of different types of candies

    and the sequence in which they are present is

    firstly a candy of type 1 then a candy of type 4 then a candy of type 8 again a candy of type 4

    total number of candy of type 1 is = 1 total number of candy of type 2 is = 0 total number of candy of type 3 is = 1 total number of candy of type 1 is = 1 total number of candy of type 4 is = 2 total number of candy of type 5 is = 1 and soon

    You have to find out the gift

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

can anyone please explain tutorial for H. I don't understand how does it work.

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    I too am unable to understand the very first few lines.

    "Initially all values are zeros except $$$dp_{i,1}=1$$$ for each $$$i$$$ from $$$0$$$ to $$$n−1$$$."

    Why is this the case? Won't it count two different occurrences of the same character as two instead of one ( we need distinct subsequences right? )

    UPD: (Adding example)

    If the given string is "abca", then $$$dp[0][0]=1$$$ and $$$dp[3][0]=1$$$, but I thought we must count it as 1 times "a" is there not twice. How is that being taken care of?

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 2   Vote: I like it +3 Vote: I do not like it

      You will not consider the first 'a' when you'll calculate the number of subsequences of length one. The same with other lengths, two dynamic programming states $$$dp_{i_1, j}$$$ and $$$dp_{i_2, j}$$$ can be non-zero and $$$s_{i_1} = s_{i_2}$$$ but you will not consider the first state in the answer.

      • »
        »
        »
        »
        5 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Oh okay. So correct me if I am wrong, we are making dp such that each subsequence $$$t$$$ that ends with say some character $$$X$$$, will be counted at the maximum position $$$i$$$ such that $$$i \ge len(t)$$$ and $$$s[i]$$$ is $$$X$$$. ( $$$s$$$ is original string ).

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

          Yes, this is true.

          • »
            »
            »
            »
            »
            »
            5 years ago, # ^ |
              Vote: I like it +5 Vote: I do not like it

            Thanks a lot! Finally understood and implemented.

            • »
              »
              »
              »
              »
              »
              »
              5 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Hey, can u please explain the solution in a detailed manner, I am not able to understand why are we doing the steps mentioned. Thanks in advance!

              • »
                »
                »
                »
                »
                »
                »
                »
                5 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Maybe you should try to explain what exactly you did not understand, instead of asking someone to tell you the whole thing, especially when there is editorial where someone has tried to do what you exactly want.

                Now, I will try to explain a simple example to you, to make sense of the dp ( not go through the whole process ). Maybe that will clear everything.

                Consider given string $$$s = aba$$$. I will just tell you how to understand the $$$dp$$$.

                Firstly, let's note ourselves, what are the subsequences. We have

                {$$$aba,ba,aa,ab,a,b$$$}

                Out of these,

                1. $$$a$$$ is counted at index $$$0$$$ and $$$2$$$,
                2. $$$b$$$ is counted at index $$$1$$$,
                3. $$$ab$$$ is counted at index $$$1$$$,
                4. $$$aa$$$ is counted at index $$$2$$$,
                5. $$$ba$$$ is counted at index $$$2$$$,
                6. $$$aba$$$ is counted at index $$$2$$$.

                Since $$$a$$$ is counted twice, I was confused, but finally while calculating total counts, we are only considering $$$last[n-1][ch]$$$ for each character $$$ch$$$ = [ $$$a$$$ to $$$z$$$ ], thus in case of $$$a$$$ only the one at index $$$2$$$ is counted at the end. The one at index $$$0$$$, however, is also important, because only using it you can count the strings $$$aba$$$ or $$$aa$$$.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem B : Equalize Prices

i have just checked if (min+k >= max-k) then print min+k ; else print -1; still wrong answer on test case 3.Can somebody please explain.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Simple solution for H:

Let $$$dp[n][k]$$$ be the number of distinct subsequences of length $$$k$$$ using only the first $$$n$$$ characters of $$$s$$$. Then

dp[n][k] = dp[n-1][k] + dp[n-1][k-1] - dp[l-1][k-1]

where $$$l = lst_{n-1,s[n]}$$$.

Now greedily add substrings to $$$S$$$. Note that two subsequences of different length must be different. (We first use the $$$dp[n][n]$$$ subsequences, then $$$dp[n][n-1]$$$, etc)

»
5 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

I solved problem D with O(N) solution. I like when you guys hide the solution of the problems in the way you did in this tutorial. So when i go to read the solution of the problem A i don't end up reading the solution of problem B and a lot of times i don't want to do it.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

vovuh Why do n/2, n/3, n/5 never divide each other completely? Can this be proved? Thanks :)

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    cause n / 2 has more 3 factors than n / 2 and n / 3 has more 2 factors than n / 2. :)

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem F editorial Let's imagine that the maximum element is divisible by 2, 3 and 5 and there are three following numbers in the array: maximum divided by 2, by 3 and by 5. Then their sum is greater than the maximum (and may be greater than the answer we have!) because 12+13+15>1. can someone give an example of such case... Thanks in advance

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Consider one of test cases in problem sample:

    4
    30 15 10 6
    

    Obviously, $$$n < n/2 + n/3 + n/5$$$ is $$$30 < 15 + 10 + 6$$$ here

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody explain the complexity for Problem F (brute force) solution ,it would be really helpful?

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

For problem F,https://mirror.codeforces.com/contest/1183/submission/56308081
My submission is just a N^2 complexity solution, why can I get it passed?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For all those who can't understand solution for H can try this problem first https://www.spoj.com/problems/DSUBSEQ/

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

What is the time complexity for solution of E?? .... they used set count which is O(n) (even more with strings)...but using map is slower than this !!

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For Question A. Max step should be 7. Number = 997 steps taken will be 7 But the editorial says it's 5.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

After rigorously proving problem G, I found it very cool. Thanks Mike!

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

You can also solve G with a priority_queue of pairs 80920737. In this case the default of priority_queue returning the max element is what you want unlike when you're implementing Dijkstra.