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;
}