Cod sursa(job #1074920)

Utilizator Juve45UAIC Alexandru Ionita Juve45 Data 8 ianuarie 2014 09:52:01
Problema Infasuratoare convexa Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.28 kb
#include <fstream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <iostream>

#define dmax 120000
#define inf 1000000006

using namespace std;

//ifstream fin("infasuratoare.in");
//ofstream fout("infasuratoare.out");

struct point
{

    double x,y;
};

vector <point> v(dmax);
vector <point> hull;
point a,b;
int n;
double s;

//functions declaration

void read();
//reads the input file

double area(point x1, point x2, point x3);
// calculates the area of the triangle formed by the two points x1 & x2 with the origin O(0,0)

bool compare(point p1, point p2);
// compares the two polar angles

double polar(point p1);
// returnes the polar angle of the point p1

void write();


//END of declarations

int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    read();
    a=v[0];

    sort(v.begin()+1, v.begin()+n, compare);

    hull.push_back(a);
    hull.push_back(v[1]);

    for(int i=2; i<n; i++)
    {
        while(area(hull[hull.size()-2],hull[hull.size()-1],v[i])>0 && hull.size()>2)
            hull.pop_back();
        hull.push_back(v[i]);
    }
    write();
    return 0;
}

void read()
{
    int poz=0;
    scanf("%i",&n);
    //cin>>n;
    for(int i=0; i<n; i++)
    {
        scanf("%lf%lf",&v[i].x,&v[i].y);
        //cin>>v[i].x>>v[i].y;
        if(v[i].x<v[poz].x)
            poz=i;
        else if(v[i].x==v[poz].x)
            if(v[i].y>v[poz].y)
                poz=i;

    }
    swap(v[0],v[poz]);
}

double area(point p1, point p2, point p3)
{
    return (p1.x*p2.y+p1.y*p3.x+p2.x*p3.y-p2.x*p1.y-p1.x*p3.y-p3.x*p2.y)/2;
}

bool compare(point p1, point p2)
{
    return (polar(p1)>polar(p2));

}

double polar(point x1)
{
    point p1;
    p1.x=x1.x-a.x;
    p1.y=x1.y-a.y;

    return p1.y/(sqrt(p1.y*p1.y+p1.x*p1.x));
}

void write()
{

    printf("%i%c",hull.size(),'\n');//fout<<hull.size()<<'\n';
     printf("%.12f%c%.12f%c",hull[0].x,' ',hull[0].y,'\n');//fout<<fixed<<setprecision(12)<<hull[0].x<<' '<<hull[0].y<<'\n';
    for(int i=hull.size()-1; i>0; i--)
        printf("%.12f%c%.12f%c",hull[i].x,' ',hull[i].y,'\n');
        //fout<<fixed<<setprecision(12)<<hull[i].x<<' '<<hull[i].y<<'\n';
}