Cod sursă (job #173424)

Utilizator avatar PetruZ Petru Zatic PetruZ IP ascuns
Problemă Înfășurătoare convexă Compilator cpp | 1,46 kb
Rundă Arhiva de probleme Status evaluat
Dată 15 dec. 2015 14:36:11 Scor 0
#include <fstream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <vector>

using namespace std;

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

#define x first
#define y second

typedef pair <double,double> pn;
       
pn stk[100010],v[100010];;

double ccw(pn p1, pn p2, pn p3) // <0 dup acele cs // >0 impotriva acelor cs // =0 coliniare
{
       return( (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x) );
       }
       
bool cmp(pn p1, pn p2){
     return (ccw(v[1],p1,p2)<0);
     }


int N, head;

///////////////////////////////////////////////////////////////////////////

int main() {
    
    int poz=1;
    
    cin >> N;
    for(int i(1); i<=N; ++i){
            cin >> v[i].x >> v[i].y;
            if(v[i]<v[poz]) poz=i;
            }
     
    swap(v[1],v[poz]);
    sort(v+2, v+N+1, cmp);
    
    stk[1]=v[1]; stk[2]=v[2];
    head=2;
    for(int i=3; i<=N; ++i) {
                             while( (head>=2) && (ccw(stk[head-1],stk[head],v[i])>0) ) --head;
                             stk[++head]=v[i]; 
                              }

    vector<pn> rep (stk+1, stk+head+1);

    head--;    
             
    for(int i=0; i<=head; i++) if( ccw(rep[i],rep[(i+1)%head],rep[(i+2)%head])==0 ) { rep.erase(rep.begin()+i); head--;}

    cout << head << "\n";
    for(int i=head-1; i>=0; i--) cout << rep[i].x << " " << rep[i].y << "\n";

    
return 0;    
}