Cod sursa(job #1057296)

Utilizator PenReturnedIsUNIBUC ion824 mathboy andreiv PenReturnedIs Data 14 decembrie 2013 14:38:51
Problema Triangulare Scor 0
Compilator cpp Status done
Runda ONIS 2014, Runda 1 Marime 2.24 kb
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <unordered_map>
#define Nmax 51
using namespace std;

int T,N;
struct pct
{
    int x,y;
}v[Nmax],st[Nmax];
bitset<Nmax> out;
vector< pair<int,int> > sol;
void read_test();
double det(double x1,double y1,double z1,double x2,double y2,double z2,double x3,double y3,double z3)
{
    return (x1*y2*z3 + x2*y3*z1 + x3*y1*z2 - z1*y2*x3 - z2*y3*x1 - z3*y1*x2);
}
void init_test()
{
    out.reset();
    sol.clear();
}
int count(int x,int y)
{
    int in_right = 0;
    for(int i=1;i<=N;++i)
    {
        if(i == x || i == y || out[i])
            continue;

        if(det(v[x].x, v[x].y, 1, v[y].x, v[y].y, 1, v[i].x, v[i].y, 1) > 0)
            ++in_right;
    }
    return in_right;
}
void remove_in_right(int x,int y)
{
    for(int i=1;i<=N;++i)
    {
        if(i == x || i == y || out[i])
            continue;

        if(det(v[x].x, v[x].y, 1, v[y].x, v[y].y, 1, v[i].x, v[i].y, 1) > 0)
            out[i] = true;
    }
}
void solve()
{
    init_test();
    int a, b,cnt;
    a = 1;
    b = 3;
    int aux;
    for(cnt = 0;cnt<N-3;)
    {
        aux = count(a,b);
        if(aux == 1)
        {
            sol.push_back( make_pair(a, b) );
            remove_in_right(a, b);
            ++cnt;
            do
            {
                ++b;
                if(b>N)b=1;
            }while(out[b]);
        }
        else
        {
            do
            {
                ++a;if(a>N)a=1;
            }while(out[a]);
            b = a+2;
            if(b > N)
                b = b%N;

            while(b<=N && out[b])
            {
                b = b+1;
                if(b > N)
                    b = 1;
            }
        }
    }
}
void write()
{
    for(int i=0;i<sol.size();++i)
        cout<<sol[i].first - 1<<" "<<sol[i].second - 1<<"\n";
}
int main()
{
    freopen("triangulare.in","r", stdin);
    freopen("triangulare.out", "w", stdout);
    cin>>T;
    while(T--)
    {
        read_test();
        solve();
        write();
    }
    return 0;
}
void read_test()
{
    cin>>N;
    for(int i=1;i<=N;++i)
        cin>>v[i].x>>v[i].y;
}