Bobjoehi

ConvexHullProblem

Jan 30th, 2021
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class ConvexHullProblem {
  4.    
  5.     static int direction(Point a, Point b, Point c) {
  6.         return (b.x - a.x) * (c.y - b.y) - (c.x - b.x) * (b.y - a.y);  
  7.     }
  8.    
  9.     public static void main(String[] args) throws IOException{
  10.         readLine();
  11.         int n = nextInt();
  12.        
  13.         Point[] points = new Point[n];
  14.        
  15.         for(int i = 0; i < n; i ++) {
  16.             readLine();
  17.             points[i] = new Point(nextInt(), nextInt());
  18.         }
  19.         Arrays.sort(points);
  20.        
  21.         Stack<Point> top = new Stack<Point>();
  22.        
  23.         for(int i = 0; i < n; i ++) {
  24.             Point next = points[i];
  25.             boolean cw = false;
  26.             while(top.size() > 1 && ! cw) {
  27.                 Point b = top.pop();
  28.                 Point a = top.peek();
  29.                 if(direction(a, b, next) < 0) {
  30.                     cw = true;
  31.                     top.push(b);
  32.                 }
  33.             }
  34.             top.push(next);
  35.         }
  36.        
  37.         Stack<Point> bottom = new Stack<Point>();
  38.         for(int i = n - 1; i >= 0; i --) {
  39.             Point next = points[i];
  40.             boolean cw = false;
  41.             while(bottom.size() > 1 && ! cw) {
  42.                 Point b = bottom.pop();
  43.                 Point a = bottom.peek();
  44.                 if(direction(a, b, next) < 0) {
  45.                     cw = true;
  46.                     bottom.push(b);
  47.                 }
  48.             }
  49.             bottom.push(next);
  50.         }
  51.        
  52.         bottom.pop();
  53.         bottom.remove(0);
  54.         top.addAll(bottom);
  55.         System.out.println(top);
  56.     }
  57.     static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  58.     static StringTokenizer st;
  59.     static void readLine() throws IOException{
  60.         st = new StringTokenizer(in.readLine());
  61.     }
  62.     static int nextInt() {
  63.         return Integer.parseInt(st.nextToken());
  64.     }
  65.     static class Point implements Comparable<Point>{
  66.         int x, y;
  67.         Point(int x, int y){
  68.             this.x = x;
  69.             this.y = y;
  70.         }
  71.         @Override
  72.         public int compareTo(Point o) {
  73.             if(this.x == o.x) {
  74.                 return this.y - o.y;
  75.             }
  76.             return this.x - o.x;
  77.         }
  78.         public String toString(){
  79.             return "(" + x + ", " + y + ")";
  80.         }
  81.        
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment