传送门

题面:

The Power Cube is used as a stash of Exotic Power. There are nn cities numbered 1,2,…,n1,2,…,n where allowed to trade it. The trading price of the Power Cube in the ii-th city is aiai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the ii-th city and choose exactly one of the following three options on the ii-th day:

1. spend aiai dollars to buy a Power Cube 
2. resell a Power Cube and get aiai 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 nn 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.

Input

There are multiple test cases. The first line of input contains a positive integer TT (T≤250T≤250), indicating the number of test cases. For each test case: 
The first line has an integer nn. (1≤n≤1051≤n≤105) 
The second line has nn integers a1,a2,…,ana1,a2,…,an where aiai means the trading price (buy or sell) of the Power Cube in the ii-th city. (1≤ai≤1091≤ai≤109) 
It is guaranteed that the sum of all nn is no more than 5×1055×105.

Output

For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.

Sample Input

3
4
1 2 10 9
5
9 5 9 10 5
2
2 1

Sample Output

16 4
5 2
0   

Hint

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

题目意思:

有n个城市,第i天你会达到第i个城市,在第i个城市中,你可以用ai元购买一个物品,或者用ai元卖掉一个物品,你可以同时保存多个物品。最开始你身上没有物品,但是又无限的金钱,现在要求你从城市1走到城市n,问你最大的收益是多少。

题目分析:

听说这又是个CF的原题?(CF867E)

首先考虑这样的一个问题:假设我在第i个城市入货Ai元物品,在i+1个城市发现,此时我们考虑将物品卖出,则此时的利润为:。同时,倘若我在第i+2个城市发现,则我们也考虑将物品卖出,则此时的利润为:,而我们发现,对于这三天而言,总利润为:,我们发现,对于这种状态,中间商对答案是没有影响的(换句话说,就是没有中间商是赚差价)

因为要求我们赚的最多,因此我们必定是优先将最贵的跟买入最便宜的相减,因此我们可以考虑对价值开一个小根堆进行维护。

而倘若我们发现在第i天中,Ai>之前的最小值,则此时的Ai可能作为中间商,因此我们可以将Ai压入队列两次,代表Ai可能作为中间商影响答案。

至于统计次数cnt,我们发现,倘若Ai是中间商(即Ai在队列内还影响答案了),则我们则不需要统计这一次的记录,否则我们需要将次数cnt+1即可。

代码:

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
priority_queue<int,vector<int>,greater<int> >que;//优先队列
map<int,int>mp;
typedef long long ll;
int main()
{int t;scanf("%d",&t);while(t--){int n;while(!que.empty()) que.pop();mp.clear();scanf("%d",&n);ll res=0,cnt=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);if(!que.empty()&&que.top()<x){int now=que.top();que.pop();res+=x-now;//统计答案que.push(x);//对x压入队列两次cnt++;//次数+1if(mp[now]){//如果当前队顶元素在之前存在过,则证明它为中间商,则需要消除这次的记录数cnt--;mp[now]--;}mp[x]++;}que.push(x);}cout<<res<<" "<<cnt*2<<endl;}
}

转载于:https://www.cnblogs.com/Chen-Jr/p/11007219.html

HDU6438(优先队列+思维)相关推荐

  1. codeforces 948C / 923B Producing Snow 【优先队列+思维】

    戳我 传送至 Producing Snow 戳我 到参考网站 **题意:**每天产生一堆雪,大小为v[i]个单位体积,然后每天对应一个数值a[i]代表当天温度,然后剩下的每一堆的雪都会减少这个温度的数 ...

  2. Producing Snow CodeForces - 948C 优先队列+思维

    题目链接:https://vjudge.net/problem/CodeForces-948C 转自:https://blog.csdn.net/doncoder/article/details/81 ...

  3. 2019爪哇部落第十届新生选拔赛 题解

    博采众长,共同进步 A.空军十一号 阅读题 筛选信息 送气球 B.小爪的子阵和 贪心 最大连续字段和的二维升级版 C.爪爪逃逸 模拟+思维 D.小爪的三视图 模拟 立方体 暴力+思维 E.爪哇的路 最 ...

  4. 聪明的木匠(优先队列,思维)

    一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,-,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且没有木材损 ...

  5. HDU--5575、Discover Water Tank (思维、优先队列)

    题目链接 题面: 题意: 有一个 1∗n1*n1∗n 的水箱,水箱的四周的高度为无穷大.现在用 n−1n-1n−1 高度为 hih_ihi​ 的隔板将水箱分为 nnn 个 1∗11*11∗1 的部分. ...

  6. 【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)

    题干: Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is s ...

  7. Buy and Resell hdu-6438 贪心 优先队列

    题目链接:Problem - 6438 题面: 题意:你按顺序去n个城市,每个城市商品买入和卖出的价格一样,你可以用一天时间买入或者卖出,问最多可以获取多少钱,已经最少的天数 思路:在每个位置处贪心寻 ...

  8. AcWing - 175 电路维修(思维建边+最短路)

    题目链接:点击查看 题目大意:我们要从左上角走到右下角,只能斜着走,问最少翻转道路的次数 题目分析:很直白的一个中文题,也没有多少坑,主要是思路问题,这里先说思路,我们可以将给出的道路建边,建成一个无 ...

  9. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  10. 【51Nod - 1272 】最大距离 (思维,排序sort的空间优化)

    题干: 给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,则这两个数可以组成一对.每个元素和自己也可以组成一对.例如:{5, 3, 6, 3, 4, 2},可以组成1 ...

最新文章

  1. 让我为你介绍一个神器:Chimee,一套可扩展的 H5 视频播放器解决方案
  2. 寻找内存泄漏:一个案例研究
  3. linux 分区 文件,Linux的分区与文件结构
  4. 388. 文件的最长绝对路径
  5. 微信H5页面自定义微信分享内容
  6. 用C#实现汉字转化为拼音
  7. Blender - Proportional Edit Mode - 按比例编辑模式(3D版的液化、挤压工具)
  8. 017年Android百大框架排行榜
  9. springMvc中的校验框架@valid和@validated
  10. .dwg(sw)-exb
  11. scp 是我小看了你---基于密钥传输!
  12. Iog4j2漏洞相关技术分析
  13. 为什么无线AP标称的速率和实际速率不一致?(空口速率)
  14. 放下手机,我得到了什么?
  15. Python datetime.datetime.isoweekday和date.weekday()
  16. 修改Windows的git bash的主题(样式)
  17. Linux下的eeprom读写操作
  18. 更改linux root颜色,linux 颜色配置
  19. tp5 HBuilder h5打包app,并下载
  20. python 调起wps 打开 excel

热门文章

  1. 微信屏蔽的是域名还是服务器ip,域名从未使用也会被微信屏蔽,这个你怎么看?...
  2. c语言 continue什么意思,continue在C语言中什么意思?
  3. MPB:中大魏泓组-​​无菌小鼠肠道粪菌移植(视频)
  4. 大数据毕业论文:基于大数据的金融量化分析2021-07
  5. Navicat15导入DMP数据文件
  6. iframe框架下的子父级页面监控页面关闭事件
  7. CMD批处理实现dot命令自动运行更新
  8. Axure8与Axure9交互差异总结-1 移动元件交互事件的差异
  9. js中 push pop shift unshift使用的小问题
  10. Linux 怎么防止 ssh 被暴力破解