codeforces 867 E Buy Low Sell High(优先队列)
2017-10-22 09:39
681 查看
题目地址
题意:你能找到n天的股票价格,但是你每天只能买一只股票或者卖出一只股票,问你n天以后你最多能赚多少钱。
思路:每天都把每天的股票价格取负放入优先队列中两次(为什么取负?因为有限队列是数字越大越优先。二我们需要最小的价格,取负就变成了最大的负数了,为什么两次?因为如果我们两次取到的话就说明这次没有操作,但是在今天卖掉了之前的股票(这个能理解就好了,就是说先取到把之前加上的抵消掉,后面那个就是单纯的卖了)),然后我们按这个模拟就好了。
题意:你能找到n天的股票价格,但是你每天只能买一只股票或者卖出一只股票,问你n天以后你最多能赚多少钱。
思路:每天都把每天的股票价格取负放入优先队列中两次(为什么取负?因为有限队列是数字越大越优先。二我们需要最小的价格,取负就变成了最大的负数了,为什么两次?因为如果我们两次取到的话就说明这次没有操作,但是在今天卖掉了之前的股票(这个能理解就好了,就是说先取到把之前加上的抵消掉,后面那个就是单纯的卖了)),然后我们按这个模拟就好了。
#include <iostream> #include <cstring> #include <string> #include <queue> #include <vector> #include <map> #include <set> #include <cmath> #include <cstdio> #include <algorithm> #include <iomanip> #define N 100010 #define LL __int64 #define inf 0x3f3f3f3f #define lson l,mid,ans<<1 #define rson mid+1,r,ans<<1|1 #define getMid (l+r)>>1 #define movel ans<<1 #define mover ans<<1|1 using namespace std; const LL mod = 1000000007; const double eps = 0.001; priority_queue<LL>q; int main() { cin.sync_with_stdio(false); LL n, num, sum; while (cin >> n) { while (!q.empty()) { q.pop(); } sum = 0; for (int i = 0; i < n; i++) { cin >> num; q.push(-num); q.push(-num); int a = q.top(); sum += num + a; q.pop(); } cout << sum << endl; } return 0; }
相关文章推荐
- Codeforces 867 E. Buy Low Sell High (贪心)
- Codeforces Round #437 (Div. 2)E. Buy Low Sell High(优先队列)
- codeforces 865 C. Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- E. Buy Low Sell High(贪心+set)
- codeforces865D - Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2) E. Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- MemSQL Start[c]UP 3.0 - Round 2 and Codeforces Round #437 (Div. 1) D. Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)E - Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- Codeforces Round #437 Div. 2 E. Buy Low Sell High
- Codeforces Round #437 (Div. 2 E. Buy Low Sell High 先买后卖 贪心
- D - Buy Low Sell High(multiset)