Cod sursa(job #889418)

Utilizator andunhillMacarescu Sebastian andunhill Data 24 februarie 2013 15:05:56
Problema Infasuratoare convexa Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include<fstream>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<ctime>
using namespace std;

#define eps 0.000000000001

clock_t start=clock();

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

int N;
pair<double, double>points[120009];

int vf;
int stiva[120009];

int semn(pair<double, double>i, pair<double, double>j, pair<double, double>k)
{
	return (j.first - i.first) * (k.second - i.second) -
		(j.second - i.second) * (k.first - i.first);
}

bool cmp(pair<double, double>p1, pair<double, double>p2)
{
	return semn(points[1], p1, p2) > 0;
}

int main()
{
	int i, j;

	f>>N;
	for(i = 1, j = 1; i <= N; i++)
	{
		f>>points[i].first>>points[i].second;
		if(points[i].second - points[j].second <= 0)
			j = i;
		else if(points[i].second == points[j].second && points[i].first - points[j].first <= 0)
			j = i;

	}

	swap(points[1], points[j]);
	sort(points + 2, points + N + 1, cmp);

	stiva[1] = 1; stiva[2] = 2; vf = 2;

	for(i = 3; i <= N; i++)
	{
		while(vf >= 2 && semn(points[stiva[vf - 1]], points[stiva[vf]], points[i]) <= 0)
			vf--;
		stiva[++vf] = i;
	}

	g<<vf<<'\n';
	for(i = 1; i <= vf; i++)
		g<<fixed<<setprecision(6)<<points[stiva[i]].first<<" "<<points[stiva[i]].second<<'\n';

    cout << 1.0*(clock()-start)/(1.0*CLOCKS_PER_SEC) << '\n';

    f.close();
    g.close();
    return 0;
}