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

Автор Monogon, история, 4 года назад, По-английски

I hope you enjoyed the problems! Implementations will be added soon.

UPD: Implementations are added!

1450A - Avoid Trygub

Tutorial

Implementation

1450B - Balls of Steel

Tutorial

Implementation

1450C1 - Errich-Tac-Toe (Easy Version)

Tutorial

Implementation

1450C2 - Errich-Tac-Toe (Hard Version)

Tutorial

Implementation

1450D - Rating Compression

Tutorial

Implementation

1450E - Capitalism

Tutorial

Implementation

1450F - The Struggling Contestant

Tutorial

Implementation

1450G - Communism

Tutorial

Implementation

1450H1 - Multithreading (Easy Version)

Tutorial

Implementation

1450H2 - Multithreading (Hard Version)

Tutorial

Implementation

Разбор задач Codeforces Global Round 12
  • Проголосовать: нравится
  • +382
  • Проголосовать: не нравится

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

Thanks for superfast editorial

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

Too difficult contest :(

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

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

    I always fail B. Did C2 but failed B yet again. The cost of getting C2 instead of D was also not worth it.

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

I didn't like C1, C2

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

    I didn't either during the contest but after reading the editorial I loved them. Although I think it's better to be replaced with d.

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

    It took me about 1 hour to solve C1. Right after I solved it and it passed pretests I was quite content but then I came to conclusion that my solution was wrong. And right now it turns out it was correct after all. C1 is quite a confusing task

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

This contest was not for below pupil I guess :). The questions were tough(for me at least), but the questions were good and it was a good learning experience. Thanks for the superfast editorial

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

Problem C is quite similar to this problem: 1438C - Engineer Artem

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

can you add who were the authors of each problem as well?

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

C is hard this time lol

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

    It is a beautiful problem. You can just try to brute force one of these 3 patterns over the grid, and one of them will always satisfy the condition.

    1001	0100	0010
    0010	1001	0100
    0100	0010	1001
    1001	0100	0010
    

    The implementation is also easy.

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

      Same idea. but i was really not sure this conclusion is right, and even pretty sure it will fail system test, but it really work, what a surprise:)

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

      Yes, that’s what I did to C1, and it wasn’t an easy observation to see at first. For example, the first time I submitted, I only used one of the three pattern, but of course it fails lol.

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

Overkilled B and wasted 1 hour

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

I was solving a complete different problem for B until the last 20 minutes, where I sadly ignored the attract all points statement. Lets say it is possible to "choose" which points to attract to itself for a point and not all. The problem being similar to the making all elements equal in a 1D array, but here in a 2d plane, with the k parameter. How would we solve such a problem?

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

Accidentally solved C, but the proof is really hard to think out

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

    I was able to come up with the strategy to solve C1 but with only few minutes left on the clock. Gave all my time to D.

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

      Have the idea but don't have time is really sad

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

        Even worse is if you start implementing solution like 45 minutes before end and still can't make it on time

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

C1 is like asking for some formular. You saw that one before and can remember, or not.

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

    No, it isn't. I haven't seen C1 before and there are a lot them.
    This is actually puzzle, sometimes you would be able to solve, sometimes you wont. I just needed a simple observation for C2 and I somehow missed that. This is how it goes.

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

      Coloring arguments like this one are very common in math olympiads. That may be where "saw before" comes from.

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

Even I don't know how I solved D. Just kept adding bunch of if's and miraculously AC'D.

Beautiful Editorials though :)

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

I got TLE during system test for D even though my solution was O(nlog^2(n)) where n<=1e5. Feels very sad man! I hope from next time the pretest for TL would be stronger.

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

    During contest your codes time was 1824 ms so with this information you can easily understand code need some more speed.

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

The point distribution suggests that doing both C1 and C2 was as difficult as doing D, but I felt like D was muuuch easier than even C1.

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

Problem C WAS confusing.

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

Please give my brain cells back nvm they are already dead.

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

