ch_egor's blog

By ch_egor, 5 years ago, translation, In English

Thanks for the participation!

1313A - Fast Food Restaurant was authored by Endagorion and prepared by ch_egor

1313B - Different Rules was authored by meshanya and prepared by DebNatkh

1313C2 - Skyscrapers (hard version) was authored by meshanya and prepared by Sehnsucht

1313D - Happy New Year was authored and prepared by voidmax

1313E - Concatenation with intersection was authored and prepared by isaf27

Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
  • Vote: I like it
  • +80
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +92 Vote: I do not like it

How Can I read Such a big editorial for B. havoc editorial to read...

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

In the B problem, I think we just need to compare a + a with b + c

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

Solution for B looks like some neural network ;)

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

    can you explain how to do the problem i am not able to understand the tutorial

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

Contest has become mathforces with B

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

All comments are related to B :D

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

Is there any easier understanding of problem B?

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +24 Vote: I do not like it
    • For the minimum possible overall place:

    For a number $$$a$$$ in round 1 and another number $$$b$$$ in round 2, we should make sure $$$a + b \geq x + y + 1$$$. $$$1$$$ should find $$$x + y$$$, $$$2$$$ should find $$$x + y - 1$$$... And if $$$x + y > n$$$, $$$1$$$ should find the smallest number in round 2 which can be selected.

    So, we can see that the final answer will be $$$x + y - n + 1$$$, cause we can not arrange number $$$[1, x + y - n]$$$ to make them greater than $$$x + y$$$, and don't forget the ans must in $$$[1,n]$$$.

    • For the maximum possible overall place:

    In round 1, for each number $$$a$$$ between $$$1$$$ and $$$x-1$$$(it can be empty), we can find a number $$$b$$$ in round 2, and make $$$a + b \leq x + y$$$.

    In round 2, for each number $$$b$$$ between $$$1$$$ and $$$y-1$$$(it can be empty), we can find a number $$$a$$$ in round 1, and make $$$a + b \leq x + y$$$.

    We cannot find any other pairs to make $$$a + b \leq x + y$$$. Thus, the answer is $$$x - 1 + y - 1 + 1$$$, and don't forget the ans must in $$$[1,n]$$$.

    Let $$$a = x - 1, b = y + 1$$$, while $$$a$$$ is decreased by $$$1$$$, we can add $$$1$$$ for $$$b$$$. If $$$b$$$ is greater than $$$n$$$, we can select any number remaining, cause at this time, $$$a + b < x + y$$$. To proof the second one, we can just swap the round 1 and round 2.

    It seems like it's a kind of greedy, but the editorial can proof the answer strictly. And also sorry for my poor English. Wish it can help you. :D

    UPD: swap((a,b),(x,y)), sorry for the mistake.

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

      what is a & b ?

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

      thanks

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

      thx, it helped alot

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

      For the first case (minimum possible overall place) did you find a+b>=x+y+1 because it will give the count of all the combinations of final ranks which are more than x+y sum's rank? Also, I don't understand what you are saying in the next line ( the x+y>n condition)

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

        Of course, we can't make sure we'll always find such $$$a$$$ and $$$b$$$ that $$$a + b \geq x + y + 1$$$. And at that time, the answer will be increased by 1, because whichever we select the $$$b$$$, we can't make $$$a + b >= x + y + 1$$$, in other world, the person who get the $$$a$$$ place in round 1 will finally placed before Nikolay.

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

    Not sure if it is easier, but I solved it with the following intuition: let's rephrase this task in a different way: you have a square grid of size n. Each participant with scores (xi, yi) will mark one cell with same coordinates. There should be only 1 marked cell on each vertical and horizontal line. Total score of a participant is xi + yi and participants with a same score share same diagonal (from top-right to bottom-down), participants with lower score are on diagonals on the left, and participants with higher score are on diagonals on the right.

    Now the task is the following — having Nikolay's mark on (x, y) how can we put other marks in order to have max or min number of marks on a right side from Nikolay's diagonal. You can check three cases depending on a position of Nikolay's diagonal from main diagonal x + y ? n + 1, where ? on of <,=,>

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

    Fire woman is beautiful!~

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

