CF1774C Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
To complain a little, the official solution was rather difficult to understand. I remained quite confused after reading it for a long time (actually, I am also just not good enough). After understanding it, I felt that this problem was quite clever, so I came to write a solution.
Approach
We first need to make an observation: for the string , its final consecutive segment does not increase the number of possible winners. For example, when , the ending does not increase the number of possible winners.
Why? Suppose that, after any number of matches, the set of possible player combinations is . Then, for any , after any number of consecutive matches in environment , the final winner must be the player in with the highest temperature. This is because every remaining player must play continuously in environment , and the only player who can win every match must be the largest. Similarly, after the players in play any number of consecutive matches in environment , the final winner must be the player in with the lowest temperature.
For example, when , player 4 must be the final winner.
Thus, if the ending segment is (the same applies to an ending of ; for convenience, the examples below use ), we only need to calculate how many player combinations with different maximum values (player temperatures) can be constructed by the preceding part. This tells us the answer for at its current length.
Now consider how to construct the maximum number of player combinations with different maximum values. If there are players, then before any matches, the maximum value is . To make the maximum value different, we can only remove the current maximum.
Special Case
The preceding description may be relatively abstract. The example makes it easier to understand.
For the first , every player other than the one with the lowest temperature in the player combination can be removed (that lowest-temperature player can win no matter what). We can make the player with the lowest temperature play against any other player, giving the following cases:
Observation shows that only the first case changes the maximum value (why? Because it removes the maximum). In the other cases, a consecutive segment of numbers at the end must be removed before the maximum value can change.
This is where the second has an effect. In the second case, it can remove , making the maximum value of the player combination become . Following this pattern, we can generalize the following conclusion: suppose the length of the consecutive segment before the ending segment is . The number of player combinations it can produce with different maximum values is . Specifically, the possible range of maximum values is . (The is because we can choose not to change the original maximum value.)
At this point, we can already find the answer when contains only two consecutive segments. It is , where represents the length of the ending segment.
Generalization
Now let us change the example to and see whether the conclusion still holds ( contains more than one segment). Similarly, we can list the possible player combinations after the first match. Because the first environment is , only players other than the maximum can be removed:
Although none of these cases changes the maximum value of the player combination, we only need to play one more match in the following environment and remove to produce two new maximum values. In the first case, the maximum becomes ; in the second case, it becomes . There are also possible answers in total.
What if there are even more segments? For example, . The conclusion still holds. We can regard as a group of environments, in which can remove any player in the range , while can remove any player in . Combining these two kinds of environments allows us to choose any three players from to remove, constructing four different maximum values (depending on how many of the players with the highest temperatures are removed from the beginning).
Code and Implementation
Through the preceding examples, we have determined that solving the problem only requires knowing the length of the final consecutive segment in the substring of . However, scanning it again for every is too slow, so we need to use something similar to dynamic programming. The specific explanation is in the code comments:
1 |
|






![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)