Cod sursa(job #2349189)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 20 februarie 2019 11:29:01
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 120007;

typedef pair <double, double> Point;

#define x first
#define y second

Point v[DIM];
Point stiva[DIM];

double cross_product(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 cross_product(v[1], A, B) < 0;
}

int main()
{
	int n;
	in >> n;
	
	for(int i = 1; i <= n; i++)
	{
		in >> v[i].x >> v[i].y;
	}
	
	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 + 1 + n, cmp);
	
	stiva[1] = v[1];
	stiva[2] = v[2];
	
	int k = 2;
	
	for(int i = 3; i <= n; i++)
	{
		while(k >= 2 && cross_product(stiva[k - 1], stiva[k], v[i]) > 0)
			k--;
		
		stiva[++k] = v[i];
	}
	
	out << k << '\n';
	
	for(int i = k; i >= 1; i--)
	{
		out << fixed << setprecision(9) << stiva[i].x << ' ' << stiva[i].y << '\n';
	}
}