HDU 6438
1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai dollars if he has at least one Power Cube
3. do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.
In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16 In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5 In the third case, he will do nothing and earn nothing. profit = 0
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int Case=;Case<=t;Case++)
{
int n;
scanf("%d",&n);
priority_queue<int , vector<int>,greater<int> >pq;//优先队列从小到大排,
long long ans=,cnt=;
map<int,int>vis;//用来标记是否有中间变量
for(int i=;i<n;i++)
{
int x;
scanf("%d",&x);
pq.push(x);
if(pq.top() < x)//如果前面最小值比这个数小
{
cnt++;//进行交易
ans+=x-pq.top();
if(vis[pq.top()] > )//判断是否是中间变量
{
cnt--;
vis[pq.top()]--;
}
pq.pop();
pq.push(x);
vis[x]++;//代表在以后的交易中 该数为中间变量
}
}
printf("%lld %lld\n",ans,cnt*);
}
return ;
}
HDU 6438的更多相关文章
- HDU - 6438(贪心+思维)
链接:HDU - 6438 题意:给出 n ,表示 n 天.给出 n 个数,a[i] 表示第 i 天,物品的价格是多少.每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少 ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 【hdu 6438】Buy and Resell
[链接] 我是链接,点我呀:) [题意] 有一个物品的价格在1..n这些位置各不相同. 你初始有无限的钱. 问你从1走到n. 你每次可以选择买入一个或者卖出一个该种物品(或啥都不做) 问你最后的最大利 ...
- HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )
题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...
- HDU 6438 Buy and Resell
高卖低买,可以交易多次 维护一个优先队列,贪心 相当于每天卖出 用当前元素减优先队列最小得到收益 用0/卖出,1/买入标志是否真实进行了交易,记录次数 #include<bits/stdc++. ...
- HDU 6438"Buy and Resell"(贪心+优先级队列)
传送门 •参考资料 [1]:HDU6438(优先队列+思维) •题意 有n个城市,第 i 天你会达到第 i 个城市: 在第 i 个城市中,你可以用 ai 元购买一个物品,或者用 ai 元卖掉一个物品, ...
- 2018CCPC网络赛
A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing
SIGCOMM17 摘要 在现有的网络测量任务中包括流量监测.数据收集和一系列网络攻击的预防.现有的基于sketch的测量算法存在严重性能损失.大量计算开销以及测量的精确性不足,而基于硬件的优化方法并 ...
- ASP.NET Core Middleware (转载)
What is Middleware? Put simply, you use middleware components to compose the functionality of your A ...
- git相关命令
查看分支:git branch创建分支:git branch <name>切换分支:git checkout <name>创建+切换分支:git checkout -b < ...
- Mysql查询正在运行的事务
查询 正在执行的事务:SELECT * FROM information_schema.INNODB_TRX 根据这个事务的线程ID(trx_mysql_thread_id): 可以使用mysql命令 ...
- oracle与infomix异同点
之前是做oracle数据库应用开发的,现在工作用的是informix,特别不习惯.用了一段时间后才慢慢适应,最近做系统升级,把informix换成oracle数据库.顺便整理了一下informix与o ...
- 【js】Redux基本原理和使用
Redux不是说任何的应用都要用到它,如果遇到了react解决不了得问题,可以考虑使用它. 例如: 用户的使用方式复杂不同身份的用户有不同的使用方式(比如普通用户和管理员)多个用户之间可以协作与服务器 ...
- C++快速开发样本工程的建立--建立工程
因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...
- JQuery弹出Dialog关闭方式close vs destroy
$editDialog.iDialog('close') $(this).dialog('close'); 等Close方法关闭Dialog时,Dialog并不是完全消失,只是隐藏起来.两个Dial ...
- Quick find Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 视图 ...
- mysql5.6升级为mysql5.7部署jboss/wildfly应用项目
一.部署mysql5.7二进制版 解压tar -xvf mv mysql-5.7 /usr/local/mysql5.7 或者其他文件夹 cd /usr/local/mysql.57 usera ...