Cod sursa(job #1580979)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 26 ianuarie 2016 13:23:07
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.83 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;
long double det(point p, point q, point r)
{
    return (long double)q.x*r.y + (long double)r.x*p.y + (long double)p.x*q.y - (long double)q.y*r.x - (long double)r.y*p.x - (long double)p.y*q.x;
}
long double pow(double v) {
    return (long double) v*v;
}
long double dist(point a)
{
    return pow(a.x-o.x) + pow(a.y-o.y);
}

bool cmp(point a, point b)
{
    return ((double)(a.y-o.y)*(b.x-o.x)<=(double)(a.x-o.x)*(b.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;
    int index = 0;
    pv.push_back(p);
    for (int i = 1; i < n; i++)
    {
        f>>x>>y;
        if (x < o.x) { index = i; o.x = x;o.y = y;}
        if (x == o.x && y < o.y)  { index = i; o.x = x;o.y = y;}
        p.x = x;
        p.y = y;
        pv.push_back(p);
    }

    swap(pv[index],pv[0]);

    sort(pv.begin()+1, pv.end(), cmp);
    //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';
    }

}