Cod sursa(job #2586920)

Utilizator OvidRata Ovidiu Ovid Data 21 martie 2020 19:00:25
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
ifstream fin("infasuratoare.in"); ofstream fout("infasuratoare.out");



int n;
pair<double, double> p[120000];



double cross_product( pair<double, double> &p1, pair<double, double> &p2, pair<double, double> &p3 ){

return (p2.ft-p1.ft)*(p3.sc-p2.sc)-(p3.ft-p2.ft)*(p2.sc-p1.sc);

}








int main(){
fin>>n;

int p0=0;
for(int i=0; i<n; i++){
fin>>p[i].ft>>p[i].sc;
if(p[i].sc<p[p0].sc){
p0=i;
}
else{
    if(p[i].sc==p[p0].sc){
        if(p[i].ft<p[p0].ft){
            p0=i;
        }
    }
}

}



vector<pair<double, int> > g;

g.pb(mp(-100, p0) );

for(int i=0; i<n; i++){
    if(i!=p0){
        g.pb( mp(atan2( (p[i].sc-p[p0].sc), (p[i].ft-p[p0].ft )   ), i)    );

    }
}
sort(g.begin(), g.end());






vector<pair<double, double>> h; h.pb(mp( p[g[0].sc].ft, p[g[0].sc].sc ) ); h.pb(mp( p[g[1].sc].ft, p[g[1].sc].sc )); h.pb(mp( p[g[2].sc].ft, p[g[2].sc].sc )); g.erase(g.begin(), g.begin()+3);


while(!g.empty()){



while(cross_product(h[h.size()-3], h[h.size()-2], h[h.size()-1])<=0  ){
    h.erase(h.begin()+h.size()-2, h.begin()+h.size()-1);
}

h.pb(mp( p[g[0].sc].ft, p[g[0].sc].sc ));
g.erase(g.begin(), g.begin()+1);


}
cout<<"\n";

while(cross_product(h[h.size()-3], h[h.size()-2], h[h.size()-1])<=0  ){
    h.erase(h.begin()+h.size()-2, h.begin()+h.size()-1);
}


fout<<h.size()<<"\n";
for(int i=0; i<h.size(); i++){
    fout<<fixed<<setprecision(9)<<h[i].ft<<" "<<h[i].sc<<"\n";
}

return 0;
}