HackerEarth练习不正确的结果

时间:2018-08-23 16:42:04

标签: java

我正在尝试从HackerEarth解决一个problem,但是提交我的代码后,它说我的所有结果都不正确。我看不到正确的输出和我的输出之间的差异,因为该选项在站点上不起作用。但是我看了看我的输出和正确的结果,它们看起来是一样的。
这是我的代码:

import java.util.*;

class TestClass {


    class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Point point = (Point) o;

            if (x != point.x) return false;
            return y == point.y;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        Map<Point, Integer> map = new HashMap<>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            Point point = new TestClass().new Point(x, y);
            Integer value = map.get(point);
            if (value == null) {
                map.put(point, 1);
            } else map.put(point, value + 1);
        }
        StringBuilder sb = new StringBuilder();

        Comparator<Map.Entry<Point, Integer>> lexicoComparator = (e1, e2) -> {
            return e1.getKey().x - e2.getKey().x;
        };

        map.entrySet().
                stream().
                sorted(lexicoComparator).
                forEach(entry -> sb.append(entry.getKey().x + " " + entry.getKey().y + " " + entry.getValue() + "\n"));
        sb.delete(sb.length() - 1, sb.length());
        System.out.print(sb);
    }

}

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,您还应该考虑坐标y,而不仅仅是x,因为描述中说:

  

按字典顺序打印坐标点

所以不要写:

Comparator<Map.Entry<Point, Integer>> lexicoComparator = (e1, e2) -> {
            return e1.getKey().x - e2.getKey().x;
        };

...您可以编写如下内容:

Comparator<Map.Entry<Point, Integer>> lexicoComparator = (e1, e2) -> {
            Point p1 = e1.getKey();
            Point p2 = e2.getKey();
            if (p1.x == p2.x){
                if (p1.y > p2.y)
                    return 1;
                if (p1.y == p2.y)
                    return 0;
                return -1;
            }
            if (p1.x > p2.x)
                return 1;
            return -1;
        };

您可能想知道为什么我不像其他x例子那样仅减去ycompareTo坐标。这是因为说明中说:

  

-10 ^ 9 <= x,y <= 10 ^ 9

这意味着减去坐标可能会导致溢出(不一定,但最好的方法是将它们与条件比较)。

完整解决方案:

import java.util.*;

class TestClass {


    class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Point point = (Point) o;

            if (x != point.x) return false;
            return y == point.y;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }

    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        Map<Point, Integer> map = new HashMap<>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            Point point = new TestClass().new Point(x, y);
            Integer value = map.get(point);
            if (value == null) {
                map.put(point, 1);
            } else map.put(point, value + 1);
        }
        StringBuilder sb = new StringBuilder();

        Comparator<Map.Entry<Point, Integer>> lexicoComparator = (e1, e2) -> {
            Point p1 = e1.getKey();
            Point p2 = e2.getKey();
            if (p1.x == p2.x){
                if (p1.y > p2.y)
                    return 1;
                if (p1.y == p2.y)
                    return 0;
                return -1;
            }
            if (p1.x > p2.x)
                return 1;
            return -1;
        };

        map.entrySet().
                stream().
                sorted(lexicoComparator).
                forEach(entry -> sb.append(entry.getKey().x + " " + entry.getKey().y + " " + entry.getValue() + "\n"));
        sb.delete(sb.length() - 1, sb.length());
        System.out.print(sb);
    }

}