Cod sursa(job #2040828)

Utilizator Horia14Horia Banciu Horia14 Data 16 octombrie 2017 16:42:14
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include<cstdio>
#include<algorithm>
#define MAX_N 120000
using namespace std;

struct point {
    double x, y;
};

point v[MAX_N], st[MAX_N];
int n, head;

void readPoints() {
    FILE *fin = fopen("infasuratoare.in","r");
    fscanf(fin,"%d",&n);
    for(int i=0; i<n; i++)
        fscanf(fin,"%lf%lf",&v[i].x,&v[i].y);
    fclose(fin);
}

inline double det(point a, point b, point c) {
    return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
}

inline int cmp(point a, point b) {
    return det(v[0],a,b) > 0;
}

void sortPoints() {
    int minIndex = 0;
    for(int i=1; i<n; i++)
        if(v[i].y < v[minIndex].y)
            minIndex = i;
        else if(v[i].y == v[minIndex].y && v[i].x < v[minIndex].x)
            minIndex = i;
    swap(v[0],v[minIndex]);
    sort(v+1,v+n,cmp);
}

void convexHull() {
    sortPoints();
    st[0] = v[0];
    st[1] = v[1];
    head = 2;
    for(int i=2; i<n; i++) {
        while(head >= 2 && det(st[head-2],st[head-1],v[i]) < 0)
            head--;
        st[head++] = v[i];
    }
}

void printPoints() {
    FILE *fout = fopen("infasuratoare.out","w");
    fprintf(fout,"%d\n",head);
    for(int i=0; i<head; i++)
        fprintf(fout,"%.6f %.6f\n",st[i].x,st[i].y);
    fclose(fout);
}

int main() {
    readPoints();
    convexHull();
    printPoints();
    return 0;
}