33分求调(玄关)
查看原帖
33分求调(玄关)
836804
封禁用户楼主2025/6/20 18:33

返回贪心

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n;
struct node{
	int x,y;
};
node a[N];
bool operator < (node l,node r){
	return l.y<r.y;
}
bool cmp(node l,node r){
	return l.x<r.x;
}
priority_queue<node> q;
int t;
int ans;
signed main(){
	scanf("%lld",&n);
	for(int i=1; i<=n; i++){
		scanf("%lld%lld",&a[i].x,&a[i].y);
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1; i<=n; i++){
		if(t+1<=a[i].x){
			t++;
			q.push({i,a[i].y});
			ans+=a[i].y;
		}
		else{
			if(q.top().y<a[i].y){
				ans-=q.top().y;
				q.pop();
				q.push({i,a[i].y});
				ans+=a[i].y;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}
2025/6/20 18:33
加载中...