Stock Wave+csuoj+简单dp

来源:互联网 发布:有蛇精吐火的软件 编辑:程序博客网 时间:2024/06/05 15:01

Stock Wave

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 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

51.008.103.005.004.00 41.012.023.034.04

Sample Output

50

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.


解决方案:求波浪形子序列,开始题目没看全,也没理解全,就做了,wa了好多次。对于数组L[maxn]:1)dp[i]=max{dp[i],dp[j]+1}&&dp[j]%2==0&&L[i]>L[j];2)dp[i]=max{dp[i],dp[j]+1}&&dp[j]%2!=0&&L[i]<L[j];
code:
#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;}

0 0
原创粉丝点击