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

import scala.io.StdIn.{readInt,readLine}
import scala.collection.mutable.Stack

object Main {
  def getAngle(p0:(Double,Double),p:(Double,Double)):Double =
    (p._1-p0._1)/Math.sqrt(Math.pow(p._1-p0._1,2)+Math.pow(p._2-p0._2,2))

  def product(p1:(Double,Double),p2:(Double,Double),p3:(Double,Double)):Double =
    (p2._1-p1._1)*(p3._2-p1._2) - (p2._2-p1._2)*(p3._1-p1._1)

  def remove(i:Int,j:Int,sorted:Array[(Double,Double)],n:Int):(Int,Int) = {
    if(product(sorted(j-1),sorted(j),sorted(i)) >= 0) (i,j+1)
    else {
      if(j>1) remove(i,j-1,sorted,n) else if(i==n) (i,j+1) else remove(i+1,j,sorted, n)
    }
  }

  def scan(i:Int,j:Int,sorted:Array[(Double,Double)],n:Int):Int = {
    if(i>n) j
    else {
      val (x,y) = remove(i,j,sorted,n)
      val temp = sorted(y); sorted(y) = sorted(x); sorted(x) = temp
      scan(x+1,y,sorted,n)
    }
  }

  def main(args:Array[String]) = {
    val n = readInt
    var minVal = (Double.MaxValue,Double.MaxValue,0)
    val points = {for(i <- 0 until n) yield readLine.split(" ").map(_.toDouble) match {
        case Array(x,y) =>
          if( (y < minVal._2) || (y==minVal._2 && x < minVal._1) ) minVal = (x,y,i)
          (x,y,i)
      }
    }.toArray
    points(minVal._3) = points(0)
    points(0) = minVal

    val sorted = Array((0D,0D)) ++ points.map{ p =>
      (p._1, p._2, getAngle((minVal._1,minVal._2),(p._1,p._2)))
    }.sortWith(_._3 > _._3).map( p => (p._1,p._2) )

    sorted(0) = sorted(n)
    val m = scan(2,1,sorted,n)
    println(m)
    for(i<-1 to m) { println(sorted(i)._1.toInt + " " + sorted(i)._2.toInt) }
  }
}