2083: [Poi2010]Intelligence test
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 294 Solved: 130
Description
霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列。Lyx很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算做很多练习,所以他希望你写一个程序来快速判断他的答案是否正确。
Input
第一行为一个整数m(1<=m<=1000000)第二行包括m个用空格分开的整数ai(1<=ai<=1000000),组成了最初的序列,第三行为一个整数n(1<=n<=1000000),表示n个Lyx经过一系列删除得到的序列,每个序列两行,第一行给出长度L(1<=L<=m),然后下一行为L个由空格分开的整数bi(1<=bi<=1000000)。
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
Source
by poi
题解:
vector。
STL就是棒!
对每个权值开一个vector,然后每次对一个序列二分一下位置就好了,显然每个位置越靠前越好。
Code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> a[1001000];
int n,m,b[1001000];
int in(){
int x=0; char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
bool work(int l){
int last=-1;
for (int i=1; i<=l; i++){
vector<int>::iterator it=upper_bound(a[b[i]].begin(),a[b[i]].end(),last);
if (it==a[b[i]].end()) return false;
last=*it;
}
return true;
}
int main(){
n=in();
for (int i=1; i<=n; i++){
int x=in();
a[x].push_back(i);
}
m=in();
for (int i=1; i<=m; i++){
int l=in();
for (int j=1; j<=l; j++) b[j]=in();
if (work(l)) printf("TAK\n");
else printf("NIE\n");
}
return 0;
}