BZOJ2083: [Poi2010]Intelligence test

2083: [Poi2010]Intelligence test

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 241  Solved: 96
[Submit][Status]

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

题解:
被这道题坑了。。。
刚开始还是没思路,后来前天忽然想到是不是可以记录一个数的后继出现位置然后用mx表示当前最右位置,然后每次碰到x,都让x next 到>mx 的第一个位置
这样的复杂度是多少呢?感觉是O(n)的,最多不会next超过n次,然后就开开心心打代码。
WA了几发之后改了改,然后又T了。。。
不知道原因。。。
代码:
 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 1000000+5
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 
45 using namespace std;
46 
47 inline int read()
48 
49 {
50 
51     int x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 int n,a[maxn],b[maxn],cur[maxn],next[maxn],head[maxn];
61 
62 int main()
63 
64 {
65 
66     freopen("input.txt","r",stdin);
67 
68     freopen("output.txt","w",stdout);
69 
70     n=read();
71     for1(i,n)a[i]=read();
72     for3(i,n,1)
73     {
74         next[i]=head[a[i]];
75         head[a[i]]=i;
76     }
77     memset(cur,-1,sizeof(cur));
78     int cs=read();
79     while(cs--)
80     {
81         int i,x,mx=1,m=read();
82         for(i=1;i<=m;i++)b[i]=read();
83         for(i=1;i<=m;i++)
84         {
85             x=b[i];
86             if(cur[x]==-1)cur[x]=head[x];else cur[x]=next[cur[x]];
87             while(cur[x]<mx&&cur[x])cur[x]=next[cur[x]];
88             //cout<<i<<' '<<mx<<' '<<x<<' '<<cur[x]<<endl;
89             if(cur[x]<=0)break;
90             mx=cur[x];
91         }
92         if(i<=m)printf("NIE\n");else printf("TAK\n");
93         for(i=1;i<=m;i++)cur[b[i]]=-1;
94     }
95 
96     return 0;
97 
98 }
View Code

二分的思路其实差不多,但多一个log,但是能A。汗!

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 1000000+5
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 #define it vector<int>::iterator
45 
46 using namespace std;
47 
48 inline int read()
49 
50 {
51 
52     int x=0,f=1;char ch=getchar();
53 
54     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
55 
56     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
57 
58     return x*f;
59 
60 }
61 int n,a[maxn];
62 vector<int>v[maxn];
63 
64 int main()
65 
66 {
67 
68     freopen("input.txt","r",stdin);
69 
70     freopen("output.txt","w",stdout);
71 
72     n=read();
73     for1(i,n)v[read()].push_back(i);
74     int cs=read();
75     while(cs--)
76     {
77         int m=read(),mx=0;bool flag=1;
78         for1(i,m)
79         {
80             int x=read();
81             if(flag)
82             {
83                 it y=upper_bound(v[x].begin(),v[x].end(),mx);
84                 if(y==v[x].end())flag=0;else mx=*y;
85             }
86         }
87         if(flag)puts("TAK");else puts("NIE");
88     }
89 
90     return 0;
91 
92 }
View Code
posted @ 2014-11-14 18:33  ZYF-ZYF  Views(243)  Comments(0Edit  收藏  举报