原来要从两边开始for啊。。。。。感谢http://blog.csdn.net/XY20130630/article/details/50635756
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #define maxn 200500 using namespace std; int t,n,a[maxn],aft[maxn],pre[maxn]; map <int,int> p,q; bool check(int l,int r) { if (l>=r) return true; int p1=l,p2=r; while (p1<=p2) { if ((pre[p1]<l) && (aft[p1]>r)) return check(l,p1-1)&&check(p1+1,r); if ((pre[p2]<l) && (aft[p2]>r)) return check(l,p2-1)&&check(p2+1,r); p1++;p2--; } return false; } void work() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); p.clear();q.clear(); for (int i=1;i<=n;i++) { if (!p.count(a[i])) pre[i]=0; else pre[i]=p[a[i]]; p[a[i]]=i; } for (int i=n;i>=1;i--) { if (!q.count(a[i])) aft[i]=n+1; else aft[i]=q[a[i]]; q[a[i]]=i; } if (check(1,n)) printf("non-boring\n"); else printf("boring\n"); } int main() { scanf("%d",&t); for (int i=1;i<=t;i++) work(); return 0; }