// #牛客春招刷题训练营# https://www.nowcoder.com/discuss/726480854079250432 // 动态规划, 当天买入股票的最大收益 = 当天及之后的股票的最高价(后缀最大值) - 当天的股票价, 通过遍历去最大值即可,时间复杂度是O(3n)=O(n) #include<iostream> #include<vector> #include<algorithm> #define pre(i,j,k) for (int i = j; i < k; i++) using namespace std; int main(){ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); int n; cin >> n; vector<int> sufmax(n + 1), a(n); pre(i, 0, n) cin >> a[i]; for (int i = n - 1; i >= 0; i--) sufmax[i] = max(sufmax[i + 1], a[i]); int ans = 0; pre(i, 0, n) ans = max(ans, sufmax[i] - a[i]); cout << ans; return 0; }