CF1692G Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
Problem links: (CF, Luogu) | I strongly recommend reading it on the blog.
The first CF Div. 4 I participated in.
This problem is the kind that becomes very, very simple once you think of the key point, but if you do not, then… you are done for (I was one of those who were done for).
1. Problem Statement:
Given an array of length , find how many intervals of length in the array satisfy the following condition:
2. Approach
Brute force is still easy to implement: just calculate every possible interval in the array. However, seeing the condition tells us that this will not work.
Therefore, we need a method that can determine whether an interval satisfies the condition in time. (If you can produce an method, that is also acceptable.)
We can see that brute force is needed because the starting position of the interval changes each time, so the number by which each item of the array must be multiplied is uncertain. But if we can find a condition independent of the interval’s starting position, the problem is solved.
Next comes the key point.
Looking more carefully at the condition given in the problem, we can see that, for the condition to hold, the preceding item in the array must be less than twice the following item, namely:
This property is independent of the position and length of the interval. As long as two adjacent numbers satisfy this condition, they can appear in an interval of any length and at any position.
However, these are only two numbers in the interval. If we want an entire interval to be valid, we need every in the interval to be less than .
In other words, as long as an interval of length contains pairs satisfying , the interval satisfies the condition.
Counting the number of valid pairs in an interval… and querying the result in time—is that not exactly a prefix sum?
Therefore, after determining whether each pair is valid, we naturally create a prefix-sum array valid_sum[i] to count how many valid pairs there are up to .
Finally, we use another loop to count the valid intervals.
3. Code:
The code is fairly simple. The point is still relatively difficult to think of in this problem.
1 |
|
Finally, I hope this solution is helpful to you. If you have any questions, you can contact me through the comments or a private message.






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