hdu 6438 Buy and Resell(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6438

Buy and Resell
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3366 Accepted Submission(s): 1242

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中国大学生程序设计竞赛 - 网络选拔赛


题目大意:

有n个城市,在每个城市都有物品的价值a[i].当到第i 个城市时,你可以买下一个物品,也可以卖出一个物品,也可以不买不卖。最开始的时候,有无穷的钱,但没有物品,问经过n个城市后,最终赚的钱最多为多少,并是交易次数尽可能的小

分析:

要使盈利最大,那么买的次数=卖的次数,交易次数就是其两倍
并且要使在比较底的价格时 买入,在比较高的价格时卖出,从而赚取差价(说了等于白说
如何想的?
就是怎么将买入与卖出之间的那些数,通过一定的转化,将其过渡掉(每个城市的物品,要么在买卖之中,要么不在其中(也就是后面讲的将其作为中间商处理))
讲讲代码是怎么实现的吧
要用个优先队列que,当遍历到i时,队列中存的就是前面的城市中可以选择的物品价值
当 遍 历 到 i 时 , 若 a [ i ] &lt; q u e . t o p ( ) , 计 算 盈 利 a n s + = a [ i ] − t o p , 弹 出 t o p , 然 后 往 队 列 中 p u s h 两 次 a [ i ] ( 后 面 有 解 释 ) ; 否 则 往 队 列 中 p u s h 一 次 a [ i ] 当遍历到i时,\\ 若a[i]&lt;que.top(),计算盈利 ans+= a[i]- top,弹出top,然后往队列中push两次a[i](后面有解释);\\ 否则往队列中push一次a[i] ia[i]<que.top(),ans+=a[i]top,top,pusha[i](;pusha[i]
先了解一下这个情况:第i个城市入货a[i]价值的物品,在第i+1个城市,有a[i+1]>a[i],出货,盈利a[i+1]-a[i];若在第i+2个城市,有a[i+2]>a[i+1],出货(假设经过第i+1个城市时入货了),盈利a[i+2]-a[i+1],总盈利: a[i+1]-a[i]+a[i+2]-a[i+1]=a[i+2]-a[i],等价于在第i个城市买,第i+2个城市卖,第i+1个城市不做处理,这个a[i+1]就作为中间商作用,也就是代码的中一个往队列中push(a[i+1])的作用,另一个push(a[i+1])是因为,a[i+1]有可能作为入货的物品。因此要push两次a[i+1]
对于样例:1 2 10 9

que中12,22,10,10
a[pos]2109
处理a[i]>1,ans+=2-1,push两次2,弹出1a[i]>2,ans+=10-2,ans此时为9,等价于买1 卖10,2作为的中间商,同样的操作a[i]>2,ans+=9-2 ,此时就是买2 卖9,结束
代码:
#include<iostream>
#include<cmath>
#include <cstring>
#include<cstdio>
#include<algorithm>
#include <vector>
#include <map>
#include <queue>

using namespace std;
#define ll long long 
const int N=1e3+10;


priority_queue<int,vector<int>, greater<int> > que;
map<int, int > mp;
int main()
{
	
	int t;
	int n;
	int a;
	ll ans=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		ans = 0;
		while(!que.empty())
			que.pop();
		mp.clear();
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a);
			if(i==0)
				que.push(a);
			else
			{
				if(a>que.top())
				{
					ans+=a-que.top();
					mp[a]++;//记录a的卖出次数
					cnt++;
					if(mp[que.top()])//说明是中间商,就不用计算cnt,因此cnt--
					{
						cnt--;
						mp[que.top()]--;
					}
					que.pop();
					que.push(a);
					que.push(a);
				}
				else
				{
					que.push(a);
				}
			}
		}
		cout<<ans<<" "<<cnt*2<<endl;
	}
	
	return 0;
}
参考资料:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值