Can anyone give some suggestions if my thinking come to a dead end.

In D, I spend all my time to think how to perform min on neighbor elements efficiently, like from [1,5,3,4,2] to [1,3,3,2], eagering to optimize O(n^2) to O(nlogn). I also think about some properties like increasing or decreasing sequence to speed up.

But when I saw the editorial, it is totally another way.

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

    I sometimes get this problem at harder CF problems and, at least in my case, the best solution is to just try a completely different approach to the problem. If this different approach is actually correct, congrats, all is left is to implement the solution. Otherwise, you can go back to your original ideas with a fresh mindset, which means you can notice things you previously haven't thought about.

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

      One thing I realize is that, if we want to gain more information (like simulating whole process in D, instead of just deciding whether a permutation), it usually requires a higher time complexity. So it may be a good way to rethink if we record redundent information, and what is the minimum necessary factors that matter

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

One of the best contest.

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

Thanks for the beautiful contest. I think I enjoyed today's contest the most out of all the one's I have attended so far.

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

Deleted

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

    That's not a sparse table implementation. It's a segment tree. I've a feeling you've probably copied code from somewhere.

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

I couldn't solve C1 and C2 in contest but after seeing the editorial, I just loved their solution. Two of the most amazing problems I've ever seen on Codeforces. Thanks Monogon

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

.

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

My alternative solution to D:

For each element in the array, find the longest contiguous subarray containing it and surrounding elements which are no smaller than it. If that subarray has length $$$L$$$, we know that this element's value occurs as a compressed value for all $$$k \leq L$$$. Then to determine if a $$$k$$$-compressed array is a permutation, we want to know if all elements in $$$[1, N - k + 1]$$$ appear somewhere with $$$L \geq k$$$. It can be handled simply by precomputing the max $$$L$$$ for each distinct element and computing prefix mins.

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

    There is a binary search approach as well. only the first element of output can be 1 apart from it we bs the transition point for the rest of the array.

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

Alternative approach to D:

Note that if some value $$$a_i$$$ appears in a $$$k$$$-compression, it will be present in $$$(k-1)$$$-compression. Now for every $$$a_i$$$, we need to compute the greatest $$$k$$$ such that $$$a_i$$$ is present in the $$$k$$$-compression. Suppose that $$$l < i$$$ such that $$$l$$$ is the greatest possible and $$$a_l < a_i$$$. Similarly $$$r > i$$$ such that $$$r$$$ is the least possible and $$$a_r < a_i$$$. In other words, we find the closest two lower elements to the left and to the right. Then $$$a_i$$$ will appear in $$$k$$$-compressions for $$$k \in \{r - l + 1, r - l, r - l, \dots, 1\}$$$. Finding $$$l$$$ and $$$r$$$ can be done by adding indices $$$i$$$ of $$$a_i$$$ in the increasing order of values of $$$a_i$$$. We keep the indices in a set, now we can quickly find greater/lower element.

Now we can iterate through every $$$k$$$-compression by going from $$$n$$$ to $$$1$$$ and add each element, that now appears for the first time. We can easily check if the current $$$k$$$-compression is a permutation by keeping it as a set and checking its size, its minimum element and its maximum element.

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

    can you explain me how use set to get l and r

    thank in advance

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

      Order input elements of the input and make sure you keep its original index. For example keep a vector<pair<int,int>>, where first is the value, second is the original index and sort this.

      Now add indices of values in increasing order to the set. To find the next greater, call upper_bound on the set. To find the next lower, you can keep a second set with reverse order. Calling upper_bound on this one finds the next lower.

      From the way you add items to the set, you know that the index you find belongs to a element with a lower value.

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

I had an alternative $$$O(n)$$$ solution for D that uses the "next smaller" problem (see my submission). It turned out to be an overkill but maybe it's of interest for some :)

Consider our input array $$$A$$$. First observe that for any window size $$$k$$$, an integer $$$i$$$, $$$1 \leq i \leq n$$$, will be in the resulting array if and only if there is some subarray of $$$A$$$ with size $$$\geq k$$$ where $$$i$$$ is the minimum.

