DP之九

//sicily 1685. Missile

#include
<iostream> //DP, O(n^2)的时间复杂度,n<=1000,数据规模较小
using namespace std;
int main()
{
int n,h[1002],dp[1002][2]; //dp[i][0],表示以第i个数作为结束,此时第i个数在顺序上是偶("the even missile to destroy"),而dp[i][1]则表示第i个数在顺序上是奇("the odd missile to destroy")
while(cin>>n&&n)
{
for(int i=1;i<=n;++i)
cin
>>h[i];
for(int i=1;i<=n;++i)
{
dp[i][
0]=0;
dp[i][
1]=1; //显然任意一个本身都可以作为第一个"the odd missile to destroy",所以dp[i][1]=1
}
int Max;
for(int i=2;i<=n;++i)
{
Max
=0;
for(int j=1;j<i;++j)
if(h[j]>h[i]&&dp[j][1]>Max) //the even missile to destroy is lower than the previous one.
{
Max
=dp[j][1];
}
if(Max>0)
dp[i][
0]=Max+1;

Max
=0;
for(int j=1;j<i;++j)
if(h[j]<h[i]&&dp[j][0]>Max)
{
Max
=dp[j][0];
}
if(Max>0)
dp[i][
1]=Max+1;
}
int s=0;
for(int i=1;i<=n;++i)
s
=max(s,max(dp[i][0],dp[i][1]));
cout
<<s<<endl;
}
return 0;
}

  

posted on 2011-08-22 12:15  sysu_mjc  阅读(121)  评论(0编辑  收藏  举报

导航