Cod sursa(job #3273850)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 4 februarie 2025 10:12:17
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
struct Punct {
    long long x, y;
    //int idx;
} p[200002];
long long n, i, j, pozMi, top;
Punct rasp[200002];

static inline long long Det(Punct p1, Punct p2, Punct p3) {
    long long det = (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
    return det;
}

static inline bool Cmp(Punct a, Punct b) {
    int det = Det({0, 0}, a, b);

    if(det != 0) return det > 0;
    return a.x * a.x + a.y * a.y > b.x * b.x + b.y * b.y;
}

int main() {
    fin >> n;

    int pozMi = 1;
    for(i = 1; i <= n; i++) {
        fin >> p[i].x >> p[i].y;
        //p[i].idx = 1;

        if(p[i].y < p[pozMi].y || (p[i].y == p[pozMi].y && p[i].x < p[pozMi].x)) {
            pozMi = i;
        }
    }

    p[0] = p[pozMi];
    p[pozMi] = p[1];
    p[1] = p[0];
    for(i = 1; i <= n; i++) {
        p[i].x -= p[0].x;
        p[i].y -= p[0].y;
    }

    sort(p + 2, p + n + 1, Cmp);

    j = 3;
    for(j = 3; j <= n; j++) {
        if(Det(p[1], p[2], p[j]) != 0) break;
    }

    reverse(p + 2, p + j);

    rasp[++top] = p[1];
    rasp[++top] = p[2];
    for(i = 3; i <= n; i++) {
        while(2 <= top && Det(rasp[top - 1], rasp[top], p[i]) < 0) top--;
        rasp[++top] = p[i];
    }

    fout << top << "\n";
    for(i = 1; i <= top; i++) {
        fout << rasp[i].x + p[0].x << " " << rasp[i].y + p[0].y << "\n";
    }

	return 0;
}