2486: Stock Wave
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 32768K | 1208 | 188 | Standard |
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.
Problem Source: provided by skywind
#include<iostream>
using namespace std;
const int maxn=1000;
int main()
{
int n;
double a[maxn+1];
while(cin>>n)
{
int i,j;
for(i=0;i<n;i++)
cin>>a[i];
int sum[maxn+1];//表示前n个数的最大波长
int lab[maxn+1];//标记状态 0表示位于波峰 1表示位于波谷
lab[0]=sum[0]=1;
int t=-1;
for(int k=1;k<n;k++)
{
if(a[k]>a[k-1])
{
t=k;
break;
}
}
if(t==-1) {cout<<"0"<<endl;continue;}
for(i=0;i<t;i++)
sum[i]=lab[i]=1;
for(i=t;i<n;i++)
{
int max=0;
int f=2;
for(j=i-1;j>=0;j--)
{
if(lab[j]==0&&a[i]<a[j])//表示可形成波谷
{
if(sum[j]>max)
{
max=sum[j];
f=1;//及当前状态为波峰
}
}
else if(lab[j]==1&&a[i]>a[j]) //表示可形成波峰
{
if(sum[j]>max)
{
max=sum[j];
f=0;//记当前状态为波谷
}
}
}
sum[i]=max+1;
lab[i]=f;
}
if(sum[n-1]<3) sum[n-1]=0;
cout<<sum[n-1]<<endl;
}
return 0;
}