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# (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            Point[] G = new Point[n];

            for (int i = 0; i < n; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                G[i] = new Point(input[0], input[1]);
            }

            Array.Sort(G);  // (a,b) => orderby a.y-b.y  thenby a.x-b.x

            Point startP = G[0];
            Point nextP = new Point();
            Point target = new Point();
            double minRd = 1.0;

            for (int i = 1; i < n; i++)
            {
                double r = Math.Atan2((G[i].y - startP.y), (G[i].x - startP.x));

                if (r < minRd)
                {
                    nextP = G[i];
                    minRd = r;
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(startP.ToString());
            int addCnt = 1;

            do
            {
                double maxRd = 0.0;

                for (int i = 0; i < n; i++)
                {
                    if (G[i] != startP || G[i] != nextP)
                    {
                        double rd = CalcAngle(startP, nextP, G[i]);

                        if (rd >= maxRd)
                        {
                            target = G[i];
                            maxRd = rd;
                        }
                    }
                }

                startP = nextP;
                nextP = target;

                sb.AppendLine(startP.ToString());
                addCnt++;

            } while (target.x != G[0].x || target.y != G[0].y);

            Console.WriteLine(addCnt);
            Console.Write(sb);
        }

        static double CalcAngle(Point a, Point b, Point c)
        {
            double baX = a.x - b.x;
            double baY = a.y - b.y;
            double bcX = c.x - b.x;
            double bcY = c.y - b.y;

            double babc = baX * bcX + baY * bcY;
            double ban = baX * baX + baY * baY;
            double bcn = bcX * bcX + bcY * bcY;

            return Math.Acos(babc / Math.Sqrt(ban * bcn));
        }
    }

    class Point : IComparable<Point>
    {
        static readonly double EPS = 1E-10;

        internal double x, y;

        public Point() { }

        public Point(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double Abs { get { return Math.Sqrt(Norm); } }
        public double Norm { get { return x * x + y * y; } }

        public double Dot(Point p) { return x * p.x + y * p.y; }
        public double Cross(Point p) { return x * p.y - y * p.x; }

        public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); }
        public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); }
        public static Point operator *(Point p, double d) { return new Point(p.x * d, p.y * d); }
        public static Point operator /(Point p, double d) { return new Point(p.x / d, p.y / d); }
        public static bool operator <(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; }
        public static bool operator >(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; }
        public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; }
        public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; }

        public int CompareTo(Point other)
        {
            return y != other.y ? (int)(y - other.y) : (int)(x - other.x);
        }

        public override bool Equals(object obj)
        {
            Point other = obj as Point;
            return Math.Abs(x - other.x) < EPS && Math.Abs(y - other.y) < EPS;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", x, y);
        }
    }

    class Segment
    {
        internal Point p1, p2;

        public Segment(Point p1, Point p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    class Circle
    {
        internal Point p;
        internal double r;

        public Circle(Point p, double r)
        {
            this.p = p;
            this.r = r;
        }
    }
}