题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1572
题目大意:
约翰接到了N 份工作,每份工作恰好占用他一天的时间。约翰从第一天开始工作,他可以任意安排这些工作的顺序,第i 份工作有Pi 的报酬,但必须在第Di 天结束之前完成。在截止日期后完成的工作没有报酬。请帮助约翰规划每天的工作,使得他赚到的钱最多。
题解:
贪心
先按截止日期排个序。
对于每份工作都先“来者不拒”,但如果接受工作的份数大于天数,就是说无法在截止日期前把每份接受的工作都完成了,那么这个时候就要舍弃一些。贪心的思想嘛~就舍弃那些工资低的。用优先队列来维护就好了。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
#define maxn 101000
struct node
{
LL d,x;
friend bool operator < (node x,node y)
{
if (x.d!=y.d) return x.d<y.d;
return x.x<y.x;
}
}a[maxn];
priority_queue<LL,vector<LL>,greater<LL> > q;//按从小到大排
int main()
{
//freopen("job.in","r",stdin);
//freopen("job.out","w",stdout);
LL n,i,tot=0,ans=0;
scanf("%lld",&n);
for (i=1;i<=n;i++)
scanf("%lld%lld",&a[i].d,&a[i].x);
sort(a+1,a+1+n);
for (i=1;i<=n;i++)
{
ans+=a[i].x;tot++;q.push(a[i].x);
if (tot>a[i].d) {ans-=q.top();q.pop();tot--;}//把工资最低的减掉
}printf("%lld\n",ans);
return 0;
}