散落星河的记忆🌠
Published on 2017-09-02 11:31 in 暂未分类 with 散落星河的记忆🌠

[BZOJ 2083] [POI 2010] Intelligence test

Description

霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列。Lyx很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算做很多练习,所以他希望你写一个程序来快速判断他的答案是否正确。

Input

第一行为一个整数 \(m\)

第二行包括 \(m\) 个用空格分开的整数 \(a_i\),组成了最初的序列;

第三行为一个整数 \(n\),表示 \(n\) 个Lyx经过一系列删除得到的序列,每个序列两行,第一行给出长度 \(L\),然后下一行为 \(L\) 个由空格分开的整数 \(b_i\)

Output

\(n\) 行,如果Lyx的序列确实是由最初的序列删除一些数得到,就输出TAK,否则输出NIE。

Sample Input

7
1 5 4 5 7 8 6
4
5
1 5 5 8 6
3
2 2 2
3
5 7 8
4
1 5 7 4

Sample Output

TAK
NIE
TAK
NIE

HINT

\(1\le n,m,a_i,b_i,\sum L \le 10^6\)

Solution

建立链表,其中 \(head[i]\) 这条链记录当前需要匹配的数字为 \(i\) 的序列编号。

比如样例,刚开始时 \(head[1]\) 所引出的链记录 \(1\)\(4\),弹掉 \(1\)\(4\) 后,\(head[5]\) 所引出的链记录 \(3,1,4\)

注意代码中加注释的那一行不能直接 \(push\),因为如果 \(a=\{5,1,2,3,4\},b=\{5,5,5,5,5\}\),弹掉 \(b\) 中的第一个 \(5\) 后直接 \(push\) 下一个 \(5\),会导致这几个 \(5\) 全都与 \(a\) 中的第一个 \(5\) 相匹配,从而输出TAK。

时间复杂度 \(O(n)\)

Code

#include <cstdio>

const int N = 1000005;
int n, m, a[N], q[N][2];

struct List {
	int h[N], t[N], cnt, sta[N], top;
	struct Edge { int v, nxt; } e[N];
	void push(int u, int v) {
		int i = top ? sta[top--] : ++cnt;
		if (!t[u]) h[u] = i; else e[t[u]].nxt = i;
		t[u] = i, e[i].v = v;
	}
	void pop(int u) {
		sta[++top] = h[u], h[u] = e[h[u]].nxt, e[sta[top]].nxt = 0;
		if (!h[u]) t[u] = 0;
	}
} b, c;

int read() {
	int x = 0; char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x;
}
int main() {
	int m = read();
	for (int i = 1; i <= m; ++i) a[i] = read();
	int n = read();
	for (int i = 1; i <= n; ++i) {
		int l = read();
		for (int j = 1; j <= l; ++j) b.push(i, read());
		c.push(b.e[b.h[i]].v, i);
	}
	for (int i = 1; i <= m; ++i) {
		int t = 0;
		while (c.h[a[i]]) {
			int j = c.e[c.h[a[i]]].v;
			b.pop(j), c.pop(a[i]);
			if (b.h[j]) q[++t][0] = b.e[b.h[j]].v, q[t][1] = j; //不能直接push
		}
		for (int j = 1; j <= t; ++j) c.push(q[j][0], q[j][1]);
	}
	for (int i = 1; i <= n; ++i) puts(b.h[i] ? "NIE" : "TAK");
	return 0;
}
posted @ 2019-02-11 17:15  散落星河的记忆🌠  阅读(125)  评论(0编辑  收藏  举报