Day3-F-Buy Low Sell High-CodeForces867E
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
9
10 5 4 7 9 12 6 2 10
20
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
41
Note
In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is - 5 - 4 + 9 + 12 - 2 + 10 = 20.
思路:依旧是贪心,维护一个优先队列,每次读取一个数与队顶判断,如果大于就出队并入队2次读取的数,答案加上差值,否则就入队一次。为什么要入队两次,因为有可能这次不应该卖,应该买,所以就需要入2次,例如:1 3 5 6,读取3时,答案加上2,这次应该是不卖而是买,入队2次,这样下次就是叠加上次的操作,5 - 3 + 3 - 1 == 5 - 1代码如下:
int n;
int main() {
scanf("%d", &n);
priority_queue<LL,vector<LL>,greater<LL>> q;
LL sum = , now;
scanf("%I64d", &now);
q.push(now);
for(int i = ; i < n; ++i) {
scanf("%I64d", &now);
if(now > q.top()) {
sum += now - q.top();
q.pop();
q.push(now);
}
q.push(now);
}
printf("%I64d\n",sum);
return ;
}
Day3-F-Buy Low Sell High-CodeForces867E的更多相关文章
- 【CF865D】Buy Low Sell High(贪心)
[CF865D]Buy Low Sell High(贪心) 题面 洛谷 CF 题解 首先有一个\(O(n^2)\)的\(dp\)很显然,设\(f[i][j]\)表示前\(i\)天手中还有\(j\)股股 ...
- Codeforces Round #437 E. Buy Low Sell High
题意:买卖股票,给你n个数,你可以选择买进或者卖出或者什么都不做,问你最后获得的最大收益是多少. Examples Input 910 5 4 7 9 12 6 2 10 Output 20 Inpu ...
- CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...
- CF865D Buy Low Sell High
/* 贪心来选择, 如果能找到比当前小的, 就用最小的来更新当前的 优先队列即可 */ #include<cstdio> #include<algorithm> #includ ...
- [ CodeForces 865 D ] Buy Low Sell High
\(\\\) \(Description\) 给出\(N\)天股票的价钱\(A_1,...,A_N\),每天可以什么都不做,或者买入或卖出\(1\)支股票,分别花出或收入\(A_i\)元,求最大收益 ...
- CF867E: Buy Low Sell High(贪心, STL) (hdu6438)
Description 有nn个城市,第ii个城市商品价格为aiai,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...
- CF865D Buy Low Sell High 贪心
正解:贪心 解题报告: 传送门! 这题首先有个很显然的dp,太基础了不说QAQ 然后考虑dp是n2的,显然过不去,所以换一个角度 然后发现这题和普通的dp的题有什么不同呢?就它这儿是一天只能买一支股, ...
- Buy Low Sell High CodeForces - 867E (思维,贪心)
大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...
- 【CodeForces】866D. Buy Low Sell High
[题意]已知n天股价,每天可以买入一股或卖出一股或不作为,最后必须持0股,求最大收益. [算法]堆 贪心? [题解] 不作为思想:[不作为=买入再卖出] 根据不作为思想,可以推出中转站思想. 中转站思 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
随机推荐
- ArrayList的底层实现原理
ArrayList源码分析 1.java.util.ArrayList<E> : List 接口的大小可变数组的实现类 ArrayList 内部基于 数组 存储 各个元素. 所谓大小可变数 ...
- socket中文奇数个出现乱码的解决办法
用MyEclipse试了一下JAVA获取系统正在运行进程代码,结果Console输出的时候中文部分输出为乱码,在网上找了很多办法,都没有解决问题.后来发现一个方法,解决了问题,特此分享. 下面成功解决 ...
- Python与线性代数——Numpy中的matrix()和array()的区别
Numpy中matrix必须是2维的,但是 numpy中array可以是多维的(1D,2D,3D····ND).matrix是array的一个小的分支,包含于array.所以matrix 拥有arra ...
- 研究Zookeeper的原理(一)
阅读声明:以下内容是结合网上材料所写个人理解,如有不当,欢迎大家指正~~~谢谢 一.Zookeeper介绍 zookeeper,见名知意嘛,zoo动物园,keeper保持者.管理员,结合起来就是动物管 ...
- 牛客网Sql
牛客网Sql: 1.查询最晚入职的员工信息 select * from employees where hire_date =(select max(hire_date) from employee ...
- 腾讯云 docker 镜像 dotnet/core sdk aspnet
ccr.ccs.tencentyun.com/mcr.microsoft.com/dotnetcoresdk = mcr.microsoft.com/dotnet/core/sdk => 3 ...
- VIM学习笔记一
键位图 转自:链接 永久显示行号: vi ~/.vimrc 加入 :set number 命令 简单说明 :w 保存编辑后的文件内容,但不退出vim编辑器.这个命令的作用是把内存缓冲区中的数据写到启动 ...
- spring boot 中的配置文件可以存放的位置
- 解决:Field xxMapper in xx.service.impl.xxServiceImpl required a bean of type 'xx.mapper.xxMapper'
1.启动 SpringBoot项目报错,使用的是Springboot.Spring.Mybatis连接Mysql数据库,启动SpringBoot项目报错,错误如下所示: _____ .__/\ .__ ...
- Python数据类型-7 bytes
bytes 在Python3以后,字符串和bytes类型彻底分开了.字符串是以字符为单位进行处理的,bytes类型是以字节为单位处理的. bytes数据类型在所有的操作和使用甚至内置方法上和字符串数据 ...