HDU-4864 Task(贪心)

24 篇文章 0 订阅

Task

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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

题目大意:有n台机器,每台机器都有x值和y值,有m个任务,每个任务都有x值和y值,每完成一个任务,会获得500*x+2y(任务的x值和y值)元,机器和任务都最多使用一次,求最多能获得多少元?

解法一:排序+贪心

大致思路:很容易就能想到排序贪心,但是贪心还有技巧,否则就不能取得最大值

首先x值大于任务的机器均可以使用一次,若存在x大于任务的机器,则该任务必定会执行,因为500*x不会小于后面任务所得,则难点就在如何在众多符合要求的机器中选择

此时再考虑y值,由于未被选择的机器可以在后面的任务中使用,所以为了得到最优解,当前任务必定匹配y值大于它的y值且最接近的那一台机器


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN=100005;

struct Node {
    int x,y;

    bool operator < (const Node& a) const {
        return x<a.x||(x==a.x&&y<a.y);
    }
}mach[MAXN],task[MAXN];

int n,m,cnt,y[105];
long long ans;

int main() {
    while(scanf("%d%d",&n,&m)==2) {
        for(int i=0;i<n;++i) {
            scanf("%d%d",&mach[i].x,&mach[i].y);
        }
        for(int i=0;i<m;++i) {
            scanf("%d%d",&task[i].x,&task[i].y);
        }
        sort(mach,mach+n);
        sort(task,task+m);
        ans=cnt=0;
        memset(y,0,sizeof(y));
        for(int i=m-1,j=n-1;i>=0;--i) {
            while(j>=0&&mach[j].x>=task[i].x) {//统计x值大于任务的机器的y值出现的次数
                ++y[mach[j].y];
                --j;
            }
            for(int k=task[i].y;k<=100;++k) {//寻找第一个大于机器y值的y值
                if(y[k]!=0) {
                    ++cnt;
                    ans+=500*task[i].x+2*task[i].y;
                    --y[k];
                    break;
                }
            }
        }
        printf("%d %I64d\n",cnt,ans);
    }
    return 0;
}


解法二:排序+二分贪心

打多校时再次碰到,但只记得做过,忘了怎么做了,想了一会又想到另一种以来STL的方法

由于y最大只有100,所以可以对机器以y值为下标建立vector<int>,存储相应的x值,然后对其排序,从小到大枚举机器的y值,找到第一个符合的机器


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN=101;
const long long MOD=1e9+7;

struct Node {
    int x,y;

    bool operator < (const Node& a) const {
        if(x!=a.x) {
            return x>a.x;
        }
        return y>a.y;
    }
}cur,task[100005];

vector<int> q[105];
vector<int>::iterator it;

int n,m,cnt;
long long ans;

int main() {
    while(2==scanf("%d%d",&n,&m)) {
        for(int i=0;i<=100;++i) {
            q[i].clear();
        }
        for(int i=0;i<n;++i) {
            scanf("%d%d",&cur.x,&cur.y);
            q[cur.y].push_back(cur.x);
        }
        for(int i=0;i<m;++i) {
            scanf("%d%d",&task[i].x,&task[i].y);
        }
        for(int i=0;i<=100;++i) {
            sort(q[i].begin(),q[i].end());
        }
        sort(task,task+m);
        ans=cnt=0;
        for(int i=0;i<m;++i) {
            for(int j=task[i].y;j<=100;++j) {
                it=lower_bound(q[j].begin(),q[j].end(),task[i].x);
                if(it!=q[j].end()) {
                    ans+=500*task[i].x+2*task[i].y;
                    ++cnt;
                    q[j].erase(it);
                    break;
                }
            }
        }
        printf("%d %I64d\n",cnt,ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值