比赛 20160909 评测结果 AAAAAAATTT
题目名称 超级钢琴 最终得分 70
用户昵称 农场主 运行时间 8.729 s
代码语言 C++ 内存使用 109.38 MiB
提交时间 2016-09-09 21:58:37
显示代码纯文本
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#define maxn 1000000
using namespace std;
typedef long long ll;
ll sum[maxn]={0};
int a[maxn]={0};
int next[maxn]={0};
int w[maxn]={0};
int vis[maxn]={0};
int n,k,l,r;
bool cmp(int a,int b){
	return sum[a]>sum[b];
}
class poi{
public:
	ll a;
	int b;
	bool operator () (poi x, poi y) {
		if (x.a==y.a){
			return sum[x.b]>sum[y.b];
		}
		else return x.a>y.a;
	}
};
priority_queue<poi,vector<poi>,poi> pq;
void read(){
	scanf("%d%d%d%d",&n,&k,&l,&r);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum[i]+=sum[i-1]+(ll)a[i];
		next[i]=i+1;
		//printf("%d %d\n",i,next[i]);
		w[i]=i;
	}
	next[0]=1;
	next[n]=n+1;
}
int find(int x){
	return vis[next[x]]==0?next[x]:next[x]=find(next[x]);
}
void work(){
	read();
	sort(w+1,w+n+1,cmp);
	int tot=0;
	for (int i=1;i<=n;i++){
		for (int j=max(0,w[i]-r);j<=w[i]-l;j=next[j]){
			//printf("%d %d\n",j,next[j]);
			poi p;
			p.a=sum[w[i]]-sum[j]; p.b=j;
			pq.push(p);
			tot++;
			if (tot>k){
				p=pq.top();
				pq.pop();
				vis[p.b]=1;
				next[p.b]=find(p.b);
			}
		}
	}
	ll ans=0;
	while (!pq.empty()){
		poi p=pq.top();
		ans+=p.a;
		pq.pop();
	}
	printf("%lld",ans);
}
int main(){
	freopen("piano.in","r",stdin);
	freopen("piano.out","w",stdout);
	work();
	return 0;
}