CF1703 F, G Solutions
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
F. Yet Another Problem About Pairs Satisfying an Inequality
Approach (from the Official Solution)
Observe the inequality in the problem. We can see that, for any element in the array, if it does not satisfy , then it can never form a valid pair with any element. Therefore, we can directly skip elements that do not satisfy .
We can split this inequality into three parts: , , and .
For all elements satisfying , the first and last inequalities are already satisfied. We only need to find elements satisfying to form a valid pair.
Let be the array obtained by removing from the elements that do not satisfy (this sounds a little strange, but the index of every element in is the same as it was in ).
For example, suppose an element in the array has two properties, value and index, and we denote it as . If array is:
then removing all elements with gives :
Then we can solve the problem by finding all valid for every .
It is not difficult to see that increases monotonically, so binary search can be used to find the largest smaller than . Every element in whose is smaller than (as well as itself) can form a valid pair with .
In addition to binary search, we can use a Fenwick tree to find the number of elements in whose is smaller than a particular value.
Specifically, we can use a Fenwick tree to maintain a prefix-sum array and then traverse the elements in , performing upd(id) each time. This makes the value found when querying every number greater than or equal to increase by .
Thus, querying in the Fenwick tree returns all smaller than .
Of course, a difference-array method can also produce the same prefix-sum array as the Fenwick tree. This problem does not require any further updates after obtaining the prefix-sum array, so a difference array can solve the problem in time.
Code
Complexity:
1 |
|
G. Good Key, Bad Key
Approach (from the Official Solution)
We can see that alternating between good keys and bad keys for all boxes is always less worthwhile.
Moreover, using good keys consecutively on the earlier boxes is more worthwhile. (In other words, use good keys on a prefix.)
Suppose we use a bad key before a good key. Then the profit we obtain is:
But if we first use a good key and then a bad key, the profit is:
Clearly, using the good key first is more worthwhile.
A more intuitive explanation is that regardless of which key is used first, is subtracted from the profit. However, if we use the bad key first, the profits of two boxes are halved, whereas if we use the good key first, only the profit of one box is halved.
Therefore, we only use bad keys in the final part. In some cases where is relatively large, halving may be more worthwhile than subtracting .
Thus, we only need to enumerate a dividing point between the use of good and bad keys. Use good keys before this point and bad keys after it.
Let this dividing point be .
When bad keys are used after the dividing point , the profit from each box becomes:
We can see that this grows very quickly, and beyond some point becomes . There is no need to continue calculating beyond that point.
Because the maximum is , there is no need to calculate after . (Alternatively, consider continually shifting a number to the right; after some point, its entire binary representation contains no s.)
Code
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)