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. C#数字前面如何补0

    PadLeft()函数:向左补齐PadRight()函数:向右补齐 class Program { static void Main(string[] args) { ; Console.WriteL ...

  2. Anaconda使用命令

    参考的地址:https://zhuanlan.zhihu.com/p/32925500 conda 基础命令: 1.验证conda已被安装 conda --version 终端上就会以conda 版本 ...

  3. JQuery---高级类选择器

    1.ContentFilters 1.1 语法:$('div:contains(edu)').css('backgroundColor','yellow'); 只看div 本身是否包含内容 1.2 语 ...

  4. Hbase与Oracle比较(列式数据库与行式数据库)

    Hbase与Oracle比较(列式数据库与行式数据库) 1 主要区别 Hbase适合大量插入同时又有读的情况 Hbase的瓶颈是硬盘传输速度,Oracle的瓶颈是硬盘寻道时间.   Hbase本质上只 ...

  5. ElasticSearch——日志工具

    Elasticsearch: 权威指南 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.ht ...

  6. ArrayList.clear、=null、new Arraylist之间的对比区别

    参考博文 1.使用ArrayList的对象方法clear() List list = new ArrayList(); List list1 = list; list.add(1); list.add ...

  7. 给 layui upload 带每个文件的进度条, .net 后台代码

    1.upload.js 扩展 功能利用ajax的xhr属性实现该功能修改过modules中的upload.js文件功能具体实现:在js文件中添加监听函数 //创建监听函数 var xhrOnProgr ...

  8. cookie 简单用法

    cookie 简单用法 //当前登录人的组织Id HttpCookie SingleValueCookie = new HttpCookie("DepartmentId", &qu ...

  9. (总结)CentOS 6.x使用yum快速安装Apache+PHP+Tomcat(JSP)+MySQL

    (总结)CentOS 6.x使用yum快速安装Apache+PHP+Tomcat(JSP)+MySQL PS:这个是懒人yum快速安装法,用于开发和测试环境很方便,用于没有特殊要求的生产环境也可以.特 ...

  10. python 启动新进程执行脚本

    import subprocess p_restart=subprocess.Popen(['/bin/sh','/etc/init.d/xxx_service','reboot'])