- 问题链接:公共钥匙盒
- 问题分析:模拟法解决。
- 程序说明:n表示教室数量,即钥匙数量、挂钩数量;k表示教师的行为次数;key数组存放钥匙对象,tea数组存放教师的行为对象;w,s,c分别为此次要使用的钥匙编号、开始上课的时间和上课的时长
- 程序代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
class Key{
public int code;
public boolean exist;
Key(int c,boolean e){
this.code = c;
this.exist = e;
}
}
class Tea{
public int code;
public int tORr;
public int time;
Tea(int w,int t,int way){
this.code = w;
this.time = t;
this.tORr = way;
}
}
class Reader {
static StringTokenizer token =new StringTokenizer("");
static BufferedReader reader =new BufferedReader (new InputStreamReader(System.in)) ;
static String nextLine() throws IOException {
return reader.readLine() ;
}
static String next() throws IOException {
while(!token.hasMoreTokens()) {
token =new StringTokenizer(reader.readLine()) ;
}
return token.nextToken() ;
}
static int nextInt() throws IOException {
return Integer.parseInt(next()) ;
}
static double nextDouble() throws IOException {
return Double.parseDouble(next()) ;
}
}
public class Main {
public static void main(String[] args) throws IOException {
int n = Reader.nextInt();
int k = Reader.nextInt();
Key[] key = new Key[n];
Tea[] tea = new Tea[2*k];
for(int i=1;i<=n;i++) {
Key newKey = new Key(i,true);
key[i-1] = newKey;
}
for(int i=0;i<2*k;i++) {
int w = Reader.nextInt();
int s = Reader.nextInt();
int c = Reader.nextInt();
Tea newTea1 = new Tea(w,s,0);
Tea newTea2 = new Tea(w,s+c,1);
tea[i] = newTea1;
i++;
tea[i] = newTea2;
}
Comparator cmp = new MyComparator();
Arrays.sort(tea,cmp);
for(int i=0;i<2*k;i++) {
for(int j=0;j<n;j++) {
if(tea[i].tORr==0) {
if(tea[i].code==key[j].code) {
key[j].exist=false;
break;
}
}else {
if(key[j].exist==false) {
key[j].code=tea[i].code;
key[j].exist=true;
break;
}
}
}
}
for(Key nk : key) {
System.out.print(nk.code+" ");
}
}
}
class MyComparator implements Comparator<Tea>{
@Override
public int compare(Tea o1, Tea o2) {
if(o1.time!=o2.time) {
return o1.time - o2.time;
}else {
if(o1.tORr!=o2.tORr) {
return o2.tORr - o1.tORr;
}else {
return o1.code - o2.code;
}
}
}
}