2014 Multi-University Training Contest 1 D(hdu 4864 经典贪心)

来源:互联网 发布:iphone导出照片软件 编辑:程序博客网 时间:2024/05/18 09:51

题目链接:

hdu 4864


题目输入输出:

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个任务。每个机器至多能完成一个任务。对于每个机器,有一个最大运行时间xi和等级yi,对于每个任务,也有一个运行时间xj和等级yj。只有当xi>=xj且yi>=yj的时候,机器i才能完成任务j,并获得500*xj+2*yj金钱。问最多能完成几个任务,当出现多种情况时,输出获得金钱最多的情况。


思路:

将任务已x从大到小排序(x相同时已y从大到小排序)。然后也用相同排序方法排序机器。开始遍历任务,找出所有xi(xi>=xj),从中选择yi最小的一个作为这个任务的运行机器。500*xj+2*yj,所以yi可以当做次要因素,主观因素是时间。所以对任务和机器首先按时间大小,再按等级大小排序。


代码:

#include <stdio.h>#include <algorithm>#define Max 100002using namespace std;struct node{    int x,y;};bool cmp(node a,node b){    if(a.x==b.x)return a.y>b.y;    else return a.x>b.x;}node mac[Max],tas[Max];int main(){    int n,m;    while(scanf("%d %d",&n,&m)!=EOF)    {        long long ans=0;        int i,j,k;        for(i=0;i<n;i++)        {            scanf("%d %d",&mac[i].x,&mac[i].y);        }           for(i=0;i<m;i++)        {            scanf("%d %d",&tas[i].x,&tas[i].y);        }        sort(mac,mac+n,cmp);        sort(tas,tas+m,cmp);        int num[102]={0};             //用桶装十分巧妙         int cnt=0;        for(i=0,j=0;i<m;i++)            {            while(j<n&&mac[j].x>=tas[i].x)            {                num[mac[j].y]++;       //后面的加进来的任务必定可以被先前加进来的机器所完成                 j++;            }            for(k=tas[i].y;k<=100;k++)            {                if(num[k])                {                    num[k]--;                    cnt++;                    ans+=500*tas[i].x+2*tas[i].y;                    break;                }            }        }        printf("%d %lld\n",cnt,ans);    }    return 0;}
原创粉丝点击