Cod sursa(job #2335003)

Utilizator cristii2000cristiiPanaite Cristian cristii2000cristii Data 3 februarie 2019 14:30:44
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.12 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iomanip>

using namespace std;

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

struct points{
    double x, y;
}point[120005];
int n;

vector<int> s1, s2;

void citire(){
    in >> n;

    for (int i = 1; i <= n; ++i) {
        in >> point[i].x >> point[i].y;
    }
}

long double determinant(int a, int b, int c){
    return point[a].x * point[b].y + point[b].x * point[c].y + point[c].x * point[a].y - point[a].y * point[b].x - point[b].y * point[c].x - point[c].y * point[a].x;
}

bool cmp(points a, points b){
    if(a.x == b.x)
        return  a.y < b.y;
    return a.x < b.x;
}

void leftToRight(){
    s1.push_back (1);
    for (int i = 2; i <= n; ++i) {
        if(s1.size () == 1)
            s1.push_back (i);
        else{
            if(determinant(s1[s1.size () - 2], s1[s1.size () - 1], i) >= 0)
                s1.push_back (i);
            else{
                s1.pop_back ();
                while (s1.size () > 1 && determinant(s1[s1.size () - 2], s1[s1.size () - 1], i) <= 0)
                    s1.pop_back ();
                s1.push_back (i);
            }


        }
    }
}

void rightToLeft(){
    s2.push_back (n);
    for (int i = n - 1; i > 0; --i) {
        if(s2.size () == 1)
            s2.push_back (i);
        else{
            if(determinant(s2[s2.size () - 2], s2[s2.size () - 1], i) >= 0)
                s2.push_back (i);
            else{
                s2.pop_back ();
                while (s2.size () > 1 && determinant(s2[s2.size () - 2], s2[s2.size () - 1], i) <= 0)
                    s2.pop_back ();
                s2.push_back (i);
            }


        }
    }
}


int main() {
    citire();
    sort(point + 1, point + n + 1, cmp);

    leftToRight();
    rightToLeft ();

    out << s1.size () + s2.size () - 2 << "\n";

    out << fixed << setprecision (6);

    for (int i = 0; i < s1.size (); ++i) {
        out << point[s1[i]].x << " " << point[s1[i]].y << "\n";
    }

    for (int i = 1; i < s2.size () - 1; ++i) {
        out << point[s2[i]].x << " " << point[s2[i]].y << "\n";
    }

    return 0;
}