Cod sursa(job #2171336)

Utilizator Horia14Horia Banciu Horia14 Data 15 martie 2018 11:58:51
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include<cstdio>
#include<algorithm>
#define MAX_N 120000
using namespace std;

struct point {
    double x, y;
};

point p[MAX_N + 1], st[MAX_N + 1];
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",&p[i].x,&p[i].y);
    fclose(fin);
}

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

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

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

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

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

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