· Olympiad in Informatics

CH1201: Maximum Subsequence Sum — Solution

The main goal of a monotonic queue is actually quite similar to the main goal of a monotonic stack: both are about directly ruling out, in a monotone problem, the answers that can be ruled out, and computing with the rest.

In this problem, we will use a monotonic queue to solve it. First, we use prefix sums for fast computation. Then, on the basis of the prefix sums, we will use a strategy in this problem, namely: if two positions have prefix sums \(sum[i]>sum[j],其中i<j\), then the element at position \(i\) is completely useless to us. The reason is that if we want to get the subsequence with the largest sum within length \(m\), for an element like this, whose prefix sum is both larger and earlier, the optimal solution is never made up from this element.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

POJ2054: Color a Tree — Solution

Lately I've been working through problems along with the advanced algorithms tutorial written by Li Yudong. This problem has the following constraints:

  • A child node can only be colored once its parent node has been colored.
  • We want the minimum coloring cost.

Thinking about these two conditions, we can conclude that as long as we give priority to coloring the subtree with the largest average cost, we get the optimal solution; also, clearly, if we color a parent node, then the child with the largest average cost among its children will be colored first.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

CH0601: Genius ACM — Solution

This problem ate up my whole morning. This morning, after finding out my NOIp 2018 results, I couldn't steady my mind enough to go to class, so I skipped class and came to the computer lab to work on problems. And after poring over the book and the solutions for ages, I pieced together an approach.

We first store the whole sequence in the array \(pre[]\), then, starting from the endpoint \(L=1\), we enumerate the right endpoint. We use binary lifting to speed this up. Write a function \(check(num)\) to test whether the checksum of the interval meets the standard. The checksum is computed by sorting, then taking the largest and the smallest, the second largest and the second smallest, ... and computing with them. For the sorting we can use the idea of merge sort: since our interval lengths are powers of \(2\) and are enumerated from small to large, each time we only need to sort the right half of the interval and then do a merge. See the code for the details.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Things

NOIp 2018 Summary

Today is November 11. The second round of NOIp 2018 is over. At first glance, I can get full marks on the first problem of DAY 1; on the second problem I used a pseudo-extended-Euclid brute-force algorithm, so I can only get partial credit; and the third problem was purely a bidirectional depth-first search to scrape points.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P2898: [USACO08JAN] Haybale Guessing — Solution

This is another problem where I copied the solution. Let's first briefly analyze the cases that trigger a contradiction:

  • Given two intervals \(A, B\) with \(A \cap B \neq \emptyset\), but \(A_{min} \neq B_{min}\), this case is a contradiction.
  • Given two intervals \(A, B\) with \(A \subset B\), but \(A_{min} \neq B_{min}\), this case is also a contradiction.

After that, we can use a disjoint-set union to carry out the set operations. The data range is very large, and the answer only needs the earliest solution, so we can try binary search on the answer.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P1666: Prefix Words — Solution

This is a dynamic programming problem. We first consider preprocessing an array that stores whether each pair of words is safe. Then we use this information to do the DP. One point worth noting: when doing dynamic programming we have to eliminate aftereffects, otherwise we will get wrong answers. In this problem, we need to sort the strings, because after sorting, the unsafe words only exist before the \(i\)-th word, so a one-dimensional \(dp\) is enough. Consider the equation: \(dp[j] += dp[i], j \in [i, n], possible[i][j] == true\), and that's it.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P1966: Lining Up Matches — Solution

We first read \(a,b\) into a data structure, and sort both sequences with the height as the key. Let's first write out what one sample looks like after sorting (the array subscripts are the original positions): \[\{1_3,2_1,3_2,4_4\}\] and \[\{1_3,2_2,3_1,4_4\}\] Looking at this, we notice that we can obviously discretize. So let \(mapping[]\) be the discretized array; after sorting both groups, doing \(mapping[a[i].index] = b[i].index\) completes the discretization. The problem then turns into how many transformations array b needs to become identical to a (since we have already discretized, \(mapping[]\) can stay within the range of \(n\)). We can think of counting the number of inversion pairs to solve this. And the problem is solved with ease.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Mathematical Expectation and Its Linearity

Ever since the NOIp 2018 preliminary round, when I first learned that there was such a thing as mathematical expectation, I've always found it hard to understand. I never took the time to study mathematical expectation carefully, but today I took part in a contest and was forced to learn it, so now I'm writing a few things down to record this concept.

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

P2915: [USACO08NOV] Mixed Up Cows — Solution

Looking at the constraints and the problem statement, you can tell this is a bitmask DP problem. Let's design the state first: in this problem, by analogy with the Non-Aggression problem (Non-Aggression solution), we design it as the related elements of the current state and the previous state. You can come up with: dp[which cows are currently present][index of the cow just added] += dp[state after the previous addition][index of the cow added last time];

Continue reading →Chinese original · English translation by Claude Opus 5.5

· Olympiad in Informatics

Summary of October and 100 ACs

This month I hit quite a few of my goals. For example, my AC count went up visibly, and I learned some algorithms: the bitmask DP I just picked up, getting fluent at interval DP, some black-magic slick tricks in number theory, the LCA and binary-lifting RMQ algorithms the computer-lab big shots taught me, writing a Fenwick tree (BIT) (I still haven't got range max/min sorted out), storing graphs with forward star in graph theory (only learning the basics now, I'm still way too weak), minimum spanning trees (but I haven't learned Prim yet and I'm really panicking), for DP on DAGs and tree DP I also practiced some easy problems, for disjoint-set union (DSU) I picked up some other ways of writing it, plus some better replacements for the STL, bitset and so on.

Continue reading →Chinese original · English translation by Claude Opus 5.5