Buy and Resell

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1160    Accepted Submission(s): 369

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

 
Source
 
  用一个优先队列维护当前可购买的最小值有哪些,对每一天考虑如果当前这天卖出可获得的最大价值,由于在当前天卖出不一定是最优的,在卖出后在将a的值入队然后标记一下表示这个a是由另一个价值x卖掉之后得到的,和普通的a略有不同,这样在后面出现a是最小值的
时候次数就可以不变了。(x->a->y  <==>  x->y),相同的数可能出现多次所以标记可以累计,一开始只是用了0/1疯狂WA。
  

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
map<int,int>M;
int main(){
int t,n,m,i,j,k,a;
cin>>t;
while(t--){
scanf("%d",&n);
priority_queue<int,vector<int>,greater<int> >q;
M.clear();
LL s1=,s2=;
for(i=;i<=n;++i){
scanf("%d",&a);
if(q.empty() || q.top()>=a){
q.push(a);
}
else{
s1+=a-q.top();
if(M[q.top()]==){
s2++;
}
else{
M[q.top()]--;
}
q.pop();
q.push(a);
q.push(a);
M[a]++;
} }
//cout<<s1<<endl;
printf("%lld %lld\n",s1,s2*);
}
return ;
}

hdu-6438-贪心的更多相关文章

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

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

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

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

  3. HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 && 贪心 )

    题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867 ...

  4. HDU 6438"Buy and Resell"(贪心+优先级队列)

    传送门 •参考资料 [1]:HDU6438(优先队列+思维) •题意 有n个城市,第 i 天你会达到第 i 个城市: 在第 i 个城市中,你可以用 ai 元购买一个物品,或者用 ai 元卖掉一个物品, ...

  5. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  6. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  7. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  8. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  9. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

  10. HDU 2307 贪心之活动安排问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)  ...

随机推荐

  1. 使用python+ffmpeg+youtube-dl下载youtube上的视频

    一.准备工作 1.安装python,详见https://www.cnblogs.com/cnwuchao/p/10562416.html 2.安装ffmpeg,详见https://www.cnblog ...

  2. [转载]linux下core文件设置与查看

    转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...

  3. HDU 4400 Mines(好题!分两次计算距离)

    http://acm.hdu.edu.cn/showproblem.php?pid=4400 题意: 在笛卡尔坐标中有多个炸弹,每个炸弹有一个坐标值和一个爆炸范围.现在有多次操作,每次引爆一个炸弹,问 ...

  4. Windows has encountered a critical problem and will restart automatically in one minute. Please save your work now

    Windows has encountered a critical problem and will restart automatically in one minute. Please save ...

  5. arcgis 要素服务增删改查

    两种方式: 第一种 要素服务的增删改操作,在ArcGIS API for JS中给我们提供了三个类用于要素的增Add,删Delete,改Update 添加draw和要素服务 //用于操作的要素图层,注 ...

  6. MVC ---- 怎删改查

    using Modelsop; using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  7. git 修改默认编辑器

    vim,notepad(windows自带),notepad++ 当然要选notepad++ 1.首先下载notepad++ 2.将notepad++安装目录放到path中 3.git config ...

  8. MySQL学习(十三)

    编码问题 乱码是如何形成的 1 解码时与实际编码不一致 可修复 2 传输过程中,编码不一致,导致字节丢失,不可修复,如把utf8转为GB2312 连接器的特性:连接客户端和服务器,客户端的字符先发给连 ...

  9. Qt的Radio Button(单选按钮)

    1 在UI界面中加入控件 2 对QRadioButton控件进行分组 QRadioButton的分组有多重方法,如采用组合框.QWidge等,下面介绍采用QButtonGroup方法来实现分组,好处是 ...

  10. chkconfig命令参数

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...