awoo's blog

By awoo, history, 6 years ago, translation, In English

1082A - Vasya and Book

Tutorial
Solution (Ajosteen)

1082B - Vova and Trophies

Tutorial
Solution (Ajosteen)

1082C - Multi-Subject Competition

Tutorial
Solution (adedalic)

1082D - Maximum Diameter Graph

Tutorial
Solution (PikMike)

1082E - Increasing Frequency

Tutorial
Solution (adedalic)

1082F - Speed Dial

Tutorial
Solution (PikMike)

1082G - Petya and Graph

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

| Write comment?
»
6 years ago, # |
  Vote: I like it -17 Vote: I do not like it

python 6-lines AC solution to problem B

_, string = raw_input(), raw_input()
start, end, res = 0, 0, 0
for char in string:
    if char == "G": start += 1
    else: end, start = start, 0
    res = max(res, start + end + 1)
print min(res, string.count("G"))

https://codeforces.me/blog/entry/60059 for more

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

How to solve E

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

For problem E, I came up with an elegant (at least I thought during contest) O(nlogn) divide-and-conquer solution. I am just surprised to see the so easy linear solution after contest...

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

In problem D, why do we have to loop from the end of the array at the end of algorithm?

I tried forward loop but gives WA on test 18... it seems that it doesn't construct tree well.

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

    +1. having the same issue

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

    So I just thought about it. The reason why the for loop needs to be done in reverse is because there is a chance that the very last node in the bamboo tree needs a "one" node to satisfy the diameter requirement that was previously calculated.

    Consider this test case

    5
    1 3 2 2 1
    

    If you have both the bamboo construction for loop and the "ones add-on" for loop both go forwards, we'll add both one nodes to vertex 2 and never get the chance to append a one node to vertex 4 -- this would be required to make the diameter of the constructed graph 4, which would be the max.

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

      Now I got it, for the construction of the bamboo we go forward and but then we must go backward to use all the remaining edges.

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

Hello friends,

Here is my solution to problem E. The pedagogy is quite different from the one in the editorial and it might be helpful. :)

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

My problem E solution is super simple.

It might me helpful for you.

https://codeforces.me/contest/1082/submission/46421135

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

    Can you explain it?

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

      In one word, It is like a parallel two pointer for every number.

      For every 'i', you need to change left of some numbers that count(left, i, X) < count(left, i, C), to 'i'. But you don't need to immediately calculate it for every number. Because count(i, i, vec[i]) is always 1.

      Sorry for my bad English.

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

        No issues with your english but I couldn't get you

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

        Neighter do I. And it's really simple.

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

        Especially this part. Can elaborate more?

        Because count(i, i, vec[i]) is always 1.
        
        • »
          »
          »
          »
          »
          6 years ago, # ^ |
          Rev. 5   Vote: I like it 0 Vote: I do not like it

          If count(left, i, x) < count(left, i, c), we should change left to i. Then, its count is maybe 0. But We know answer always exist on some events increasing count about x. So, we don't have to change lefts immediatly. Therefore, If count(left, i, vec[i]) > count(left, i, c), we use it as it is. But else, we use it be changed about left. We can easily know we have to change left to i, and count(left, i, vec[i]) will be 1.

          Don't forget all x have different left.

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

      I will explain how I understood his code.
      Let's define an array $$$num$$$, where $$$num$$$ $$$a_i$$$ is maximum length of a subsequence like this:
      $$$c, c, \dots, c, a_i, \dots, a_i, a_i.$$$ This is the subsequence of first i elements of array $$$a$$$.
      So, how to calculate this array $$$num$$$?
      $$$if$$$ $$$a_i=c$$$ we do nothing
      $$$else$$$ $$$num$$$ $$$a_i$$$ $$$ = $$$ $$$max($$$ $$$pref$$$ $$$i$$$ $$$,$$$ $$$num$$$ $$$a_i$$$ $$$)+1$$$ This way we are either $$$1)$$$ starting a new sequence by appending $$$a_i$$$ to the sequence of $$$c$$$. or $$$2)$$$ extending $$$c, c, \dots, c, a_i, \dots, a_i, a_i.$$$ by appendind $$$a_i$$$ to the end.
      How does it help?
      Let $$$pref$$$ $$$i$$$ be number of $$$c$$$ in the prefix of array $$$a$$$. Say, we are at an index $$$i$$$. When we subtract $$$pref$$$ $$$i$$$ from $$$num$$$ $$$a_i$$$ , we get $$$cnt(l,i,a_i) - cnt(l,i,c)$$$ where $$$l$$$ is the index of first $$$a_i$$$ in the above sequence. Now, we iterate through all $$$i$$$ and find the maximum of $$$cnt(l,i,a_i) - cnt(l,i,c)$$$.
      Finally we add this value to $$$pref$$$ $$$n$$$ which is our answer.
      Note: $$$cnt$$$ is the same as defined in the editorial.

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

Could someone explain why we need this line in problem B, res = min(res, cntG); Thanks.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    int nres = 1;
    

    we considered 'S' as 'G' to combine 'G' in the two sides, trying to get the longest subsegment.

    For examble:

    input

    4 SGGG

    Before res = min(res, cntG);, res = 4. It's an wrong answer.

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

Update :- Got it :)

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

In problem D, I was wrong in test 18. The vecdict is "Given diameter is incorrect 388 389", but in my output the diameter is 389? Anyone tell me why? Thanks so muck

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

    The checker checks the diameter of your graph using your adjacency list, not just the diameter your output after "YES".

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

Here is my idea of E:

