Cod sursa(job #3169470)

Utilizator Ana_22Ana Petcu Ana_22 Data 15 noiembrie 2023 00:40:40
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdio>
#include <bits/stdc++.h>
#define NMAX 120000
#define x first
#define y second

using namespace std;

ifstream fin( "infasuratoare.in" );
ofstream fout( "infasuratoare.out" );

typedef pair<double, double> point;
point v[NMAX+1], st[NMAX+1];

double unghirel( point& a, point& b, point& c ) {
    return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x );
}

bool cmp( point& a, point& b ) {
    return unghirel( v[1], a, b ) < 0; // <=> ( b.x - a.x ) * ( c.y - a.y ) < ( b.y - a.y ) * ( c.x - a.x )
                                        // <=> ( c.y - a.y ) / ( c.x - a.x ) < ( b.y - a.y ) / ( b.x - a.x )
                                        // <=> mac < mab ( panta root - c < panta root - b )
}

int n, k;

void infasuratoare() {
    // gasesc cel mai din stanga punct
    int posmin = 1;
    for( int i = 2; i <= n; i++ ) {
        if( v[i] < v[posmin] )
            posmin = i;
    }
    swap( v[1], v[posmin] );
    //sortez dupa unghi toate punctele mai ptn primul din stanga
    sort( v + 2, v + n + 1, cmp );
    
    k = 2;
    st[1] = v[1];
    st[2] = v[2];
    for( int i = 3; i <= n; i++ ) {
        if( k >= 2 && unghirel( st[k-1], st[k], v[i] ) > 0 ) // panta pe care as avea-o cu root si v[i] e mai mica decat cea pe care o am deja
            k--;
        k++;
        st[k] = v[i];
    }
}

int main() {
    int i;
    
    fin >> n;
    for( i = 1; i <= n; i++ )
        fin >> v[i].x >> v[i].y;
    
    infasuratoare();
    
    fout << fixed;
    fout << k << '\n';
    for( i = k; i >= 1; i-- )
        fout << setprecision(9) << st[i].x << ' ' << st[i].y << '\n';
    
    return 0;
}