您的位置:首页 > 其它

codeforces 867 E Buy Low Sell High(优先队列)

2017-10-22 09:39 681 查看
题目地址

题意:你能找到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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: