机器和任务

oday 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

 

import java.util.*;
class Ma_ta{
	int time;
	int lev;
	Ma_ta(int t,int l){
		this.time=t;
		this.lev=l;
	}
}
public class Main {
    static Scanner in =new Scanner(System.in);
    static Comparator<Ma_ta> com=new Comparator<Ma_ta>() {	
 		@Override
 		public int compare(Ma_ta o1, Ma_ta o2) {
 			if(o1.time==o2.time)
 				return o2.lev-o1.lev;
 			return o2.time-o1.time;
 		}
 	};
	public static void main(String[] args) {		
		while(in.hasNext()){
	     int ma = in.nextInt();
	     int ta=in.nextInt();
	    List<Ma_ta> st1=new ArrayList<Ma_ta>();
	    List<Ma_ta> st2=new ArrayList<Ma_ta>();
	    int t,l;
		for (int i = 0; i < ma; i++) {
			t=in.nextInt();
			l=in.nextInt();
			Ma_ta f = new Ma_ta(t,l);
		    st1.add(f);
		}
		for (int i = 0; i < ta; i++) {
			t=in.nextInt();
			l=in.nextInt();
			Ma_ta f = new Ma_ta(t,l);
		    st2.add(f);
		}
		Collections.sort(st1,com);
		Collections.sort(st2,com);
	     int a=0;
	     long sum=0;
	     int[] level=new int[105];
	        int j=0;  
	        for(int i=0;i<ta;i++) {  
	            while(j<ma&&st1.get(j).time>=st2.get(i).time)
	            	level[st1.get(j++).lev]++;  
	        for(int k=st2.get(i).lev; k<=100;k++){  
	            if(level[k]!=0){  
	                a++;   
	                sum+=500L*st2.get(i).time+2L*st2.get(i).lev;                   
	                level[k]--;  
	                break;  
	            }  
	          }
	       }
	   System.out.println(a+" "+sum);
	   }
	}
}

 

 

 


    反思:这道题一开始做超时了,用的二重循环吧。。。,而且没有优化

    后来仔细思考,得到思路:
我们将任务来一个按时间的递减排序,时间最大的必然会带来最大收益,不用考虑难度,因为难度带来的影响可以忽略不计。为什么会递减排序呢?
因为我们想先处理能够给我们带来最大收益的任务吧,如果同一个机器能够处理两个任务,我们必定先择收益大的,因为收益是我们的最终决定条件。
我们在为排好序的任务选机器时,将第一个,也就是收益最大的任务(大鱼),我们应该选满足要求条件最低的机器,这样可以保证我们留下
更牛的机器来处理别的任务,因为虽然我们在任务的选择上忽略了难度,但是选机器的时候机器所决定的任难度是实实在在的
(此时机器也应按照时间做好递减排列),我们不能忽略,可以举例,有机器参数为 (1000,100)(1000,50),
有任务为(1000,49),(999,100),我们为任务(1000,49)选机器时,肯定得选(1000,50),如果选了大的,
那么剩下的任务将无法被匹配,本来可以两个任务都搞定,却只搞定了一个,这不是我们想要的结果,所以先满足最小要求时间,
再在满足最小时间的同时,在相同的最小时间内选择难度能满足要求但是是所有满足任务难度中最低的一个的机器,这样结果就显而易见。

还有,这个题目比较注意的一个地方是:数据范围,我开始就是没考虑到,老是WA,气死我了,后来才发现,
结果有可能超出int 数据类型的范围,换成long之后 ,果然AC了,真是吐血,小错误从来不对我示好啊。。

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值