Cod sursa(job #2705015)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 11 februarie 2021 19:49:20
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 120005;
int n, z;

struct Point{
    double x, y;
    bool operator==(Point p2) {
      if(x == p2.x && y == p2.y)
         return 1;
      return 0;
   }
}v[nmax], s[nmax];

int crossProduct(Point a, Point b, Point c){
    if (a == b) return 2;
    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 startIndex = 1;
    for (int i = 2; i <= n; ++i){
        if (v[i].x < start.x){
            start = v[i];
            startIndex = i;
        }
        else if (v[i].x == start.x){
            if (v[i].y < start.y){
                start = v[i];
                startIndex = i;
            }
        }
    }
    swap(v[startIndex], 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 = z; i >= 1; --i){
        fout << v[i].x << " " << v[i].y << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}