美文网首页
CCF201709-2 公共钥匙盒(JAVA版)

CCF201709-2 公共钥匙盒(JAVA版)

作者: 巨鹿lx | 来源:发表于2020-03-20 16:22 被阅读0次
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main{
    public static class Node implements Comparable<Node>{
        int w,t,f;
        public Node(int w, int t,int f) {
            this.w = w;//号码
            this.t = t;//时间
            this.f = f;//标记  用 0 和 1 分别表示拿和放
        }
        @Override
        public int compareTo(Node o) {
            if(this.t<o.t) return -1;
            if(this.t>o.t) return 1;//先按时间排序
            if(this.f>o.f) return -1;
            if(this.f<o.f) return 1;//再按拿-放排序
            if(this.w<o.w) return -1;
            if(this.w>o.w) return 1;//最后按号码排序
            return 0;
        }
    }
    static int N = 1010;
    static int hp[] = new int[N];
    static int ph[] = new int[N];
    static PriorityQueue<Node> q = new PriorityQueue<>();
    static PriorityQueue<Integer> room = new PriorityQueue<>();
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        for(int i = 1; i <= n ; i++) hp[i] = i;
        for(int i = 1; i <= n ; i++) ph[i] = i;
        while(k-->0) {
            int w = scanner.nextInt();
            int s = scanner.nextInt();
            int e = scanner.nextInt();
            q.add(new Node(w,s,0));//分解成两个操作,加入优先级队列里
            q.add(new Node(w,s+e,1));//一个拿操作,一个放操作
        }
        while(q.size()>0) {
            Node h = q.poll();
            if(h.f==0) {
                room.add(hp[h.w]);
            }else {
                int p = room.poll();//第一个可以放的空间
                hp[h.w] = p;//放进去
                ph[p] = h.w;//添加映射
            }
        }
        for(int i = 1; i <= n ; i++) System.out.print(ph[i]+" ");//打印映射
    }
}

相关文章

网友评论

      本文标题:CCF201709-2 公共钥匙盒(JAVA版)

      本文链接:https://www.haomeiwen.com/subject/vlgjyhtx.html