0
点赞
收藏
分享

微信扫一扫

【牛客 - 1080C】tokitsukaze and Soldier(思维,偏序问题)

题干:

链接:​​https://ac.nowcoder.com/acm/contest/1080/C​​​ 来源:牛客网
 

在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本。
第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和。
但是这些士兵有特殊的要求:如果选了第i个士兵,这个士兵希望团的人数不超过s[i]。(如果不选第i个士兵,就没有这个限制。)
tokitsukaze想知道,团的战力最大为多少。

输入描述:


第一行包含一个正整数n(1≤n≤10^5)。 接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。

输出描述:


输出一个正整数,表示团的最大战力。

示例1

输入

复制


2 1 2 2 2

输出

复制


3

示例2

输入

复制


3 1 3 2 3 100 1

输出

复制


100

解题报告:

按s排个序,然后贪心选就行了,优先队列维护前s[i]大元素。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
struct Node {
int s;ll v;
bool operator<(const Node & b) const {
return s > b.s;//按照人数从大到小排序
}
} R[MAX];
struct NN {
int id;ll v;
NN(int id=0,ll v=0):id(id),v(v){}
bool operator<(const NN & b) const {
return v > b.v;
}
};
priority_queue<NN> pq;//小根堆
int main()
{
int n;
cin>>n;
for(int i = 1; i<=n; i++) {
scanf("%lld%d",&R[i].v,&R[i].s);
}
ll ans = 0,tmp = 0;
sort(R+1,R+n+1);
for(int i = 1; i<=n; i++) {
tmp += R[i].v;
pq.push(NN(i,R[i].v));
while(pq.size() > R[i].s) {NN cur = pq.top();pq.pop();tmp -= cur.v;}
ans = max(ans,tmp);
}
printf("%lld\n",ans);
return 0 ;
}

 


举报

相关推荐

0 条评论