codeforces 867 E. Buy Low Sell High

翻译 from 凌幽

首先有一个$O(n^2)$的$dp$……设$f_{i,j}$表示到第$i$天为止,手上有$j$支股票时最大收益

然而这个$dp$并没什么用……

维护一个小根堆,然后遍历所有的$p_i$,如果当前堆顶比$p_i$小,那么就买堆顶,然后买$p_i$,之后$pop$,之后$push(p_i)$两次;否则直接$push(p_i)$

这样的话就是一个可以支持回滚的贪心了,如果当前选择$p_i$并不优秀,则会在之后的时候将$p_i$忽略掉

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N = 300000 + 10;
 5 int n; priority_queue<int, vector<int>, greater<int> > pq;
 6 
 7 int main() {
 8     scanf("%d", &n);
 9     ll ans = 0;
10     for(int i = 1, p ; i <= n ; ++ i) {
11         scanf("%d", &p);
12         pq.push(p), pq.push(p);
13         ans += p - pq.top();
14         pq.pop();
15     }
16     printf("%lld\n", ans);
17 }
codeforces 867 E. Buy Low Sell High
posted @ 2018-09-21 17:09  KingSann  阅读(172)  评论(0编辑  收藏  举报