We have a naive solution: consider each segment [l, r], get the maximum appearance of a number in the segment, assume it is x, then update result with cnt(1, l - 1, c) + x + cnt(r + 1, n, c), where cnt(l, r, j) is number of apperance of j in segment [l, r].

In the solution above, for each segment [l, r], we choose the number with highest appearance and then change to c. But in fact, we can greedy change all the elements equal ar in segment [l, r]. Proof: If we choose the number with highest appearance, assume it is y, and y ≠ ar, then we can get better result if we choose segment [l, r'], where r' < r, cnt(r' + 1, r, y) = 0, y = ar', because cnt(r' + 1, n, c) ≥ cnt(r + 1, n, c) and cnt(l, r', y) = cnt(l, r, y), so cnt(1, l - 1, c) + cnt(l, r, y) + cnt(r + 1, n, c) ≤ cnt(1, l - 1, c) + cnt(l, r', y) + cnt(r + 1, n, c).

So at the position i, let fai be the most number of c in segment [1, i] if you change all the elements equal ai to c in some segment [l, i]. Then: fai = max(fai + 1, cnt(1, i - 1, c) + 1). We update result with fai + cnt(i + 1, n, c).

My submission: 46365450

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

    I like this one. GJ

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

    You saved my day

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

    I understand that all a[i] in (l,r) can change to a[r] but : why calculate cnt( 1,i ,d ) only not cnt(1,l-1,c) and cnt(l,r,d)

    sorry for my poor English....

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

      At position i, fai is the maximum c we can get if we change all number equal ai in some segment [l,  i] to c. That means there is a segment [l,  i] which the sum cnt(1,  l  -  1,  c)  +  cnt(l,  i,  ai) is maximum and we suppose this sum equal fai. So we update result with cnt(1,  l  -  1,  c)  +  cnt(l,  i,  ai)  +  cnt(i  +  1,  n,  c)  =  fai  +  cnt(i  +  1,  n,  c).

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

    What a wonderful solution!!!!

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

    Why you can prove that it's optimal to change the elements equal ar in [l, r]? The proof seems to have only proved that changing ar' is better than changing ar. Could you please explain it in more detail?

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

      I mean that changing all numbers equal ar' = y to c in segment [l, r'] is better than changing all number equals y (which have the most apperance) in segment [l, r].

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

i used binary search for problem B

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

Can anybody tell me why my code for problem C is giving WA?

The link to my code : https://codeforces.me/contest/1082/submission/46672149

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

This editorial isn't linked from problems but only from the announcement so it's a bit harder to find.

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

    Oops, sorry, now it's linked.

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

In explanation to Problem G:

"if a project-vertex belongs to S, then we take this project; if an instrument-vertex belongs to S, then we take this instrument; all other projects and instruments are discarded..If an edge between some project and some instrument is cut, then it means that the answer is incorrect (we try to take a project requiring some instrument we don't take), and the cut value is infinite. Otherwise, the value of the cut is equal to the total cost of taken instruments and discarded projects, and we need to minimize it."

  1. I didn't get what this means. Once we determine Minimum cut from the graph, it says discard all projects and instruments that are not part of the cut. But then it also states value of the cut is equal to total cost of instruments and discarded projects.
  2. Also, how can the cut value be infinite if all edges from source to projects have finite capacity and all edges from instruments to sink have finite capacity?

Can someone please explained with an example or in a simpler way. Thanks.

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

I am currently failing test 11 for prob D. This is my solution https://codeforces.me/contest/1082/submission/80575032. Can anyone please tell me what is wrong with it?

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

.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I know this post is really really old, but I wanted to share my solve for E. Altho it's far less elegant, it AC's and I don't think anyone else has mentioned it.

Basically, we'll let dp[i] be max # of c's we can make if we let the chosen segment end at position i. Naturally, dp[i] will also consider the prefix of the array before the segment as well. Once we've computed dp[i], we can simply brute force the suffix and combine it with the appropriate dp value to pick the answer which gives us the most occurrences of c. Let's also precompute ps[i] = (#of occurrences of c before and including position i).

Now I'll go over how to compute dp[i]. Here is an observation. Let's say we're at the ith character and trying to compute dp[i]. Note that the character we should choose to convert in our segment ending at position i is a[i]. This is because if we didn't choose a[i], then we wouldn't even need to extend our segment to position i in the first place. So it makes sense for the segment to convert all the instances of the value of a[i] inside it to c. Also note that it's always better for the segment to start at an instance of a[i]. If it didn't start at an instance of a[i], then we could keep moving it to the right until we hit an instance of a[i] while possibly improving our answer even further. So note that we only need to consider the past instances of a[i] when considering the transitions for dp[i]. Let's create a map M such that M[k] will store the maximal value of the following expression: (ps[j-1] + #of instances of k at and after position j), where j is some instance of a[i] to the left. Note that as we iterate i from 1 to n, if we don't see any previous instances of a[i], then dp[i] = 1 + ps[i-1], where the 1 is from a segment starting and ending at position i that converts a[i] to c. We can then update M[a[i]] = 1 + ps[i-1]. Otherwise if a past instance of a[i] has occurred, then we can first increment M[a[i]] by 1. This is b/c we have reached a new instance of a[i] at our current position, so by starting a segment at the optimal position j up to position i, our best answer will be M[a[i]] plus the 1 from converting the value of a[i] at position i to a c. Finally, we can update M[a[i]] by picking the max of its existing value and 1 + ps[i-1]. Set dp[i] equal to this new quantity. Keep doing this process to successfully compute the dp array.

238795645