全错,还有个超时!
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));
}
}