can anyone write the bruteforce solution for problem A.

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

    You can use Greedy method

        /// Sort a > b > c
        if (a < b) swap(a, b);
        if (a < c) swap(a, c);
        if (b < c) swap(b, c);
     
        int res = 0;
    
        /// Take 1 disk
        if (c) res++, c--;
        if (b) res++, b--;
        if (a) res++, a--;
        /// Take 2 disks
        if (a && b) res++, a--, b--;
        if (a && c) res++, a--, c--;
        if (b && c) res++, b--, c--;
        /// Take 3 disks
        if (a && b && c) res++, a--, b--, c--;
    
        cout << res << endl;
    
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Notice that you should take (a-b) and (a-c) before take (b-c) else you will get WA

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

B looks so hard in the editorial:)

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

I would like to understand D. Why we use bit mask of size k?

What does it mean if "we can find free bit and create match to this segment" to create the dp?

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

    Let’s imagine this situation. You gave every segment some number 1..k, such there are no intersecting segments with the same number. In this case you can easy maintain dp.

    When we try to add new segment, we take some remaining color to match it to this segment (you have mask of free colors). So we came back to previous situation.

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

      Why no need to consider the combination of segments with same number after numbered them according to your method? According to 71735081 from gigabuffoon, numbers for each segment are determined before doing dp.

      When leaving color 3 available and color 4 taken, why can't another later segment with number 4 take the available color 3?

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

    Suppose we sort and process a start/end event queue over the segments. At each start event, we will assign that segment the smallest integer not in use by another active segment. Since there is no index with more than $$$K$$$ segments covering it, during this process we will assign segments at most $$$K$$$ unique values.

    These values correspond to an index of a bit in a bitmask; this bit is on if we currently have this segment selected. Because each index is covered by at most $$$K$$$ segments, a bitmask $$$[0, 2^K)$$$ can uniquely identify any subset of segments which are crossing some index.

    Then, you can write a take/leave DP on the segments, and score ranges you cover based on the parity of your bitmask. code

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

      thanks a lot

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

      Thanks for the hint and code.

      A side question: no need to worry about stack overflow for recursive dp implementation since N <= 1e5?

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

      What happens when a certain bit / color is on and you arrive at a the start of a segment of the same color? Wouldn't it mean that two segments that the parity flips because you now have two segments that don't intersect? I see that you are adding them instead of XOR

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

        Ah, it seems that this cannot happen because if they are of the same color, then they don't intersect, meaning eventA.end < eventB.start. Then therefore, the color / bit would have been switched off before arriving at the start of the new segment.

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

Can someone please help me find error in my logic with question B.

my submission

For minimum case, I started making pairs such that their score is one greater than our score. Then after that, some scores in first contest remain which are greater than a, and some in second contest which are greater than b. we take min of these to give additional people with score greater than us.

sg1 = min(n-1-x, y-1)

sg2 = min(n-y-1, x-1)

og = min(n-x-sg1, n-y-sg2)

mn = n - sg1 -sg2 - og

Similarly I do for max, but for this case, we search for equal score. After that we search for scores less than a in first contest, and scores less than b in second contest. We add min of these to give additional people that may come at or before us in position.

gr1 = min(n-x, y-1)

gr2 = min(n-y, x-1)

ol = min(x-1-gr2, y-1-gr1)

mx = gr1+gr2+ol+1

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

    The answer should in range [1,n].

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

      my god I feel silly now. I only had to change sg1 and sg2 so that they dont go negative! Thanks mate. The problem was that when a, b were last then sg1 and sg2 should have been zero, but I overlooked that issue!

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

Problem B



For worst rank:
Round 1 ......x........
Round 2 ..........y....
The total score needs to be made as greater as possible.
So,
Step 1 — We can couple entries like (x+1,y-1), (x+2,y-2)..... and (x-1,y+1), (x-2,y+2).....
Step 2 — After this step we will be left with some left out numbers is the first few ranks of few rounds, so we'll couple them together. The reason we are going only for the starting ranks is that coupling of these elements will result in score less than (x+y).

