zoukankan      html  css  js  c++  java
  • HDU6438 Buy and Resell 2018CCPC网络赛 -低买高卖-贪心经典题

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

    Catalog

    Problem:Portal传送门

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


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

    Solution:

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

    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
    ",ans,tot);
      }
      return 0;
    }
    

    Problem Description:

    这里写图片描述


    #pragma GCC optimize(3,"Ofast","inline")
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    bool Finish_read;
    template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
    template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
    template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('
    ');}
    template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
    #define clr(a, b) memset((a), (b), sizeof((a)))
    #define rep(i,s,t) for(register int i=s;i<t;++i)
    #define per(i,s,t) for(register int i=s;i>=t;--i)
    #define iis std::ios::sync_with_stdio(false);cin.tie(0)
    #define my_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
    // mt19937 rng(time(NULL));
    // mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
    // mt19937_64 generator(std::clock());
    // shuffle(arr, arr + n, generator);
    void debug_out() { cout << '
    '; }
    template <typename T, typename... R>
    void debug_out(const T &f, const R &... r) {
        cout << f << " ";
        debug_out(r...);
    }
    #ifndef ONLINE_JUDGE
    #define debug(...) cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__);
    #else
    #define debug(...) ;
    #endif
    /*================Header Template==============*/
    constexpr int maxn = 4e5 + 5;
    constexpr int maxe = 2e6 + 5;
    int n, m;
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("D:in.in", "r", stdin);
        freopen("D:out.out", "w", stdout);
    #endif
        int tim;
        read(tim);
        while(tim --) {
            read(n);
            int cnt = 0;
            LL ans = 0;
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q1;
            for(int i = 1, x; i <= n; ++i) {
                read(x);
                if(!Q1.empty() && x > Q1.top().first) {
                    ans += x - Q1.top().first;
                    if(Q1.top().second == 1) cnt += 2;
                    Q1.pop();
                    Q1.push(make_pair(x, 0));
                }
                Q1.push(make_pair(x, 1));
            }
            printf("%lld %d
    ", ans, cnt);
        }
        return 0;
    }
    
  • 相关阅读:
    c++ 存储连续性,作用域和链接性注意点
    函数模板的知识点总结
    c++ 左值引用的注意点
    VS2015如何在同一个解决方案下建立多个项目及多个项目之间的引用
    编译opencv4.1.0+tesseract5.0 的Realease x64版本遇见的问题解决
    逻辑化简-卡诺图
    从Word Embedding到Bert模型—自然语言处理中的预训练技术发展史 (转载)
    matlab绘图
    多个EXCEL文件合并成一个
    数学建模及机器学习算法(一):聚类-kmeans(Python及MATLAB实现,包括k值选取与聚类效果评估)
  • 原文地址:https://www.cnblogs.com/Cwolf9/p/9535794.html
Copyright © 2011-2022 走看看