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

Автор xiaowuc1, история, 10 лет назад, По-английски

Hi all,

The third contest of the 2016-2017 USACO season will be open from February 10th to February 13th. This will be our last contest before the US Open. Feel free to discuss the problems here after the contest is over!

  • Проголосовать: нравится
  • +60
  • Проголосовать: не нравится

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

Is the contest over? Anyone minds to share any problem's solution for silver division?

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

Hints for Platinum division:

  1. BIT
  2. Segtree
  3. 2d range tree. Needed a few optimizations to fit into ML. There's probably a faster way.

I derped on the first one because I didn't realize that rotating just one side wasn't enough — you had to check rotating both (in addition, I solved #3 first, so I copied my 2D range tree code from it instead of just using a simple 1D BIT, greatly overcomplicating the problem :( ).

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

Why is the site still stating that the contest is running?

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

@xiaowuc1 Is there a better solution than 2D range tree or Sqrt decomposition + fenwick in better order?

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

The problems in platinum division showed a lot of creativity. Like why the hell would someone think that putting 3 DS problems in a single problemset of 3 problems is cool?

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

    #2 can be solved with nothing more than DP and arrays. I do think it's a bit much to have DS questions for both 1 and 3 though (unless they also have better solutions).

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

    And more is that, the trick I used to solve Problem 1 and Problem 3 are both involving Number of Inversions.

    I think the problems each is interesting, but when the authors intend to put series of problems with similar background(cow crossing roads?). Although the solutions themselves are not the same, it is very easy that those problems involves similar tricks or approaches because the background of them limited the range of possible topics.

    Still, I appreciate the author brilliant idea of preparing problems with similar background and same title(is it same for Bronze as well?). It is not that easy to come up with a large number of problems that all of them can share same background. The first moment when I realized the names of problems in next division were all same as the previous division, I was just expecting problems with greater constraints, stricter limitations to increase the difficulty of the problems, but it turns out to be in 9 of the problems, only two of them overlaps, which is quite a small number than I expected.

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

This contest is really memorable to me. As I long time did not participate USACO, my previous division was Silver. As a result, I participated in the Silver contest, and got in-contest promotion to the Gold division, and participated in the Gold contest, and got in-contest promotion again to the Platinum division, and finally, participated in the Platinum contest.

So I decided to share all my solutions and ideas to the contest from Silver division to Platinum division.

As the solutions are quite long, I decide to split the solutions into 3 parts according to their divisions. This can also let us discuss the solutions for different divisions without colliding on the same comment.

Silver Division

Problem 1. Why Did the Cow Cross the Road

By getting maximum number of pair (i, j) such that Aj ≤ Ti ≤ Bj, we can greedily match each j to some i. We can perform this by starting from the chicken with smallest Ti, greedily match this chicken to the cow (that fulfill Aj ≤ Ti ≤ Bj) with the maximum Bj. I used priority_queue as a heap to do so.

Problem 2. Why Did the Cow Cross the Road II

We can just pre-compute the prefix sum so that we can calculate the number of damaged signals with range i..i + K - 1. And thus we can iterate through all possible i to find the answer.

Problem 3. Why Did the Cow Cross the Road III

Before looking for the answer, we can first find the groups of cows that they can visit each other without crossing any road. This can be done by simply DFS, we can go from (x, y) to (x', y') only if there are no roads between them and they are adjacent grids.

At last, we can compute the answer by knowing the fact that the cows in seperated groups are distant with each other. Therefore, if we have M groups in total, and the i - th group consists of Gi cows, the answer is G2 × G1 + G3 × (G1 + G2) + G4 × (G1 + G2 + G3).... We can quickly calculate the answer with the aid of calculating the prefix sum of Gi.

»
10 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +31 Проголосовать: не нравится

Gold Division

Problem 1. Why Did the Cow Cross the Road

I solved this with Dijkstra(this is my first thought after reading the problem, I am quite sure there are easier solutions). Let us denote dist[x][y][k] as the minimum cost to reach (x, y) such that this is the (k mod 3)-th step from the starting point. We will only add the cost Gi, j if the previous k is 2, otherwise, the additional cost is zero.

Problem 2. Why Did the Cow Cross the Road II

Before talking about the solution to this problem, I have to state that this is the easier version of the Problem 2 in Platinum Division.

Okay, I used 2-dimension DP to solve this problem. Here, we denote dp[i][j] as the maximum number of crosswalks that can be drawn if we just consider the first i cows on one side of the road and the first j cows on the other side of the road.

Then we can use this transition formula to compute the whole dp[][] array: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]),

