Codeforces Round #437 Div. 2 E. Buy Low Sell High
来源:互联网 发布:中兴网络机顶盒说明书 编辑:程序博客网 时间:2024/04/28 03:18
Description
You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don’t own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.
Input
Input begins with an integer N (2 ≤ N ≤ 3·105), the number of days.
Following this is a line with exactly N integers p1, p2, …, pN (1 ≤ pi ≤ 106). The price of one share of stock on the i-th day is given by pi.
Output
Print the maximum amount of money you can end up with at the end of N days.
Examples
input910 5 4 7 9 12 6 2 10output20input203 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4output41
题目大意
数据给出第n天的股票价格,你可以在某一天买入一个股票或者卖出一个股票或者什么都不做,问最大的获益是多少。
解题思路
用小顶堆来维护每天股票的价格,当堆顶元素小于插入的当日股票价格时,则将该股票卖出,盈利为差价。将当日股票价格插入两次是因为一次作为当天买入股票,另一次作为中转价格保存,比如第1天买入2元的股票,第3天股票价格为4元,则将2元股票卖出盈利2元,然后第4天股票价格为7元,则将4元股票卖出盈利3元,此时相当于第1天买入股票然后再第4天卖出,盈利5元。
代码实现
#include<bits/stdc++.h>using namespace std;#define ll long longpriority_queue<int,vector<int>,greater<int> >qu;int main(){ int n,t; ll ans; while(~scanf("%d",&n)) { ans=0; scanf("%d",&t); qu.push(t); for(int i=1;i<n;i++) { scanf("%d",&t); if(t>qu.top()) { ans+=(t-qu.top()); qu.pop(); qu.push(t); qu.push(t); } else qu.push(t); } printf("%I64d\n",ans); } return 0;}
- 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 867E Buy Low Sell High
- Codeforces 867 E. Buy Low Sell High (贪心)
- codeforces 867 E Buy Low Sell High(优先队列)
- Codeforces Round #437 div2-贪心&好题-Buy Low Sell High
- codeforces 865 C. Buy Low Sell High
- E. Buy Low Sell High(贪心+set)
- CodeForces 865D Buy Low Sell High(思维)
- Codeforces Round #437 (Div. 2)-A-B-C-E
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces Round #103 (Div. 2) E题
- Codeforces Round #147 (Div. 2) E
- Codeforces Round #184 (Div. 2) D、E
- Codeforces Round #197 (Div. 2) (C~E)
- Codeforces Round #102 (Div. 2) //缺E
- 第一个只出现一次的字符java实现
- ubuntu 下U盘被锁住,不能复制粘贴文件-------解决
- CSDN-markdown编辑器[备用]
- 浅谈微服务
- Codeforces 863E
- Codeforces Round #437 Div. 2 E. Buy Low Sell High
- java发送邮件
- 大数据能做什么?
- cmake安装
- 数位DP小结(阶段性)
- python_lintcode_69二叉树的层次遍历
- LeetCode 24. Swap Nodes in Pairs
- 如何判断LSTM模型中的过拟合和欠拟合 By 机器之心2017年10月02日 11:09 判断长短期记忆模型在序列预测问题上是否表现良好可能是一件困难的事。也许你会得到一个不错的模型技术得分,但了解
- Matlab学习笔记(8)——hist函数