首页 > 其他分享 >E. Kolya and Movie Theatre

E. Kolya and Movie Theatre

时间:2023-08-25 09:48:21浏览次数:40  
标签:le Kolya Theatre cdot Movie value movies movie test

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 $n$ days. So, on the day with the number $1 \le i \le n$, the movie theatre will show the premiere of the $i$-th movie. Also, Kolya found out the schedule of the movies and assigned the entertainment value to each movie, denoted by $a_i$.

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 $d \cdot cnt$, where $d$ is a predetermined value and $cnt$ 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 $0$. So if we visit the movie theatre the first time on the day with the number $i$, then $cnt$ — the number of days since the last visit to the movie theatre will be equal to $i$.

For example, if $d = 2$ and $a = [3, 2, 5, 4, 6]$, then by visiting movies with indices $1$ and $3$, $cnt$ value for the day $1$ will be equal to $1 - 0 = 1$ and $cnt$ value for the day $3$ will be $3 - 1 = 2$, so the total entertainment value of the movies will be $a_1 - d \cdot 1 + a_3 - d \cdot 2 = 3 - 2 \cdot 1 + 5 - 2 \cdot 2 = 2$.

Unfortunately, Kolya only has time to visit at most $m$ 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 $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$, and $d$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le n$, $1 \le d \le 10^9$).

The second line of each set of input data contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$) — the entertainment values of the movies.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

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 $2$, $3$, $5$, $6$, so the total entertainment value of the visited movies will be $45 - 6 \cdot 2 + 1 - 6 \cdot 1 + 39 - 6 \cdot 2 + 11 - 6 \cdot 1 = 60$.

 

解题思路

  没发现式子的性质没做出来,好似喵。

  假设从小到大选择了$k$个下标$i_1, i_2, \ldots, i_k$,此时的结果是$$\begin{align*}&(a_{i_1} - d \cdot i_1) + (a_{i_2} - d \cdot (i_2 - i_1)) + \ldots + (a_{i_k} - d \cdot (i_k - i_{k-1})) \\ = \ &(a_{i_1} + a_{i_2} + \ldots + a_{i_k}) - d \cdot (i_1 + i_2 - i_1 + i_3 - i_2 + \ldots + i_k - i_{k-1}) \\ = \ &(a_{i_1} + a_{i_2} + \ldots + a_{i_k}) - d \cdot i_k \end{align*}$$

  可以发现结果中减去的部分只取决于最大的下标,因此可以从小到大枚举最后一个下标$k$选什么,然后再从前面的下标$i \in [1, k-1]$中选出最大的$m-1$个$a_i$(此时已经选择了$a_k$),来使得结果最大。因此可以开个堆来维护前缀的前$m-1$个最大值以及他们的和。需要注意的是,如果$a_k \leq 0$那么我们永远不会选择这个元素,从上面的式子可以看出,如果$k$不作为最大的下标,那么我们将$a_k$删除结果不会变小,而如果$k$作为最大的下标,那么删除$a_k$且$d \cdot i_k$变小,结果会变小。总之删除$a_k$结果一定不会变小。

  AC代码如下,时间复杂度为$O(n \log {m})$:

 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

标签:le,Kolya,Theatre,cdot,Movie,value,movies,movie,test
From: https://www.cnblogs.com/onlyblues/p/17656041.html

相关文章

  • movielens数据集分析python
    Movielens数据集分析Python实现概述本文将介绍如何使用Python对Movielens数据集进行分析。Movielens是一个常用的电影评分数据集,包含了用户对电影的评分、电影信息和用户信息等数据。通过对这个数据集的分析,我们可以探索用户对电影的评分情况,了解用户和电影的特征,并进一步进行推......
  • MATLAB模糊C均值聚类FCM改进的推荐系统协同过滤算法分析MovieLens电影数据集
    全文链接:http://tecdat.cn/?p=32594原文出处:拓端数据部落公众号在当今信息爆炸的时代,电影作为人们生活中不可或缺的娱乐方式,受到了越来越多的关注。而为了让观众能够更好地选择适合自己口味的电影,推荐系统成为了一个备受关注的研究领域。协同过滤算法是其中一种被广泛使用的方法......
  • moviepy操作
      1.首先下载安装moviepy  使用指令pipinstallmoviepy(加源)  注:可以选择在terminal安装,也可以在cmd安装 个人喜欢在cmd安装 有时候pycharm安装需要重启软件 2.导入俩个库importrequestsfrommoviepyimport*   3.首先爬取一个视频并且保存伪装......
  • python - moviepy音频剪切与拼接
    pip3installmoviepy-ihttps://pypi.tuna.tsinghua.edu.cn/simplefrommoviepy.audio.io.AudioFileClipimportAudioFileClipfrommoviepy.editorimportconcatenate_audioclipsa=AudioFileClip('a.mp3')#读入音频audio1=a.subclip(0,83)#剪切0-83秒......
  • Movie collection UVA - 1513
    有n个影碟,标号为1~n,位置为0~n-1,每次取出一个影碟看完后,将其放在最前面(标号为0处),问每个影碟取出前,其位置之前有多少个影碟 开2倍数组,"i放置前面"这个操作add(i,-1),add(newi,1)  #include<iostream>#include<cstring>#include<algorithm>#include<vector>usingn......
  • Theatres in the World
    MariinskyII新国立劇場梅田芸術劇場MesaArtsTheater盛京大剧院哈尔滨大剧院......
  • js mouse movie获取坐标
    document.addEventListener('mousemove',function(e){//mousemove鼠标一移动,就会触发事件//获取鼠标最新的坐标console.log("y:",e.clientY......
  • B. Kolya and Tandem Repeat -- codeforces
    B.KolyaandTandemRepeathttps://codeforces.com/problemset/problem/443/B 思路如果补充字符长度k大于等于s长度,则新的字符串,一份两半,前半分包括s,可能包括部分补......
  • LeetCode-SQL-620. Not Boring Movies
    题目Xcityopenedanewcinema,manypeoplewouldliketogotothiscinema.Thecinemaalsogivesoutaposterindicatingthemovies’ratingsanddescriptions......
  • moviepy模拟视频拼接叠化转场效果
    在网上搜了一圈,没有找到类似【剪映】中两个视频拼接的时候叠化转场的效果,就这种:  那就自己动手实现一个吧比如有两个视频,a.mp4和b.mp4叠化转场效果看起来就像a......