简单易懂 多关键字排序 暴力求解
Java 代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int[] ints = new int[N + 1];
for (int i = 0; i <= N; i++) {
ints[i] = i;
}
LinkedList<key> ops = new LinkedList<>();
//用-1标记使用中
while (K-->0) {
int id = sc.nextInt();
int start = sc.nextInt();
int duration = sc.nextInt();
key key1 = new key(id, start, true);
key key2 = new key(id, start+duration, false);
ops.add(key1);
ops.add(key2);
}
ops.sort(new Comparator<key>() {
@Override
public int compare(key o1, key o2) {
if (o1.time != o2.time) return o1.time - o2.time;
if (o1.type != o2.type) return !o1.type ? -1:1;
return o1.id - o2.id;
}
});
// 遍历操作
int time = 1;
for (key op : ops) {
if (!op.type) {
//如果是还钥匙
//找到空位置
for (int i = 1; i<=N;i++) {
if (ints[i] == -1) {
ints[i] = op.id;
break;
}
}
} else {
//如果借钥匙
for (int i = 1; i<=N;i++) {
if (ints[i] == op.id) {
ints[i] = -1;
break;
}
}
}
}
for (int i = 1;i<=N;i++) {
System.out.print(ints[i]+" ");
}
// System.out.println();
}
static class key {
int id;
int time;
boolean type;
key(int id, int time, boolean type) {
this.id = id;
this.time = time;
this.type = type;
}
}
}