Now, we can compute the array $$$B$$$, where $$$B[i]$$$ is the maximum value of $$$k$$$ for which $$$i$$$ will appear in the resulting array. So for the example array $$$A = [1, 3, 3, 3, 2]$$$ the corresponding array $$$B$$$ is $$$B = [5, 4, 3, 0, 0]$$$. We can compute this array in $$$O(n)$$$ time by first pre-computing for every index $$$j$$$, the first index to the left and first index to the right that the number is smaller than $$$A[j]$$$ (if you don't know how to compute those indices, see this article).

Finally, for a particular $$$k$$$, notice that the array resulting from the window-min operation will be a permutation if and only if $$$B[i] \geq k$$$ for every $$$1 \leq i \leq n - k +1$$$. Why? It's clear that if it is not fulfilled, the array will not be a permutation. On the other hand, if the condition holds, we'll clearly have at least one of each integer between $$$1$$$ and $$$n - k + 1$$$. At the same time, our array will have exactly $$$n-k+1$$$ integers, therefore it'll have exactly one of each integer.

Therefore, we can compute prefix "sums" (using min instead of addition) on the array $$$B$$$ which will give us an array $$$C$$$ that allows us to quickly check whether the condition above holds in constant time--we will have: $$$k$$$ will result in a permutation $$$\iff$$$ $$$C[n - k + 1] >= k$$$. In our example, we'll have $$$C = [5, 4, 3, 0, 0]$$$ and checking for the condition will yield us the output $$$00111$$$.

Overall, for each step we required linear time so the time complexity is $$$O(n)$$$.

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

    I was thinking in similar way of maintaining the prev smaller and next smaller and finding the maximum length for which it remains smaller. But couldn't connect dots of how to find solution with it.

    Thanks for the explanation!

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

      yeah, even I am thinking of the same approach I request Monogon to please add this as an alternate solution.

      Thanks for your explanation :)

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

The moment when you use FFT instead of trivial Vandermonde's identity (https://en.wikipedia.org/wiki/Vandermonde%27s_identity) is the moment you should realize people can be too experienced

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

    How does FFT help compute what we had to compute using vandermonde? I'd love to understand

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

      FFT lets you do the following:

      You are given two sequences: $$$a_0, ..., a_n$$$ and $$$b_0, ..., b_m$$$. Compute sequence $$$c_0, \ldots, c_{n+m}$$$ such that $$$c_i = a_0b_i + a_1b_{i-1} + \ldots + a_{i-1}b_1 + a_ib_0$$$.

      This is exactly what I needed here, where $$$a_i = {n \choose i}$$$ and $$$b_i = {m \choose i}$$$. If I thought for a little longer I would realize that $$$c_i = {n + m \choose i}$$$ instead of computing it using FFT.

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

I had another idea for D. It seems to have passed the tests but correct me if I'm wrong.

What I tried to to do is to find for each value x (1 <= x <= n) what is the maximum value of k so that there exists a segment of length k, where x is a minimum value. Let's call this maximum value k_x.

So for example for this case: 1 ? ? 2 ? ? if x = 2 and ? >= 2 then k_x = 5

Of course it is possible that there is more than 1 number equal to x in the array. In this case k_x is equal to maximum of k_x of all positions where a_i = x.

Calculating k_x for all x can be done with stack.

Then to create answer we do the following:

Let's iterate over x from 1 to n. If x was not present in array we break the loop. Otherwise let's calculate what k must be equal to if we want to create permutation from 1 to x. It is equal n - x + 1. Then let's calculate what is the greatest value of k that can be used. It is equal to min(k_j) where 1 <= j <= x. So if min(k_j) >= n - x + 1 then answer is 1 else it is 0.

Here is my code

Edit: Someone had already wrote about this approach while I was writing this comment. You can refer to this link to see it

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

To those who don't know anything about sparse table or segment tree yet and wanted to solve D, My approach:

