Cod sursa(job #2942231)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 19 noiembrie 2022 13:34:39
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
#include <fstream>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
 
struct point
{
    long long x, y;
 
    bool operator < (const point &that) const
    {
        return (y < that.y) || (y == that.y && x < that.x);
    }
};
 
int n, m;
 
int st[100005];
 
point v[100005];
 
long long 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)
{
    long long cp = cross_product(v[1], a, b);
    if (cp == 0) {
        return cross_product({v[1].x, (long long)-1e9}, a, b) < 0;
    }
    return cp < 0;
}
 
int main()
{
    f >> n;
 
    for (int i = 1; i <= n; i++)
        f >> v[i].x >> v[i].y;
 
    for (int i = 2; i <= n; i++)
        if (v[i] < v[1])
            swap(v[i], v[1]);
 
    sort(v + 2, v + n + 1, cmp);
    cout << "in_x = np.array(["; for (int i = 1; i <= n; i++) cout << v[i].x << ", "; cout << "])\n";
    cout << "in_y = np.array(["; for (int i = 1; i <= n; i++) cout << v[i].y << ", "; cout << "])\n";
    // cout << cross_product(v[n-1], v[1], v[n]);
 
    m = 2;
    st[1] = 1;
    st[2] = 2;
 
    for (int i = 3; i <= n; i++)
    {
        while (m > 2 && cross_product(v[st[m - 1]], v[st[m]], v[i]) >= 0)
            m--;
 
        m++;
        st[m] = i;
    }
 
    int start = m;
    for (int i = 1; i <= m; i++)
        if (v[st[i]] < v[start])
            start = i;

    g << m << "\n";
    for (int i = start; i >= 1; i--)
        g << v[st[i]].x << " " << v[st[i]].y << "\n";
    for (int i = m; i > start; i--)
        g << v[st[i]].x << " " << v[st[i]].y << "\n";
 
    return 0;
}