Cod sursa(job #1773786)

Utilizator elffikkVasile Ermicioi elffikk Data 8 octombrie 2016 11:06:13
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

struct Point {
    double x, y;
};

vector<Point> a, h;

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

bool comparePoints(Point a, Point b) {
    return (a.x<b.x) || (a.x==b.x) && (a.y<b.y);
}

bool compareAngles(Point x, Point y) {
    return det(a[0], x, y)<0;
}

void read() {
    ifstream cin("infasuratoare.in");
    int n;
    cin>>n;
    a.resize(n);
    for (int i = 0; i<n; i++) {
        cin>>a[i].x>>a[i].y;
    }
}

void convexHull() {
    swap(a[0], *min_element(a.begin(), a.end(), comparePoints));
    sort(a.begin()+1, a.end(), compareAngles);
    h.push_back(a[0]);
    h.push_back(a[1]);
    for (int i = 2; i < a.size(); i++) {
        while (h.size()>1 && det(h[h.size()-2], h[h.size()-1], a[i])>0) {
            h.erase(h.end()-1);
        }
        h.push_back(a[i]);
    }
    reverse(h.begin(), h.end());
}

void write() {
    ofstream cout("infasuratoare.out");
    cout<<fixed;
    cout<<h.size()<<"\n";
    for (int i = 0; i < h.size(); i++) {
        cout<<setprecision(9)<<h[i].x<<" "<<h[i].y<<"\n";
    }
}

int main() {
    read();
    convexHull();
    write();
    return 0;
}