201709-2 公共钥匙盒(Java 100)

  • 问题链接公共钥匙盒
  • 问题分析:模拟法解决。
  • 程序说明: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;
	}//code表示钥匙编号;exist表示是否存在,即是否被取走,为true表示在,反之已被取走————被取走此处只剩挂钩,所以此时表示可用的挂钩
}

class Tea{
	public int code;
	public int tORr;//take-0,ret-1;
	public int time;
	Tea(int w,int t,int way){
		this.code = w;
		this.time = t;
		this.tORr = way;//code表示钥匙编号,time表示教师何时开始的借或还的行为;tORr为0此次行为表示取走,为1此次行为表示还回
	}
}
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;
		}
		//由题意可知,可得到以下优先级关系:1.时间由小到大进行2.若时间相等,先还再取3.若时间相等,且同为还,则按编码由小到大进行(注意:不会出现同时取走的情况)
		//所以对各行为按以上优先级进行排序即可,得出的数组,按正常顺序执行,即可得到最终答案。
		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;//time升序
    	}else {
    		if(o1.tORr!=o2.tORr) {
    			return o2.tORr - o1.tORr;//tORr降序
    		}else {
    			return o1.code - o2.code;//code升序
    		}
    	}		
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值