2021 Huazhong University of Science and Technology Freshmen Cup
发表于|更新于
|总字数:833|阅读时长:4分钟
2021 Huazhong University of Science and Technology Freshmen Cup - Solution
G. Awson Loves Chipmunks
Awson loves chipmunks, especially the k-th one among all n chipmunks! He wants to keep a consecutive interval of chipmunks in his home, and the interval should include the k-th chipmunk.
However, chipmunks eat a lot and it is a heavy burden for Awson to buy food for all chipmunks. Specifically, the i-th chipmunk has a hunger value ai. If Awson decides to raise the chipmunks from index l to r (1≤l≤k≤r≤n), then the burden brought to Awson will be
f(l,r)=i=l∑rai
Awson does not want to worry too much about chipmunks’ food, so he will choose the interval with the m-th smallest burden. Now you are required to tell Awson the burden he will carry.
Input
The first line contains three integers n (1≤n≤106), k (1≤k≤n) and m (1≤m≤k(n−k+1)) as described above.
The second line contains n integers. The i-th integer ai (∣ai∣≤109) represents the hunger value of the i-th chipmunk.
Output
Print an integer representing the m-th smallest burden.
Scoring
The problem contains several subtasks. You can get the corresponding score for each passed test case.
Subtask 1 (30 points): n≤103.
Subtask 2 (70 points): no additional constraints.
Examples
1 2 3 4 5
input 4 2 3 1 2 4 8 output 6
1 2 3 4 5
input 4 2 2 -1 2 -4 8 output -2
Note
In the first example, the second chipmunk must be chosen, and there are 6 ways to choose the interval in total. They are f(2,2)=2, f(1,2)=3, f(2,3)=6, f(1,3)=7, f(2,4)=14, and f(1,4)=15. Among them, f(2,3)=6 is the third smallest, so the answer is 6.
思路
二分答案。
记和不超过 x 的区间个数为f(x)。若 f(x)≥m 且f(x−1)<m,则答案为x
考虑如何快速计算 f(x)。预处理两个数组 R=∑k<j≤iaj(i>k) 与 L=∑i≤j≤kaj(i≤k),则每一个包含k的区间都可以表示成 L 和 R 中两个元素之和。