Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1.     class Point {
  2.         int x;
  3.         int y;
  4.  
  5.         public Point(int x, int y) {
  6.             this.x = x;
  7.             this.y = y;
  8.         }
  9.     }
  10.  
  11.     void solve() {
  12.         int n = in.nextInt();
  13.         Point[] points = new Point[n];
  14.         for (int i = 0; i < n; i++) {
  15.             int x = in.nextInt();
  16.             int y = in.nextInt();
  17.             points[i] = new Point(x, y);
  18.         }
  19.         Arrays.sort(points, (p1, p2) -> Integer.compare(p1.x, p2.x));
  20.         long countx = 0;
  21.         long ans = 0;
  22.         for (int i = 1; i < n; i++) {
  23.             if (points[i].x == points[i - 1].x) {
  24.                 countx++;
  25.                 if (i == n - 1){
  26.                     ans += (countx * (countx + 1)) / 2;
  27.                     countx = 0;
  28.                 }
  29.  
  30.             } else {
  31.                 ans += (countx * (countx + 1)) / 2;
  32.                 countx = 0;
  33.             }
  34.         }
  35.         Arrays.sort(points, (o1, o2) -> {
  36.             if (o1.y < o2.y)
  37.                 return -1;
  38.             if (o1.y > o2.y)
  39.                 return 1;
  40.             if (o1.x < o2.x)
  41.                 return -1;
  42.             if (o1.x > o2.x)
  43.                 return 1;
  44.             return 0;
  45.         });
  46.         long county = 0;
  47.         long dec = 0;
  48.         for (int i = 1; i < n; i++) {
  49.             if (points[i].y == points[i - 1].y) {
  50.                 county++;
  51.                 if (points[i].x == points[i - 1].x) {
  52.                     dec++;
  53.                 }
  54.                 if (i == n - 1){
  55.                     ans += (county * (county + 1)) / 2 - (dec*(dec+1))/2;
  56.                     county = 0;
  57.                     dec = 0;
  58.                 }
  59.             } else {
  60.                 ans += (county * (county + 1)) / 2 - (dec*(dec+1))/2;
  61.                 county = 0;
  62.                 dec = 0;
  63.             }
  64.         }
  65.         out.print(ans);
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement