Noip2013 T5 gardener

Source: Internet
Author: User
[Noip2013t5] Watcher time limit: 1000 ms memory limit: 131072kb description descript.

A row of flowers is planted in the building of the gardener, and each flower has its own height. The longer the flowers grow, the more crowded they are. Dong decided to remove some of the flowers in the row and leave the remaining flowers in the same place so that the remaining flowers could grow up. At the same time, Dong hoped that the remaining flowers would be arranged in a more chic manner.

Specifically, can the height of a tall building be regarded as a column of integers? 1 ,? 2 ,... ,? N. When some flowers are removed, the height of the remaining flowers is G1, G2 ,... , GM, then Dongdong hopes that at least one of the following two conditions is met:

Condition A: For all 1≤i ≤ m/2, g2i> g2i −1, and g2i> g2i + 1;

Condition B: For all 1≤i ≤ m/2, g2i <g2i −1, and g2i <g2i + 1.

Note that the above two conditions are met at the same time when M = 1, and at most one condition can be met when m> 1.

How many flowers can be kept in the same place in Dongdong.

The first line of input contains an integer, indicating the number of flowers in the start time.
The second row contains an integer, which is? 1 ,? 2 ,... ,? N indicates the height of each flower. Output output outputs a row containing an integer m, indicating the maximum number of flowers that can be left in the same place. Sample input data
5 5 3 2 1 2 
Output Data
3
Hint

[Input and output sample description]

There are multiple ways to keep exactly three flowers, for example, leave 1st, 4, 5 plants, respectively 5, 1, 2 in height, meet the condition B.

 

[Data Scope]

For 20% of the data, n ≤ 10;

For 30% of the data, n ≤ 25;

For 70% of the data, n ≤ 1000,0 ≤? I ≤ 1000;

For 100% of data, 1 ≤ n ≤ 100,000, 0 ≤? I ≤ 1,000,000, all? I random generation. All random numbers are evenly distributed within a certain range.

 

Bare DP + line segment tree

 1 #include<queue> 2 #include<vector> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 200010;10 #define Ch1 T[kth].lc[i]11 #define Ch2 T[kth].rc[i]12 #define For(i,n) for(int i=1;i<=n;i++)13 #define Rep(i,l,r) for(int i=l;i<=r;i++)14 15 struct tnode{16     int l[N],r[N],mid[N];17     int max[N],rt,lc[N],rc[N];18 }T[2];19 20 struct flower{21     int h,id,rh;22 }p[N];23 24 bool fcmp(flower A,flower B){return A.rh<B.rh;}25 bool scmp(flower A,flower B){return A.id<B.id;}26 int Lim,n,cnt,f[N][2];27 28 void init(){29     scanf("%d",&n);30     For(i,n) {31         scanf("%d",&p[i].rh);32         p[i].id=i;33     }34     sort(p+1,p+n+1,fcmp);35     p[1].h=1;36     Rep(i,2,n)37       if(p[i].rh==p[i-1].rh) p[i].h=p[i-1].h;38       else                   p[i].h=p[i-1].h+1;39     Lim=p[n].h;40     sort(p+1,p+n+1,scmp);41 }42 43 void Build(int kth,int &i,int l,int r){44     i=++cnt;45     T[kth].l[i]=l;T[kth].r[i]=r;T[kth].mid[i]=(l+r)>>1;46     if(l==r) return;47     Build(kth,Ch1,l,T[kth].mid[i]);Build(kth,Ch2,T[kth].mid[i]+1,r);48 }49 50 void Modify(int kth,int i,int x,int delta){51     if(T[kth].l[i]==T[kth].r[i]){52         T[kth].max[i]=max(T[kth].max[i],delta);53         return;54     }55     if(x<=T[kth].mid[i]) Modify(kth,Ch1,x,delta);56     else                 Modify(kth,Ch2,x,delta);57     T[kth].max[i]=max(T[kth].max[Ch1],T[kth].max[Ch2]);58 }59 60 int query(int kth,int i,int l,int r){61     if(l>r) return 0;62     if(l==T[kth].l[i]&&r==T[kth].r[i]) return T[kth].max[i];63     if(r<=T[kth].mid[i]) return query(kth,Ch1,l,r);else64     if(l>T[kth].mid[i])  return query(kth,Ch2,l,r);else65     return max(query(kth,Ch1,l,T[kth].mid[i]),query(kth,Ch2,T[kth].mid[i]+1,r));66 }67 68 void DP(){69     f[1][0]=1;70     f[1][1]=1;71     Modify(0,T[0].rt,p[1].h,1);72     Modify(1,T[1].rt,p[1].h,1);73     Rep(i,2,n){74         f[i][0]=query(1,T[1].rt,p[i].h+1,Lim)+1;75         Modify(0,T[0].rt,p[i].h,f[i][0]);76         f[i][1]=query(0,T[0].rt,1,p[i].h-1)+1;77         Modify(1,T[1].rt,p[i].h,f[i][1]);78     }79     printf("%d\n",max(f[n][0],f[n][1]));80 }81 82 int main(){83     init();84     Build(0,T[0].rt,1,Lim);cnt=0;85     Build(1,T[1].rt,1,Lim);86     DP();87     return 0;88 }

 

Noip2013 T5 gardener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.