Cod sursa(job #2704795)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 11 februarie 2021 11:52:59
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 120005;
int n;

struct Point{
    double x, y;
    bool operator==(Point p2) {
      if(x == p2.x && y == p2.y)
         return 1;
      return 0;
   }
}v[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
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; ++i){
        fin >> v[i].x >> v[i].y;
    }
    Point start = v[1];
    for (int i = 2; i <= n; ++i){
        if (v[i].x < start.x){
            start = v[i];
        }
        else if (v[i].x == start.x){
            if (v[i].y < start.y){
                start = v[i];
            }
        }
    }
    Point current = start;
    vector <Point> ans;
    ans.push_back(current);
    int contor = 0;
    while (true){
        Point nextTarget = v[1];
        for (int i = 2; i <= n; ++i){
            if (current == v[i]){
                continue;
            }
            int cp = crossProduct(current, nextTarget, v[i]);
            if (cp == 2){
                nextTarget = v[i];
            }
        }
        if (nextTarget == start){
            break;
        }
        else{
            ans.push_back(nextTarget);
            current = nextTarget;
        }
    }
    fout << ans.size() << "\n";
    fout << fixed << setprecision(6);
    for (auto it : ans){
        fout << it.x << " " << it.y << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}