Stock Wave+csuoj+简单dp

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

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.


解决方案:求波浪形子序列,开始题目没看全,也没理解全,就做了,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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值