# 洛谷P2599||bzoj1413 [ZJOI2009]取石子游戏

bzoj1413

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<vector>
5 using namespace std;
6 #define fi first
7 #define se second
8 #define mp make_pair
9 #define pb push_back
10 typedef long long ll;
11 typedef unsigned long long ull;
12 typedef pair<int,int> pii;
13 int T;
14 int n;
15 int a[1010];
16 int lft[1010][1010],rht[1010][1010];
17 //[l,r]区间左侧加/右某个元素使得成为必败态
18 int main()
19 {
20     int i,l,r,t1,t2,x;
21     scanf("%d",&T);
22     while(T--)
23     {
24         scanf("%d",&n);
25         for(i=1;i<=n;++i)
26             scanf("%d",a+i);
27         if(n==1)
28         {
29             printf("%d\n",1);
30             continue;
31         }
32         if(n==2)
33         {
34             printf("%d\n",int(a[1]!=a[2]));
35             continue;
36         }
37         for(i=1;i<=n;++i)
38             lft[i][i]=rht[i][i]=a[i];
39         for(i=2;i<=n;++i)
40         {
41             for(l=1;l<=n-i+1;++l)
42             {
43                 r=l+i-1;
44                 t1=lft[l][r-1];t2=rht[l][r-1];x=a[r];
45                 if(t2==x)    lft[l][r]=0;
46                 else if(x<t1&&x<t2)    lft[l][r]=x;
47                 else if(x>t1&&x>t2)    lft[l][r]=x;
48                 else if(t1<=x&&x<t2)    lft[l][r]=x+1;
49                 else if(t2<x&&x<=t1)    lft[l][r]=x-1;
50             }
51             for(l=1;l<=n-i+1;++l)
52             {
53                 r=l+i-1;
54                 t1=lft[l+1][r];t2=rht[l+1][r];x=a[l];
55                 if(t1==x)    rht[l][r]=0;
56                 else if(x<t1&&x<t2)    rht[l][r]=x;
57                 else if(x>t1&&x>t2)    rht[l][r]=x;
58                 else if(t2<=x&&x<t1)    rht[l][r]=x+1;
59                 else if(t1<x&&x<=t2)    rht[l][r]=x-1;
60             }
61         }
62         printf("%d\n",int(lft[2][n]!=a[1]));
63     }
64     return 0;
65 }
View Code

posted @ 2018-11-09 20:47  hehe_54321  阅读(250)  评论(0编辑  收藏  举报