CSP201709-2 公共钥匙盒 Java

考试的时候不知道哪一步错了,又要考试了想着还是再写一下吧。

import java.util.*;

public class Csp2017092 {
    static List<Use> uses = new ArrayList<Use>();
    static int[] list;
    static int[] weizhi;
    static PriorityQueue<Integer> pq = new PriorityQueue<Integer>();

    public static void main(String[] args) {

        int N;// 教室数目
        int K;// K位老师

        // 初始化
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        K = sc.nextInt();
        int w, s, c;
        for (int i = 0; i < K; i++) {
            w = sc.nextInt();
            s = sc.nextInt();
            c = sc.nextInt();
            uses.add(new Use(w, s, s + c));
        }
        list = new int[N + 1];
        weizhi = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            list[i] = i;
            weizhi[i] = i;
        }
        // pq记录所有当前的空位
        Collections.sort(uses);
        // 两种思路 1.每个时间检测有没有
        // 2.每个Use检查时间然后挂过去
        int nowpos;
        for (int i = 1; i < 11000; i++) {
            Iterator<Use> it = uses.iterator();
            while (it.hasNext()) {
                Use u = it.next();
                if (u.end == i) {
                    nowpos = pq.poll();// 从pq中取出值最小的
                    list[nowpos] = u.windex;// 放回
                    weizhi[u.windex] = nowpos;
                    // System.out.println("钥匙" + u.windex + "放回" + nowpos + "时间"
                    // + i);
                    it.remove();
                }

            }
            it = uses.iterator();
            while (it.hasNext()) {
                Use u = it.next();
                if (u.start == i) {
                    nowpos = weizhi[u.windex];
                    // System.out.println("钥匙" + u.windex + "由位置" + nowpos +
                    // "在时间" + i + "借出");
                    list[nowpos] = -1;
                    pq.add(nowpos);
                }
            }
        }

        for (int i = 1; i <= N; i++) {
            switch (list[i]) {
            case -1:
                System.out.print("X ");
                break;
            default:
                System.out.print(list[i] + " ");
            }
        }

    }
}

class Use implements Comparable<Use> {
    public int windex;
    int start;
    int end;

    public Use(int windex, int start, int end) {
        this.windex = windex;
        this.start = start;
        this.end = end;
    }

    @Override
    public int compareTo(Use o) {
        return this.windex - o.windex;
    }

}

 

转载于:https://www.cnblogs.com/bnpop/p/8549205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值