美文网首页
CCF/CSP 公共钥匙盒

CCF/CSP 公共钥匙盒

作者: 卷毛_3ee0 | 来源:发表于2020-09-09 15:02 被阅读0次

    无情的copy机器...
    【解题思路】:排序+模拟
    【代码如下】:

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            new Main().solve();
        }
    
        public void solve() {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt(),k = sc.nextInt();
    
            int[] key = new int[n+1];
            for (int i=0; i<=n; i++) {
                key[i] = i;
            }
    
            Node[] nodes = new Node[k];
            for (int i=0; i<k; i++) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                int c = sc.nextInt();
                nodes[i] = new Node(a, b, b+c);
            }
            Node[] start = nodes.clone();
            Node[] end = nodes.clone();
    
            Arrays.sort(start, (o1, o2) -> {
                if (o1.s == o2.s) return o1.w - o2.w;
                return o1.s - o2.s;
            });
            Arrays.sort(end, (o1, o2) -> {
                if (o1.c == o2.c) return o1.w - o2.w;
                return o1.c - o2.c;
            });
    
            int t = 0,s = 0,e = 0;
            while (t++ < 10101) {
                for (; e< end.length; e++) {
                    if (t<end[e].c) break;
                    if (t == end[e].c)
                        unUse(end[e].w, key);
                }
                for (; s< start.length; s++) {
                    if (t<start[s].s) break;
                    if (t == start[s].s)
                        use(start[s].w, key);
                }
                if (e== end.length && s == start.length) break;
            }
    
            for (int i=1; i<=n; i++) {
                System.out.print(key[i]);
                if (i != n) System.out.print(" ");
            }
            System.out.println();
    
        }
    
        public void use(int tar,int [] key) {
            for (int i=0; i< key.length; i++) {
                if (key[i] == tar) {
                    key[i] = -1;
                    break;
                }
            }
        }
    
        public void unUse(int tar, int[] key) {
            for (int i=0; i< key.length; i++) {
                if (key[i] == -1) {
                    key[i] = tar;
                    break;
                }
            }
        }
    
        class Node {
            public int w;
            public int s;
            public int c;
    
            public Node(int w,int s,int c) {
                this.w = w;
                this.s = s;
                this.c = c;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:CCF/CSP 公共钥匙盒

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