Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Solution {
  5.     Scanner in = new Scanner(new InputStreamReader(System.in));
  6.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  7.    
  8.     private class Point implements Comparable<Point> {
  9.         int x;
  10.         int y;
  11.         int id;
  12.        
  13.         public Point(int x, int y) {
  14.             this(x, y, 0);
  15.         }
  16.        
  17.         public Point(int x, int y, int id) {
  18.             this.x = x;
  19.             this.y = y;
  20.             this.id = id;
  21.         }
  22.        
  23.         @Override
  24.         public int compareTo(Point other) {
  25.             if (x != other.x) {
  26.                 return x - other.x;
  27.             }
  28.             return y - other.y;
  29.         }
  30.  
  31.         @Override
  32.         public String toString() {
  33.             return "Pair [id=" + id + ", x=" + x + ", y=" + y + "]";
  34.         }
  35.     }
  36.    
  37.     private final int MAX = 100000;
  38.  
  39.     private void solution() throws IOException {
  40.         int n = in.nextInt();
  41.         Point[] points = new Point[n];
  42.         for (int i = 0; i < n; ++i) {
  43.             points[i] = new Point(in.nextInt(), in.nextInt(), i);
  44.         }
  45.         int[] from = initDistances(points);
  46.         int[] to = initDistances(inv(points));
  47.         int maxLength = maxLength(from, to);
  48.  
  49.         int[] count = new int[MAX + 1];
  50.         for (int i = 0; i < to.length; ++i) {
  51.             if (from[i] + to[i] == maxLength) {
  52.                 ++count[to[i]];
  53.             }
  54.         }
  55.  
  56.         List<Integer> A = new ArrayList<Integer>();
  57.         List<Integer> B = new ArrayList<Integer>();
  58.         for (int i = 0; i < to.length; ++i) {
  59.             if (from[i] + to[i] == maxLength) {
  60.                 A.add(i);
  61.                 if (count[to[i]] == 1) {
  62.                     B.add(i);
  63.                 }
  64.             }
  65.         }
  66.        
  67.         out(A);
  68.         out(B);
  69.         out.flush();
  70.     }
  71.  
  72.     private void out(List<Integer> a) {
  73.         out.print(a.size());
  74.         for (int i = 0; i < a.size(); ++i) {
  75.             out.print(" " + (a.get(i) + 1));
  76.         }
  77.         out.println();
  78.     }
  79.  
  80.     private int[] initDistances(Point[] points) {
  81.         Point[] p = compress(points);
  82.         int[] res = new int[points.length];
  83.  
  84.         int size = Integer.highestOneBit(maxY(p)) * 2;
  85.         int[] max = new int[size << 1];
  86.         int i = p.length - 1;
  87.         while (i >= 0) {
  88.             List<Point> c = new ArrayList<Point>();
  89.             c.add(p[i--]);
  90.             while (i >= 0 && p[i].x == c.get(c.size() - 1).x) {
  91.                 c.add(p[i--]);
  92.             }
  93.            
  94.             for (Point next : c) {
  95.                 res[next.id] = get(next.y + 1, size - 1, max) + 1;
  96.             }
  97.            
  98.             for (Point next : c) {
  99.                 update(next.y, res[next.id], max);
  100.             }
  101.         }
  102.         return res;
  103.     }
  104.  
  105.     private void update(int i, int dx, int[] max) {
  106.         i += max.length >> 1;
  107.         while (i > 0) {
  108.             max[i] = Math.max(max[i], dx);
  109.             i >>= 1;
  110.         }
  111.     }
  112.  
  113.     private int get(int i, int j, int[] max) {
  114.         i += max.length >> 1;
  115.         j += max.length >> 1;
  116.         int res = Integer.MIN_VALUE;
  117.         while (i <= j) {
  118.             if ((i & 1) == 1) {
  119.                 res = Math.max(res, max[i++]);
  120.             }
  121.             if ((j & 1) == 0) {
  122.                 res = Math.max(res, max[j--]);
  123.             }
  124.             i >>= 1;
  125.             j >>= 1;
  126.         }
  127.         return res;
  128.     }
  129.  
  130.     private int maxY(Point[] p) {
  131.         int max = Integer.MIN_VALUE;
  132.         for (int i = 0; i < p.length; ++i) {
  133.             max = Math.max(max, p[i].y);
  134.         }
  135.         return max;
  136.     }
  137.  
  138.     private Point[] compress(Point[] points) {
  139.        
  140.         return res;
  141.     }
  142.  
  143.     private int maxLength(int[] from, int[] to) {
  144.         int max = 0;
  145.         for (int i = 0; i < from.length; ++i) {
  146.             if (from[i] + to[i] > max) {
  147.                 max = from[i] + to[i];
  148.             }
  149.         }
  150.         return max;
  151.     }
  152.  
  153.     private Point[] inv(Point[] points) {
  154.         for (Point p : points) {
  155.             p.x = -p.x;
  156.             p.y = -p.y;
  157.         }
  158.         return points;
  159.     }
  160.  
  161.     private class Scanner {
  162.         BufferedReader reader;
  163.         StringTokenizer tokenizer;
  164.        
  165.         public Scanner(Reader reader) {
  166.             this.reader = new BufferedReader(reader);
  167.             tokenizer = new StringTokenizer("");
  168.         }
  169.  
  170.         public boolean hasNext() throws IOException {
  171.             while (!tokenizer.hasMoreTokens()) {
  172.                 String line = reader.readLine();
  173.                 if (line == null) {
  174.                     return false;
  175.                 }
  176.                 tokenizer = new StringTokenizer(line);
  177.             }
  178.             return true;
  179.         }
  180.        
  181.         public String nextLine() throws IOException {
  182.             tokenizer = new StringTokenizer("");
  183.             return reader.readLine();
  184.         }
  185.        
  186.         public String next() throws IOException {
  187.             hasNext();
  188.             return tokenizer.nextToken();
  189.         }
  190.        
  191.         public int nextInt() throws IOException, NumberFormatException {
  192.             return Integer.parseInt(next());
  193.         }
  194.        
  195.     }
  196.  
  197.     public static void main(String[] args) throws IOException {
  198.         new Solution().solution();
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement