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

题意

题目链接

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;
}
posted @ 2018-10-15 18:53  自为风月马前卒  阅读(522)  评论(0编辑  收藏  举报

Contact with me