A set of observations to start with:

0) Kth compression array is 2nd compression of K-1th array. Try proving this by taking some sample cases :)

1) We find the lowest number which has been repeated in the array. To do this, just sort the array and check whether a[i]==i for 1<=i<=n. If its not, break at that i. Benefit: the integers above this can never form a permutation, since this number is duplicate and smaller than them. Store 0's in the answer string

2)If we have an integer a[i] such a[i-1] > a[i] and a[i] < a[i+1], it will make a duplicate of itself when we compress it, so we need to find the smallest integer and above that integer, any array wont be a permutation since it would have duplicates. Store 0's in answer string upto this integer.

3) Now we have an array left which has a[i]=i for 1<=i<=n and the array is free of an a[i] such that a[i-1]> a[i] and a[i]<a[i+1]. The number of elements left in the array is the number of 1's you would have in your answer string.

  • Also do check that if the array is free of duplicates at the start, the first compression array wd be a permutation regardless of the 2nd observation.

Feel free to check my code as well here

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

So many alternate ideas to D in the last few comments

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

Can somebody please explain the editorial version of problem D

for the test case n = 3, array = [2,2,1] as per my interpretation: 1-compression is [2,2,1] -- not happy , 2-compression is [2,1] -- happy, 3-compression is [1] — happy.

But as per editorial we will fix l = 1,r = 3 initially and we iterate over i = 1,2,3:

so for i = 1 , as arr[3] is 1 ,so we decrement r to 2 and min(a[1],a[2]) is 2 which is i+1

and next for i = 2 ,as arr[1] is 2 , so we increment l to 2 but min(a[2]) which is 2 != i+1 , so we are failing at this i,

so as per editorial k>2 are happy and rest are unhappy, but we know 2 compression is happy , did I misunderstood somewhere please help me out

Thanks in advance :)

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

    I did the same thing using two pointers — $$$O(n)$$$.

    Idea
    Code
»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
why this code is giving wrong ans for D

its same as explained in editorial

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

Too difficult contest.I spend lot of time on c1. But it's solution is very nice :).

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

I expected some text art in some of the test cases in either C1 or C2. Alas!

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

In problem D, wasn't sample output were big hint . In my case i got hint that except for k=1 , sequence for 1 will be contiguous .Thus i separately solved for k=1 and did binary search for rest and it got accepted .

In codeforces i find that many times sample's give a lot of hint compared to CodeChef short contests . That's why accuracy of most people on CodeChef is less compared to CF.

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

    Can u explain binary search approch

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

      Just check for k=1 separately . Then you can do binary search from k=2 till k=n . For checking a particular k , you can do what is told in the question . You can find range minimum using segment tree .Complexity will be nlog(n)log(n) . You can prove why it works on lines similar to the editorial .

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

    Interestingly enough, it gets TL if you use multiset to squeeze the array inside binary search

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

      Yeah this is because of high constant factor in multiset,set and map. Segment tree i guess is faster, the only overhead is function calls.

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

I solved D using Binary Search.

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

For C, what if the direction of the color diagonals are to the other way? So it goes from top left to bottom right, shouldn't there be a check for that case as well?

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

Can someone provide an example for which greedy in C1 fails. Greedy being swapping 'X' which is contained in most triples of 'X' with 'O' until there are no more triples of 'X'?100579257 this is how I tried to solve the problem.

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

I hate constructive algorithms

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

.

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

    Bro, did you find out your flaw. Actually I was trying but couldn't find out a testcase which you were missing. But I am pretty much sure that your code fails on the case where we can't take the same coloring for both of X's and O's..

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

      Yeah, I found a case in which I was changing more than k/3 tokens.

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

        Can you give that case.

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

          Yeah sure...

          6
            OO.XX.
            ...OXO
            X..X..
            XOOOXX
            X.XX.O
            ....XO
          

          Output:

          OX.XX.
          ...XOO
          O..O..
          XXOOXO
          X.XX.O
          ....XX
          

          There are 8 changed tokens but allowed are only (21/3) = 7

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

