Problem Description
Long, long ago, country A invented a missle 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 from a non-decrease seqence.
But recently, the scientists found that the system is not strong enough. So they invent another missle system. The new system can launch one single missle 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 destorys 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 the height of missiles from near to far, please find the most missiles that can be destroy by one missile launched by the new system.
But recently, the scientists found that the system is not strong enough. So they invent another missle system. The new system can launch one single missle 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 destorys 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 the height of missiles from near to far, please find the most missiles that can be destroy 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 (<=109), the height of the missiles followed by distance.
The input is terminated by n=0
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 (<=109), 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
Sample Output
3
1
Author
代码:
#include<stdio.h>
#include<malloc.h>
struct mm
{
int a;// 输入的值
int b;// 存放长度
}*x;
int main()
{
int n,max,i,j;
while(scanf("%d",&n)!=EOF&&n)
{
x=(mm *)malloc(sizeof(mm)*n);
for(i=0;i<n;i++)
{ // 1 2 2 3
scanf("%d",&x[i].a); // 5 3 2 4
x[i].b=1;//每个数对应长度为一 // 1 1 1 1
}
for(i=0;i<n;i++)
{
for(max=0,j=0;j<i;j++)
{
if(x[j].b%2==0){if(x[i].a>x[j].a&&x[j].b>max) max=x[j].b;}// 当前数长度 偶升
else if(x[i].a<x[j].a&&x[j].b>max)max=x[j].b; // 前数长度 奇降
}
x[i].b=max+1;// 前面的最长长度加上本身
}
for(i=0,max=0;i<n;i++)
if(max<x[i].b)max=x[i].b;// 找出最长长度
printf("%d\n",max);
/*
for(i=0;i<n;i++)
printf("%d ",x[i].b);
*/
free(x);
}
return 0;
}