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) ...
随机推荐
- FastDFS文件上传和下载流程
文件上传流程 客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息.文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名. 组名:文件上传后所在的 ...
- mysql之指定为definer的用户不存在
问题描述: java.sql.SQLException: The user specified as a definer ('tsingsoft'@'%') does not exist 解决: 1. ...
- SearchRequest用于与搜索文档、聚合、定制查询有关的任何操作
SearchRequest用于与搜索文档.聚合.定制查询有关的任何操作,还提供了在查询结果的基于上,对于匹配的关键词进行突出显示的方法. 1,首先创建搜索请求对象:SearchRequest sear ...
- Interesting丨当我们用蚂蚁的视角看待世界
分享一组很有意思的图片~
- 设计模式课程 设计模式精讲 4-2 简单工厂coding
1 代码演练 1.1 未使用简单工厂模式代码 1.2 使用简单工厂模式 1.3 使用反射机制简单工行模式 1 代码演练 1.1 未使用简单工厂模式代码 测试类: package com.geely.d ...
- 女神说拍了一套写真集想弄成素描画?很简单,用Python就行了!
素描作为一种近乎完美的表现手法有其独特的魅力,随着数字技术的发展,素描早已不再是专业绘画师的专利,今天这篇文章就来讲一讲如何使用python批量获取小姐姐素描画像.文章共分两部分: 第一部分介绍两种使 ...
- UIScrollView学习指南
--前言 笔者结合自己的工作经验,梳理关于UIScrollView究竟需要掌握哪些知识才算是一个好手.至于具体的实施方案,网上资源很多,自行了解吧. --正文 1.涉及到滚动和放大的功能,优先考虑使用 ...
- 第六周之Hadoop学习(六)
继续上周开启telnet的过程,这个过程发现win10上运行不了telnet的命令 原因大概在于没有开启telnet服务,从网上下载好telent服务端,安装后继续尝试是否能在win10上使用hado ...
- C++代码书写规范——给新手程序员的一些建议
代码就是程序员的面子,无论是在工作中在电脑上写程序代码还是在面试时在纸上写演示代码我们都希望写出整洁,优雅的代码.特别在工作中当我们碰到需要维护别人的代码,或者是多人参与一个项目大家一起写代码的时候, ...
- 本机配置集群主机名访问(Windows配置hosts)
Windows配置hosts C:\Windows\System32\drivers\etc\hosts 主机IP 主机名 示例: 192.168.1.1 master 192.168.1.2 sla ...