1 条题解
-
0
思路
至多两笔等价于允许零利润交易,允许同日卖出再买入;负价格与两次交易状态。
参考程序(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