This month I completed the first formal OI contest of my life — NOIp. Although the result is heartbreaking, I can only blame my own lack of ability and my thin contest experience. The big shots of FZOI 2017 did really well, XG's second place in Jiangxi was rock-steady, and in DOFY's last NOIp, DOFY was still, as always, the one crushing the field. This month I learned to regulate my emotions and adjust my state. I'm very thankful to the big shots in the computer lab, and as for Teacher Lei's concern for me after my emotions fell apart, I'm very grateful for that too. Right now I feel I'm in good shape, even though the problems with my regular classes are starting to show more and more.
Continue reading →Chinese original · English translation by Claude Opus 5.5
While thinking about it, I could vaguely sense that this should be done with a monotonic stack, but I never managed to figure out the details of how. Now that I understand it, let me write down some thoughts.
Continue reading →Chinese original · English translation by Claude Opus 5.5
In Advanced Guide to Algorithm Competitions, Li Yudong describes the Huffman problem like this:
Construct a k-ary tree containing n child nodes, where the i-th leaf node carries weight \(w_j\), and minimize \(\sum w_i*l_i\), where \(l_i\) denotes the distance from the i-th leaf node to the root node.
We can consider using a segment tree to maintain the area of an interval (a pair of people who don't know each other can be represented in 2D space by a block with side length \(1\). Our problem then turns into finding the area within some range of the coordinate system (the area is the number of acquainted people in the interval). If we study it further, we find that each time we operate on an interval, the shape we operate on is represented in the 2D coordinate system as one whose two opposite corners lie on the line \(y=x\). We first blacken the points on the line \(y=x\) (you always know yourself), and then write a segment tree. When merging two intervals we need to be careful: if the merge involves two overlapping rectangles, we need to take the highest value to represent the upper edge of the rectangle. Within an interval, the height of the upper edge is always monotonically increasing, so we can binary search for an offset where the height is less than the covering height, which speeds up the merge. See the code for details:
Continue reading →Chinese original · English translation by Claude Opus 5.5
For this problem, the first thing we inevitably do is sort every sequence. The sum of the tops of all the sequences is the largest sum, and the second-largest sum is the sum of the largest sum of \(n-1\) sequences and the head value of the sequence with the smallest head value. So let's narrow our view a bit and discuss merging two sequences.
Continue reading →Chinese original · English translation by Claude Opus 5.5
How do we find a pair of numbers whose XOR value is the largest? Let's start from binary arithmetic (bitwise operations) itself. If we want to make sure the \(Xor\) value is larger, then we need as many \(1\)s as possible. Besides that, the position of the \(1\)s also matters a lot: in some cases, nearer the front is better. We can consider building a Trie starting from the highest bit down to the lowest bit. Let \(num[i]\) denote bit \(i\)'s \(0或1\) state. After building the Trie, we compute, for each number, the largest XOR value it corresponds to. Our strategy is:
Continue reading →Chinese original · English translation by Claude Opus 5.5
This problem asks us to find, for a periodic prefix, the smallest period length and the largest frequency. We can solve it with KMP's next array. Let's first review what the next array means. \(next[i]\) is the longest common part of a prefix and a suffix within the range \(str[1,i]\). If the length of the range is a multiple of \(next[i]\), then it contains a repeating unit. See the code for details.
Continue reading →Chinese original · English translation by Claude Opus 5.5
At first I wrote brute force + prefix-doubling sort, got WA on every single test, and started questioning my life. Then I looked at the intended solution, and sure enough, the binary search is really nasty.
Continue reading →Chinese original · English translation by Claude Opus 5.5
After reading the solutions I felt really gutted: it doesn't even need digit DP! Let's first consider a partial solution. When \(num<10\), the answer is just \(num\). If \(num \geq 10\), the answer is \(9+\frac{x}{10}\). Let's see: consider the answer for the number \(1921\). We can consider that the \(192-\) part contains \(\lfloor \frac{1920}{10} \rfloor\) pairs of numbers whose first and last digits are the same. Then we add the answer for when there is only one digit.
Continue reading →Chinese original · English translation by Claude Opus 5.5
Let's first briefly go over string hashing. First, we convert the characters of the string into numbers; for example, we can handle that with the following function:
\[toNum(character) = character - numberOf('a') + 1\]
Then, we define a value \(bitNum\) to serve as the radix. So our prefix hash function has the following recurrence:
Continue reading →Chinese original · English translation by Claude Opus 5.5