Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define INF 0x3f3f3f3f
- #define pi pair<int,int>
- #define mp make_pair
- #define x first
- #define y second
- #define sqr(x) (x) * (x)
- #define sz(x) static_cast<int>((x).size())
- using namespace std;
- ifstream fin("infasuratoareconvexa.in");
- ofstream fout("infasuratoareconvexa.out");
- int det(pi O, pi A, pi B) {
- return (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y);
- }
- int fcmp(const pi &a, const pi &b) {
- int d = det(mp(0, 0), a, b);
- if(d != 0)
- return d > 0;
- return sqr(a.x) + sqr(a.y) > sqr(b.x) + sqr(b.y);
- }
- int main() {
- int N;
- fin >> N;
- vector<pi> a(N + 1);
- a[0] = mp(INF, INF);
- int first = 0;
- for(int i = 1; i <= N; ++i) {
- fin >> a[i].x >> a[i].y;
- if(a[i].y < a[first].y || (a[i].y == a[first].y && a[i].x < a[first].x))
- first = i;
- }
- a[0] = a[first];
- a[first] = a[1];
- a[1] = a[0];
- for(int i = 1; i <= N; ++i) {
- a[i].x -= a[0].x;
- a[i].y -= a[0].y;
- }
- sort(a.begin() + 2, a.end(), fcmp);
- int j;
- for(j = 3; j <= N; ++j)
- if(det(a[1], a[2], a[j]))
- break;
- int i = 2;
- --j;
- while(i < j) {
- swap(a[i], a[j]);
- ++i, --j;
- }
- vector<pi> S;
- S.emplace_back(a[1]), S.emplace_back(a[2]);
- for(int i = 3; i <= N; ++i) {
- while(sz(S) > 1 && det(S[sz(S) - 2], S.back(), a[i]) < 0)
- S.pop_back();
- S.emplace_back(a[i]);
- }
- fout << sz(S) << '\n';
- for(const pi &x : S)
- fout << x.x + a[0].x << ' ' << x.y + a[0].y << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement