Cod sursa(job #1580198)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 25 ianuarie 2016 17:44:47
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 122000
struct point {
    double x, y;
};
int n;
vector<point> pv, sol;
point o;
double det(point p, point q, point r)
{
    return q.x*r.y + r.x*p.y + p.x*q.y - q.y*r.x - r.y*p.x - p.y*q.x;
}
double pow(double v) {
    return v*v;
}
double dist(point a)
{
    return pow(a.x-o.x) + pow(a.y-o.y);
}
bool Compare(point a, point b) {
    return det(o, a, b) > 0 || (det(o, a, b) == 0  && dist(a) < dist(b));
}
bool compMax(point a, point b) {
    if (a.x == b.x)
        return a.y > b.y;
    return a.x > b.x;
}
int main()
{
    ifstream f("infasuratoare.in");
    ofstream g("infasuratoare.out");

    f>>n;
    double x, y, sx = 0, sy = 0;
    point p;
    f>>x>>y;
    o.x = x;o.y = y;
    p.x = x;
    p.y = y;
    pv.push_back(p);
    for (int i = 1; i < n; i++)
    {
        f>>x>>y;
        if (x < o.x) {o.x = x;o.y = y;}
        if (x == o.x && y < o.y)  {o.x = x;o.y = y;}
        p.x = x;
        p.y = y;
        pv.push_back(p);
    }

    sort(pv.begin(), pv.end(), Compare);
    //rotate(pv.begin(), righty, pv.end());
    sol.push_back(pv[0]);
    sol.push_back(pv[1]);


    for (int i = 2; i < n; i++)
    {

        while (sol.size() >=2 && det(sol[sol.size()-2], sol[sol.size()-1], pv[i]) < 0)
            sol.pop_back();
        sol.push_back(pv[i]);
    }
    g<<sol.size()<<'\n';
    for (int i = 0; i < sol.size(); i++)
    {
        g<<sol[i].x<<' '<<sol[i].y<<'\n';
    }

}