# 【启发式拆分】bzoj4059: [Cerc2012]Non-boring sequences

4
5
1 2 3 4 5
5
1 1 1 1 1
5
1 2 3 2 1
5
1 1 2 1 1

non-boring
boring
non-boring
boring

## 题目分析

1 #include<bits/stdc++.h>
2 const int maxn = 200035;
3
4 int T,n,cnt;
5 int a[maxn],t[maxn],pre[maxn],nxt[maxn],lst[maxn];
6
7 inline char nc()
8 {
9     static char buf[100000],*p1=buf,*p2=buf;
10     if (p1==p2) {
12         if (p1==p2) return EOF;
13     }
14     return *p1++;
15 }
16 #define getchar nc
18 {
19     char ch = getchar();
20     int num = 0;
21     bool fl = 0;
22     for (; !isdigit(ch); ch=getchar())
23         if (ch=='-') fl = 1;
24     for (; isdigit(ch); ch=getchar())
25         num = (num<<1)+(num<<3)+ch-48;
26     if (fl) num = -num;
27     return num;
28 }
29 bool split(int l, int r)
30 {
31     if (l >= r) return 1;
32     int x = l, y = r;
33     for (int i=l; i<=r; i++)
34         if (i&1){
35             if (pre[x] < l&&nxt[x] > r)
36                 return split(l, x-1)&&split(x+1, r);
37             x++;
38         }else{
39             if (pre[y] < l&&nxt[y] > r)
40                 return split(l, y-1)&&split(y+1, r);
41             y--;
42         }
43     return 0;
44 }
45 int main()
46 {
48     while (T--)
49     {
50         cnt = n = read();
51         memset(lst, 0, n<<2);
52         memset(pre, 0, n<<2);
53         memset(nxt, 0, n<<2);
54         for (int i=1; i<=n; i++) t[i] = a[i] = read();
55         std::sort(t+1, t+n+1);
56         cnt = std::unique(t+1, t+n+1)-t-1;
57         for (int i=1; i<=n; i++){
58             a[i] = std::lower_bound(t+1, t+cnt+1, a[i])-t;
59             nxt[lst[a[i]]] = i, pre[i] = lst[a[i]];
60             lst[a[i]] = i, nxt[i] = n+1;
61         }
62         puts(split(1, n)?"non-boring":"boring");
63     }
64     return 0;
65 }

END

posted @ 2018-10-29 18:49  AntiQuality  阅读(202)  评论(0编辑  收藏  举报