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

Автор awoo, история, 5 лет назад, По-русски

1082A - Вася и книга

Разбор
Решение (Ajosteen)

1082B - Вова и кубки

Разбор
Решение (Ajosteen)

1082C - Мультипредметная олимпиада

Разбор
Решение (adedalic)

1082D - Граф максимального диаметра

Разбор
Решение (PikMike)

1082E - Увеличение частоты

Разбор
Решение (adedalic)

1082F - Быстрый набор

Разбор
Решение (PikMike)

1082G - Петя и граф

Разбор
Решение (Ajosteen)
  • Проголосовать: нравится
  • +68
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится -17 Проголосовать: не нравится

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.com/blog/entry/60059 for more

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

How to solve E

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

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...

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

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.

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

    +1. having the same issue

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

    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.

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

      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.

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

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. :)

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

My problem E solution is super simple.

It might me helpful for you.

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

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

    Can you explain it?

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

      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.

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

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

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

        Neighter do I. And it's really simple.

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

        Especially this part. Can elaborate more?

        Because count(i, i, vec[i]) is always 1.
        
        • »
          »
          »
          »
          »
          5 лет назад, # ^ |
          Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

          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 года назад, # ^ |
      Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

      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.

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

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

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    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.

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

Update :- Got it :)

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

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

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

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

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

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

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

    I like this one. GJ

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

    You saved my day

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

    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....

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

      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).

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

    What a wonderful solution!!!!

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

    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?

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

      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].

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

i used binary search to solve B

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

i used binary search for problem B

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

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

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

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

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

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

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.

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

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

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

.

»
4 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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