And when |a[i] - b[j]| ≤ 4, we also have to consider: dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1).

So the answer would be dp[n][n]

Problem 3. Why Did the Cow Cross the Road III

We can observe that pair (a, b) is a "crossing" pair if and only if the pattern a, b, a, b appears in the circle. In other words, we can just consider the pattern a, b, a and b, a, b in the input (not a circle but a straight line).

I calculate the number of occurrence of that two patterns with the aid of Mo's algorithm, and the time complexity of the whole solution is . I think there should be faster solutions?

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

    For gold 3rd I sorted all the segments and then processed then one by one starting from the "shortest" one, then used BIT with the sums, because the smaller intervals have been processed, their value in BIT array will be set to zero

»
10 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +47 Проголосовать: не нравится

Platinum Division

Problem 1. Why Did the Cow Cross the Road

We can transform the problem into counting the number of inversions on one side if we keep shifting the sequence on the other side. For instance, the sample can be transformed into considering the second sequence as 3, 4, 5, 1, 2 (if we decide to shift the second sequence).

We can maintain the number of smaller elements that came after the element i for each i with Segment Tree. For simplicity, we denote this number as counti. The number of inversion of the permutation will be the sum of counti. And each time we move the first element k to the back, the value countk becomes 0, and all counti with i > k will increase 1. Maintaining the values of counti with Segment Tree, we can easily update and calculate the number of inversions with O(logN)

Problem 2. Why Did the Cow Cross the Road II

As I mentioned above, this problem is an harder version of Problem 2 in Gold Division. I just improve my solution to O(NlogN) instead of O(N2).

We can observe that for every abs(ai - bj) ≤ 4, dp[i][j] is the maixmum of all dp[x][y] with x < i and i < j. Therefore, by iterating i, we can reduce the dp[][] array to one-dimension, dp[], where dp[j] denotes the number of crosswalks for just the first i and first j elements on two sides with abs(ai - bj) ≤ 4. We can maintain this dp[] with Segment Tree. For every i, we should pre-compute all the values j such that abs(ai - bj) ≤ 4. Then, when we iterate i from 1 to n, we can update dp[j] as the maximum value among dp[1..(j - 1)] plus one.

Problem 3. Why Did the Cow Cross the Road III

By renumbering the input so that the first n numbers A1..n are 1, 2, .., n. We can just consider the second set of n numbers B1..n, and thus simplify the problem as calculating the number of pairs (i, j) such that Bi > Bj and A[Bi] - A[Bj] > K.

Recalling that during the process of merge sort, we can calculate the number of inversions as well. Here I used this trick and handle the rule Ai - Aj > K as well. Every time we merge the two sorted sequences, let's say S and T, we can maintain a Segment Tree which can help us that for each element Si, after finding the maximum j such that Si > Tj, we can calculate the number of elements in T1..j that A[Tx] + K < A[Si] or A[Tx] - K > A[Si]. So, every time we increase j, we can update the ranges 1..(A[Tj] - K - 1) and (A[Tj] + K + 1)..N.

Thus, the time complexity of my whole solution should be

Conclusions

Actually this is my first time sharing solutions of many tasks of one contest, and also as I participate quite early in the 4 days window, my memory to the problems are not very fresh. I apologize for any mistakes or stupid things I have made in this text.

I would be appreciated if you could share your thoughts to my solutions as well :D

Last but not least, the only problem I can't solve during the whole contest (may someone help me?) — Why did the cow cross the road?

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

So why exactly did the cow cross the road?