Description
PTY 进行IQ 测试,测试的项目是判断一个序列是否是另外一个序列删除若干个数字之后得到的,PTY 深知自己的IQ 低于sqrt(-1),所以他请来了智商超高的你来替他解决问题。
Input
第一行为一个整数n,第二行包括n 个用空格分开的整数ai,组成 了最初的序列,第三行为一个整数m,表示n 个IQ 测试询问的序列,每个序列两行,第一行给出长度 L(1<=L<=n),然后下一行为L 个由空格分开的整数bi。
Output
共m 行,如果询问的序列确实是由最初的序列删除一些数得到,就输出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
对于30%的数据
n<=1000,m<=1000
对于100%的数据
1<=ai,bi<=1000000,ΣL<=1000000,n,m<=1000000
题解
这题考场70分我真是蠢得没边。。
我的做法是记下来原数组里每一个数的所有存在的位置,在处理新串时记录上一位在原数组里能找到的最小的位置,在当前数对应的所有位置中找最小的大于上一个位置的,如果找不到就说明不满足条件。
在找最小的大于上一个位置的时候只要二分一下,最坏情况就是
O(nlogn)
貌似并不会被卡,然而我脑残地直接找就被卡了三个点233。
附上
O(n)
的标算:
首先匹配的时候注意一个性质:
原序列:a1,a2,a3….an
询问序列:b1,b2,b3…bm
若
bi
和
aj
匹配了,则
bi+1
应该和
j
之后的第一个和
1、很容易想到一个
n×m
的算法,即对于每个询问序列,都从原序列从前往后扫,
O(N)
判断是否为子序列。这样能得到大约50%的分数
2、由于
n
较大,还能设计出一个算法,即每次询问
3、瓶颈在于应该要一次解决多个询问序列,我们来模拟这样一个过程。
a>开始顺序扫描原序列,若a1=i,则把所有询问序列中第一个数字=i 的那个数字删除;
b>然后跳到a2,重复以上过程,直到结束。
c>如果有的序列没有全空,则说明不是原序列的自序列,否则就是。
这样一个过程,我们需要的数据结构是:
1、 快速定位到第一个数字=i 的那个序列
2、 删除第一个数字后,快速把第二个数字当成第一个数字
可以用链表实现
那么最后的复杂度是
O(ΣL+n)
,可以得到100%的分数。
//By KikiDMW
#include<ctime>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int N = 1000000 + 10;
int a, n, m, l, b;
vector<int> t[N];
void init(){
n = read();
for(int i = 1; i <= n; i++){
a = read();
t[a].push_back(i);
}
}
void work(){
m = read();
int last, ok;
vector<int>::iterator j;
while(m--){
l = read();
ok = 1;
last = 0;
for(int i = 1; i <= l; i++){
b = read();
if(ok){
//len = t[b].size();
/*for(j = 0; j < len && t[b][j] <= last; j++);
if(j == len) ok = 0;*/
j = upper_bound(t[b].begin(), t[b].end(), last);
if(j == t[b].end()) ok = 0;
else last = *j;
}
}
if(ok) puts("TAK");
else puts("NIE");
}
}
int main(){
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
init();
work();
//printf("%.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
//By pty(std)
# include<cstdio>
# include<cstring>
# include<cstdlib>
# include<iostream>
# include<set>
using namespace std;
const int maxx=40000008;
int n,l[1000008],q,m,a[1000008],len,tot;
int f[10000008],b[10000008],start[1000008],ans[1000008];
struct arr
{
int a,next;
}e[10000008];
char Input[maxx+5],*ipos;
#define read() (strtol(ipos,&ipos,10))
void Add(int x,int y)
{
e[++tot].a=y;e[tot].next=start[x];start[x]=tot;
}
void Update(int i)
{
if (f[i]) ans[f[i]]=1;
else Add(b[i+1],i+1);
}
void Init()
{
n=read();
for (int i=1;i<=n;i++) a[i]=read();
q=read();
for (int i=1;i<=q;i++)
{
l[i]=read();
for (int j=1;j<=l[i];j++)
b[++len]=read();
Add(b[len-l[i]+1],len-l[i]+1);
f[len]=i;
}
}
void Work()
{
for (int i=1;i<=n;i++)
{
int tmp=start[a[i]];start[a[i]]=0;
for (int j=tmp;j;j=e[j].next)
Update(e[j].a);
}
for (int i=1;i<=q;i++)
printf("%s\n",ans[i]?"TAK":"NIE");
}
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
fread(Input,maxx,1,stdin);ipos=Input;
Init();
Work();
return 0;
}