PAT 1017_C++已过

全错,还有个超时!

package p1;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

//time in seconds, not minutes, much more convenient and more accurate!!!

class Customer {
	int arrTime;		//all time in seconds, compare with 00:00 
	int startTime;		// 
	int waitTime; 		//wait = start - arrive
	int proTime;		 
	int endTime;		// start + process = end
	
	public Customer(int arrTime, int proTime){//don't put complicated StringToInt logic in constructor
		this.arrTime = arrTime;
		this.proTime = proTime > 3600 ? 3600 : proTime; //no window can be occupied by a single customer for more than 1 hour		
	}
}

class cmpCus implements Comparator{
	public int compare(Object a, Object b){
		Customer c1 = (Customer)a;
		Customer c2 = (Customer)b;
		return (c1.arrTime - c2.arrTime);
	}
}


public class Main{
	public static int getTime(String time){
		int res = 0;
		char [] c = time.toCharArray();
		int h = (c[0]-'0')*10 + (c[1]-'0');
		int m = (c[3]-'0')*10 + (c[4]-'0');
		int s = (c[6]-'0')*10 + (c[7]-'0');
		
		res += h*3600 + m*60 + s;
		
		return res;
	}
	
	
	public static void main(String [] args) throws IOException{
//		Scanner in = new Scanner(new File("./in.txt"));
		Scanner in = new Scanner(System.in);
		
		int i,n,k;//n is customer numbers, k is window numbers
		double res = 0;
		
		n = in.nextInt();
		k = in.nextInt();
		Customer [] records = new Customer[n];
		PriorityQueue<Integer> windOpen = new PriorityQueue<Integer>();//only need to know which window is firstly open again
		
		int validCustCnt = 0; //customer who arrive at or before 17:00:00
		for(i = 0; i < n; i++){
			String strArrTime = in.next();
			int arrTime = getTime(strArrTime);
			
			int proTime = in.nextInt();
			proTime *= 60;//minutes to seconds
			
//			if(strArrTime.substring(0,2).compareTo("17") < 0 || strArrTime.equals("17:00:00")){	//arrive at or before 17:00:00
			if(arrTime <= 17*3600){
				records[validCustCnt++] = new Customer(arrTime, proTime);
			}
		}
		
		Arrays.sort(records, 0, validCustCnt, new cmpCus()); // good sort ! fromIndex , toIndex
		
		//test
//		for(i = 0;i < validCustCnt;i++){
//			Customer c = records[i];
//			System.out.println("arrive="+c.arrTime+"  pro="+c.proTime);
//		}
		
		for(i = 0;i < validCustCnt;i++){
			Customer c = records[i];
			
			int openTime;
			if(i < k){
				openTime = 8*3600;//first k customers start at 08:00
			}else{
				openTime = windOpen.poll();//find the earliest free window
			}
			
			c.startTime =  c.arrTime < openTime ? openTime : c.arrTime;
			c.waitTime = c.startTime - c.arrTime;
			
			res += c.waitTime;

			c.endTime = c.startTime + c.proTime;
			windOpen.add(c.endTime);
		}
		
		if(validCustCnt > 0){
			res = res/(validCustCnt*60);//seconds to minutes
		}else{
			res = 0;
		}
		System.out.println(String.format("%.1f", res));
	}
	
	
}


转载于:https://my.oschina.net/kaneiqi/blog/289826

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值