Cod sursa(job #2705193)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 12 februarie 2021 09:31:20
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 120005;
struct Point{
    double x, y;
}v[nmax], s[nmax];
int n, z;

int crossProduct(Point a, Point b, Point c){
    double area = a.x*b.y+b.x*c.y+c.x*a.y-b.y*c.x-c.y*a.x-a.y*b.x;
    if (area == 0) return 0; // colinear
    if (area > 0) return 1; // c se afla in stanga lui AB
    return 2; // c se afla in dreapta lui AB
}

bool cmp(Point p1, Point p2){
    return crossProduct(v[1], p1, p2) == 1;
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; ++i){
        fin >> v[i].x >> v[i].y;
    }
    Point start = v[1];
    int index = 1;
    for (int i = 2; i <= n; ++i){
        if (v[i].x < start.x){
            index = i;
            start = v[i];
        }
    }
    swap(v[index], v[1]);
    sort(v + 2, v + n + 1, cmp);
    s[++z] = v[1];
    s[++z] = v[2];
    for (int i = 3; i <= n; ++i){
        while (z >= 2 && crossProduct(s[z - 1], s[z], v[i]) == 2) --z;
        s[++z] = v[i];
    }
    fout << z << "\n";
    fout << fixed << setprecision(6);
    for (int i = 1; i <= z; ++i){
        fout << s[i].x << " " << s[i].y << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}