Missile

Problem Description

Long, long ago, country A invented a missile system to destroy the missiles from their enemy. That system can launch only one missile to destroy multiple missiles if the heights of all the missiles form a non-decrease sequence. But recently, the scientists found that the system is not strong enough. So they invent another missile system. The new system can launch one single missile to destroy many more enemy missiles. Basically, the system can destroy the missile from near to far. When the system is begun, it chooses one enemy missile to destroy, and then destroys a missile whose height is lower and farther than the first missile. The third missile to destroy is higher and farther than the second missile… the odd missile to destroy is higher and farther than the previous one, and the even missile to destroy is lower and farther than the previous one. Now, given you a list of the height of missiles from near to far, please find the most missiles that can be destroyed by one missile launched by the new system.

Input

The input contains multiple test cases. In each test case, first line is an integer n (0<n<=1000), which is the number of missiles to destroy. Then follows one line which contains n integers (<=10^9), the height of the missiles followed by distance. The input is terminated by n=0

Output

For each case, print the most missiles that can be destroyed in one line.

Sample Input

4
5 3 2 4
3
1 1 1
0
#include<stdio.h>
#include<string.h>
int p[1005];
int sum[1005];
int main()
{
    int n,i;
 while(scanf("%d",&n)&&n)
 {
  for(i=0;i<n;i++)
  {
   scanf("%d",&p[i]);
  }
  memset(sum,0,sizeof(sum));
  for(i=n-1;i>=0;i--)
  {
            if(i==n-1)  sum[i]=1;
   else if(i==n-2)
   {
    if(p[i]>p[i+1]) sum[i]=2;
    else sum[i]=1;
   }
   else
   {
               if(p[i]>p[i+1]&&p[i+1]<p[i+2])  sum[i]=sum[i+2]+2;
      else sum[i]=sum[i+1];
   }
  }
  printf("%d\n",sum[0]);
 }
 return 0;
}
posted @ 2013-04-21 23:30  forevermemory  阅读(368)  评论(0编辑  收藏  举报