CF994B 题解
题面
CF994B Knights of a Polygonal Table
solution
一道贪心题。
设当前枚举到的骑士为
如果要在一个乱序的序列里找到所有
然后就是经典的贪心了,一个优先队列维护所有的最优决策点,然后一个变量维护这个最优决策点里所有
由于一名骑士最多杀死
显然我们动态维护的变量就是
正确性:堆中的每一个决策显然都是对于
Code
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int o=2222222;
struct node{
int p;
int c;
int id;
int ans;
}a[o];
struct cmp{
bool operator ()(node a,node b){
return a.c>b.c;
}
};
priority_queue<node,vector<node>,cmp>q;
int n,k;
bool cmp1(node c,node b){
return c.p<b.p;
}
bool cmp2(node c,node b){
return c.id<b.id;
}
void in(){
scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i].p);
}
for(int i=1;i<=n;i++){
scanf("%lld",&a[i].c);
}
for(int i=1;i<=n;i++){
a[i].id=i;
}
}
void work(){
sort(a+1,a+n+1,cmp1);
int now=0;
for(int i=1;i<=n;i++){
a[i].ans=now+a[i].c;
q.push(a[i]);
now+=a[i].c;
while(q.size()>k){
now-=q.top().c;
q.pop();
}
}
sort(a+1,a+n+1,cmp2);
}
void out(){
for(int i=1;i<=n;i++){
printf("%lld ",a[i].ans);
}
}
signed main(){
in();
work();
out();
return 0;
}
update
可以看下[TJOI2013] 奖学金。
如果做过,那么这个题就非常显然了。
本题于
广告位