Cod sursa(job #902650)

Utilizator palcuiealexAlex Palcuie palcuiealex Data 1 martie 2013 15:49:16
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.56 kb
/***************************************************
 * Alex Palcuie
 * Romania
 * alex [dot] palcuie [at] gmail [dot] com
 * http://palcu.blogspot.com/
 * 2013
****************************************************/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <cstring>

using namespace std;

// Defines
#define NAME(n) #n
#define pr(x) db((x), (#x))
#define prv(v,n) dbv((v), (#v), (n))
#define prw(v) dbw((v), (#v))
#define X first
#define Y second
#define pb push_back

// Helpers
template <typename T>
inline void db(const T x, const char * name){
	cerr << name << " = " << x << '\n';
}
template <typename T> inline void dbv(const T * v, const char * name, int n){
	fprintf(stderr, "=== %s ===\n", name);
	for(int i=0; i<n; i++)
		cerr << v[i] << " ";
	cerr << '\n';
}
template <typename T> inline void dbs(T x){
	cerr << x << ' ';
}

template<typename T>
void dbw(const std::vector<T>& t, const char * name){
	fprintf(stderr, "=== %s ===\n", name);
	unsigned n = t.size();
	for(typename std::vector<T>::size_type i=0; i<n; ++i)
		std::cerr << t[i] << ' ';
	cerr << '\n';
}

// Structs
typedef pair<float,float> Point;

// Constants
const int N = 1<<10;
const int INF = 0x3f3f3f3f;

// Globals
int n;
Point v[N];

// Functions
double cross_product(Point a, Point b, Point c){
    return (b.X - a.X) * (c.Y - a.Y) - (b.Y - a.Y) * (c.X - a.X);
}
bool cmp(Point a, Point b){
    return cross_product(v[0], a, b) < 0;
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("infasuratoare.in","r",stdin);
	freopen("infasuratoare.out","w",stdout);
	#endif

	int i;
	scanf("%d", &n);
	for(i=0; i<n; i++)
        scanf("%f%f", &v[i].X, &v[i].Y);

    Point myMin = v[0]; int myPos=0;
    for(i=1; i<n; i++){
        if(v[i] < myMin){
            myMin = v[i];
            myPos = i;
        }
    }

    swap(v[0], v[myPos]);
    sort(v+1, v+n, cmp);

    stack<int> hull; hull.push(0); hull.push(1);
    for(i=2; i<n; i++){
        while(hull.size() >= 2){
            int top = hull.top(); hull.pop();
            if(cross_product(v[hull.top()], v[top], v[i]) > 0)
                continue;
            else{
                hull.push(top);
                hull.push(i);
                break;
            }
        }
    }

    printf("%d\n", hull.size());
    while(!hull.empty()){
        int k = hull.top(); hull.pop();
        printf("%f %f\n", v[k].X, v[k].Y);
    }


	return 0;
}