TOJ 3573: Job!Job!Job!

描述

Foreverlin is working in a company. In order to make boss happier, he must work as hard as possible, there are n projects on the todolist. Now is time 1, after time m ,foreverlin has to go back to the school .each project has two properties , the finally completion time and the value you can make if you finish this project . At every unit of time ,he can choose a project to finish . However, he can only change one project to do in one unit time, that means in one unit time ,he can choose a project to do and finish in this unit time. As the best friend of him ,can you help him to find out how to arrange these projects so that he can make the biggest values.

输入

There are several test cases, in each test case, there are two numbers n,m(1<=n<=100000,1<=m<=1000000) . The next n lines each contains two number D[i],V[i] (1 <= D[i] <= 100000,1<=V[i]<=10000) (1<=i<=n ,D[i] means if you choose to do project i ,you can not do this after time D[i],V[i] means the value of project i ) The input will finish with the end of file.

输出

For each the case ,ouput a number means the biggest values.

样例输入

4 10
1 8
1 3
2 10
5 12

样例输出

30

题目大意:
F先生有N项任务,每项任务有最后截至时间和完成任务可获得的价值(一个时间只能选一个任务完成,截止时间过后任务无效),但F先生得在M时间后回学校(时间从1开始)。问,F先生最多可以在回去前获得的最大价值是多少?
思路:
逆着来,先将每个任务按截至时间排序(降序或升序都可以),然后从M开始一直到1,如果还有任务,则将截至时间大于等于当前时间的任务的价值放进优先队列(降序),接着把队首的价值累加给总价值,然后一直重复着过程;
因为任务在截止时间后就不能接了,就相当与不存在了,所以就逆着来,把截止时间看作到了这个点就可以接这个任务,在每个时间接完任务后从已有的任务中选出价值最大的完成(这个工作由优先队列完成)。
总体来说是贪心;
代码:

#include<bits/stdc++.h>
using namespace std;
int read()
{
    int re=0;
    char ch;
    ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        re=(re<<3)+(re<<1)+ch-'0';
        ch=getchar();
    }
    return re;
}
struct node
{
    int ed,val;
};
node jb[100001];
priority_queue<int>pq;
bool cmp(node a,node b)
{
    return a.ed>b.ed;
}
int main()
{
    int n,m,sm,ed,pos;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sm=0;
        for(int i=0;i<n;i++)
        {
            jb[i].ed=read();
            jb[i].val=read();
        }
        sort(jb,jb+n,cmp);
        pos=0;
        for(int i=m;i;i--)
        {
            while(pos<n&&jb[pos].ed>=i)pq.push(jb[pos++].val);
            if(!pq.empty())
            {
                sm+=pq.top();
                pq.pop();
            }
        }
        while(!pq.empty())pq.pop();
        printf("%d\n",sm);
    }
    return 0;
}

看了下学长的题解发现还可以正着贪心(遍历任务),优先队列升序,每次丢掉一个小于当前任务价值(此任务可选)的任务价值,最后再把队列里的价值加起来

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值