题意 && 解题思路 && 坑:
和1014有点像,不过只有一个人在黄线内等。
动态规划问题。
所有人processing时间不能超过1小时,也就是他一旦服务了一小时,不再继续回到队列最后排队,而是直接离开(是我想复杂了=.=)
所有的时间转化成秒来计算。
对于等待时间的计算,分两种情况:
(1)顾客到达的时候所有窗口都在服务,必须等,直到一个窗口空闲。窗口服务完后更新当前currentTime += 顾客.processingTime即可。
(2)顾客到达的窗口空闲,直接去服务,不用等。窗口服务完后currentTime更新为顾客服务完成的时间。currentTime = 顾客.arrivingTime + 顾客.processingTime
测试点:只要顾客在17:01前到达,不管他能开始的时间是什么时候,都要给他服务完,比如说最后一个16:00来的,等了90分钟,到了17:30,虽然银行已经下班了,但是依然要给他服务完。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Collections;
public class Main {
public final static int ET = 8*3600;
public final static int LT = 17*3600;
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String str[] = rd.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
LinkedList<Customer> list = new LinkedList<>();
int currentTime[] = new int[k];
for(int i=0; i<n; i++) {
String info[] = rd.readLine().split(" ");
Customer c = new Customer(info[0], Integer.parseInt(info[1]));
if(c.totalSecond <= LT)
list.add(c);
}
for(int i=0; i<k; i++)
currentTime[i] = ET;
Collections.sort(list);
int size = list.size();
double totalWaitingTime = 0.0;
while(!list.isEmpty()) {
int index = 0;
int minCurrentTime = Integer.MAX_VALUE;
for(int i=0; i<k; i++) {
if(minCurrentTime > currentTime[i]) {
minCurrentTime = currentTime[i];
index = i;
}
}
Customer c = list.removeFirst();
//从顾客的Arriving Time开始算起,要等一会儿才能服务
if(c.totalSecond < currentTime[index]) {
totalWaitingTime += currentTime[index] - c.totalSecond;
currentTime[index] += c.processingTime;
}
//立马就能服务,不用等
else
currentTime[index] = c.totalSecond + c.processingTime;
}
System.out.printf("%.1f", totalWaitingTime/size/60);
}
static class Customer implements Comparable<Customer>{
int totalSecond;
int processingTime;
int waitingTime;
String time;
public Customer(String time, int processingTime) {
this.time = time;
String str[] = time.split(":");
this.totalSecond = Integer.parseInt(str[0])*3600
+ Integer.parseInt(str[1])*60 + Integer.parseInt(str[2]);
this.processingTime = processingTime>60 ? 3600: processingTime * 60;
}
@Override
public int compareTo(Customer c) {
return this.totalSecond - c.totalSecond;
}
}
}