Cod sursa(job #1142884)

Utilizator federerUAIC-Padurariu-Cristian federer Data 14 martie 2014 12:56:43
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.5 kb
/*#include<fstream>
#include<algorithm>
#include<vector>
#include<stack>
#include<iomanip>
#define Nmax 120010
#define x first
#define y second
using namespace std;
typedef pair<double, double> Point;
vector<Point>V;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
Point S[Nmax];
int top;

double cross_product(const Point &A, const Point &B, const Point &C)
{
	return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

bool cmp(const Point &a, const Point &b)
{
	return cross_product(V[0], a, b)<0;
}

void Sort()
{
	int pos = 0, i;
	for (i=1; i < V.size();++i)
	if (V[i] < V[pos])
		pos = i;
	sort(V.begin() + 1, V.end(), cmp);
}

void ConvexHull()
{
	Sort();
	top = 0;
	S[++top] = V[0];
	S[++top] = V[1];
	for (int i = 2; i < V.size(); ++i)
	{
		while (cross_product(S[top-1], S[top], V[i])>0 && top >= 2)
			top--;
		S[++top] = V[i];
	}
}

int main()
{
	int N;
	double a, b;
	fin >> N;
	for (int i = 1; i <= N; ++i)
	{
		fin >> a >> b;
		Point p = make_pair(a, b);
		V.push_back(p);
	}
	ConvexHull();
	fout << top << '\n';
	fout << fixed;
	for (int i = 1; i <= top; ++i)
		fout << setprecision(12)<<S[i].x << ' ' << S[i].y << '\n';

	return 0;
}*/
#include <cstdio>

#include <algorithm>
#include <fstream>
#include <iomanip>

using namespace std;

#define x first
#define y second

typedef pair<double, double> Point;

ifstream fin("infasuratoare.in");
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;
}

inline double cross_product(const Point& A, const Point& B, const Point& C) {
	return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

inline int cmp(const Point& p1, const Point& p2) {
	return cross_product(v[1], p1, p2) < 0;
}

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]) > 0)
			--head;
		stack[++head] = v[i];
	}
}

void write() {
	fout << fixed;
	fout << head << "\n";
	for (int i = head; i >= 1; --i)
		fout << setprecision(9) << stack[i].x << " " << stack[i].y << "\n";
}

int main() {
	read();
	convex_hull();
	write();
}