PikMike's blog

By PikMike, history, 4 weeks ago, translation, In English,

1197A - DIY Wooden Ladder

Idea: adedalic

Tutorial
Solution (adedalic)

1197B - Pillars

Idea: BledDest

Tutorial
Solution (BledDest)

1197C - Array Splitting

Idea: Roms

Tutorial
Solution (Roms)

1197D - Yet Another Subarray Problem

Idea: BledDest

Tutorial
Solution (Roms)

1197E - Culture Code

Idea: adedalic

Tutorial
Solution (adedalic)

1197F - Coloring Game

Idea: BledDest

Tutorial
Solution (BledDest)
 
 
 
 
  • Vote: I like it
  • +68
  • Vote: I do not like it

»
4 weeks ago, # |
Rev. 2   Vote: I like it +40 Vote: I do not like it

I challenge you to write D in $$$O(n)$$$

Solution
»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

What's the intuition behind D?

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

    I can explain a different (easier) solution. Let's define $$$dp_i$$$ as answer, such that $$$i$$$ is the last used element. Now $$$dp_i = max(0, a_i - k, max(dp[j] - k + \sum_{j + 1}^{i} a_t)$$$, where $$$i - j \leq m$$$. The intuition is as follows: we need to divide all the segment, into subsegments, each length $$$\leq m$$$.

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

      I used the technique but the it fails on test case 19 here is the link to it.

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

      Can you explain a bit more the meaning of each term in the recursive formula in terms of the decision it represents?

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

        Basically we want to find the best segment, such that after splitting it into $$$cnt$$$ subsegments each length $$$\leq m$$$, $$$\sum_{l}^{r} a_i - cnt \cdot k$$$ is maximum. So when we want to count $$$dp_i$$$, we take all lengths $$$1$$$ to $$$m$$$ and try to make $$$[i - len + 1, i]$$$ the last of subsegments. That's how we update dp. This solution relies on the fact that $$$k \geq 0$$$.

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

      so for this solution, how does it take into account segments with length > m?

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

        Every segment consists of subsegments of length $$$\leq m$$$. For each small subsegment you have to subtract $$$k$$$. So each segment is just continuous merge of small subsegments. That's why we can apply DP. Each segment is either a small segment or a merge of segment and a small segment.

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

      This easier solution is O(m*n) right?

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

      You can check my solution, Only $$$j = i - m$$$ needs to be considered, Otherwise just take max subarray ending at $$$i$$$ with length $$$\leq m$$$.

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

        Yes, your solution is actually the same, I simply don't differ those cases. It still works in $$$O(n\cdot m)$$$.

        • »
          »
          »
          »
          »
          4 weeks ago, # ^ |
          Rev. 2   Vote: I like it +8 Vote: I do not like it

          It can actually be improved to $$$O(n)$$$ easily by also storing the 2 best subarrays of length $$$\leq m$$$ for $$$i - 1$$$ so you don't have to do a for loop.

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

      consider this case: say $$$j = i - 1, dp_j = a_j - k$$$, when you try to update the value for $$$dp_i$$$ using $$$dp_j - k + a_i$$$, $$$k$$$ will be deducted twice when you calculate $$$dp_i$$$. Is that the case or am I misunderstanding something?

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

        Yes, in this case it subtracts twice, but we will find the best answer anyway.

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

          Is it ok for you to explain why this is the case?

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

            I assume that [i, i] is the last small segment. It means that before i there were small segments only of length m. This leads to another subtraction as [i, i] is a different segment.

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

              I mean why we can still find the optimal answer despite that k is subtracted twice

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

                This is the situation, where length is x * m + 1, we have to subtract k x + 1 times.

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

                  I kindda get your idea after re-reading your comments. In your method, although $$$dp[i]$$$ is miscalculated when we try to update it using $$$dp[i-1]$$$, the optimal answer will surface out when we try to update it using $$$dp[i-2]$$$, is that the case?

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

                  Yes, we choose the maximal value of all, so we will get the best answer.

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

                  I think I finally get it. Thanks for answering my questions patiently!

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

