【USACO】2009 Open Work Scheduling 工作安排

来源:互联网 发布:linux traceroute没有 编辑:程序博客网 时间:2024/05/21 09:48

Work Scheduling 工作安排


  • Description

为了让农场有效运转,约翰必须靠自己工作来赚钱。他接到了N项任务,完成每项任务 都要花费一个单位的时间。第i项任务的截止时间为Di,如果能按时完成,可以得到报酬Pi。约翰从0时刻开始工作,由于他在同一个时间内只能从事一项任务,所以他很难按时完成所 有任务。请帮助约翰计算一下,他最多可以赚多少钱?

  • Input Format

第一行:单个整数:N,1≤N≤10^5第二行到N+1行:在第i+1行有两个用空格分开的整数:Di和Pi,1≤Di, Pi≤10^9

  • Output Format

第一行:单个整数,表示约翰可赚得的最多钱

  • Sample Input

3
2 10
1 5
1 7

  • Sample Output

17

  • Hint

先做第三个任务,再做第一个任务


  • 分析

贪心思想,能按时完成的就按时完成,不能按时完成的就把之前价值最小的和当前作比较,取最优的情况。


#include <queue>#include <stack>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Data{long long d,p;}a[100005];priority_queue <Data> q;inline bool cmp(const Data&a,const Data&b){return a.d<b.d;}inline bool operator < (Data a,Data b){return a.p>b.p;}int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    long long n,ans=0;    scanf("%lld",&n);    for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].d,&a[i].p);    sort(a+1,a+1+n,cmp);    for (int i=1;i<=n;i++)        if (a[i].d<=q.size()){if (q.top().p<a[i].p) ans+=a[i].p-q.top().p,q.pop(),q.push(a[i]);}        else q.push(a[i]),ans+=a[i].p;    printf("%lld",ans);    fclose(stdin); fclose(stdout);    return 0;}
0 0