贝壳笔试:多米诺骨牌-越界一直没处理出来

import java.util.*;
public class 多米诺骨牌 {
    static class Node implements Comparable{
        private int x;
        private int h;
        private int cnt;
        private int ans;
        private int max;
        public Node(int x, int h, int cnt, int ans, int max) {
            this.x = x;
            this.h = h;
            this.cnt = cnt;
            this.ans = ans;
            this.max = max;
        } 
        @Override]
         public int compareTo(Node node) {
            return this.x - node.x;
        }
    }
    static class cmpCnt implements Comparator {
    @Override]
     public int compare(Node o1, Node o2) {
            return  o1.cnt - o2.cnt;
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        List list = new ArrayList();
        for (int i = 0; i < n; i++) {
            String[] strs = in.nextLine().split(" ");
            int x = Integer.parseInt(strs[0]);
            int h = Integer.parseInt(strs[1]);
            int cnt = i;
            int ans = 1;
            int max = x + (h - 1);
            Node temp = new Node(x, h, cnt, ans, max);
            list.add(temp);
        }
        //处理越界用的Node
        Node temp = new Node(0, 0, 0, 0, 0);
        list.add(temp);
        Collections.sort(list);
        for (int i = n - 1; i >= 1; i--) {
            int j = i + 1;
            int max = list.get(i).max;
            while (j <= n && list.get(j).x <= max){
                list.get(i).ans += list.get(j).ans;
                max = Math.max(max, list.get(j).max);
                j += list.get(j).ans;
            }
            list.get(i).max = max;
        }
        Collections.sort(list,new cmpCnt());
        System.out.println();
        for (int i = 1; i < n; i++) {
            System.out.print(list.get(i).ans + " ");
        }
        System.out.print(list.get(n).ans);
    }
}
没有多余的测试用例,也不知道对错.
考完了照着大佬的改了下。
#贝壳找房#
全部评论
百度一下 56E Codeforce E Domino Principle有答案
点赞 回复 分享
发布于 2018-09-05 19:26

相关推荐

2025年初,新的一年开始,我给自己暗暗打气,发誓今年一定要拿到offer。如今2025年即将结束,找工作仍然没有任何水花,如今的失意和落魄和年初信心满满的姿态形成鲜明对比,想必也是因为被社会毒打,认清现实了吧。先分享一下贴主的背景,本人女,本科末流985文科专业,后来保送到华五,成绩一直是班级第一,有过国奖,实习有多段头部大厂经历。发贴的直接原因是今天华为面试挂,在反思中有很多复杂的想法,包括对自身能力的怀疑、对面试官所提问题的不解、对大环境的无奈。贴主是一个说话温柔、不喜欢咄咄逼人、有点社恐的人(基本上算是人们眼中对小女生的刻板印象,所以在历次群面中基本全挂(看到大家争抢当leader、t...
在找内推的小虾米:感觉这一段经历和我好像啊,前段时间面了很多车企,面试项目经历各种被拷打,大多数都没过一面,最有希望拿offer的一个终面挂了把我干破防了,打电话给爸妈哭了一个多小时才缓过来。我也开始否定自己,否定自己的一切,包括性格,能力,成长经历。。。最后面了深圳的某家公司,面试官人都挺友好,提的问题有深度但找到切入点 ,最后hr也按岗位最高的标准给的offer,我才发现自己并没有这么不堪,只是我的能力和经验和之前的岗位要求不那么符合而已。帖主一定不要灰心,招聘的窗口期还有很长很长,保持自信扬长避短,一定有企业能发现你的闪光点,祝好。
我的求职进度条
点赞 评论 收藏
分享
10-17 13:54
上海大学 运营
雾凇岛:这还说什么了,冲了兄弟们
点赞 评论 收藏
分享
迷茫的大四🐶:那你问他上班之后老实了没
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务