我觉得是第二题里面最难的,然后这次认证的第三题也是我觉得最难的?,不过好像第四题和第五题可以骗点分。
用TreeMap来存储所有的时间点及该时间点要还和要借的钥匙,其中不论是借的还是还的钥匙也用TreeSet来存。这些都是为了达到排序的效果。思路就是遍历时间点,每个时间点先还钥匙,然后借钥匙。
奉上java满分代码
import java.util.*;
public class Main{
static class TimeSection{
public TreeSet<Integer> getKeys = new TreeSet<>();
public TreeSet<Integer> bacKeys = new TreeSet<>();
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String[] firstLine = scanner.nextLine().split(" ");
int n = Integer.parseInt(firstLine[0]);
int k = Integer.parseInt(firstLine[1]);
List<Integer> keys = new ArrayList<>();
for(int i = 0; i < n; i++){
keys.add(i + 1);
}
TreeMap<Integer, TimeSection> map = new TreeMap<>();
for(int i = 0; i < k; i++){
String[] data = scanner.nextLine().split(" ");
int key = Integer.parseInt(data[0]);
int start = Integer.parseInt(data[1]);
int end = Integer.parseInt(data[2]) + start;
{
TimeSection startSection = new TimeSection();
if(map.containsKey(start)){
startSection = map.get(start);
}
startSection.getKeys.add(key);
map.put(start, startSection);
}
{
TimeSection endSection = new TimeSection();
if(map.containsKey(end)){
endSection = map.get(end);
}
endSection.bacKeys.add(key);
map.put(end, endSection);
}
}
scanner.close();
for(Iterator<Integer> iterator = map.keySet().iterator(); iterator.hasNext();){
TimeSection timeSection = map.get(iterator.next());
for(int key : timeSection.bacKeys){
for(int i = 0; i < keys.size(); i++){
if(keys.get(i) == -1){
keys.set(i, key);
break;
}
}
}
for(int key : timeSection.getKeys){
for(int i = 0; i < keys.size(); i++){
if(keys.get(i) == key){
keys.set(i, -1);
break;
}
}
}
}
for(int key : keys){
System.out.print(key + " ");
}
}
}