For example:
Round 1: 1 2 3 4 5(x) 6 7 8 9 10
Round 2: 1 2 3 4 5 6 7 8(y) 9 10
In step 1, we'll couple (6,7),(7,6),(8,5),(9,4),(10,3) and (4,9), (3,10)
After step 1, we'll look for the left out ranks among the starting ranks of rounds 1 and 2 --> 1,2 (round 1) and 1,2 (round 2). So, we'll couple them.
Answer = 1(including itself) + 5(same score part 1) + 2(same score part 2) + 2(score less than x+y)


For best rank:
Similar to the worst rank case. Here we will try to make score greater than (x+y). Sum = (x+y+1) will yield the most number of possible results, so we'll aim for sum to be x+y+1 (similar to what we did for sum=x+y in the previous case).
Edge case: min(x-1,n-x-1) may produce negative numbers, so we'll need to take max with 0.
This will give us the maximum possible number of scores that are greater than (x+y). So we'll subtract this from N.

Code for reference:

// worst rank
part1 = min(x-1,n-y)
part2 = min(y-1,n-x)
w = part1 + part2 + min(x-1-part1,y-1-part2)

worst = 1 + w

// best rank
part1=min(y-1,n-x-1)
part2=min(x-1,n-y-1)
part1=max(part1,0)
part2=max(part2,0)

best = N — (part1 + part2 + min(n-x-part1,n-y-part2)

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

    I did exactly same as this, but stupidly I didnt do the part1 = max(part1, 0) for best rank. Bye bye expert, on the bright side I can give div 3 ;-)

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

Problem B. I forgot about the borders and place max(1,min(n,x+y-n+1)) wrote max(1,x+y-n+1). and finally I didn't solve the problem during the time of the Contest

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

Why cant I view other people's code ? :/

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

What is the approach of finding "nearest" numbers to the right and to the left that are smaller than the current one?

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

How to search the "nearest" in c2 (second solution)?

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

Problem C. in editorial(second solution). please help. can you this code O(n*n) optimize to O(n log n) or O(n). I could not find j faster.

for (int i = 1; i <= n; i++) {
  int j = 0;
  for (int k = 1; k < i; k++) {
    if (m[k] <= m[i]) {
      j = k;
    }
  }
  l[i] = l[j] + (i - j) * m[i];
}
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Use a strictly ascending stack (one for L and one for R) — O(n) as every element will only be popped once.

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

What if in C2 we have more than one min element??

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

    It does not matter, in case it has more than 1 min elements, choose the first one and divide the array into two parts — one that is left to it and one that is right to it. Calculate ans recursively for them

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

Please explain solution of problem 'D' in detail

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

    Well detailed and explained solution of problem 'D' Dp states explained

    71761226 Hope it helps :)

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

      good job bro

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

      How can we memoize using dp[id][mask] ? The same mask can denote a different set of segments right ?

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

        A mask can represent different set of segments but a mask at a particular 'id' represents a unique set of segments.(This is due to the fact that atmost only 8 segments can be active)

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

          Is the fact that only 8 segments can be active the only reason why for an id, a mask corresponds to a unique segment set ? I didn't understand how the code is ensuring that a mask for an id is a unique set of segments.

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

      for a segment, why is l, r+1 added and not l, r ?

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

      Woah

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

I am getting Runtime error "Exit code is -1073741571" on problem C2 with C++14

71730114

while the same code is ACCEPTED with C++17

71727975

What could be causing the difference?

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

In the recursive approach for C2 how to make choice after finding minimum i.e, if we have to recurse on the right or left part?

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

    You have to recurse on both the parts and calculate their respective scores. Depending upon which side has better score, you need to set the opposite side to the min value.

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

      thanks, but what if there are multiple minima ? do we have to consider any one of those or

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

        Yes just consider the first such minima (from left) and break the array at the point.

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

          thanks! so for finding minima(RMQ), we have to use segment tree/ sqrt decomposition. What is the O(N) approach?

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

            Sparse table.

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

              sparse table supports query operation in O(1) time but O(N Log N) preprocessing time.

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

                And segment tree preprocessing is O(N logN)? But with sparse table you can answer RMG query in O(1).

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

                  Segment Tree construction is O(N), because there are ~2*N nodes in the tree and each node needs constant time.Query/Update takes O(Log N) time

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

                  And solution with segment tree is O(N logN) and with sparse table is O(N).

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

                  We look to main solution complexity in this we always say that preprocessing take O(NlogN), and solution take O(N).

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

                  Hi in (C1)easy version problem i have found the minimum element and then found the sum of elements on the left side of minimum and similarly on the right hand side . If the sum on the right side is less than that of left side then then i have assigned minimum value to all elements to the right side and vice versa. can u pls tell why this approach is not working

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

