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

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("");
}

 

以上是关于Knights of a Polygonal Table CodeForces - 994B (贪心)的主要内容,如果未能解决你的问题,请参考以下文章

CF994B Knights of a Polygonal Table 第一道 贪心 set/multiset的用法

Codeforces 994B. Knights of a Polygonal Table

POJ 2942Knights of the Round Table(二分图判定+双连通分量)

UvaLive3523 Knights of the Round Table(点双联通分量+二分图染色)

[Usaco2005 Dec]Knights of Ni 骑士

poj 2942 Knights of the Round Table - Tarjan