can someone explain problem D

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

    There are editorials from the author and also from other participants' comments. Try to read them.

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

I solved D using binary search. Take k=1 as special case and binary search from 2 to n. Anyone else did this? It passed but is it a correct solution ?

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

For problem D, if the check fails on index i, then shouldn't the answer be 1 for n-i+1 <= k <= n ? Consider 1 5 3 4 2. Check fails for i=3 and k=3 gives a permutation.

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

Thank You Monogon for such a nice contest, really loved the problems

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

Errichto did't intimidate Monogon from taking his top contributor spot on Codeforces!

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

Even though I didn't do well in this contest, I'd like to say that the problems were of great quality.

Kudos to all the authors!

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

lbyyds

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

.

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

Hello! I found this testcase of problem F

1
13
1 1 3 1 1 2 3 2 3 2 3 1 1

The correct answer is 6, but i can't get how to reach it. Can anybody help?

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

I can't understand the tutorial for E. I thought if there's a cycle with positive weight also give a contradiction. For ex :

u1 -> u2 -> .. -> uk -> u1

Suppose each edge in cylce above is 1, then the weight of this cycle is positive. In other words, i can say : u1 > u2 > ... uk > u1 (?)

I think any cycle that exist must be 0-weight. Can anybody explain what was wrong with me T.T

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

    Since it is a bipartie graph, the cycle has an even length.

    If all edge are given directed as a cycle, there will be a negative cycle(all with weight -1).

    Otherwise, you can direct them in an alternating way, so $$$w_2 = w_1 + 1, w_3 = w_2 - 1 = w_1, \dots$$$. So there are no contradictions.

    UPD: If the sum of the cycle is positive, you can direct some undirected edges so that the sum is $$$0$$$.

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

      Thanks you! I got it. But "The property of shortest paths tell us..." what's the property which author referred to? (if you see me too weak to understand E, pls tell me and i will skip this :D)

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

        I think it is $$$d_u + w(u, v) \ge d(v)$$$, where $$$d(u)$$$ is the distance from $$$S$$$ to $$$u$$$.

        You can see $$$|d_u - d_v| \le 1$$$.

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

Monogon Same problem as C1 in Hackerearth Contest 2 days back. Link

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

it's too hard to wait for 12 days without a contest :(

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

Can anyone explain why, in 1450H1, the final equation has a factor of 1/(2^F) instead of 1/2? I would suppose that since each appropriate parity subset of F is a bijection to a valid colouring with f(c) = 1/2 * |x-{size of subset}|, the constant factor is 1/2.

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

    There are $$$2^{F - 1}$$$ colorings.

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

      Thanks, took me too long. For the others:

      There are a total of $$$2^{F-1}$$$ possible colourings, since the last colour is fixed by the rest to maintain parity. We want the expectation / average, so that's why we multiply by $$$\frac{1}{2^{F-1}}$$$

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

How to approach this slightly modified problem b,

Instead of attracting all the balls (in the original problem) what if we can deselect any number of balls to attract in any operation.

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

Can anyone help how does one get the idea of (i+j)%3 for the problems C1, C2?

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

    Working on small dense examples by hand, you might come across this pattern of every third diagonal.

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

I took a different approach to problem E.

I start by assigning a $$$a_{src}=0$$$ to a "source" node. According to this, all remaining nodes $$$i$$$ are associated with a set of possible values for $$$a_i$$$. For example, consider the example of the following graph:

4 3
1 2 0
2 3 0
3 4 1

Taking node 1 as the source, the set of possible values of $$$a$$$ associated with node 1 = {$$$0$$$}, node 2 = {$$$-1,1$$$}, node 3 = {$$$-2,0,2$$$}, node 4 = {$$$-1,1,3$$$}.

