• HDU-6438-Buy and Resell


    思路参考自https://www.cnblogs.com/zbh2047/p/9736378.html

    • 贪心
      Accepted 6438 234MS 2300K 1054 B G++
      #include "bits/stdc++.h"
      using namespace std;
      typedef long long LL;
      const int MAXN = 1e5 + 5;
      struct Node {
          int v;
          // op表示在此城市的操作,true为卖出,false为不操作,因为无需把买入的城市加入优先队列所以这里不考虑买入。 
          bool op;
          friend bool operator < (Node n, Node m) {
              // 先按城市物价排序,在价格相同的情况下为保证购买次数最少,优先选择op为true的城市。 
              if (n.v != m.v) {
                  return n.v > m.v;
              }
              return !n.op;
          }
      };
      priority_queue<Node> qu;
      int main() {
          int t, k, cnt;
          Node n, m;
          LL res;
          scanf("%d", &t);
          while (t--) {
              res = cnt = 0;
              scanf("%d", &k);
              for (int i = 0; i < k; i++) {
                  scanf("%d", &n.v);
                  n.op = false;
                  if (!qu.empty()) {
                      Node m = qu.top();
                      if (m.v < n.v) {
                          n.op = true;
                          qu.pop();
                          if (m.op) {
                              m.op = false;
                              qu.push(m);
                              res += n.v - m.v;
                          } else {
                              res += n.v - m.v;
                              cnt += 2;
                          }
                      }
                  }
                  qu.push(n);
              }
              printf("%lld %d
      ", res, cnt);
              while (!qu.empty()) {
                  qu.pop();
              }
          }
          return 0;
      }
  • 相关阅读:
    wpf 计算公式
    c#读取文件
    wpf布局控件总结
    一个接口多次使用的值得引起思考的小片段
    wpf之渐变色LinearGradientBrush
    路由
    HtmlHelper2
    HtmlHelper1
    ssh连接Ubuntu之access denied
    百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/Angel-Demon/p/10419383.html
Copyright © 2020-2023  润新知