整道题整体思路就是设置一个时间轴,一个一个单位时间的走。刚开始有点乱,看了一个大神的思路才做出来,把他的答案粘贴在这https://blog.csdn.net/zz2013215/article/details/78561461?reload。
我做完了提交了三次,全是0分,后来发现答案换行了,引以为戒。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int k = cin.nextInt();
int time = 1;
ArrayList<Teacher> list = new ArrayList<>();
int[] gua = new int[n];
for(int i = 0;i < n;i++) {
gua[i] = i+1;
}
while(k > 0) {
Teacher teacher = new Teacher(cin.nextInt(),cin.nextInt(),cin.nextInt());
list.add(teacher);
k--;
}
while(time <= maxtime(list)) {
returnKey(list,gua,time,n);
borrowKey(list,gua,time,n);
time++;
}
for(int i = 0;i < n;i++) {
if(i == n-1)
System.out.print(gua[i]);
else
System.out.print(gua[i]+" ");
}
}
//取钥匙
public static int borrowKey(ArrayList<Teacher> list,int[] gua,int time,int n) {
for(Teacher teacher : list) {
if(time == teacher.s) {
for(int i = 0; i < n;i++) {
if(gua[i] == teacher.w) {
gua[i] = 0;
}
}
}
}
return 0;
}
//还钥匙
public static int returnKey(ArrayList<Teacher> list,int[] gua,int time,int n) {
ArrayList<Integer> key = new ArrayList<>();
for(Teacher teacher : list) {
if(time == teacher.e) {
key.add(teacher.w);
}
}
if(key.size() == 0) {
return 0;
}
Collections.sort(key);
int temp = 0;
for(int i = 0;i < n;i++) {
if(gua[i] == 0) {
gua[i] = key.get(temp);
if(key.size() == temp+1) {
return 0;
}
temp++;
}
}
return 0;
}
//最久时间
public static int maxtime(ArrayList<Teacher> list) {
int temp = 0;
for(Teacher teacher : list) {
if(teacher.e > temp) {
temp = teacher.e;
}
}
return temp;
}
}
class Teacher{
int w;
int s;
int c;
int e;
public Teacher(int w,int s,int c) {
this.w = w;
this.s = s;
this.c = c;
this.e = this.s + this.c;
}
}