I don't understand the solution for C, I guess this solution works because the initial array is sorted? Is there known algorithm that leads to this solution using differences between adjacent elements?

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

    Let a[1], a[2].. a[n] be the elements in the array. Let 1 <= i < j < k < l....< n be the k-1 indices we choose for dividing the array into k segments. So, cost of division = (a[i] — a[1]) + (a[j] — a[i+1]) + .... + (a[k] — a[j+1]) + (a[n] — a[k+1]), on rearranging : (a[n] — a[1]) — ((a[i+1] — a[i]) + (a[j+1] — a[j]) +.....+ (a[k+1] — a[k])). So, we will store the difference of a[i+1]-a[i] for each 1<=i<n , sort them in decreasing order and take first k-1 differences and finally subtract from (a[n]-a[1]).

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

    To start with, we have to break array on k segments. Let's create an array of differences: let d[i] = a[i+1]-a[i]. For a better understanding of the phrase "break array on k segments" let's say that we have to put k-1 partitions between numbers in array. Then, let's look on the answer, when we put each partition in the array: at the beginning the answer is equal to a[n-1]-a[0] = (a[1]-a[0])+(a[2]-a[1])+...+(a[n-1]-a[n-2]) = d[0]+...+d[n-2], after breaking the array into two parts (let's say that we put the partition between a[q+1] and a[q], 0 <= q < n-1) then the answer will be equal to (a[n-1]-a[q])+(a[q-1]-a[0]) = (a[1]-a[0])+...+(a[q-1]-a[q-2])+(a[q+1]-a[q])+...+(a[n-1]-a[n-2]) = d[0]+...+d[q-2]+d[q]+...+d[n-2]. Then, it is easy to notice that with each new partition we can delete 1 element from array d to decrease the answer, so let's delete k-1 biggest elements from d and sum up the rest, this will be the answer to the problem :) PS. Sorry for my bad English :p

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

Can someone explain how to solve E problem. Why do we need a segment tree. And how do we find the value of the minimum for the whole set of the dolls?

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

    If there is no $$$j$$$, that $$$matryoshka_j.in \ge matryoshka_i.out$$$, then $$$d_i = (matryoshka_i.in, 1)$$$

    else

    $$$d_i.first = min[pos,n] - (matryoshka_i.out - matryoshka_i.in)$$$, where $$$pos$$$ is the first $$$j$$$, that $$$matryoshka_j.in \ge matryoshka_i.out$$$ (It means that we can put matryoshka $$$i$$$ inside matryoshka $$$j$$$)

    $$$d_i.second = numberOfMinimums[pos,n]$$$

    And $$$d$$$ — is our segment tree, so we need it to calculate minimum and number of minimums on suffix!

    You can check my solution, I think it's clear enought.

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

    I haven't mastered segment trees yet (did an object-pointer based one for practice but probably need to learn how to do it with bitwise-math and array indices for better efficiency) but I managed to solve this one using only a priority queue; code here if interested:

    https://codeforces.com/contest/1197/submission/57589994

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

What does len mean in the editorial of D? And how does that expression len * m come?

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

    I suppose len is the number of segments that are fully covered. So $$$best_i$$$ is the answer if only segments of length divisible by m are considered.

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

    len means the mutiple of m u r checking backwards from your current position as u need to check only multiples of m for the correct answer from any given position

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

I come with something for problem D but I can't develop it to a solution if anyone solves it by the help of the following please tell me:

we deal with the array as the blocks, we try each possible one let's call it j from 1 to m, and for every j we try to start from 0 to j — 1 and move by j step to partition the array.

it does nothing but I think it could be developed in some way.

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

Very nice explanation of problem E. Thank you so much)

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

why is it besti+sum(i−len,i−1)−k. in the end and not besti + sum(i-len+1,i) — k ?

»
4 weeks ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

I was discussing problem E with my friend SparklingRa1n and after he solved it we read the editorial. And we noticed this part ( It's because not big enough subsets are not optimal in terms of minimality of extra space.)

However, we are not sure if the part that adedalic says is true, please correct us if we're wrong.

Take this example with 2 dolls {4, 1} {6, 5}

The minimum extra space of big enough is 2, but if you take the first doll only, the extra space is 1

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

    Let's extend your example as {2,1} {4,2} {6,5}

    $$$d[3] = (5, 1)$$$ — it should be obvious. When we try to calculate $$$d[2]$$$ we can see, that the second matryoshka can be nested inside the third one — so "we must put it inside". Then $$$d[2] = (5-2=3, 1)$$$.

    The first doll can be nested both in the second and third dolls, but putting it inside the third doll will lead us to not big enough subset. But! it also makes the extra space not optimal since $$$d[2].first < d[3].first$$$. Then $$$d[1] = (3-1=2, 1)$$$ and it's correct.

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

      Ohh, I understand, I was a little confused, but now is totally clear. Thanks.

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

Can someone explain the intution behind D and as to how are we connecting the maximum sum subarray problem to this problem in greater depth. Also I'm not able to understand the intuition behind the idea of introducing len (as done in the editorial) Please help.

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

This is an alternate Solution 57537548 of Problem 1197C - Array Splitting

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

In problem E, Why cann't we add subset {4,7} as one of good subsets in Test Case 1.

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

can someone give me some further explanations about problem c? more specific: why should we "add up the k−1 minimal ones to the answer"?

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

In the solution given for D why do we fix some elements of best_i with best_i + sum(i-len,i-1) — k ?

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

Can someone explain in more details solution of C, with proofs? Thanks

»
4 weeks ago, # |
Rev. 2   Vote: I like it +27 Vote: I do not like it

I don't see why we need a segment tree for E since all queries are suffix queries and all updates are at the position we are currently at. We can just maintain a suffix minimum as we go along.

Solution with editorial's approach minus segment tree: 57644319

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

So, I actually got hacked on problem A because of exceeded time limit. How do you know if your solution needs to be O(n) or O(nlogn) given the time constraint? Would you just assume 2 seconds means my solution needs to be O(n)?

Sorry for too many questions, I'm just new to CP.

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

    The rule of thumb I use is to plug the maximum input size into the big O, and look for something $$$\le 10^8$$$.

    So for $$$n=10^3$$$ for example, $$$O(n^2)$$$ is fine ($$$10^6 < 10^8$$$), but for $$$n=10^5$$$, it'd be too slow ($$$10^{10}>10^8$$$).

    In your solution you used quicksort, which is usually $$$O(n \log n)$$$, fast enough, but you were hacked by an input that causes it to become $$$O(n^2)$$$, which is too slow.

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

Could somebody explain me, if we want to calc T(i, j), we must know colors of r1, r2, r3. But I can't find any mention of it in the tutorial.

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

Logic behind soulution of C?

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

I think problem F can be solved simply by reducing the number of states. The number of states that are possible to reach is actually way lower than 64, it's 25. Then I don't think there's any need for optimization, just brute 25^3*1000*log2(10^9) will work. (Correct me if I'm wrong).

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

