# 51nod round3# 序列分解（折半枚举+字典树）

1.他们不能相互重叠。

2.他们要完全一样。

Input
第一行给出一个T，表示T组数据。(1<=T<=5)

Output
如果小刀可以完成游戏，输出"Good job!!" (不包含引号)，否则 输出"What a pity!" (不包含引号)。
Input示例
2
4
1 1 2 2
6
1 2 3 4 5 6
Output示例
Good job!!
What a pity!

//Accepted 375 ms 96120 KB
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define refeach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;
const int N = 1<<18;
struct node
{
node* child[45];
bool flag;
node()
{
memset(child,0,sizeof(child));
flag = 0;
}
}trie[N];
int sz;
node *root,*cur;
node* createnode()
{
memset(trie[sz].child,0,sizeof(trie[sz].child));
trie[sz].flag = 0;
return &trie[sz++];
}
void Insert(int s[],int L,int R)
{
cur = root;
for(int p = L; p <= R; p++)
{
int val = s[p] ;
if(cur->child[val] == NULL )
cur->child[val] = createnode();
cur = cur->child[val];
}
cur->flag = 1;
}
bool query(int s[],int L,int R)
{
cur = root;
for(int p = R;p >= L; p--)
{
int val = s[p];
if(cur->child[val] == NULL) return false;
cur = cur->child[val];
}
return cur->flag;
}
pair<int,int>b[50];
int a[50];
int s1[50],s2[50];
int top1,top2;
int main()
{

int T;
cin>>T;
while(T--)
{
sz = 0;
root = createnode();
int n;
scanf("%d",&n);
REP(i,n) scanf("%d",&b[i].first),b[i].second = i;
sort(b+1,b+1+n);
int maxx = 0;
a[b[1].second] = maxx;
for(int i = 2 ; i<= n ; i++)
if(b[i].first == b[i-1].first) a[b[i].second] = maxx;
else a[b[i].second] = ++maxx;
{
top1 = top2 =0;
int i;
for(i = 0; i < n/2; i++)
{
{
s1[++top1] = a[i+1];
if(top1 <= top2 && s1[top1] != s2[top1]) break;
}
else
{
s2[++top2] = a[i+1];
if(top2 <= top1 && s1[top2] != s2[top2]) break;
}
}
if(i == n/2)
{
if(top1 <= top2) Insert(s2,top1+1,top2);
else Insert(s1,top2+1,top1);
}
}

bool KO = 0;
{
top1 = top2 = 0;
int i;
for(i = 0; i < n/2; i++)
{
{
s1[++top1] = a[n-i];
if(top1 <= top2 && s1[top1] != s2[top1]) break;
}
else
{
s2[++top2] = a[n-i];
if(top2 <= top1 && s1[top2] != s2[top2]) break;
}
}
if(i == n/2)
{
if(top1 <= top2 && query(s2,top1+1,top2)) KO = 1;
else if(top1 > top2 && query(s1,top2+1,top1)) KO = 1;
}
}
if(KO) puts("Good job!!");
else puts("What a pity!");
}
return 0;
}

#### Hash+折半枚举 51Nod1440 序列分解

2016-04-14 10:51:54

#### [51nod1228]序列求和

2016-07-10 22:09:13

#### 【折半枚举】

2017-03-15 21:26:45

#### [伯努利数] 51Nod 1258 序列求和 V4

2017-04-02 13:32:00

#### 51nod 1229 序列求和 V2 数学+自然数幂和

2018-01-11 15:40:05

#### 51nod 1400 序列分解

2016-11-17 21:19:24

#### 51nod 1341 混合序列 （矩阵快速幂）

2017-11-25 10:33:40

#### 51NOD 序列求和 V5题解

2016-12-01 23:57:14

#### 51nod 1202：子序列个数

2015-12-19 15:09:59

#### 51nod1510 最小化序列

2016-09-23 20:07:53