题目传送门

题目描述:

有n座城市,每座城市都可以对一个物品进行一次的买进或者卖出,可以同时拥有多个物品,计算利润最大值,并且交易次数要最少。(买入卖出算两次操作)

思路:

建立两个小根堆 优先队列,q1放可以买的物品,q2放可以卖的物品。

如果两个队列都是空的,则把这个物品放入q1.

如果q1是有的,而q2是空的,则把a[i]和q1的顶比一下,如果比他大,则q1 pop一次,把a[i]塞入q2,并且把差值累计到ans上。

如果q1无,q2有,则拿a[i]和q2顶比一下,如果比它大,则把q2顶元素放入q1,a[i]放入q2,差值累积到ans上。

如果两个都有,如果a[i]和两个队列顶部元素差值相同,则优先替换q2的,计算差值,a[i]放入q2,原来的q2顶放入q1.这样可以保证交易天数最小。  如果不相等,则替换放入大的那个,元素也要放到应该放的队列里,如果不能替换,就放入第一个。

这样做是因为我们只关心一个物品卖出后的利润,所以买入的价格不重要,重要的是卖出后的价格,因为这个是可以被替换的,而相同情况下,要减少天数,所以先替换已经卖出的东西。

感谢薛佬教我!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int rd(void) {int x=0;int f=1;char s=getchar();while(s<'0'||s>'9') {if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}x*=f;return x;
}
priority_queue<ll,vector<ll> ,greater<ll> >q1;
priority_queue<ll,vector<ll> ,greater<ll> >q2;
int n;
const int maxn=100010;
ll a[maxn];
int main() {int T;cin>>T;while(T--) {while(!q1.empty())q1.pop();while(!q2.empty())q2.pop();scanf("%d",&n);ll sum=0;for(int i=1; i<=n; i++) {scanf("%lld",&a[i]);if(q2.empty()) {if(q1.empty()) {q1.push(a[i]);} else {ll temp=q1.top();if(a[i]>temp) {sum+=a[i]-temp;q1.pop();q2.push(a[i]);} else {q1.push(a[i]);}}} else {if(!q1.empty()) {ll temp1=q1.top();ll temp2=q2.top();if(a[i]>temp2&&temp1>=temp2) {q1.push(temp2);q2.pop();q2.push(a[i]);sum+=a[i]-temp2;} else if(a[i]>temp1) {q1.pop();sum+=a[i]-temp1;q2.push(a[i]);} else {q1.push(a[i]);}} else {ll temp=q2.top();if(a[i]>temp) {sum+=a[i]-temp;q2.pop();q1.push(temp);q2.push(a[i]);} elseq1.push(a[i]);}}}printf("%lld %d\n",sum,q2.size()*2);}
}

Buy and Resell

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

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

转载于:https://www.cnblogs.com/mountaink/p/9536699.html

hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心相关推荐

  1. Jumping Monkey(CCPC网络赛重赛)

    Jumping Monkey(CCPC网络赛重赛) 题意: n个点的树,每个点有一个不同的值aia_iai​.现在一个猴子在树上,这个猴子从点u跳到点v当且仅当ava_vav​是u到v最短路径上的最大 ...

  2. (四面体)CCPC网络赛 HDU5839 Special Tetrahedron

    1 CCPC网络赛 HDU5839 Special Tetrahedron 2 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 3 思路:枚举四 ...

  3. 2020 CCPC网络赛 赛后感

    第一次参加,做过去年19年网络赛的题,去年是四个水题稳做出,然后还有两个线段树和树状数组的题(好像是),所以本来对这次网络赛挺有信心的(去年好像四个题就能进,当然要手速快). 这次网络赛还是有四个水题 ...

  4. CCPC 网络赛总结

    下午这场比赛一开始就没有给自己太大的压力,本着把水题全部 AC 的意愿 大学第一次网络赛就这样结束了,一开始跟着榜做,签到题很快就解决了,不过这次的比赛真的是刷新了我对题意的认识,各种数学公式交叉在一 ...

  5. HDU6438 Buy and Resell

    题目链接:https://cn.vjudge.net/problem/HDU-6438 知识点: 贪心 题目大意: 本题讨论的是同一种物品的买卖.有 n 天,每一天这种物品都有一个价格.每天可以选择购 ...

  6. HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)

    写在前面 此题是一个很容易想到的贪心题目,但是正确性的证明是非常复杂的.然而,目前网上所有题解并未给出本题贪心算法的任何正确性证明,全部仅停留在描述出一个贪心算法.本着对算法与计算机科学的热爱(逃), ...

  7. HDU 6889 Graph Theory Class(CCPC网络赛)

    hdu 6889 传说中的人均min25 题意: n个点的完全图,边权为lcm(i+1,j+1),求mst(最小生成树) 题解: 我一开始以为是推公式,毕竟数据范围这么大,但是自己画图来看看mst的情 ...

  8. HDU 5842—— Lweb and String CCPC 网络赛 1011

    题意: 按字母出现的顺序编号,问最长上升子序列. 思路: 最长为26,出现一个新的字母就加上,答案为字母的种数,无聊的题目. code: #include <cstdio> #includ ...

  9. 2017 ccpc网络赛 1001 Vertex Cover(二分图 构造)HDU6150

    题意:"最小点覆盖集"是个NP完全问题 有一个近似算法是说-每次选取度数最大的点(如果有多个这样的点,则选择最后一个) 让你构造一个图,使得其近似算法求出来点数是你给定的覆盖点数的 ...

最新文章

  1. 算法----- 下一个更大元素 I
  2. 使用js冒泡实现点击空白处关闭弹窗
  3. 23种设计模式之外观模式(Facade)
  4. C语言标准库函数qsort排序的介绍与使用
  5. ArduinoIDE安装与配置与第一个程序的烧录和运行——人人都能玩硬件
  6. JZOJ 4238. 【五校联考5day2】纪念碑
  7. Windows XP时代终结:假设你还在用它怎么办
  8. go语言和python结合_Go+Python双语言混合开发-第1章 【阶段一:Go语言基础】
  9. js 难点之call,apply实现
  10. 《从强盗资本家到慈善家》--转载自思维的乐园
  11. oracle常用系统变量,Oracle环境变量
  12. linux HA工作模型详解
  13. 漫谈WebQQ 协议
  14. 智慧农业IOT-onenet平台简单介绍
  15. GitLab迁移升级大作战
  16. kux播放器android,kux格式转换工具
  17. Kubernetes 在知乎上的应用
  18. 网络显示dns服务器错误,电脑出现网络dns异常是怎么回事
  19. 腾讯CDC标叔的十年交互设计感悟-20141117早读课
  20. 学计算机颈椎,电脑颈

热门文章

  1. iosttableViewCell右侧的箭头,圆形等
  2. 如何优雅的研究 RGSS3 (七) 加入LOGO屏幕
  3. MVC4做网站后台:栏目管理1、添加栏目
  4. java实现选择排序
  5. 读史以明志,把握好自己的明天
  6. cvpr2018论文阅读
  7. error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or
  8. MapReduce基础开发之九JDBC连接Hive
  9. 泛架构之于外包IT工程
  10. 汉诺塔的改编题(用栈求解,分别递归和非递归)