传送门:HDU 6438


Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai 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 i-th city and choose exactly one of the following three options on the i-th day:
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.

Input
There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
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.

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 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个城市中,你有三种策略,你可以用ai元购买一个物品,或者用ai元卖掉一个物品,或者不买也不卖,你可以同时保存多个物品。最开始你身上没有物品,但是又无限的金钱,现在要求你从城市1走到城市n,问你最大的收益是多少,同时交易次数是多少。

题解:

很容易想到的是,我们应该花较少的钱买入,然后卖出更多的钱。如果我们在第i天买入,第j天卖出那么必须满足a[j]>a[i]a[j]>a[i]a[j]>a[i],收入为a[j]−a[i]a[j]-a[i]a[j]−a[i],如果后面存在第k天,使得a[k]>a[j]a[k]>a[j]a[k]>a[j],那么我们就应该在第k天卖出物品,因为收益更大,为a[k]−a[i]a[k]-a[i]a[k]−a[i]。
我们发现有恒等式:a[k]−a[i]=a[k]−a[j]+a[j]−a[i]a[k]-a[i]=a[k]-a[j]+a[j]-a[i]a[k]−a[i]=a[k]−a[j]+a[j]−a[i]; 这说明第j天有没有卖出并不会影响最终结果(相当于中间商,且无差价)
但是我们又考虑到,第j天虽然有没有卖出不影响结果,但是有没有买入是影响结果的,所有我们还要考虑买入的情况。

优先队列小根堆)来实现上述过程,如果 j 的中间商,那么我们就往队列里存两次,一次是中间商(卖出),一次是买入。当当前的a大于队首元素时,我们就进行交易,同时如果队首元素是中间商的话交易次数是不改变的。


AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int t,n;
ll cnt,ans;
priority_queue<ll,vector<ll>,greater<ll> >q;
map<ll,int> mp;
int main()
{ll a;scanf("%d",&t);while(t--){while(!q.empty()) q.pop();mp.clear();cnt=0,ans=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%I64d",&a);if(!q.empty()&&q.top()<a){ll tmp=q.top();q.pop();ans+=a-tmp;q.push(a);//两次存入队列cnt++;mp[a]++;if(mp[tmp]!=0)//中间商{cnt--;//交易次数不改变mp[tmp]--;}}q.push(a);}printf("%I64d %I64d\n",ans,cnt*2);}return 0;
}

HDU 6438 Buy and Resell 【贪心+思维+优先队列】相关推荐

  1. HDU - 6438 Buy and Resell(思维+ 贪心)

    题目链接 题意 t组输入,每组n个城市,给出每个城市的货物的价格,在每个城市可以进行三个操作:购买货物,卖出货物,不做操作,问从城市1到城市n(不能返回)最多能获得多少利润,且操作数最少(买和卖分别算 ...

  2. HDU 6438 Buy and Resell【贪心】

    http://acm.hdu.edu.cn/showproblem.php?pid=6438 Problem Description The Power Cube is used as a stash ...

  3. Buy and Resell(贪心好题!)

    这个贪心可以说是很巧妙了. Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. Buy and Resell HDU - 6438(补)更新贪心

    The Power Cube is used as a stash of Exotic Power. There are nn cities numbered 1,2,-,n1,2,-,n where ...

  5. HDU - 6438(贪心+思维)

    链接:HDU - 6438 题意:给出 n ,表示 n 天.给出 n 个数,a[i] 表示第 i 天,物品的价格是多少.每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少 ...

  6. hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心

    题目传送门 题目描述: 有n座城市,每座城市都可以对一个物品进行一次的买进或者卖出,可以同时拥有多个物品,计算利润最大值,并且交易次数要最少.(买入卖出算两次操作) 思路: 建立两个小根堆 优先队列, ...

  7. HDU6348 Buy and Resell

    Bryce1010模板 HDU6348 Buy and Resell 题意: 从前往后开始旅游,在每个村庄可以选择三个操作: (1)买入物品 (2)卖出物品 (3)不买不卖 求最后的最大获益. 思路: ...

  8. Buy and Resell

    题目链接: Buy and Resell 大致题意: 有n个城市, 在每个城市中你可以选择花费a[i]的价格买一个能量块, 也可以选择以a[i]的价格卖出一个能量块(前提是你要有能量块可以卖). 你会 ...

  9. 【牛客每日一题】tokitsukaze and Soldier 题目精讲 贪心、优先队列、堆

    链接:https://ac.nowcoder.com/acm/problem/50439 来源:牛客网 ACM在线模板 今天才发现牛客推出了一个每日一题的版块,3月25号就开始了,今天才发现,赶紧补救 ...

  10. HDU 2795 Billboard (线段树+贪心)

    HDU 2795 Billboard (线段树+贪心) 手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/articl ...

最新文章

  1. 查询在一张表不在另外一张表的记录
  2. java web jsp相对路径_Java Web中的相对路径与绝对路径总结
  3. OLEDB IMEX行数限制的问题
  4. 7.3 MASS批量修改数据(Excel文件上传数据)
  5. CSDN2013博客之星评选(求投票支持)
  6. 从零开始学前端:浮动 --- 今天你学习了吗?(CSS:Day15)
  7. 《实践与思考》系列连载(6)——IT从业人员工作环境及状态调查 抽奖结果公布...
  8. 小贷公司的风险成因及应对策略——风险篇
  9. php当前页面删除处理,php删除记录同时刷新当前页面的实现代码
  10. 快讯:Nginx 之父被抓了!
  11. IO(一)----字节流
  12. robotium3.6与4.0以后的区别
  13. heidi修改mysql的账号密码_HeidiSQL使用教程
  14. 中文简体繁体转换(JS 字符串 简体转繁体 繁体转简体)
  15. 如何成为团队核心?从写出不可维护的代码开始
  16. 数据显示:中国程序员是世界上最牛的程序员
  17. SSM 校园外卖系统的设计与实现
  18. 为什么很多人会觉得FPGA难学?
  19. 如何做一份好的竞品分析:商业WIFI竞品分析让你不再跳坑
  20. Oracle - 回滚段

热门文章

  1. phpstorm关闭语法检查的解决方案
  2. 介孔二氧化硅纳米粒子应用在组织工程
  3. 万事开头难 实践出真知
  4. 模拟点击事件php,利用js脚本模拟鼠标点击事件
  5. 学习Flutter:Hello Word篇
  6. MySQL中emoji表情包的存储问题
  7. 金融风控项目(数据分析最后阶段精华总结很久!)
  8. 微博html5版打不开,电脑新浪微博页面打不开怎么办|电脑微博无法打开的解决方法...
  9. Mac运行Win 10画面出问题?教你如何修复这个问题
  10. CS:GO开服架设服务器搭建游戏配置方法教程教学插件配置下载资源配置