首页 > 代码库 > HDU 4864 Task(基本算法-贪心)

HDU 4864 Task(基本算法-贪心)

Task


Problem Description
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 (500*xi+2*yi) 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
 

Author
FZU
 

Source
2014 Multi-University Training Contest 1
 

Recommend
We have carefully selected several similar problems for you:  4906 4904 4903 4902 4901 
 

题目大意:

有n台机器,m个任务,每台机器有xi,yi,每个任务也有xj,yj,当一个任务可以被处理的条件是,xj<=xi 且 yj<yi,处理完产生 500*xj+2*yj 的价值,问你最多产生的价值是多少?


解题思路:

注意y的范围是 y<100,也就是x相差1,y不管相差多少价值都很少。

根据贪心的做法,肯定从高价值物品生产也就是按x排好序,再贪心,高价值的物品只需要在x比它大的所有机器中选择y满足条件的最小的那个(这个思考一下)


解题代码:

#include <iostream>
#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=110000;

struct node{
    int x,y;
    friend bool operator < (node a, node b){
        if(a.y!=b.y) return a.y<b.y;
        else return a.x<b.x;
    }
};

int n,m;
node a[maxn],b[maxn];

bool cmp(node a,node b){
    if(a.x!=b.x) return a.x>b.x;
    else return a.y>b.y;
}

void solve(){
    multiset <node> mys;
    sort(a,a+n,cmp);
    sort(b,b+m,cmp);
    int r=0,cnt=0;
    long long ans=0;
    for(int i=0;i<m;i++){
        while(r<n && a[r].x>=b[i].x) mys.insert(a[r++]);
        multiset <node>::iterator it=mys.lower_bound(b[i]);
        if(it!=mys.end()){
            mys.erase(it);
            cnt++;
            ans+=b[i].x*500+b[i].y*2;
        }
    }
    cout<<cnt<<" "<<ans<<endl;
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y);
        for(int i=0;i<m;i++) scanf("%d%d",&b[i].x,&b[i].y);
        solve();
    }
    return 0;
}