· Olympiad in Informatics

P1896: [SCOI2005] Mutual Non-Aggression — Solution

This is a bitmask DP problem. I had never learned bitmask DP before this; this morning, with the explanation from the big shot lornd, I came to understand bitmask DP. However, my ability is limited, so I can't sum up this slick trick of bitmask DP in general terms. The essence of bitmask DP lies in merging multiple states into a binary number and then doing subset generation. Here we design a state, and this state encodes how the pieces are placed in the current row. For example, 01110 means pieces are placed in the middle three positions; for simplicity, we can compress this state directly into the decimal number 14. This is state compression.

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

· Olympiad in Informatics

P2014: Course Selection — Solution

At first I thought it was topological sort; later, while writing the code, I thought this problem through carefully, and it should be a tree DP.

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

· Olympiad in Informatics

P1373: Little a and uim's Great Escape — Solution

This is a fairly typical DP problem for counting the number of ways. This is the first time I've done a DP problem on counting the number of ways, so let this solution be the first article in my topic series on counting DP.

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

· Olympiad in Informatics

P1352: The Dance Party Without a Boss — Solution

This is a very classic tree DP; the problem states outright that the structure is a tree. Once the tree is built, the state transition equations are not hard to come up with:

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

· Olympiad in Informatics

P1841: [JSOI2007] Important Cities — Solution

This problem was given to me to solve by lornd, a big shot. Since I'm too weak, I had no choice but to give up and read the solution. After reading the solution, I found the approach quite clever, so I'm here to share it.

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

· Olympiad in Informatics

P1052: River Crossing — Solution

The data in this problem is huge, and with the conventional path DP the array won't fit. However, we can apply a modulo to the distances: after taking the modulo, as the frog jumps, the spots it should reach will always be reached, and the spots it can't reach will never be reached (compare with the basics of game theory). Now that we've compressed the path, we can do a normal DP.

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

· Olympiad in Informatics

P1156: Garbage Trap

This problem took me a whole day to get AC... it was really painful.

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

· Olympiad in Informatics

P1594: Convoy — Solution

One look at this problem and you know it is a DP that partitions the vehicles. At first I thought of F[first i vehicles][number of partitions j] = the time for the first i vehicles split j times, then found this formulation is very slow; besides, this problem does not say how many partitions there are, and we only need the solution for the first n vehicles.

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

· Olympiad in Informatics

P1070: Road Game — Solution

After poring over the solutions for a long time, I understood the ideas of the big shots. One dimension is enough for this problem, but the O(n^3) time complexity is truly staggering. Alas, I'm just too weak and could only understand this one, so all I can do is write an n^3 solution.

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

· Olympiad in Informatics

P2279: [HNOI2003] Setting Up Fire Stations — Solution

The data structure given in the problem statement is quite obviously a tree, and we need to find the minimum number of fire stations that can cover the whole tree. We first read the input as required, decide on the root (here I use node 1), then sort by depth and start checking from the deepest node. If a node is not covered, we cover its grandparent node, which maximizes the covered area; while covering, we spread out in both directions and set the vis[i] mark. Marked nodes are skipped, and for an unmarked node we put the fire station at its grandparent. Since our placement proceeds step by step from deep to shallow by depth, there won't be any hidden problems.

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