Cod sursa(job #2745788)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 26 aprilie 2021 23:55:37
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define imax INT_MAX
#define llmax LLONG_MAX
#define sz(x) (int((x).size()))
#define start ios_base::sync_with_stdio(false), cin.tie(0)
#define finish fin.close(), fout.close()


using ll = long long;
using uint = unsigned int;

const string filename = "infasuratoare";

fstream fin(filename + ".in");
ofstream fout(filename + ".out");

const int nmx = 120005;


#define x first
#define y second

using pct = pair<double, double>;

int n;
pct a[nmx];
vector<pct> st;

inline double crossP(pct &a, pct &b, pct &c)
{
	return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

inline bool cmp(pct &p1, pct &p2)
{
	return crossP(a[1], p1, p2) < 0;
}

int main()
{
	start;

	int top;

	fin >> n;
	for(int i = 1; i <= n; ++i)
		fin >> a[i].x >> a[i].y;

	int p = 1;
	for(int i = 2; i <= n; i++)
		if(a[i] < a[p])
			p = i;
	swap(a[1], a[p]);
	sort(a + 2, a + n + 1, cmp);

	st.pb(a[1]);
	st.pb(a[2]);

	for(int i = 3; i <= n; i++){
		int top = st.size() - 1;
		while(top >= 2 && crossP(st[top - 1], st[top], a[i]) > 0)
		{
			top--;
			st.pop_back();
		}
		st.pb(a[i]);
	}

	fout << st.size() << '\n';

	reverse(all(st));

	fout << fixed;
	for(auto i : st)
		fout << setprecision(15) << i.x << ' ' << i.y << '\n';

	finish;	
	return 0;	
}