Cod sursa(job #1970614)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 19 aprilie 2017 14:45:43
Problema Infasuratoare convexa Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 2.86 kb
//Convex Hull
//Graham Scan
#include <bits/stdc++.h>
using namespace std;
ofstream g("infasuratoare.out");
int N;

#define DIM (1<<21)
char buff[DIM];
int poz=0;

void read(int &numar)
{
     numar = 0;
     //cat timp caracterul din buffer nu e cifra ignor
     while (buff[poz] < '0' || buff[poz] > '9')
          //daca am "golit" bufferul atunci il umplu
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     //cat timp dau de o cifra recalculez numarul
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
}

void read(double &numar)
{
     numar = 0;
     char semn='+';
     //caut inceputul numarului
     while (buff[poz] < '0' || buff[poz] > '9')
     {
          semn = buff[poz];
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     //citesc partea de dinainte de virgula
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     //daca are virgula citesc si partea de dupa
     if (buff[poz] == '.')
     {
          double tmp,cnt;
          tmp = 0;
          cnt = 1;
          ++poz;
          while ('0'<=buff[poz] && buff[poz]<='9')
          {
               cnt = cnt/10;
               tmp = tmp + cnt*(double(buff[poz]-'0'));
               if (++poz == DIM)
                    fread(buff,1,DIM,stdin),poz=0;
          }
          numar += tmp;
     }
     //daca are semnul minus il fac negativ
     if (semn == '-')
          numar = - numar;
}

struct Point
{
    double x;
    double y;

} V[120001],Stack[120001];
int head;

inline double cross_product(const Point& A, const Point& B, const Point& C) {
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}


inline bool cmp(const Point& p1, const Point& p2)
{
    return cross_product(V[1], p1, p2) < 0;
}


void sort_points()
{
    int pos = 1;
    for (int i = 2; i <= N; ++i)
        if (V[i].x < V[pos].x)
            pos = i;
    swap(V[1],V[pos]);
    sort(V+2,V+N+1, cmp);
}

void convex_hull()
{

    Stack[1] = V[1];
    Stack[2] = V[2];
    head = 2;
    for (int i = 3; i <= N; ++i)
    {
        while (head >= 2 && cross_product(Stack[head - 1], Stack[head], V[i]) > 0)  --head;
        Stack[++head] = V[i];
    }
}

int main()
{
    freopen ("infasuratoare.in", "r", stdin);
    read(N);
    for(int i=1; i<=N; i++)
    {
        double x,y;
        read(x);read(y);
        V[i]={x,y};
    }
    sort_points();
    convex_hull();
    g<< fixed;
    g<< head << "\n";
    for (int i = head; i >= 1; --i)
    g << setprecision(9) << Stack[i].x << " " << Stack[i].y << "\n";
}