Cod sursa(job #1948231)

Utilizator vasileefrosVasile Efros vasileefros Data 31 martie 2017 21:33:16
Problema Infasuratoare convexa Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include<bits/stdc++.h> 
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#define x first
#define y second

typedef std:: pair<double, double> Point;
 
std:: ifstream fin("infasuratoare.in");
std:: ofstream fout("infasuratoare.out");
 
const int MAX_N = 120005;
 
int n;
Point v[MAX_N];
 
Point stack[MAX_N];
int head;
 
void read() {
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> v[i].x >> v[i].y;
}
 
bool cross_product(const Point& A, const Point& B, const Point& C) {
    return atan2(B.y-A.y, B.x-A.x)>atan2(C.y-A.y, C.x-A.x);
}
 
inline int cmp(const Point& p1, const Point& p2) {
    return cross_product(v[1], p1, p2);
}
 
void sort_points() {	
    int pos = 1;
    for (int i = 2; i <= n; ++i)
        if (v[i] < v[pos])
            pos = i;
    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, cmp);
}
 
void convex_hull() {
    sort_points();
 
    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]))
           --head;
        stack[++head] = v[i];
    }
}
 
    void write() {
 
   fout << head << "\n";
    for (int i = head; i >= 1; --i)
	      fout <<std:: setprecision(9) << stack[i].x << " " << stack[i].y << "\n";
}
 
int main() {
    read();
    convex_hull();
    write();
}