Knights of a Polygonal Table CodeForces - 994B (贪心)

大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数

按战力排序后, 堆维护动态前k大即可

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll;

const int N = 2e5+10;
int n, m, k;
ll sum, a[N];
struct _ {int p, c, id;}q[N];

priority_queue<int,vector<int>,greater<int> > s;

int main() {
	s.push(0x7fffffff);
	scanf("%d%d", &n, &k);
	REP(i,1,n) scanf("%d",&q[i].p),q[i].id=i;
	REP(i,1,n) scanf("%d",&q[i].c);
	sort(q+1,q+1+n,[](_ a,_ b) {return a.p<b.p;});
	REP(i,1,n) {
		a[q[i].id] = sum+q[i].c;
		if (s.size()<=k) s.push(q[i].c),sum+=q[i].c;
		else if (s.top()<q[i].c) { 
			sum += q[i].c-s.top();
			s.pop();s.push(q[i].c);
		}
	}
	REP(i,1,n) printf("%lld ", a[i]);
	puts("");
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10398569.html