1 #include <bits/stdc++.h>
2 using namespace std;
3 const int maxn = 10005;
4 struct node{
5 int lt,rt,value;
6 }tree[maxn<<2];
7 struct Block {
8 int lt,rt;
9 } block[maxn];
10 void build(int lt,int rt,int v) {
11 tree[v].lt = lt;
12 tree[v].rt = rt;
13 tree[v].value = 0;
14 if(lt == rt) return;
15 int mid = (lt + rt)>>1;
16 build(lt,mid,v<<1);
17 build(mid+1,rt,v<<1|1);
18 }
19 int query(int id,int v) {
20 if(tree[v].lt == tree[v].rt) return 0;
21 int mid = (tree[v].lt + tree[v].rt)>>1;
22 if(id <= mid) return max(query(id,v<<1),tree[v<<1|1].value);
23 return query(id,v<<1|1);
24 }
25 void update(int id,int v,int value) {
26 int mid = (tree[v].lt + tree[v].rt)>>1;
27 tree[v].value = max(tree[v].value,value);
28 if(tree[v].lt == tree[v].rt) return;
29 if(id <= mid) update(id,v<<1,value);
30 else update(id,v<<1|1,value);
31 }
32 int n,m,d[maxn],discrete[maxn],width[maxn];
33 int main() {
34 while(~scanf("%d",&n)) {
35 for(int i = m = 0; i < n; ++i) {
36 scanf("%d",d+i);
37 discrete[i] = d[i];
38 }
39 sort(discrete,discrete+n);
40 int len = unique(discrete,discrete+n) - discrete;
41 build(0,len-1,1);
42 block[m].lt = block[m].rt = 0;
43 for(int i = 1; i < n; ++i)
44 if(d[i-1] < d[i]) block[m].rt++;
45 else {
46 ++m;
47 block[m].lt = block[m].rt=i;
48 }
49 for(int i = 0; i <= m; ++i)
50 for(int j = block[i].rt; j >= block[i].lt; --j)
51 width[j] = block[i].rt-j+1;
52 int ans = 0;
53 for(int i = m; i >= 0; --i) {
54 for(int j = block[i].rt; j >= block[i].lt; --j) {
55 int id = lower_bound(discrete,discrete+len,d[j])-discrete;
56 ans = max(j - block[i].lt + 1 + query(id,1),ans);
57 update(id,1,width[j]);
58 }
59 }
60 printf("%d\n",ans);
61 }
62 return 0;
63 }