It's a tough question.
But it is also the problem of the road card.
The first and the last element of the same element is discretized.
It is recommended to use two-times sort discretization, otherwise it is easy to tle
Then we consider the legality of all the substring of [l,r], and we consider finding the first and last elements that are not in this interval from both sides, and then we can divide the interval into two parts recursively
That is, first determine whether L only appear once, then Judge R, then Judge L+1, and then Judge R-1 ...
So the complexity is NLOGN.
Principle, you can think of this process as the inverse process of heuristic merging, namely "heuristic splitting"
Number of data sets, pay attention not to use memset
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime > #include <cmath> #include <algorithm> #include <iomanip> #include <vector> #include <map
> #include <set> #include <bitset> #include <queue> #include <stack> using namespace std;
#define MAXN 200010 #define MAXM 1010 #define INF 1000000000 #define MOD 1000000007 #define EPS 1e-8 #define LL Long
struct data{int v;
int x;
int p;
};
Char xb[1<<15],*xs=xb,*xt=xb; #define GETC () (xs==xt&& (xt= (XS=XB) +fread (Xb,1,1<<15,stdin), xs==xt) 0:*xs++) inline int read () {int x=0
, F=1;char ch=getc (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') f=-1;ch=getc (); while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; ch=getc ();} return x*f
;
BOOL Cmp1 (data X,data y) {return x.v<y.v;} bool Cmp2 (data X,data y) {return x.p<y.p;} int n;
Data A[MAXN];
int TAI[MAXN];
int FRO[MAXN],NXT[MAXN]; BOOL Jud (int l,iNT R) {if (l>r) {return 1;
int l=l,r=r;
while (l<=r) {if (fro[l]<l&&nxt[l]>r) {return Jud (L,L-1) &jud (l+1,r);
} if (Fro[r]<l&&nxt[r]>r) {return Jud (l,r-1) &jud (r+1,r);
} l++;
r--;
return 0;
int main () {int i;
int tmp;
scanf ("%d", &tmp);
while (tmp--) {scanf ("%d", &n);
for (i=1;i<=n;i++) {scanf ("%d", &A[I].V);
A[i].p=i;
Sort (A+1,A+N+1,CMP1);
for (i=1;i<=n;i++) {a[i].x=a[i-1].x; if (i==1| |
A[I].V!=A[I-1].V) {a[i].x++;
} sort (A+1,A+N+1,CMP2);
for (i=1;i<=n;i++) {nxt[tai[a[i].x]]=i;
Fro[i]=tai[a[i].x];
Tai[a[i].x]=i;
nxt[i]=n+1; printf (Jud (1,n)?
non-boring\n ":" boring\n "); for (i=1;i<=n;i++) {Tai[a[I].x]=0;
} return 0; }
/*
*/