Cod sursa(job #2433512)

Utilizator AndreiStrAndrei Stroici AndreiStr Data 27 iunie 2019 18:02:35
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
#define X first
#define Y second
#define punct pair<int64_t,int64_t>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
const int N = 120010;
int64_t Det(punct A,punct B,punct C)
{
    return A.X*B.Y+B.X*C.Y+C.X*A.Y
        -( A.Y*B.X+B.Y*C.X+C.Y*A.X );
}
punct P[N],P0,P1;
int64_t x,y;
int n;
bool crit(punct A,punct B)
{
    int64_t aux=Det(P[0],A,B);
    if(aux>=0)return true;
    return false;
}
vector<punct> sol;
int main()
{
    f>>n;
    for(int i=0;i<n;i++)
    {
        f>>x>>y;
        P[i]=make_pair(x,y);
        if(P[i]<P[0])swap(P[i],P[0]);
    }
    sort(P+1,P+n,crit);
    P0=P[0];P1=P[1];
    sol.push_back(P[0]);
    sol.push_back(P[1]);
    for(int i=2;i<n;i++)
    {
        while(Det(P0,P1,P[i])<0)
        {
            sol.pop_back();P1=P0;P0=sol.back();
        }
        P0=P1;P1=P[i];sol.push_back(P1);
    }
    g<<sol.size();
    for(auto it:sol)
        g<<it.X<<' '<<it.Y<<'\n';
    return 0;
}