In the tutorial for D, last line, you wrote: $$$best_i + sum(i - len, i - 1) - k$$$. But in the code posted below it seems that you are calculating $$$best_i + sum(i + 1, i + len)$$$. I think that is a typo in your tutorial.

»
3 weeks ago, # |
  Vote: I like it -8 Vote: I do not like it

Best solution for B. Your text to link here...

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

Can E be solved by Graph Theory? Just saw the "Shortest Path" tag.

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

    There's an edge from i to j with length $$$in_i-out_j$$$ ,if and only if $$$in_i\ge out_j$$$ holds. Sort the matryoshkas by $$$out_i$$$ ,then we can use segment tree to build the graph. To solve the problem more easily,we use two more nodes,S and T. For all node i with indeg=0,add an edge from S to i with length 0. For all node i with outdeg=0,add an edge from i to T with length $$$in_i$$$ . The answer is the number of shortest paths from S to T. Since the graph is a DAG,the solution above works in $$$O(n\log n)$$$ time.

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

whats is the idea behind F solution? and is there any easier way to solve F? thanks

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

Here is an easier $$$O(n \log m)$$$ solution for D:

Let $$$p$$$ be the prefix sum of $$$a$$$. Then maximum cost of subarray ending at $$$i$$$ is $$$ \max_{j < i} \left\lbrace p_i - p_j - k\left \lceil \frac{i - j}{m} \right\rceil\right\rbrace$$$

Main observation is --

$$$\left\lceil \frac{i - j}{m} \right\rceil = \begin{cases}\left\lfloor \frac{i}{m} \right\rfloor - \left\lfloor \frac{j}{m} \right\rfloor + 1, & \text{if }(j \text{ mod } m) < (i \text{ mod } m) \\ \left\lfloor \frac{i}{m} \right\rfloor - \left\lfloor \frac{j}{m} \right\rfloor, & \text{if } (j \text{ mod } m) \geq (i \text{ mod } m) \end{cases}$$$

So, our formula becomes --

$$$\begin{cases}p_i - k\left\lfloor \frac{i}{m} \right\rfloor + \left(k\left\lfloor \frac{j}{m} \right\rfloor - p_j\right) - k, & \text{if }(j \text{ mod } m) < (i \text{ mod } m) \\ p_i - k\left\lfloor \frac{i}{m} \right\rfloor + \left(k\left\lfloor \frac{j}{m} \right\rfloor - p_j\right), & \text{if }(j \text{ mod } m) \geq (i \text{ mod } m) \end{cases}$$$

To evaluate the formula quickly, we can keep the maximum of $$$\left\lbrace k\left\lfloor \frac{i}{m} \right\rfloor - p_i\right\rbrace$$$ at index $$$(i\text{ mod } m)$$$ of a segment tree. Which enables us to get the prefix/suffix maximum in $$$O(\log m)$$$ resulting in total complexity $$$O(n \log m)$$$.

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

the second example of B: 3 1 2. why it is NO?we can move it like this.

-------------------------1------------------1---------------1-------------------------------2
3-1-2 ——> 3-null-2 ——> null-3-2 ——> null-3-2 ——> 1-3-2 ——> 1-3-null ——> finished and yes

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

    Please notice the second condition among the three in the problem statement:

    2.pillar i contains exactly one disk

    According to this, you won't be able to perform your third (also fourth) move.