1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class A1095 {
public static final DateFormat df = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int K = Integer.parseInt(input[1]);
ArrayList<Car> totalCar = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] line = br.readLine().split(" ");
totalCar.add(new Car(line[0], df.parse(line[1]), line[2]));
}
Collections.sort(totalCar, new Comparator<Car>() {
@Override
public int compare(Car o1, Car o2) {
if (!(o1.id.equals(o2.id)))
return o1.id.compareTo(o2.id);
else
return o1.time.compareTo(o2.time);
}
});
ArrayList<Car> validCar = new ArrayList<>();
Map<String, Long> packTime = new HashMap<>();
long maxTime = -1L;
for (int i = 0; i < totalCar.size() - 1; i++) {
if (totalCar.get(i).id.equals(totalCar.get(i + 1).id) && totalCar.get(i).state.equals("in") && totalCar.get(i + 1).state.equals("out")) {
validCar.add(totalCar.get(i));
validCar.add(totalCar.get(i + 1));
long time = (totalCar.get(i + 1).time.getTime() - totalCar.get(i).time.getTime()) / 1000;
if (!packTime.containsKey(totalCar.get(i).id))
packTime.put(totalCar.get(i).id, 0L);
packTime.put(totalCar.get(i).id, packTime.get(totalCar.get(i).id) + time);
maxTime = Math.max(packTime.get(totalCar.get(i).id), maxTime);
}
}
Collections.sort(validCar, new Comparator<Car>() {
@Override
public int compare(Car o1, Car o2) {
return o1.time.compareTo(o2.time);
}
});
Date now = null;
int num = 0;
int numberCar = 0;
int[] t = new int[K];
for (int i = 0; i < K; i++) {
String str = br.readLine();
now = df.parse(str);
while (num < validCar.size() && now.compareTo(validCar.get(num).time) >= 0) {
if (validCar.get(num).state.equals("in"))
numberCar++;
else
numberCar--;
num++;
}
t[i] = numberCar;
}
ArrayList<String> str = new ArrayList<>();
Iterator<Map.Entry<String, Long>> entryIterator = packTime.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, Long> next = entryIterator.next();
if (next.getValue().equals(maxTime))
str.add(next.getKey());
}
Collections.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
for (int i = 0; i < str.size(); i++) {
System.out.print(str.get(i) + " ");
}
System.out.println(String.format("%02d", maxTime / 3600) + ":" + String.format("%02d", maxTime % 3600 / 60) + ":" + String.format("%02d", maxTime % 60));
}
}
class Car {
String id;
Date time;
String state;
public Car(String id, Date time, String state) {
this.id = id;
this.time = time;
this.state = state;
}
@Override
public String toString() {
return "Car{" +
"id='" + id + '\'' +
", time=" + time +
", state='" + state + '\'' +
'}';
}
}
3 要点
(1)思路:①读入所有车辆信息到totalList后,按照车牌号、时间对所有车辆进行排序,然后遍历整个totalList,将其中无效的时间信息剔除,有效的保留在validList中,并在遍历的过程中保存所有的车辆停放时间信息到map中。②然后,按照时间对validList进行排序,便于后续的车辆信息计算。依次读入需要查询的时间,遍历validList,如果validList中元素时间小于查询的时间,并且状态status为in,那么当前时间的停放车辆+1,反之,状态status为out,那么当前时间的停放车辆-1;并使用一个数量数组保存每个时间段的数量。③从map中寻找出停留时间最长的车辆信息到strList,并排序。 ④按规则依次输出数量数组、strList和最长停放时间。 (2)坑点:①如果每读入一个时间信息且立即输出当前停放车辆信息,会导致输出的格式错误,需要多打一个回车控制。如下:
for (int i = 0; i < K; i++) {
String str = br.readLine();
now = df.parse(str);
while (num < validCar.size() && now.compareTo(validCar.get(num).time) >= 0) {
if (validCar.get(num).state.equals("in"))
numberCar++;
else
numberCar--;
num++;
}
System.out.println(numberCar);
}
②:map中使用包装类Long型数据时,利用==进行比较数据大小已经失效,需要使用Long.equals() 。 ③:注意最终时间输出时的位数,以及Date.getTime()表示的是毫秒值。
|