Cod sursa(job #1132371)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 3 martie 2014 01:37:51
Problema Overlap Scor 44
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.67 kb
#include <map>
#include <fstream>
#include <algorithm>

using namespace std;

const int sinus[] = {0, 1, 0, -1};
const int cosinus[] = {1, 0, -1, 0};
const int NMax = 810;
int n;
int nr_noduri;
int ans[NMax], sol[NMax];
bool viz[NMax];
int gradext[NMax];
struct punct
{
    int x, y, ind;
    punct() {x = y = 0;}
    punct(const int x, const int y, const int ind)
    {
        this -> x = x;
        this -> y = y;
        this -> ind = ind;
    }
    punct(const int x, const int y)
    {
        this -> x = x;
        this -> y = y;
    }
    bool operator < (const punct &other) const
    {
        if (x == other.x)
            return y < other.y;
        return x < other.x;
    }
};
punct a[NMax];
map <punct, int> mp;
int G[NMax];

void Read()
{
    ifstream f("overlap.in");
    f>>n;
    for (int i = 1; i<=n; ++i)
    {
        int x, y; f>>x>>y;
        a[i] = punct(x, y, i);
    }
    f.close();
}

inline void DFS(const int &node, const int &type)
{
    if (node == 0)
        return ;
    ++nr_noduri;
    viz[node] = true;
    ans[node] = type;
    int nxt = G[node];
    if (!viz[nxt])
        DFS(nxt, 3 - type);
}

void ReInit()
{
    for (int i = 1; i<=n; ++i)
    {
        viz[i] = false;
        G[i] = 0;
        ans[i] = gradext[i] = 0;
    }
}

void Write()
{
    ofstream g("overlap.out");
    for (int i = 1; i <= n; ++i)
        sol[a[i].ind] = ans[i];
    for (int i = 1; i <= n; ++i)
        g<<sol[i]<<"\n";
    g.close();
}

inline int BS(const int &x, const int &y)
{
    int st = 1, dr = n, mij;
    while(st <= dr)
    {
        mij = (st+dr)>>1;
        if (a[mij].x == x)
        {
            if (a[mij].y == y)
                return mij;
            if (a[mij].y < y)
                st = mij + 1;
            else
                dr = mij - 1;
        }
        if (a[mij].x < x)
            st = mij + 1;
        else
            dr = mij - 1;
    }
    return -1;
}

void Solve()
{
    /**
    matricea x'  =  matricea cos alfa    - sin alfa  *  matricea    x
             y'              sin alfa      cos alfa                 y
    deci : x' = x * cos(alfa) - y * sin(alfa)
           y' = x * sin(alfa) + y * cos(alfa)
    */
    sort(a+1, a+n+1);
    for (int k = 0; k < 4; ++k)
    {
        int x = a[1].x, y = a[1].y;
        int newx, newy;
        newx = x * cosinus[k] - y * sinus[k];
        newy = x * sinus[k] + y * cosinus[k];
        for (int i = 2; i<=n; ++i)
        {
            int shift_x = a[i].x - newx, shift_y = a[i].y - newy;
            G[1] = i;
            ++gradext[1];
            for (int j = 2; j <= n; ++j)
            {
                int nowx, nowy;
                int x = a[j].x, y = a[j].y;
                nowx = x * cosinus[k] - y * sinus[k]; nowx += shift_x;
                nowy = x * sinus[k] + y * cosinus[k]; nowy += shift_y;
                int position = BS(nowx, nowy);
                if (position == -1)
                    continue;
                G[j] = position;
                ++gradext[j];
            }
            bool OK = true;
            int nr_visited = 0;
            for (int j = 1; j<=n && OK; ++j)
                if (!viz[j] && gradext[j])
                {
                    nr_noduri = 0;
                    DFS(j, 1);
                    nr_visited += nr_noduri;
                    if (nr_noduri & 1)
                        OK = false;
                }
            if (OK && nr_visited == n)
            {
                Write();
                return ;
            }
            ReInit();
        }
    }
}

int main()
{
    Read();
    Solve();
    return 0;
}