hdu 4864 Task 贪心

题意:有n个机器。m个任务。每一个机器至多能完毕一个任务。对于每一个机器。有一个最大执行时间xi和等级yi。对于每一个任务,也有一个执行时间xj和等级yj。仅仅有当xi>=xj且yi>=yj的时候。机器i才干完毕任务j。并获得500*xj+2*yj金钱。问最多能完毕几个任务,当出现多种情况时,输出获得金钱最多的情况。

题解:

将任务已x从大到小排序(x同样时已y从大到小排序)。然后也用同样排序方法排序机器。開始遍历任务。找出全部xi(xi>=xj),从中选择yi最小的一个作为这个任务的执行机器。为什么这么贪心,由于若还存在任务(xk,yk)使得这个机器能被使用。但xj>=xk,所以获得金钱很多其它,优先选择j;若k不能使用这个机器,那么必然也就不存在其它机器能被使用,除非是新增加的机器,但新增加的必然不能完毕任务j。所以完毕任务保证了最多。



代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

#define LL __int64
const int maxn=1e5+10;
struct node{
    int x,y;
}e[maxn],f[maxn];
int c[101];
int cmp(node a,node b)
{
    if(a.x==b.x)return a.y>b.y;
    return a.x>b.x;
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\×ÀÃæ\\in.txt","r",stdin);
    //freopen("C:\\Documents and Settings\\Administrator\\×ÀÃæ\\out.txt","w",stdout);
    //double a = clock();
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j,k,num=0;
        LL ans=0;
        for(i=0;i<n;i++)
            scanf("%d%d",&e[i].x,&e[i].y);
        for(i=0;i<m;i++)
            scanf("%d%d",&f[i].x,&f[i].y);
        sort(e,e+n,cmp);
        sort(f,f+m,cmp);
        memset(c,0,sizeof(c));
        for(i=0,j=0;i<m;i++)
        {
            while(j<n&&e[j].x>=f[i].x)
            {
                c[e[j].y]++;
                j++;
            }
            for(k=f[i].y;k<=100;k++)
            {
                if(c[k])
                {
                    num++;
                    c[k]--;
                    ans=ans+500*f[i].x+2*f[i].y;
                    break;
                }
            }
        }
        printf("%d %I64d\n",num,ans);
    }
    //double b = clock();
    //printf("%lf\n", (b - a) / CLOCKS_PER_SEC);
    return 0;
}


posted @ 2017-07-04 16:06  yangykaifa  阅读(148)  评论(0编辑  收藏  举报