In fact, I can prove (using induction) that all such sets will be either continuous odd numbers or continuous even numbers. So, I can conveniently represent them as a pair $$$[i,j]$$$ denoting the range of the associated set {$$$i,i+2,i+4,...,j$$$}, which are stored in the vector $$$range$$$.

Now I start a dfs at the source, for an edge between nodes $$$u$$$ and $$$v$$$, set:

  1. $$$range[v]=[range[u].first-1, range[u].second+1]$$$ if the edge is undirected
  2. $$$range[v]=[range[u].first+1, range[v].second+1]$$$ if the edge is directed from $$$u$$$ to $$$v$$$
  3. $$$range[v]=[range[u].first-1, range[v].second-1]$$$ if the edge is directed from $$$v$$$ to $$$u$$$

If there is already some value of $$$range$$$ associated with $$$v$$$, take the intersection of the new value with the original value. If this intersection is $$$\phi$$$, the society cannot be capitalist. If the intersection is not the same as the original value, update $$$range[v]$$$ and call dfs on $$$v$$$, since the $$$range$$$ of other nodes may also need updating.

Repeating this process by keeping each node as the source iteratively, there will be at least one such source which gives the maximum income inequality as $$$\max\limits_{1 \leq i \leq n} (range[i].second)$$$. In such a case, setting $$$a_i=range[i].second$$$ for all $$$i$$$ gives a valid construction of the answer.

My submission: 100720392

EDIT: I have now concluded that the complexity of dfs is $$$O(nm)$$$.

For any node, the maximum range that can be assigned to it is $$$[-n,n]$$$. Every single time this range is updated, it must decrease by at least 2, since we always take the intersection of the original range and new range. Thus, each node can be updated at most $$$O(n)$$$ times.

Furthermore, every single time we update a node, we iterate over all its adjacent edges. If the degree of a node is $$$d_u$$$, the dfs will perform at most $$$n*d_u$$$ steps for node $$$u$$$. Summing over all nodes, $$$\sum n*d_u = 2nm = O(nm)$$$.

Thus, the solution runs in overall $$$O(n^2m)$$$ time which is just enough (for me, a privileged C++ user) to pass the time limits. But if you disagree, please feel free to hack the solution if you can. :P

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

    The ranges have size $$$O(n)$$$ and shrink every time dfs is called on it. An edge is processed every time one of its endpoints has a dfs call. This way, every edge is processed $$$O(n)$$$ times, so it seems your complexity guess is correct.

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

      Yes!! Thank you!

      Sorry, it seems I was in the middle of editing my comment just as you posted this one (xD). Thanks a ton though!

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

Nice Problem C!

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

In the non-optimized ver. of problem G, how did 3^C come out? Had trouble with bitmasks complexities.

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

    For each bitmask, you iterate over only its submasks, not all masks. For each position, (mask, submask) has 3 possibilities: (0, 0), (1, 0), and (1, 1). Therefore the are 3^C bitmask-submask pairs.

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

Also I would like to ask about the memory limit of G which is unusual, what kind of solutions did the author want to block?

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

    There is a solution with subset sum convolution that doesn't use the observation about overlapping ranges.

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

For problem B(Balls of Steel) how the answer can only be 1 and -1. Could any one please explain this to me.

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

    I proved my claim in the editorial. Basically you assume for contradiction that there is a solution with 2 or more moves, and show that it's impossible.

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

I have a solution for problem D. I used segment trees to find the largest length where a[i] is the minimum, and I did this for all i from 1 to n. Then I maintained the largest length for each number from 1 to n such that the number is the minimum in its range using the previous information. Finally, I used a range prefix sum technique: I knew that if value k has a maximum length of a, then it will only be able to be used in a 1, 2, 3, . . . , min(a, n — k + 1)-compressions. So I will have a prefix sum array where I increase ps[1] by 1 and ps[min(a, n — k + 1)] by -1. Then in O(n), I compute my prefix sum array. Now for each i, all I need to do is make sure that ps[i] = n — i + 1 in order to have a 1 in the string at the ith position. Otherwise, there's a 0 at the ith position.

153769266