Cod sursa(job #2365615)

Utilizator victorv88Veltan Victor victorv88 Data 4 martie 2019 15:13:08
Problema Triangulare Scor 0
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 2.9 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int t, n, ind_vector,ramase;

struct p
{
    double x, y;
} puncte[55];

class dreapta
{
public:
    double x1, y1, x2, y2;
    dreapta(double x1, double y1, double x2, double y2)
    {
        this->x1=x1;
        this->y1=y1;
        this->x2=x2;
        this->y2=y2;
    }
    dreapta (p a, p b)
    {
        this->x1=a.x;
        this->y1=a.y;
        this->x2=b.x;
        this->y2=b.y;
    }
    dreapta()
    {
        this->x1=this->x2=this->y1=this->y2=0;
    }
};

dreapta drepte_existente[70];



vector<pair<int,int>>sol;

p cautare_mijloc(p a, p b)
{
    p nou;
    nou.x=(a.x+b.x)/2;
    nou.y=(a.y+b.y)/2;
    return nou;
}

double determinant(dreapta dr, p punct)
{
    return (dr.x1*dr.y2+dr.x2*punct.y+punct.x*dr.y1-dr.y2*punct.x-dr.y1*dr.x2-dr.x1*punct.y);
}

bool intre_puncte(dreapta a, p b)
{
    if (a.y1>a.y2)
    {
        if (a.y1>=b.y && a.y2<b.y)
            return true;
    }
    if (a.y1<=b.y && a.y2>b.y)
        return true;
    return false;
}

void cautare_adaugare()
{
    for (int i=1; i<=n-2; ++i)
    {
        for (int j=i+2; j<=n; ++j)
        {
            if (i==1 && j==n)
                continue;
            p punct_mijloc=cautare_mijloc(puncte[i],puncte[j]);
            int nr_bune=0;
            for (int index_dreapta=1; index_dreapta<=n; ++index_dreapta)
            {
                if (intre_puncte(drepte_existente[index_dreapta],punct_mijloc))
                {
                    double d=determinant(drepte_existente[index_dreapta],punct_mijloc);
                    if (d<0)
                    {
                        nr_bune++;
                    }
                    else
                        nr_bune--;
                }
            }
            if (nr_bune!=0 && nr_bune%2==0)
            {
                dreapta d(puncte[i],puncte[j]);
                drepte_existente[ind_vector++]=d;
                ramase--;
                sol.push_back({i,j});
                if (ramase==0)
                    return;
            }
        }
    }
}


int main()
{
    f >> t;
    for (int test=0; test<t; ++test)
    {
        f >> n;
        ind_vector=1;
        ramase=n-3;
        f >> puncte[1].x >> puncte[1].y;
        for (int i=2; i<=n; ++i)
        {
            f >> puncte[i].x >> puncte[i].y;
            dreapta d(puncte[i-1].x,puncte[i-1].y,puncte[i].x,puncte[i].y);
            drepte_existente[ind_vector++]=d;
        }
        dreapta d(puncte[n].x,puncte[n].y,puncte[1].x,puncte[1].y);
        drepte_existente[ind_vector++]=d;
        cautare_adaugare();
        for (auto &v:sol)
        {
            g << v.first-1 <<' ' << v.second-1 << '\n';
        }
        sol.clear();
    }
    return 0;
}