0
点赞
收藏
分享

微信扫一扫

tokitsukaze and Soldier 优先队列+排序

boom莎卡拉卡 2022-02-15 阅读 35
算法c++

登录—专业IT笔试面试备考平台_牛客网

对各种STL容器还是不太熟悉以及自己的贪心策略总是太想当然了。QAQ

优先队列默认从大到小,加上greater以后从小到大。

优先队列用法 - skyli - C++博客

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define PII pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define rep2(i, l, r) for (int i = l; i * i <= r; i++)
#define rep3(i, l, r) for (LL i = l; i * i * i <= r; i++)
#define Min(a, b) a > b ? b : a
#define Max(a, b) a > b ? a : b
#define endl '\n'
#define debug "-----"
using namespace std;
typedef long long LL;
const LL mod = 1e9;
const int N = 1e5 + 10, M = 10100;
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }

priority_queue<LL, vector<LL>, greater<LL>> pq;
struct Node
{
	int v,s;
}sol[N];

bool cmp( Node a , Node b ){
	return a.s > b.s;
}

int main()
{
	IOS;
	int n;
	cin >> n;
	rep( i , 1 , n+1 ) 
		cin >> sol[i].v >> sol[i].s;
	sort( sol+1 , sol+1+n , cmp );
	LL ans = 0 , res = 0;
	rep( i , 1 , n+1 ){
		res += sol[i].v;
		pq.push( sol[i].v );
		while( pq.size() > sol[i].s ){
			res -= pq.top();
			pq.pop();
		}
		ans = max( res , ans );
	}
	cout << ans;
	return 0;
}
举报

相关推荐

0 条评论