Cod sursa(job #826544)

Utilizator shuleavSulea Vlad shuleav Data 30 noiembrie 2012 20:58:57
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<fstream>
#include <algorithm>
#include <iomanip>
#define max 120003
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
struct Punct{
    double x,y;
};
Punct a[max],stack[max];
int n,i,k;
bool cmp(Punct a, Punct b)
{
    if(a.x < b.x || (a.x == b.x && a.y < b.y)) return true;
    return false;
}
int semn(Punct a , Punct b, Punct c)
{
    if((b.x * c.y + a.x * b.y + c.x * a.y
         - a.y * b.x - c.x * b.y - a.x * c.y) >=0) return 1;
    else  return 0;
}

int main()
{
    f>>n;
    for(i = 1; i <= n; i++)     f>>a[i].x>>a[i].y;
    sort(a + 1, a + n + 1, cmp);
    stack[1]=a[1];
    k = 1;
    for(i = 2; i <= n; i++)
    {
        while(k > 1 && !semn(stack[k-1], stack[k], a[i])) k--;
        stack[++k]=a[i];
    }
    for(i = n - 1; i >= 1; i--)
    {
        while(!semn(stack[k-1], stack[k], a[i]))k--;
        stack[++k]=a[i];
    }
    g<<fixed; g<<setprecision(6);
    g<<k-1<<'\n';
    for(i = 1; i <= k-1 ; i++) g<< stack[i].x <<" "<<stack[i].y<<'\n';
    g.close();
    return 0;
}