E. Kolya and Movie Theatre
E. Kolya and Movie Theatre
Recently, Kolya found out that a new movie theatre is going to be opened in his city soon, which will show a new movie every day for days. So, on the day with the number , the movie theatre will show the premiere of the -th movie. Also, Kolya found out the schedule of the movies and assigned the entertainment value to each movie, denoted by .
However, the longer Kolya stays without visiting a movie theatre, the larger the decrease in entertainment value of the next movie. That decrease is equivalent to , where is a predetermined value and is the number of days since the last visit to the movie theatre. It is also known that Kolya managed to visit another movie theatre a day before the new one opened — the day with the number . So if we visit the movie theatre the first time on the day with the number , then — the number of days since the last visit to the movie theatre will be equal to .
For example, if and , then by visiting movies with indices and , value for the day will be equal to and value for the day will be , so the total entertainment value of the movies will be .
Unfortunately, Kolya only has time to visit at most movies. Help him create a plan to visit the cinema in such a way that the total entertainment value of all the movies he visits is maximized.
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers , , and (, , ).
The second line of each set of input data contains integers () — the entertainment values of the movies.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the maximum total entertainment value that Kolya can get.
Example
input
6 5 2 2 3 2 5 4 6 4 3 2 1 1 1 1 6 6 6 -82 45 1 -77 39 11 5 2 2 3 2 5 4 8 2 1 1 -1 2 6 3 2 -8 8 -2 -1 9 0
output
2 0 60 3 0 7
Note
The first test case is explained in the problem statement.
In the second test case, it is optimal not to visit any movies.
In the third test case, it is optimal to visit movies with numbers , , , , so the total entertainment value of the visited movies will be .
解题思路
没发现式子的性质没做出来,好似喵。
假设从小到大选择了个下标,此时的结果是
可以发现结果中减去的部分只取决于最大的下标,因此可以从小到大枚举最后一个下标选什么,然后再从前面的下标中选出最大的个(此时已经选择了),来使得结果最大。因此可以开个堆来维护前缀的前个最大值以及他们的和。需要注意的是,如果那么我们永远不会选择这个元素,从上面的式子可以看出,如果不作为最大的下标,那么我们将删除结果不会变小,而如果作为最大的下标,那么删除且变小,结果会变小。总之删除结果一定不会变小。
AC代码如下,时间复杂度为:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int N = 2e5 + 10; 7 8 int a[N]; 9 10 void solve() { 11 int n, m, d; 12 scanf("%d %d %d", &n, &m, &d); 13 for (int i = 1; i <= n; i++) { 14 scanf("%d", a + i); 15 } 16 priority_queue<int, vector<int>, greater<int>> pq; 17 LL ret = 0, s = 0; 18 for (int i = 1; i <= n; i++) { 19 if (a[i] <= 0) continue; 20 s += a[i]; 21 if (pq.size() == m) { 22 s -= pq.top(); 23 pq.pop(); 24 } 25 pq.push(a[i]); 26 ret = max(ret, s - 1ll * i * d); 27 } 28 printf("%lld\n", ret); 29 } 30 31 int main() { 32 int t; 33 scanf("%d", &t); 34 while (t--) { 35 solve(); 36 } 37 38 return 0; 39 }
参考资料
Codeforces Round #894 (Div.3) Editorial:https://codeforces.com/blog/entry/119715
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17656041.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效