Cod sursa(job #1515631)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 1 noiembrie 2015 22:25:14
Problema Overlap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.46 kb
#include <fstream>
#include <algorithm>
#include <cstring>

#define DIM 800 + 5
#define x first
#define y second.first
#define index second.second

using namespace std;

ifstream fin("overlap.in");
ofstream fout("overlap.out");

pair<int, pair<int, int> > points[DIM];

int n;

bool vis[DIM];

int f[DIM], pointMatch[DIM], solution[DIM];

int cnt, found;

void path(int point, bool x) {

    if (point == 0 || vis[point])
        return;

    cnt++;

    vis[point] = true;
    solution[point] = x + 1;

    path(pointMatch[point], x ^ 1);

}

bool binarySearch(pair<int, pair<int, int> > p) {

    int left = 1, right = n;

    while(left <= right) {

        int middle = (left + right) / 2;

        if(p.x == points[middle].x && p.y == points[middle].y) {

            found = points[middle].index;

            return true;

        }

        if(p.x < points[middle].x)
            right = middle - 1;
        else if (p.x > points[middle].x)
            left = middle + 1;
        else if (p.y < points[middle].y)
            right = middle - 1;
        else
            left = middle + 1;

    }

    return 0;

}

int main() {

    fin >> n;

    for (int i = 1; i <= n; i++) {

        fin >> points[i].x >> points[i].y;

        points[i].index = i;

    }

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

    for (int step = 1; step <= 4; step++) {

        for (int i = 2; i <= n; i++) {

            pair< int, pair<int,int> > crtPoint = make_pair(-points[i].y, make_pair(points[i].x, points[i].index));

            for (int j = 1; j < step; j++)
                crtPoint = make_pair(-crtPoint.y, make_pair(crtPoint.x, crtPoint.index));

            int tx = points[1].x - crtPoint.x;
            int ty = points[1].y - crtPoint.y;

            memset(vis, false, sizeof vis);
            memset(f, 0, sizeof f);
            memset(pointMatch, 0, sizeof pointMatch);

            for (int j = 1; j <= n; j++) {

                crtPoint = make_pair(-points[j].y, make_pair(points[j].x, points[j].index));

                for (int k = 1; k < step; k++)
                    crtPoint = make_pair(-crtPoint.y, make_pair(crtPoint.x, crtPoint.index));

                crtPoint.x += tx;
                crtPoint.y += ty;

                if(!binarySearch(crtPoint))
                    continue;

                pointMatch[points[j].index] = found;
                f[found]++;

            }

            bool ok = true;

            for (int j = 1; j <= n; j++) {

                if (!f[points[j].index]) {

                    cnt = 0;

                    path(points[j].index, 1);

                    if (cnt % 2 == 1){

                        ok = false;
                        break;

                    }

                }

            }

            if(!ok)
                continue;

            for (int j = 1; j <= n; j++) {

                if (!vis[points[j].index]) {

                    cnt = 0;

                    path(points[j].index, 1);

                    if(cnt % 2 == 1) {

                        ok = false;
                        break;

                    }

                }

            }

            if (!ok)
                continue;

            for (int j = 1; j <= n; j++) {

                fout << solution[j] << "\n";

            }

            return 0;

        }

    }


    return 0;

}

//Miriam e tare!