HDU 4864 Task 贪心+二分

Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task’s level yi cannot complete this task. If the company completes this task, they will get (500xi+2yi) dollars.
The company has n machines. Each machine has a maximum working time and a level. If the time for the task is more than the maximum working time of the machine, the machine can not complete this task. Each machine can only complete a task one day. Each task can only be completed by one machine.
The company hopes to maximize the number of the tasks which they can complete today. If there are multiple solutions, they hopes to make the money maximum.

Input

The input contains several test cases.
The first line contains two integers N and M. N is the number of the machines.M is the number of tasks(1 < =N <= 100000,1<=M<=100000).
The following N lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the maximum time the machine can work.yi is the level of the machine.
The following M lines each contains two integers xi(0<xi<1440),yi(0=<yi<=100).xi is the time we need to complete the task.yi is the level of the task.

Output

For each test case, output two integers, the maximum number of the tasks which the company can complete today and the money they will get.

Sample Input

1 2
100 3
100 2
100 1

Sample Output

1 50004

思路:

根据已知条件,每胜任一个工作可以拿到x500+2y的钱,那么我们要想总收益最大,肯定要尽可能选x值大的工作,所以将工作按照先x后y的顺序排序。

接下来遍历所有的工作,对每一个工作,利用二分查找函数lower_bound去查找机器中第一个x值大于等于该工作的x位置,将这些机器从头到尾加入到一个容器multiset中,然后在这个容器中二分查找第一个y值大于等于该工作的机器位置,如果没找到,说明该工作无机器可做,继续循环到下一个机器位置
为了不会使得机器重复被加到容器中,我们定义一个变量last
,这样每次操作完后,last会更新到x1-1的位置(原来的区间是【x1,last】,这段区间必定会满足下一个工作,所以这个区间可以舍弃不要,优化程序)

说的可能比较啰嗦,具体细节见代码~
Code:

#include<bits/stdc++.h>
#define int long long
typedef long long ll;
#define js ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
struct node{
	int x,y;
	bool operator<(const node &a) const{
		return y<a.y;
	}
};
node ma[100005],wo[100005];
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x<b.x;
	else return a.y<b.y;
}
bool cmp1(node a,node b){
	return a.x<b.x;
}
multiset<node> ss;
signed main(){
    js;
	int n,m;
	while(cin>>n>>m){
		ss.clear();
		for(int i=0;i<n;i++){
			cin>>ma[i].x>>ma[i].y;
		}
		for(int i=0;i<m;i++){
			cin>>wo[i].x>>wo[i].y;
		}
		sort(ma,ma+n,cmp);
		sort(wo,wo+m,cmp);
		int sum=0,cnt=0,last=n-1;
		for(int i=m-1;i>=0;i--){
			int x1=lower_bound(ma,ma+n,wo[i],cmp1)-ma;
			if(x1==n) continue;
			for(int j=x1;j<=min(n,last);j++){
				ss.insert(ma[j]);
			}
			last=x1-1;
			if(ss.empty()) continue;
			auto x2=ss.lower_bound(wo[i]);
			if(x2==ss.end()) continue;
			ss.erase(x2);
			cnt++;
			sum+=wo[i].x*(ll)500+wo[i].y*(ll)2;
			
		}
		cout<<cnt<<" "<<sum<<endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值