1055. 股票买卖 II

在这里插入图片描述
这道题是贪心的一道题,主要考查一个序列中,上升子序列长度大于等于2的首位和,因为首位差价最大,然后就判断就可以了。

#include<iostream>
using namespace std;
const int N=1e5+10;
const int Inf=0x3f3f3f3f;
int w[N];
int dp[N][2];
int main(void)
{
    
    
    int n,last=Inf,st=Inf;
    long long res=0;
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
    
    
        cin>>w[i];
        if(w[i]>last)
        {
    
    
            last=w[i];
        }
        else
        {
    
    
            res+=last-st;
            st=last=w[i];
        }
    }
    res+=last-st;
    cout<<res;
    
}

猜你喜欢

转载自blog.csdn.net/qq_52358098/article/details/113644385