2 solutions

  • -1
    @ 2024-11-6 20:21:52
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int f[N][2];
    /*
    f[i][0] 表示第i天没有股票的价值
    f[i][1] 表示第i天有股票的价值
    */
    int w[N];
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
        }
        memset(f,0xcf,sizeof f);
        f[0][0]=0; //起点
        for(int i=1;i<=n;i++)
        {
            f[i][0]=max(f[i-1][0],f[i-1][1]+w[i]);//第i-1天没有股票,卖了第i天的股票
            f[i][1]=max(f[i-1][1],f[i-1][0]-w[i]);//第i天有股票,买了第i天的股票
        }
        cout<<f[n][0];
        return 0;
    }

    Information

    ID
    217
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    43
    Accepted
    17
    Uploaded By