ZROJ#397. 【18提高7】模仿遊戲(爆搜)

自為風月馬前卒發表於2018-10-15

題意

題目連結

Sol

考試的時候調了1.5h沒調出來我真是菜爆了。。。

讀完題目後不難發現,每次約束的條件相當於是(b[((x[i] + i) % N + (i / N) % N) % N] = y[i])

因為資料隨機,暴力搜(a_i)就行了。搜尋的時候結合給出的資訊判斷一下是否合法。

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second 
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int T, N, x[MAXN], y[MAXN];
int a[30], b[30], vis[MAXN];
struct Node {
    int po, ti;
};
vector<Node> v[MAXN];
void dfs(int x) {
    if(x == N) {
        for(int i = 0; i < N; i++) printf("%d ", a[i]); puts("");
        for(int i = 0; i < N; i++) printf("%d ", b[i]);
        exit(0);
    }

    for(int i = 0; i < N; i++) {
        if(!vis[i]) {
            a[x] = i;
            bool flag = 0;
            for(int j = 0; j < v[x].size(); j++) {
                int pos = v[x][j].po, ti = v[x][j].ti;
                int date = (a[(pos + ti) % N] + (ti / N) % N) % N;
                if((b[date] == -1) || (b[date] == y[ti])) ;
                else {flag = 1; break;}
            }
            if(flag) continue;
            vector<int> cha;
            for(int j = 0; j < v[x].size(); j++) {
                int pos = v[x][j].po, ti = v[x][j].ti;
                int date = (a[(pos + ti) % N] + (ti / N) % N) % N;
                if(b[date] == -1) cha.push_back(date), b[date] = y[ti];
            }
            vis[i] = 1;
            dfs(x + 1);
            vis[i] = 0; 
            for(int j = 0; j < cha.size(); j++) b[cha[j]] = -1;
        }
    }
}
int main() {
    //freopen("ex_a2.in", "r", stdin);
    memset(b, -1, sizeof(b));
    T = read(); N = read();
    for(int i = 0; i < T; i++) x[i] = read();
    for(int i = 0; i < T; i++) y[i] = read();
    for(int i = 0; i < T; i++) {
        int ax = (x[i] + i) % N, pa = a[ax] + (i / N) % N;
        v[ax].push_back((Node){x[i], i});
    }
    dfs(0);
    return 0;
}

相關文章