美文网首页
算法11 抓住波粒二象性的火星人

算法11 抓住波粒二象性的火星人

作者: holmes000 | 来源:发表于2017-10-29 20:25 被阅读0次

    题目:小伙伴们发现, 火星人都有神奇的波粒二象性:

    单单凭眼睛, 不能准确地判定他们的位置, 还需要用手触摸一下,才能固定他们的位置
    单单用手触摸,也不能锁定他们的位置, 还必须 先 看到
    火星人的波粒二象性很奇怪, 每一次触摸至少要对应一次预先的看到, 即看到一次, 触摸两次, 第二次的就会失效, 但是先看到 3 次, 再触摸 3 次, 则是有效的
    同一个人,在同一个地点的观察和触摸才可以匹配
    所以 同学们收集了过去一天内所有的观察和触摸到火星人的记录, 希望发现前一天中 无效的触摸 有多少次.
    记录格式为: 用逗号格开的 4 元组. 4 部分的信息分别为: 观察者ID, 观察地点, 当时的动作, 动作发生的时间戳(精确到秒)
    例如
    1710170708481678574,1895915,click,1508195328
    1607272005511968269,1191643,see,1507998070
    要求输出的结果为: 一个整数

    思路:

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String msg;
        Map<String, ArrayList<Pos>> watcher = new HashMap<>();
        while ((msg = br.readLine()) != null && !"".equals(msg)) {
            String[] x = msg.split(",", 4);
            String key = x[0] + ":)" + x[1];
            System.out.println("click".equals(x[2]));
            Pos p = new Pos(Long.parseLong(x[3]), "click".equals(x[2]) ? -1: 1);
            if(!watcher.containsKey(key))
                watcher.put(key, new ArrayList<Pos>());
            watcher.get(key).add(p);
        }
    
        long[] result = {0l};
        watcher.forEach((k, list) -> {
            long[] checker = {0l};
            list.forEach(pos -> {
                if(pos.weight == -1) {
                    if(checker[0] == 0) {
                        result[0] += 1;
                        System.out.println("r+1");
                    } else {
                        checker[0] -= 1;
                        System.out.println("c-1");
                    }
                } else if(pos.weight == 1) {
                    checker[0] += 1;
                    System.out.println("c+1");
                }
            });
        });
        System.out.println(result[0]);
    }
    
    static class Pos {
        public long timeStamp;
        public long weight;
    
        public Pos(Long timeStamp, long weight) {
            this.timeStamp = timeStamp;
            this.weight = weight;
        }
    }

    相关文章

      网友评论

          本文标题:算法11 抓住波粒二象性的火星人

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