Why second solution work(for problem C2), can someone explain?

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

Thanks for the tutorial <3

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

what stand between me and the rating add,is the math

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

I wrote a segment tree & divide and conquer solution for C2, based on the first idea in this tutorial of problem C2. But I get TLE here: https://codeforces.me/contest/1313/submission/71738916. I tried many times at my local machine, using a variant to count the time complex and found the divide and conquer procedure is O(N), and my segment tree is O(17) = O(logN).

So I used ST table to reduce complex from O(N logN) to O(N) here: https://codeforces.me/contest/1313/submission/71741474, get Accepted.

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

.

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

Can anyone just explain D more clearly ? I'm too stupid to understand the given editorial.

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

How to solve the problem C2 recursively in first solution,isn't it got a comlexity 2^n?

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

    as every number can behave as minima of a region exactly once so its complaxity not exceed 2*n in iteration

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

Help needed in problem c2.

My approach: for selecting the pick index I calculated the following for each index.

sum=dpl[i]+dpr[i]-a[i] here dpl[i] is sum from 1..i making non decreasing segment and dpr[i] is same from n..i

for which index I get the maximum sum I selected that index to be the peak index. And then just calculated the final result.

Can someone please tell me why this is wrong or any test case? my code: 71701033

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

In (B) some proofs may become much more symmetrical if you notice that you can make the substitutions

  • $$$x\leq y$$$
  • Change $$$x,y$$$ so that (case 1) $$$x=y$$$, or (case 2) $$$x+1=y$$$.

Also an observation is that you don't have to solve a completely new problem for the minimum after solving the maximum problem; specifically if $$$x+y = c$$$, then the worst possible for $$$x,y$$$ corresponds to the best possible for some imaginary opponent with $$$x'+y' = c+1$$$; one just needs to subtract the number of "ties" in the $$$x',y'$$$ case, and add in a $$$+1$$$ because $$$x,y$$$ beats itself.

