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 C++11 (judge verdict: Accepted)

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#include<functional>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

struct P
{
	double x, y;
	P(double x, double y) :x(x), y(y) {}
	P() :P(0, 0) {}
	P operator+(const P& v)const
	{
		return P(x + v.x, y + v.y);
	}
	P operator-(const P& v)const
	{
		return P(x - v.x, y - v.y);
	}
	P operator*(const double v)const
	{
		return P(x*v, y*v);
	}
	bool operator==(const P& v)const
	{
		return abs(x - v.x) <= 0.0000000000001&&abs(y - v.y) <= 0.00000000000001;
	}
	bool operator!=(const P& v)const
	{
		return abs(x - v.x) > 0.0000000000001 || abs(y - v.y) > 0.00000000000001;
	}

	double dot(const P& v)const
	{
		return x*v.x + y*v.y;
	}
	double cross(const P& v)const
	{
		return x*v.y - y*v.x;
	}
	double length()const
	{
		return sqrt(x*x + y*y);
	}
	double length2()const
	{
		return x*x + y*y;
	}
	
	bool operator<(const P& v)const
	{
		return y != v.y ? y < v.y : x < v.x;
	}
	bool operator>(const P& v)const
	{
		return y != v.y ? y > v.y : x > v.x;
	}
};

int ccw(P a, P b, P c) 
{
	b = b - a; c = c - a;
	if (b.cross(c) > 0)   return +1;			// counter clockwise
	if (b.cross(c) < 0)   return -1;			// clockwise
	if (b.dot(c) < 0)     return +2;			// c--a--b on line
	if (b.length2() < c.length2()) return -2;   // a--b--c on line
	return 0;
}

vector<P> convex_hull(vector<P>& points)
{
	int n = points.size(), k = 0;
	sort(points.begin(), points.end());
	vector<P> ch(2 * n);
	for (int i = 0; i < n; ch[k++] = points[i++]) // lower-hull
	{
		while (k >= 2) 
		{
			int state = ccw(ch[k - 2], ch[k - 1], points[i]);
			if (state > 0 || state < -1)
			{
				break;
			}
			--k;
		}
	}
	for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = points[i--]) // upper-hull
	{
		while (k >= t)
		{
			int state = ccw(ch[k - 2], ch[k - 1], points[i]);
			if (state > 0 || state < -1)
			{
				break;
			}
			--k;
		}
	}
	ch.resize(k - 1);
	return ch;
}

vector<P> points;

int main()
{
	points.reserve(100000);
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		double x, y;
		cin >> x >> y;
		points.push_back(P(x, y));
	}

	vector<P> output = convex_hull(points);

	printf("%d\n", output.size());
	for (auto& v : output)
	{
		printf("%d %d\n", (int)v.x, (int)v.y);
	}
	
	return 0;
}