http://acm.hdu.edu.cn/showproblem.php?pid=6438

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

题意:给出n个数 a[i],可以在某天买入x,并在某天卖出 y ,利润就是y -x ,问最大利润和操作次数

题解:维护一个最小价值的优先队列,每次买入,操作次数就 加二(因为要卖出)。注意卖出的时候可能在当天卖出并不是最优,这时候就要再次push一个中间价,表示把之前累积的利润抵消掉。

/***                                                                    *            .,,       .,:;;iiiiiiiii;;:,,.     .,,                   *          rGB##HS,.;iirrrrriiiiiiiiiirrrrri;,s&##MAS,                *         r5s;:r3AH5iiiii;;;;;;;;;;;;;;;;iiirXHGSsiih1,               *            .;i;;s91;;;;;;::::::::::::;;;;iS5;;;ii:                  *          :rsriii;;r::::::::::::::::::::::;;,;;iiirsi,               *       .,iri;;::::;;;;;;::,,,,,,,,,,,,,..,,;;;;;;;;iiri,,.           *    ,9BM&,WA了活该       .,:;;:,,,,,,,,,,,hXA8:   T了天命..,,,.       *   ,;&@@#r:;;;;;::::,,.   ,r,,,,,,,,,,iA@@@s,,:::;;;::,,.   .;.      *    :ih1iii;;;;;::::;;;;;;;:,,,,,,,,,,;i55r;;;;;;;;;iiirrrr,..       *   .ir;;iiiiiiiiii;;;;::::::,,,,,,,:::::,,:;;;iiiiiiiiiiiiri         *   iriiiiiiiiiiiiiiii;;;::::::::::::::::;;;iiiiiiiiiiiiiiiir;        *  ,riii;;;;;;;;;;;;;:::::::::::::::::::::::;;;;;;;;;;;;;;iiir.       *  iri;;;::::,,,,,,,,,,:::::::::::::::::::::::::,::,,::::;;iir:       * .rii;;::::,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,::::;;iri       * ,rii;;;::,,,,,,,,,,,,,:::::::::::,:::::,,,,,,,,,,,,,:::;;;iir.      * ,rii;;i::,,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,,::i;;iir.      * ,rii;;r::,,,,,,,,,,,,,:,:::::,:,:::::::,,,,,,,,,,,,,::;r;;iir.      * .rii;;rr,:,,,,,,,,,,,,,,:::::::::::::::,,,,,,,,,,,,,:,si;;iri       *  ;rii;:1i,,,,,,,,,,,,,,,,,,:::::::::,,,,,,,,,,,,,,,:,ss:;iir:       *  .rii;;;5r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sh:;;iri        *   ;rii;:;51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.:hh:;;iir,        *    irii;::hSr,.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,sSs:;;iir:         *     irii;;:iSSs:.,,,,,,,,,,,,,,,,,,,,,,,,,,,..:135;:;;iir:          *      ;rii;;:,r535r:...,,,,,,,,,,,,,,,,,,..,;sS35i,;;iirr:           *       :rrii;;:,;1S3Shs;:,............,:is533Ss:,;;;iiri,            *        .;rrii;;;:,;rhS393S55hh11hh5S3393Shr:,:;;;iirr:              *          .;rriii;;;::,:;is1h555555h1si;:,::;;;iirri:.               *            .:irrrii;;;;;:::,,,,,,,,:::;;;;iiirrr;,                  *               .:irrrriiiiii;;;;;;;;iiiiiirrrr;,.                    *                  .,:;iirrrrrrrrrrrrrrrrri;:.                        *                        ..,:::;;;;:::,,.                             */
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}struct cmp
{bool operator()(const pair<int,int> p1, const pair<int,int> p2){if(p1.first == p2.first) return p1.second > p2.second;return p1.first > p2.first; }
};
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>Q;
int main() {int t;cin>>t;while(t--){while(!Q.empty()) Q.pop();int n;cin>>n;ll x;ll sum = 0,st = 0;for(int i = 1; i <= n; i++){cin>>x;if(Q.empty()){Q.push(make_pair(x,2)); //买入}else{auto it = Q.top();ll v = it.first;//cout<<v<<endl;if(x > v){Q.pop();sum += 1ll*(x-v);st += it.second;Q.push(make_pair(x,0)); //压入一个中间价}Q.push(make_pair(x,2));  //将当前价值买入}}printf("%lld %lld\n",sum,st);}
}

HDU 6438 Buy and Resell相关推荐

  1. HDU 6438 Buy and Resell【贪心】

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

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

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

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

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

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

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

  5. Buy and Resell

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

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

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

  7. HDU6348 Buy and Resell

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

  8. 2018中国大学生程序设计竞赛 – 网络选拔赛 1001 Buy and Resell [模拟]

    1001 Buy and Resell  题目:有1-n个货物,可以在某个点buy,然后在后面的点resell,可以同时买多个,问最大的利润和最小的交易次数. 题解:模拟运算,前 i 天都是可以买的, ...

  9. 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 ...

  10. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

最新文章

  1. windows下检测网站是否正常运行并自动重启服务
  2. linux i2c调试命令,嵌入式Linux下I2C接口调试
  3. linux 正则查找email_Linux 正则表达式详解
  4. WEBBASE篇: 第八篇, JavaScript知识2
  5. QSettings生成以及解析配置文件
  6. Qt工作笔记-对主事件循环的进一步认识
  7. 7. OD-破解收费版限制天数的软件
  8. 【BZOJ3224】【codevs4543】【tyvj1728】普通平衡树,第一次的splay
  9. 小米2 android版本更新,小米MIUI再更新,又有2款手机可以用上基于Android 11的MIUI12...
  10. 西安西北大学计算机研究生学费,关于西北大学研究生学费及住宿的解答
  11. ssim 算法 java实现_图像质量评价指标之 PSNR 和 SSIM
  12. 可调稳压电源lm317实验报告_LM317可调稳压电源
  13. 有料科普 | 有一说一,BIM和CIM的这层关系,你了解吗?
  14. SparkSQL——各区域热门商品TOP3
  15. 华硕笔记本提示android,华硕主板电脑和华硕笔记本开启VT进BIOS设置方法教程
  16. 天池竞赛:金融风控-贷款违约预测
  17. C++程序设计基础之(第三章)函数
  18. BeatMark X for mac(fcpx音乐卡点神器)
  19. 在Web前端基于CAD图实现等值线在线分析
  20. 苹果x计算机怎么恢复,苹果手机怎样找回备忘录,电脑小白数据恢复全攻略

热门文章

  1. 【Rust精彩blog】Rust 中几个智能指针的异同与使用场景
  2. java台球游戏_java桌球小游戏项目
  3. 轻松认识网络防火墙及应用
  4. 【问题】SQL远程过程调用失败
  5. Readhub客户端
  6. 【NOI2017模拟3.30】原谅(计算几何,期望)
  7. 猫哥说创业:发布软文赚钱术(1)
  8. 大型医院挂号系统(数据结构课设)
  9. 普适计算-2014/03/21
  10. Visual Studio 2019 编译报错:错误 CS2012 无法打开 obj\Debug\xx.exe”进行写入 --“对路径xx.exe”的访问被拒绝)解决方法