(https://codeforces.me/contest/1313/submission/71990013)

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

In C2 editorial second soln, what's an optimal way to find "nearest" numbers to the right and to the left that are smaller than the current one.

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

Can anyone help me with problem Skyscrapers (Hard Version), I am getting memory limit exceeded for my solution I used Sparse Table for finding range minimum query and did a recursive method My submission

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

I struggled a lot with problem C / C2 (skyscrapers). Some comments said to use a monotone stack, which was a big hint, however I have only done a few problems with this approach and I didn't know what the stack actually represents. Finally I came up with it:

For now we'll focus our attention on nondecreasing skylines. For every index $$$i$$$, we will identify the optimal skyline from index $$$1$$$ to $$$i$$$ inclusive. For $$$i=1$$$, clearly we use $$$a_1=m_1$$$. More generally, for $$$i=1$$$ to $$$n$$$, we push $$$i$$$ onto the stack $$$s$$$ after popping all elements $$$j$$$ such that $$$m[j]>m[i]$$$. Finally we push $$$i$$$. The stack right now describes the optimal skyline ending at $$$i$$$; Specifically, if $$$s$$$ contains $$$i=i_k > i_{k-1} > \cdots > i_1$$$, then we make assignments as follows: indices $$$(i_{k-1},i_k]$$$ get height $$$m[i_k]$$$, indices $$$(i_{k-2},i_{k-1}]$$$ get height $$$m[i_{k-1}]$$$, $$$\ldots$$$, and indices $$$(0,i_1]$$$ get height $$$m[i_1]$$$. It is a little exercise to see that this is a valid assignment and optimal.

Let's call $$$maxIncreasingSkyline[i]$$$ the sum of the heights of this optimal skyline. It can be computed in amortized $$$O(1)$$$ at each step while doing the popping/pushing.

One can do the same procedure for nonincreasing skylines, by iterating from $$$n$$$ to $$$1$$$, and similarly obtain $$$maxDecreasingSkyline[i]$$$, which represents the optimal skyline that "begins" at index $$$n$$$ and ends at $$$i$$$.

Now the actual optimal skyline will be one of 3 possibilities: a nondecreasing skyline (whose value is $$$maxIncreasingSkyline[n]$$$), a nonincreasing skyline (whose value is $$$maxDecreasingSkyline[1]$$$), or a combination of the two, in other words, there is some $$$j$$$ such that $$$maxIncreasingSkyline[j]+maxDecreasingSkyline[j+1]$$$ is maximal. The latter can be computed in $$$O(n)$$$ time since we have already prepared the two arrays. Then one does another linear scan to reconstruct the assignments $$$a[\cdot]$$$ from the stack.

My submission is here but hopefully the explanation above is adequate.

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

Hardest Div2B ever. Were past Div2B's always this hard? :(

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

In problem C2, what is the neatest way to implement the recursive approach? Recursive approach is prone to exceed the default stack size limit.

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

Are there any restrictions for i = 1 and i = n? see 79077157 for more details. Please someone tell me where my code is not correct. Thanks in advance.

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

Solution for C2 using the divide and conquer approach: 85811938

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

EDITORIAL OF C2

OBJECTIVE: Find the Optimal Peak by both Methods:

Method 1: Create a Segment Tree which returns the index of the smallest element in [l,r].

Now , we search for the smallest element in [l,r]. Suppose it is at minindex. Then we make 2 recusrive calls,

-> First we assume that all a[l....minindex] have been changed to a[minindex] , so we have a value of a[minindex]*(minindex-l+1) . Then we make the recursive call on [minindex+1,r].

-> Second is when we change all a[minindex+1....r] to a[minindex]. Then we have a value of a[minindex]*(r-minindex +1 ). Then we make a Recursive call on [l,minindex-1].

In our base case, (l==r) , we have the value i.e. final sum of the array if l(or r) were the Peak. We simply find the Optimal Peak, i.e. the one with max. value. (Have a look at the code, I have made 2 special checks in case [l>r].See just below the recursive calls)

Once we have the optimal Peak, we can easily calculate the Array.

Method 1 Code

Method 2: This Method is nicely explained in the editorial. I referred to this to make 2 arrays pre and suf to find the smallest element on the left and right of a[i] with overall O(n).

Then I calculate value for each a[i] in left[] and right[] as per described Method in Editorial and found out the Optimal Peak. Then answer can again be calculated easily.

Method 2 Code

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

Can anyone help me in C1 . I m getting wrong answer at test case 9 . I m using divide and conquer technique .Provide me some test cases or check my soln . 1313C1 - Skyscrapers (easy version)

include<bits/stdc++.h>

using namespace std;

define ll long long

define pb push_back

int main() { ll n,i,j,k,l; cin>>n; ll a[n+1]; for(i=1;i<=n;i++){ cin>>a[i]; }

i=1;j=n; while(i<=j){

ll mi=1000000003;
  ll pos=-1;
  for(l=i;l<=j;l++){
      if(a[l]<mi){
          mi=a[l];
          pos=l;
      }
  }
  if(pos==i){
      i++;
      continue;
  }else if(pos==j){
      j--;
      continue;
  }else{
      ll a1=0,a2=0; 
      for(k=pos+1;k<=j;k++){
        if(a[k]>a[pos]){
            a2+=a[k]-a[pos];
        }
      }

      for(k=pos-1;k>=i;k--){
         if(a[k]>a[pos]){
            a1+=a[k]-a[pos];
         } 
      }

      if(a1<=a2){
         for(k=pos-1;k>=i;k--){
                a[k]=a[pos];
          }
          i=pos+1;
      }else if(a2<a1){
          for(k=pos+1;k<=j;k++){
              a[k]=a[pos];
          }
          j=pos-1;
      }
  }

}

for(i=1;i<=n;i++) cout<<a[i]<<" ";

}

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

Can anyone tell me the problem in my code for problem C2. I used Seg tree here.