HDU6438 Buy and Resell 2018CCPC网络赛 -低买高卖-贪心经典题

版权声明:转载请说明,欢迎交流! https://blog.csdn.net/qq_39599067/article/details/82056382

(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:Portal传送门

 原题目描述在最下面。
 出过很多次:51nodNOIP提高组贪心专题A,牛客寒假多校(不记得那场了),CodeforcesContest865D。
 低买高卖,每次只能买入或卖出一件商品,你买得起所有商品,问你最多盈利多少?

 ps:FZU2281 Trades是可以买入卖出许多件物品.

Solution:

 对于当前价格B而言,只要前面有比这个价格低的价格A,那么当前情况A买入B卖出一定盈利
 但是A买入B卖出不一定是最优解,所以为了有后悔药吃,就再push两个B进入优先队列,一个表示卖出,一个表示买入。
 每天都卖出,每次累加差值就可以了。累加很多个差分值肯定会得到最优解的。
 因为A买入B卖出B买入C卖出 和 A买入C卖出 效果一样

 活捉一个分治+dp的大佬:here

AC_Code:

#include<bits/stdc++.h>
#define mme(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;

int main() {
  int tim;
  int n;
  scanf("%d", &tim);
  while(tim--){
  scanf("%d",&n);
    priority_queue<pair<int,int>>q;
    LL ans = 0;
    int tot = 0;
    for(int i = 0, x; i < n; ++i){
      scanf("%d",&x);
      q.push(make_pair(-x,1));//买入
      q.push(make_pair(-x,2));//卖出
      LL tmp=x+q.top().first;
      if(q.top().second == 1)tot+=2;//对于实际交易,买入一次必对应一次卖出
      ans += tmp;
      q.pop(); 
    }
    printf("%lld %d\n",ans,tot);
  }
  return 0;
}


Problem Description:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_39599067/article/details/82056382
今日推荐