HDU 4864 Task(贪心) (机器完成目标任务, 两个权值, 小范围打表)


Task

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7171    Accepted Submission(s): 1891


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台机器, 每台机器都有两个 权值,  时间和 能力;

有m 个目标  每个目标也有两个权值  时间和 难度系数,     一天 一台机器只能干一个目标,问 这个n 台机器   做多同时 能干几个目标, 以及 赚的利润  利润等于

目标时间*500+ 目标系数*2;


思路:   因为 对于利润来说   时间因素要远远大于 难度系数 ; 因此 我们用 时间来排序, 当 时间相同时按照难度系数,   降序,  时间大再前面,   (目标和 机器也都要排序) 

 排序后    因为数据量是10w*10w  所以两层for 循环必然超时;

 因此., 我们只用一个for循环,来操作,  以目标为准,  扫描 大于当前目标的所有机器,  选择一个 能力最接近目标的 一台机器 (贪心)  让同等能力下,最弱的 去干掉目标,


问题在于怎么去找这个 最小的,  扫描必然会超时,,  所以  打表无疑是做好的,   用一个数组 记录所有 时间比他强的   以能力作为下标 存个数;

 以目标的系数为基准, 开始扫描 找到第一个 大于等于他的机器  ,



#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
#include <cstring>

typedef long long ll;
using namespace std;

struct machine{
    int t;
    int l;
    int flag;
}a[100010],b[100010];

int cmp2(machine a,machine b)
{
    if(a.t==b.t)
        return a.l>b.l;
    return a.t>b.t;
}

int main()
{
    int n,m;

    while(~scanf("%d %d",&n,&m))
    {
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
            scanf("%d %d",&a[i].t,&a[i].l);
        for(int i=0;i<m;i++)
            scanf("%d %d",&b[i].t,&b[i].l);
        sort(a,a+n,cmp2);
        sort(b,b+m,cmp2);
        ll sum=0,cont=0;
        int i,j;
    //    for(i=0;i<n;i++)
    //    {
    //        printf("<%d %d>\n",a[i].t,a[i].l);
    //    }
    //    for(i=0;i<m;i++)
    //    {
    //        printf("| %d %d |\n",b[i].t,b[i].l);
    //    }
        int c[10100];
        memset(c,0,sizeof(c));
        for(i=0,j=0;j<m;j++)
        {
            while(a[i].t>=b[j].t&&i<n)
            {
                c[a[i].l]++;
                i++;
            }
            int k=b[j].l;
            int flag=1;
            while(k<1000&&flag)
            {
                if(c[k])
                {
                    c[k]--;
                    sum+=500*b[j].t+2*b[j].l;
                    cont++;
                    flag=0;
                }
                k++;
            }
        }
        printf("%lld %lld\n",cont,sum);
    }
    return 0;
}
/*
4 5
100 4
100 3
100 2
80 5
100 1
80 3
90 2
70 4
60 3
*/


123

posted @ 2017-05-23 14:13  Sizaif  阅读(220)  评论(0编辑  收藏  举报