Cod sursa(job #2551628)

Utilizator luci.tosaTosa Lucian luci.tosa Data 20 februarie 2020 00:28:44
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.04 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#define NMAX 120005
using namespace std;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
int n,na=0,nb=0,top,nrp=0;
struct coord {
    double x,y;
} v[NMAX],a[NMAX],b[NMAX],downp,upp,stiva[NMAX];

int aria(coord p1,coord p2,coord p3) {
    int aria=p1.x*p2.y + p2.x*p3.y + p1.y*p3.x - p2.y*p3.x - p1.x*p3.y - p1.y*p2.x;
    if(aria < 0)
        return -1;
    else if(aria > 0)
        return 1;
    else
        return 0;
}
bool cmp1(coord x,coord y) {
    if(x.x<y.x) return 1;
    else if(x.x==y.x && x.y<y.y) return 1;
    else return 0;
}
bool cmp2(coord x,coord y) {
    if(x.x>y.x) return 1;
    else if(x.x==y.x && x.y>y.y) return 1;
    else return 0;
}
int main() {
    fin>>n;
    downp.x=downp.y=1000000000;
    upp.y=upp.x=-1000000000;
    for(int i=1; i<=n; i++) {
        fin>>v[i].x>>v[i].y;
        //v[i].x+=1000000000;
        //v[i].y+=1000000000;
        if(v[i].y<downp.y || (v[i].y==downp.y && v[i].x<downp.x))
            downp=v[i];
        if(v[i].y>upp.y || (v[i].y==upp.y && v[i].x>upp.x))
            upp=v[i];
    }
    a[++na]=downp;
    b[++nb]=upp;
    for(int i=1; i<=n; i++)
        if(aria(downp,upp,v[i])==-1)
            a[++na]=v[i];
        else if(aria(downp,upp,v[i])==1)
            b[++nb]=v[i];
    sort(a+1,a+na+1,cmp1);
    sort(b+1,b+na+1,cmp2);

    top=1;
    stiva[top]=a[1];
    for(int i=2; i<=na; i++)
        if(top==1)
            stiva[++top]=a[i];
        else {
            while(aria(stiva[top-1],stiva[top],a[i])==-1 && top>1)
                top--;
            stiva[++top]=a[i];
        }
    nrp+=top;

    stiva[++top]=b[1];
    for(int i=2; i<=nb; i++)
        if(top==1)
            stiva[++top]=b[i];
        else {
            while(aria(stiva[top-1],stiva[top],b[i])==-1 && top-nrp>1)
                top--;
            stiva[++top]=b[i];
        }
    nrp=top;

    fout<<setprecision(6)<<fixed;
    fout<<nrp<<'\n';
    for(int i=1; i<=nrp; i++)
        fout<<stiva[i].x<<" "<<stiva[i].y<<'\n';
    return 0;
}