HDU 6438 Buy and Resell 【贪心+思维+优先队列】

本文探讨了一个涉及多个城市的买卖策略问题,目标是通过最优的购买和销售决策,实现最大化的利润和最小化的交易次数。问题源自HDU6438竞赛题目,通过分析价格波动和交易时机,提出了一种使用优先队列的小根堆算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


传送门:HDU 6438


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

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个城市,在第i个城市中,你有三种策略,你可以用ai元购买一个物品,或者用ai元卖掉一个物品,或者不买也不卖,你可以同时保存多个物品。最开始你身上没有物品,但是又无限的金钱,现在要求你从城市1走到城市n,问你最大的收益是多少,同时交易次数是多少。


题解:

很容易想到的是,我们应该花较少的钱买入,然后卖出更多的钱。如果我们在第i天买入,第j天卖出那么必须满足 a [ j ] > a [ i ] a[j]>a[i] a[j]>a[i],收入为 a [ j ] − a [ i ] a[j]-a[i] a[j]a[i],如果后面存在第k天,使得 a [ k ] > a [ j ] a[k]>a[j] a[k]>a[j],那么我们就应该在第k天卖出物品,因为收益更大,为 a [ k ] − a [ i ] a[k]-a[i] a[k]a[i]
我们发现有恒等式: a [ k ] − a [ i ] = a [ k ] − a [ j ] + a [ j ] − a [ i ] a[k]-a[i]=a[k]-a[j]+a[j]-a[i] a[k]a[i]=a[k]a[j]+a[j]a[i]; 这说明第j天有没有卖出并不会影响最终结果(相当于中间商,且无差价)
但是我们又考虑到,第j天虽然有没有卖出不影响结果,但是有没有买入是影响结果的,所有我们还要考虑买入的情况。

优先队列小根堆)来实现上述过程,如果 j 的中间商,那么我们就往队列里存两次,一次是中间商(卖出),一次是买入。当当前的a大于队首元素时,我们就进行交易,同时如果队首元素是中间商的话交易次数是不改变的。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int t,n;
ll cnt,ans;
priority_queue<ll,vector<ll>,greater<ll> >q;
map<ll,int> mp;
int main()
{
  ll a;
  scanf("%d",&t);
  while(t--)
  {
    while(!q.empty()) q.pop();
    mp.clear();
    cnt=0,ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
      scanf("%I64d",&a);
      if(!q.empty()&&q.top()<a)
      {
        ll tmp=q.top();
        q.pop();
        ans+=a-tmp;
        q.push(a);//两次存入队列
        cnt++;
        mp[a]++;
        if(mp[tmp]!=0)//中间商
        {
          cnt--;//交易次数不改变
          mp[tmp]--;
        }
      }
      q.push(a);
    }
    printf("%I64d %I64d\n",ans,cnt*2);
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值