poj 2786 Keep the Customer Satisfied(贪心+优先队列)


Input

The first line contains the number n of orders (n can be as large as 800000 for some test cases). It is followed by n lines. Each of which describes an order made of two integer values: the amount of steel (in tons) required for the order (lower than 1000) and its due date (in seconds; lower than 2 x 10 6 ).

Output

You are required to compute an optimal solution and your program has to write the number of orders that are accepted.

Sample Input

6
7 15
8 20
6 8
4 9
3 21
5 22

Sample Output

4
<a target=_blank href="http://poj.org/problem?id=2786">http://poj.org/problem?id=2786</a>
按照任务的截止时间排序,然后使用优先队列,每次累加任务消耗时间,如果所用时间超过了限定时间,则从优先队列中取出时间消耗最大的任务。

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
int n;
pair<int,int> x[800001]; 
int cmp(pair<int,int>a,pair<int,int>b){
	return a.second<b.second;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%d %d",&x[i].first,&x[i].second);//需要的工作时间和截止日期 
	}
	sort(x,x+n,cmp);   //按截止日期排序 
	int s=n,sum=0;
	priority_queue<int> q;  //默认是从大到小排序(需要的工作时间)
	for(int i=0;i<n;++i){
		q.push(x[i].first);
		sum+=x[i].first;
		if(sum>x[i].second){
			s--;
			sum-=q.top();  //舍去需要的工作时间最久的 
			q.pop(); 
		}
	}
	printf("%d",s);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值