Cod sursa(job #2712600)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 26 februarie 2021 08:51:54
Problema Overlap Scor 52
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.89 kb
#include <bits/stdc++.h>

using namespace std;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }

public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }

    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }

    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

InParser fin("overlap.in");
OutParser fout("overlap.out");

struct pair_hash {
    template<class T1, class T2>
    size_t operator() (const pair<T1, T2> &p) const {
        return hash<T1>()(p.first)^hash<T2>()(p.second);
    }
};

const int NMAX = 808;
int N, x, y, prv[NMAX], nxt[NMAX], sol[NMAX];
pair<int,int> p[NMAX], init[NMAX];
unordered_map<pair<int,int>,int,pair_hash> match;

int main() {
    fin >> N;
    for(int i = 1; i <= N; ++i) {
        fin >> x >> y;
        init[i] = p[i] = make_pair(x, y);
        match[init[i]] = i;
    }
    // rotatie trigonometrica la 90 de grade: (-y, x)
    // rotatie in sensul acelor de ceasornic la 90 de grade: (y, -x)
    for(int rep = 0; rep < 4; ++rep) {
        for(int i = 1; i <= N; ++i) {
            swap(p[i].first, p[i].second);
            p[i].second *= -1;
        }
        for(int i = 2; i <= N; ++i) {
            int diffx = init[i].first - p[1].first,
                diffy = init[i].second - p[1].second;
            memset(prv, 0, sizeof(prv));
            memset(nxt, 0, sizeof(nxt));
            memset(sol, 0, sizeof(sol));
            for(int j = 1; j <= N; ++j)
                if(i != j && match[make_pair(p[j].first + diffx, p[j].second + diffy)]) {
                    int index = match[make_pair(p[j].first + diffx, p[j].second + diffy)];
                    nxt[j] = index;
                    prv[index] = j;
                }
            bool ok = true;
            for(int j = 1; j <= N && ok; ++j) {
                int curr = j;
                while(prv[curr] && prv[curr] != j)
                    curr = prv[curr];
                bool colour = false;
                while(!sol[curr] && curr) {
                    sol[curr] = colour + 1;
                    colour ^= 1;
                    curr = nxt[curr];
                }
                if(colour)
                    ok = false;
            }
            if(ok) {
                for(int j = 1; j <= N; ++j)
                    fout << sol[j] << '\n';
                return 0;
            }
        }
    }
}