Problem:
Convex Hull

Find the convex hull of a given set of points P. In other words, find the smallest convex polygon containing all the points of P. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees.

Please note that you should find all the points of P on both corner and boundary of the convex polygon.

Input

n
x1 y1
x2 y2
:
xn yn

The first integer n is the number of points in P. The coordinate of the i-th point pi is given by two integers xi and yi.

Output

In the first line, print the number of points on the corner/boundary of the convex polygon. In the following lines, print x y coordinates of the set of points.
The coordinates should be given in the order of counter-clockwise visit of them starting from the point in P with the minimum y-coordinate, or the leftmost such point in case of a tie.

Constraints

3 ≤ n ≤ 100000

-10000 ≤ xi, yi ≤ 10000

No point in the P will occur more than once.

Sample Input 1

7
2 1
0 0
1 2
2 2
4 2
1 3
3 3

Sample Output 1

5
0 0
2 1
4 2
3 3
1 3

Sample Input 2

4
0 0
2 2
0 2
0 1

Sample Output 2

4
0 0
2 2
0 2
0 1

Solution in JAVA (judge verdict: Time Limit Exceeded)

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	private static FastScanner sc = new FastScanner();
	static final double ALLOWABLE_ERROR = 0.000001;

	public static void main(String[] args) {
		int n = sc.nextInt();
		ArrayList<Point> p = new ArrayList<>();
		for(int i=0; i<n; i++) {
			p.add(new Point(sc.nextDouble(), sc.nextDouble()));
		}
		
		for(int i=1; i<n; i++) {
			if((p.get(i).y < p.get(0).y) || (p.get(i).y == p.get(0).y && p.get(i).x < p.get(0).x)) {
				Point temp = p.get(i);
				p.set(i, p.get(0));
				p.set(0, temp);
			}
		}
		
		for(int i=1; i<n; i++) {
			if(p.get(i).x == p.get(0).x) {
				p.get(i).ang = Math.PI / 2;
			} else if(p.get(i).x > p.get(0).x) {
				p.get(i).ang = Math.atan((p.get(i).y - p.get(0).y) / (p.get(i).x - p.get(0).x));
			} else {
				p.get(i).ang = Math.atan((p.get(i).y - p.get(0).y) / (p.get(i).x - p.get(0).x)) + Math.PI;
			}
		}
		
		Collections.sort(p, new Comparator<Point>() {
			@Override
			public int compare(Point o1, Point o2) {
				if(o1.ang < o2.ang) {
					return -1;
				} else {
					return 1;
				}
			}
		});
		
		int i = 1;
		int j = 0;
		//false:down true:up
		boolean mode = false;
		while(i < p.size()) {
			if(p.get(i).x > p.get(i-1).x) {
				mode = false;
			}
			if(p.get(i).x > p.get(i-1).x) {
				mode = true;
			}
			
			int k = i + 1;
			while(k + 1 < p.size() && p.get(k).ang == p.get(k+1).ang) {
				if(mode) {
					if(p.get(k+1).y > p.get(k).y) {
						Point temp = p.get(k);
						p.set(k, p.get(k+1));
						p.set(k+1, temp);
						if(k > i + 1) {
							k--;
						}
					} else {
						k++;
					}
				} else {
					if(p.get(k+1).y < p.get(k).y) {
						Point temp = p.get(k);
						p.set(k, p.get(k+1));
						p.set(k+1, temp);
						if(k > i + 1) {
							k--;
						}
					} else {
						k++;
					}
				}
			}
			
			if(checkCounterClockwise(p.get(i-1), p.get(i), p.get((i+1)%p.size())) == 1) {
//				System.out.println(i + " " + 1);
				j = i;
				i++;
			} else if(checkCounterClockwise(p.get(i-1), p.get(i), p.get((i+1)%p.size())) == -1) {
//				System.out.println(i + " " + -1);
				p.remove(i);
				i--;
			} else {
				if(distance(p.get(j), p.get(i)) > distance(p.get(j), p.get((i+1)%p.size()))) {
//					System.out.println(i + " " + 0);
					Point temp = p.get(i);
					p.set(i, p.get((i+1)%p.size()));
					p.set((i+1)%p.size(), temp);
					i--;
				} else {
					i++;
				}
				
			}
		}
		
		StringBuilder ans = new StringBuilder();
		ans.append(p.size());
		ans.append("\n");
		for(int k=0; k<p.size(); k++) {
			ans.append((int)p.get(k).x);
			ans.append(" ");
			ans.append((int)p.get(k).y);
			ans.append("\n");
		}
		
		System.out.print(ans);
	}
	
	static double distance(Point p1, Point p2) {
		return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
	}
	
	static int checkCounterClockwise(Point p1, Point p2, Point p3) {
		double a = (p2.y - p1.y) / (p2.x - p1.x);
		double b = p2.y - a * p2.x;
		if(p1.x == p2.x) {
			if(p3.x == p1.x) {
				return 0;
			} else if((p1.y < p2.y && p3.x < p1.x) || (p1.y > p2.y && p3.x > p1.x)) {
				return 1;
			} else {
				return -1;
			}	
			
		} else {
			if(Math.abs(p3.y - (a * p3.x + b)) < ALLOWABLE_ERROR) {
				return 0;
			} else if((p1.x < p2.x && p3.y - (a * p3.x + b) > 0) || (p1.x > p2.x && p3.y - (a * p3.x + b) < 0)) {
				return 1;
			} else {
				return -1;
			}				
		}
	}

	static class Point {
		double x;
		double y;
		double ang;
		Point(double x, double y) {
			this.x = x;
			this.y = y;
		}
	}
	
	static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if(ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch(IOException e) {
                    e.printStackTrace();
                }
                if(buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
        public boolean hasNext() { skipUnprintable(); return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}