JOB JOB JOB——优先队列 + 贪心

1397: Job!Job!Job!


Description
Eric 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 choose 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. 

Input
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.

Output

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

Sample Input
4 10
1 8
1 3
2 10
5 12
Sample Output
30



































在给定的时间m内  在m时间发生的事情最多选取 m个 

例如时间为1s 那么在1s时同时有n个事情 但是最多只能做1个事情 

2s最多只能做两个2s的事情

当为3s时 前两秒的事情就不能做了 。。。

思路好乱啊。。。。






#include <stdio.h>
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

struct Node
{
	int v,d;
}num[100010];

int cmp(Node x,Node y)
{
    return x.d<y.d;
}
priority_queue<int >q;

int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF){
		for(int i=0;i<n;i++)
		{
			scanf("%d %d",&num[i].d,&num[i].v);
		}
		sort(num,num+n,cmp);
		while(!q.empty()) q.pop();
		int l=n-1;
		int ans=0;
		for(int i=m;i>0;i--)
		{
			while(num[l].d >=i && l>=0) q.push(num[l--].v);
			if(!q.empty())
			{
				ans+=q.top();
				q.pop();
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值