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
题意是有n个城市,给出一个商品在这n个城市的价格,商人从第一个城市到第n个城市,在每个城市可以选择买入、卖出或者不操作,可以携带多个商品,问最大收益和最小操作次数

先考虑贪心进行选择,到达一个城市之后,如果之前有城市的价格比他低我就在前面的城市买入在这里卖出
但这样显然是有问题的,比如第一组样例1 ,这样操作的话答案是 2-=,但实际应该是 10-+-(或者10-+-)
我们考虑用优先队列来维护出现过的价格,当我们在某个城市选择以a价格买入b价格卖出时,我们向队列里加入两个b,并且把b-a先加进答案中
那么如果在之后的操作中,我们想以a买入以c卖出,我们的实际操作是买入队列中的b以c卖出,这时我们的收益是c-b+b-a=c-a,而且还有一个b在队列中 至于操作次数,如果是买入加入队列两次的那个物品操作次数不变,否则加二
#include <bits/stdc++.h>
#define ll long long
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
map<int,int>mp;
int T,n,x;
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
ll ans=;
int cnt=;
mp.clear();
for (int i=;i<=n;i++)
{
scanf("%d",&x);
if (!q.empty() && x>q.top())
{
ans+=x-q.top();
if (mp[q.top()]) mp[q.top()]--;
else cnt+=;
q.pop();
q.push(x);
mp[x]++;
}
q.push(x);
}
printf("%lld %d\n",ans,cnt);
while (!q.empty()) q.pop();
}
return ;
}

CCPC2018-A-Buy and Resell的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

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

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

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. 2018 CCPC 网络赛 Buy and Resell

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

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

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

  7. hdu6438 Buy and Resell

    多少年不写题了... (我把每一天看作是一个商品,第i天是第i个商品) 一开始看了半天看出来一个性质:买的所有商品中最贵的不会比卖的所有商品中最便宜的贵,然后似乎没有什么用处.... 所以最后还是看题 ...

  8. HDU6438:Buy and Resell(贪心+数据结构)

    题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少 分析:对每一个元素产生的贡献可以先计算 ...

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

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

  10. 【hdu 6438】Buy and Resell

    [链接] 我是链接,点我呀:) [题意] 有一个物品的价格在1..n这些位置各不相同. 你初始有无限的钱. 问你从1走到n. 你每次可以选择买入一个或者卖出一个该种物品(或啥都不做) 问你最后的最大利 ...

随机推荐

  1. vue+vue-video-player实现弹窗播放视频

    将视频播放器标签放在对话框标签中,实现弹窗 template 中 <el-dialog :visible.sync="dialogVisible" width='680px' ...

  2. A8

    组员:陈锦谋 今日内容: PS学习.抠图.图标像素调整 明日计划: 继续小组内安排的任务 困难: 无

  3. 测试bug

    模板在运行时出现了以下 1 个错误:---------------------------Controller.tt(-1,-1) : error : 获取 AppDomain 以便从主机运行转换时出 ...

  4. 第三章 广义线性模型(GLM)

    广义线性模型 前面我们举了回归和分类得到例子.在回归的例子中,$y \mid x;\theta \sim  N(u,\sigma ^{2})$,在分类例子中,$y\mid x;\theta \sim ...

  5. size和STL中的size_type

    为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型:size_type是STL类中定义的类型属 ...

  6. WebService(二)

    使用eclipse开发webservice的服务器端以及客户端的简单实例 1.服务端 在eclipse中像建立一个web项目一样,new->Dynamic Web Project A.建一个需要 ...

  7. mybatis_mysql

    SELECT round(avg(c.AVG_DELAY_TIME)) as AVG_DELAY FROM `result_road_saturation_day` a LEFT JOIN info_ ...

  8. 修改Oracle redo.log文件的大小

    1.查看当前日志组成员: SQL> select member from v$logfile; MEMBER ------------------------------------------ ...

  9. javascript之彻底理解prototype

    prototype很简单, 就是提供一种引用的机制. let BaseObject = { toString(){ return this.str; }, }; let Object1 = {str: ...

  10. javascript之彻底理解this

    彻底理解this,需要彻底理解函数 函数是复杂类型,存储在堆中.  函数是独立的, 对象中的方法只是对象中有个函数的引用 函数被调用时,调用者会像被调用者提供个上下文环境, 这个环境就是this 构造 ...