Stock Wave
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 45 Solved: 9
[ Submit][ Status][ Web Board]
Description
As a stock analyst, Tom can forecast the trend according to a series of historical prices. To find the "Wave" is the first thing that he needs to do.
A "Wave" is a series of prices, the value in even place is greater than the former one, and the value in odd place is less than the former one. If you draw these values in one curve, you will see "up, down, up, down ...." Yes, it is a "Wave".
Can you help Tom to find out the longest "Wave"?
Input
The first line of each case is a positive integer n (n < 1000). The next n line(s) is a price with two decimal places.
Output
Print the length of the longest "Wave" in the input, if the length is less than 3, output '0'.
Sample Input
5
1.00
8.10
3.00
5.00
4.00
4
1.01
2.02
3.03
4.04
Sample Output
5
0
HINT
The longest "Wave" is a subsequence of initial price series, the neighboring values of "Wave" may be NOT neighboring in the original series, but they must keep order of input.
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1005;
double L[maxn];
int dp[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
{
scanf("%lf",&L[i]);
dp[i]=1;
}
int Max=0;
for(int i=2; i<=n; i++)
{
for(int j=1; j<i; j++)
{
if(dp[j]%2==0&&L[i]<L[j])
{
dp[i]=max(dp[i],dp[j]+1);
if(dp[i]>Max) Max=dp[i];
}
if(dp[j]%2!=0&&L[i]>L[j])
{
dp[i]=max(dp[i],dp[j]+1);
if(dp[i]>Max) Max=dp[i];
}
}
}
if(Max>=3)
printf("%d\n",Max);
else
printf("0\n");
}
return 0;
}