Cod sursa(job #2557791)

Utilizator Anastasia11Susciuc Anastasia Anastasia11 Data 25 februarie 2020 23:40:24
Problema Infasuratoare convexa Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.8 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <iomanip>
#define pii pair <double, double>
#define fs first
#define sc second
#define Nmax 120005

using namespace std;

ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");

int n;
pii a[Nmax];
int st[Nmax], top;
bool used[Nmax];

double det(int x, int y, int z)
{
    double x1=a[x].fs, y1=a[x].sc;
    double x2=a[y].fs, y2=a[y].sc;
    double x3=a[z].fs, y3=a[z].sc;

    double ans=0;
    ans+=x1*y2+y1*x3+x2*y3;
    ans-=x3*y2+y3*x1+y1*x2;
    return ans;
}

int main()
{
    f >> n;
    for (int i = 1; i <= n; i++) f >> a[i].fs >> a[i].sc;

    sort (a+1, a+n+1);

    //for (int i = 1; i <= n; i++)
        //cout << a[i].fs << " " << a[i].sc << '\n';

    //cout << '\n';

    st[1]=1, st[2]=2;
    used[2]=1;
    top=2;

    for (int i = 3; i <= n; i++)
    {
        while (top > 1 && det(st[top-1], st[top], i) > 0)
        {
            used[st[top]]=0;
            top--;
        }
        top++;
        st[top]=i;
        used[st[top]]=1;

        for (int j = 1; j <= top; j++)
            cout << st[j] << " ";
        cout << '\n';

    }
    cout << '\n';
    for (int i = n-1; i >= 1; i--)
    {
        if (used[i] == 1) continue;

        while (top > 1 && det(st[top-1], st[top], i) > 0)
        {
            used[st[top]]=0;
            top--;
        }
        top++;
        st[top]=i;
        used[st[top]]=1;
        for (int j = 1; j <= top; j++)
            cout << st[j] << " ";
        cout << '\n';
    }

    g << top-1 << '\n';
    for (int i = top-1; i >= 1; i--)
        g << fixed << setprecision(12) << a[st[i]].fs << " " << fixed << setprecision(12) << a[st[i]].sc << '\n';


    return 0;
}