整道題整體思路就是設置一個時間軸,一個一個單位時間的走。剛開始有點亂,看了一個大神的思路才做出來,把他的答案粘貼在這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;
}
}