hdu6438(贪心+思维)

Buy and Resell

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

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 ,表示 n 天。给出 n 个数,a[i] 表示第 i 天,物品的价格是多少。每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少钱,最少操作次数是多少?

题解:依次遍历每一天,比如当前是第 i 天,用堆维护前 i - 1 天的最小值,如果最小值比 a[i] 大,直接将 a[i] 加入堆里就可以了,如果日后遇到比 a[i] 大的再更新答案。如果最小值比 a[i] 小,那么就将其卖出,更新答案: 赚的钱 ans += a[i] - min, 操作次数 cnt += 2,但是这样并不能保证答案一定最优的,因为后面可能遇到收购价格更高的那一天,采取的方法是将 min 替换成 a[i],并记录 a[i] 是已经交换过的数,如果日后遇到比 a[i] 大的数 a[j],即遇到更优答案了,就再卖一次 a[i],更新答案,但是注意此时钱更新是没有问题的,但是操作次数就不能累加了,这样相当于min买,a[j]卖。替换后,堆还要再加一次 a[i],因为最优答案里第 i 天也可能是买物品的。相当于pop一个min,push两个a[i]。

#include<bits/stdc++.h>
using namespace std;
 
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=1e5+5;
int a[maxn];
priority_queue<int,vector<int>,greater<int> > q;
map<int,int> mp;
int main()
{
	int T;scanf("%d",&T);
	while(T--)
	{
		while(!q.empty())q.pop();mp.clear();
		int n;scanf("%d",&n);
		for(int i=0; i<n; i++)
		{
			scanf("%d",&a[i]);
		}
		ll ans=0;int cnt=0;
		q.push(a[0]);
		for(int i=1; i<n; i++)
		{
			int x=q.top();
			if(x<a[i])
			{
				ans+=a[i]-x;cnt+=2;
				q.pop();
				
				if(mp[x])
				{
					mp[x]--;cnt-=2;
				}
				q.push(a[i]);
				mp[a[i]]++;
			}
			q.push(a[i]);
		}
		printf("%lld %d\n",ans,cnt);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值