[51nod]#1163最高的奖励

https://www.51nod.com/Challenge/Problem.html#!#problemId=1163
区间贪心

纯暴力解法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=50000+100;
struct node
{
	int end;
	ll cost;
} a[maxn];
bool cmp(node x,node y)
{
	if(x.cost>y.cost) return true;
	return false;
}
int book[maxn];
int main()
{
	int n,i,j,k;
	ll ans;
	ans=0;
	cin>>n;
	for(i=0;i<n;i++) 
		cin>>a[i].end>>a[i].cost;
	sort(a,a+n,cmp);
	for(i=0;i<n;i++) {
		k=a[i].end;
		while(k) {
			if(book[k]==0) {
				book[k]=1;
				ans+=a[i].cost;
				break;
			}
			k--;
		}
	}
	cout<<ans<<endl;
	return 0;
}

结果会爆int记得把结果开成long long

贪心+优先队列

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; 
const int maxn=50000+10;
pair<int,int>a[maxn];//a[i].first 最晚结束时间E[i]; a[i].second 奖励W[i]
 
int main() {
	int n;
	while(cin>>n) {
		for(int i=0;i<n;i++){
			cin>>a[i].first>>a[i].second;
		}
		sort(a,a+n);//按照权值排序;
		priority_queue<int>q; //大根堆 从小到大排序
		//每次需要加入相反数,top出绝对值小的 
		long long ans=0;
		for(int i=0;i<n;i++){
			q.push(-a[i].second);
			ans+=a[i].second;
			if(q.size()>a[i].first){
				int x=q.top();//负的,最小的,多余的舍弃
				ans+=x;
				q.pop(); 
			} 
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值