1 条题解

  • 0
    @ 2026-10-25 17:53:27

    思路

    至多两笔等价于允许零利润交易,允许同日卖出再买入;负价格与两次交易状态。

    参考程序(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    int main(){int t;cin>>t;while(t--){int n;cin>>n;long long b1=LLONG_MIN/4,b2=b1,s1=0,s2=0;while(n--){long long p;cin>>p;b1=max(b1,-p);s1=max(s1,b1+p);b2=max(b2,s1-p);s2=max(s2,b2+p);}cout<<s2<<'\n';}}
    
    • 1

    信息

    ID
    1306
    时间
    1000ms
    内存
    64MiB
    难度
    普及
    标签
    递交数
    1
    已通过
    1
    上传者