Cod sursa(job #1328916)

Utilizator smaraldaSmaranda Dinu smaralda Data 28 ianuarie 2015 21:16:46
Problema Overlap Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.19 kb
#include<stdio.h>
#include<set>
#include<string.h>
using namespace std;

const int NMAX = 805;

struct POINT {
    int x, y, ind;

    POINT (int x = 0, int y = 0, int ind = 0) {
        this->x = x;
        this->y = y;
        this->ind = ind;
    }

    void read(int ind) {
        scanf("%d%d", &x, &y);
        this->ind = ind;
    }

    bool operator < (POINT other) const {
        if(other.x == x)
            return y < other.y;
        return x < other.x;
    }

    bool operator == (POINT other) const {
        return x == other.x && y == other.y;
    }
};

set <POINT> p;
set <POINT>::iterator it;
POINT orig[NMAX], pts[NMAX];
int xShift, yShift, n, subset[NMAX];

bool ok () {
    int i, num;
    POINT pt;

    memset(subset, 0, sizeof(subset));
    for(i = 1; i <= n; ++ i) {
        if(subset[i])
            continue;

        pt = pts[i];
        it = p.find(POINT(pt.x + xShift, pt.y + yShift));
        if(it == p.end() || subset[it->ind])
            continue;

        num = 1;
        while(it != p.end() && !subset[it->ind]) {
            if(num % 2 == 1) {
                subset[pt.ind] = 1;
                subset[it->ind] = 2;
            }

            ++ num;
            pt = pts[it->ind];
            it = p.find(POINT(pt.x + xShift, pt.y + yShift));
        }

        if(num % 2 == 1)
            return 0;
    }

    for(i = 1; i <= n; ++ i)
        if(!subset[i])
            return 0;
    return 1;
}

int main() {
    freopen("overlap.in", "r", stdin);
    freopen("overlap.out", "w", stdout);
    int num, xAux, yAux, nRot, i;

    scanf("%d", &n);
    for(i = 1; i <= n; ++ i) {
        orig[i].read(i);
        p.insert(orig[i]);

        pts[i] = orig[i];
    }

    for(nRot = 1; nRot <= 4; ++ nRot) {
        for(i = 1; i <= n; ++ i)
            pts[i] = POINT(-pts[i].y, pts[i].x, i);

        for(i = 2; i <= n; ++ i) {
            xShift = orig[i].x - pts[1].x;
            yShift = orig[i].y - pts[1].y;

            if(ok()) {
                for(i = 1; i <= n; ++ i)
                    printf("%d\n", subset[i]);
                return 0;
            }
        }
    }
    return 0;
}