//贪心//不光要排序//有两个大坑的好题//task------二R

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

解题思路:
前几道题都是排序完,问题就基本解决了。可是这道题中排序只是第一步。
首先,因为这道题的数据很特殊,奖金的计算公式是500*时间+2*难度,而难度的变化范围是1到100,也就是说若优先按时间从大到小排序后,前面的任务和后面的任务,无论level的差距多大,完成前面的任务的奖金一定不低于完成后面的任务。
所以应该优先按时间排序,其次是难度。

然后我就直接用下标移动做,结果发现一直WA,到网上去搜,发现一篇帖子说的特别好,想了很半天终于理解了,就直接复制上来吧。http://www.mamicode.com/info-detail-873459.html

接着:我是用2个下标分别指向任务的开头和机器的开头,然后不断后移,贪心选取可以完成的任务。
但是这样无法得到最优,给组数据
任务:(90,90),(50,100)
机器:(100,100),(90,90)

所以要怎么办呢?
根据贪心,我们排序任务和机器后,应该做的是:
对于每一个任务:找出可以完成这个任务的所有机器中,level最低的那一个来完成。
因为这些机器的时间满足这个任务,必然也满足这个任务后面所有的任务,但是可能后面的任务的level非常大
所以我们要尽量保留level大的机器以备后面任务的使用。

#include<stdio.h>
#include<algorithm>
using namespace std;

struct node
{
    int t;
    int l;
}mac[100010],t[100010];

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

int main()
{
    int n,m,i,j,cnt,level[101];
    __int64 sum;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&mac[i].t,&mac[i].l);
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&t[i].t,&t[i].l);
        }
        sort(mac+1,mac+n+1,cmp);
        sort(t+1,t+m+1,cmp);
        i=1;
        j=i;
        cnt=0;
        sum=0;
        memset(level,0,sizeof(level));
        for(i=1;i<=m;i++)  
        {  
            while(j<=n&&mac[j].t>=t[i].t)level[mac[j++].l]++;  
            for(int lev=t[i].l;lev<=100;lev++)  
            {
                if(level[lev])  
                {
                    cnt++;   
                    sum+=500*t[i].t+2*t[i].l;   
                    level[lev]--;  
                    break;  
                }  
            }
        }
        printf("%d %I64d\n",cnt,sum);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值