Codeforces - Playlist

题目链接:Codeforces - Playlist


最开始以为具有三分性,然后就三分了,就wa了,因为存在k,所以三分性是错的。

然后我们想,如果我们知道当前选的最小的魅力值,那么肯定选k个长度最大的,并且魅力值大于最小的。

然后所以我们需要一直维护k个最大值,用个小根堆即可。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+10;
int n,k,res,sum;
struct node{int l,v;}t[N];
int cmp(node a,node b){return a.v==b.v?a.l>b.l:a.v>b.v;}
priority_queue<int,vector<int>,greater<int> > q;
signed main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++)	scanf("%lld %lld",&t[i].l,&t[i].v);
	sort(t+1,t+1+n,cmp);
	for(int i=1;i<=n;i++){
		if(q.size()<k)	q.push(t[i].l),sum+=t[i].l;
		else if(q.top()<t[i].l)	sum+=t[i].l-q.top(),q.pop(),q.push(t[i].l);
		res=max(res,sum*t[i].v);
	}
	cout<<res;
	return 0;
}
发布了725 篇原创文章 · 获赞 244 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104861483