这个贪心可以说是很巧妙了。

Buy and Resell

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

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

2018中国大学生程序设计竞赛 - 网络选拔赛

Recommend

chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443

Statistic | Submit | Discuss | Note

这道题的难点就在于怎么搞这个买和卖的关系。

假设每一个点我都买了。当后来的数字大于前边的我就要买,这个买是真的买,就是这个物品被买了之后就不在我选择要买商品的管辖范围之内了。而被卖的,只是假装前边以这个价值卖掉,但是可能这个点也有可能买,所以这种的还要把这个价值加到queue里,做待定,如果下次被买了,那么就真的被买了。

其实我上面说的话无非可以根据样例进行理解:

1 2 10 9

把1放进优先队列里。然后把2放进去。发现2>1,这个时候就可以考虑买1,1就是被真的买了(所谓真的买就是移出优先队列,从此就没有这个选项了,但是它所贡献的价值不一定是当前被卖获得的价值),ans+=(2-1),2就是没有被买,但是2可能被之后的买,所以2放进队列。(这时候有人发现2实际上放进了队列两次)。。。为什么呢?是因为2这个位置是被假卖的,可能实际上2这个位置买会比较好一点。现在为止,队列里的数字是(2,2)然后继续,10入队列。显然10>2,这个时候把ans+=(10-2).就是说实际上2这个位置是没有操作的。当时1在2的位置被“假卖”了,而2这个位置是应该真买的,又在10这个位置被卖了。所以这么一转化就是1这个东西在10这个位置被卖了(由加法拆分可知ans是对的。只是操作次数要减掉1对)。pop()后此时队列里还剩(2,10)。再加入9,发现9>2,此时ans+=(9-2)。嗯,现在知道了为什么在假卖的地方要再加一遍进队列了吧。

代码:

#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> >qq;
///priority_queue<int, vector<int>, greater<int> > pq;
typedef long long LL;
int main()
{/// int vis[100010];int n,x;LL cnt=0;LL ans=0;int t;scanf("%d",&t);while(t--){map<int, int> vis;cnt=0;ans=0;scanf("%d",&n);/// memset(vis,0,sizeof(vis));while(!qq.empty()){qq.pop();}for(int i=1;i<=n;i++){scanf("%d",&x);qq.push(x);///假设该点是买了的。if(qq.top()<x){ans+=x-qq.top();cnt++;if(vis[qq.top()]>0)///之前那个位置已经卖过了{vis[qq.top()]--;cnt--;}vis[x]++;///标志一下该点已经卖了。qq.pop();///该点已经买了刚刚被卖了。qq.push(x);///在这个点卖,但也有可能在后边的点里面买。}}printf("%lld %lld\n",ans,cnt*2);}return 0;
}

Buy and Resell(贪心好题!)相关推荐

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

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

  2. Buy and Resell

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

  3. HDU6348 Buy and Resell

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

  4. 算法_贪心 刷题总结

    目录 贪心真的太玄学了 1.入门级 2.区间覆盖升级版(多重区间覆盖) 3.CF1066B Heaters 4.拿东西(贪心+博弈) P1209 [USACO1.3]修理牛棚 Barn Repair( ...

  5. leetcode贪心算法题集锦(持续更新中)

    leetcode贪心算法题集锦 leetcode贪心算法题集锦(持续更新中).python 和C++编写. 文章目录 leetcode贪心算法题集锦 一.贪心算法 1.盛最多水的容器 2.买股票的最佳 ...

  6. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 1 /* 2 题意:n个头,m个士兵,问能否砍掉n个头 3 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 4 */ 5 #include <cstdio> 6 #i ...

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

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

  8. 2018 拼多多校招贪心算法题

    题目描述 六一儿童节,老师带了很多好吃的巧克力到幼儿园.每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i] (即w[j]>=h[i]),他才会上去表演节目.老师的目 ...

  9. 工作安排(反悔贪心板子题)

    工作安排 - 题目 - Daimayuan Online Judge 分析: 贪心,将set当链表用 从后往前考虑(这样先入队的就一直能到最后),碰到更小的时刻,就将之前入队的 能出的全出 #incl ...

最新文章

  1. Zend Studio 10代码格式化设置
  2. Authentication method 'caching_sha2_password' not supported by any of the available plugins.
  3. python读取文件名-python读取文件名并改名字的实例
  4. jvm性能调优实战 - 47超大数据量处理系统是如何OOM的
  5. 推荐Web前端初学者应该知道的书籍和网站
  6. 【转】图的点连通度边连通度总结
  7. 笔记本超频会烧吗_如何判断电脑是否支持DIY 超频?这篇文章告诉你
  8. Java写一个简单的静态文件的HTTP服务器(基于Socket)
  9. SCHAR_MIN常数,C ++中的示例
  10. 啊哈算法系列(C语言、python、Java )
  11. 一汽大众汽车宣布召回19.1万辆国产奥迪A6L
  12. 全中!七大初学者易踩的坑!
  13. 基于DOS的ipc$最详攻略。
  14. Selenium(二)——webdriver 开始
  15. 捷联惯导数值更新算法-姿态更新+速度更新+位置更新
  16. Oracle表空间增加方法
  17. 前端项目开发中碰到的坑、移动端兼容性问题
  18. 同步FIFO和异步FIFO
  19. 2019年云化国际发展趋势_2019年3种令人惊讶的云趋势-您首先在这里听到了
  20. 3.3 CPU共享功能

热门文章

  1. (PADA)Partial Adversarial Domain Adaptation笔记
  2. 计算机组装与维护试题汇总2013,匡子平2013年上期85《计算机组装与维护》期末试题及答案...
  3. 【C语言基础练习】百钱买百鸡问题。母鸡3元钱一只,小鸡1元钱三只,问100元钱要刚好买100只鸡,编程实现母鸡和小鸡各多少只?
  4. 猫抓 浏览器插件安装教程,适用Chrome浏览器和Edge浏览器
  5. win10 SystemParametersInfo 设置屏保 不好使_[教程]win10 ,ubuntu双系统安装避坑指南
  6. 利用网络劫持解决微信远程域名真机调试Api问题
  7. Djano3.0使用-CBV使用实例
  8. python实例100例百度文库-18个Python爬虫实战案例(已开源)
  9. Android系统完整的启动流程
  10. uniapp拍照上传功能