2 条题解

  • 4
    @ 2021-9-20 19:00:34

    另一种使用二分的方法:

    // Written By Rosmarinus
    
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #define int long long
    using namespace std;
    
    const int inf = 0x7f7f7f7f7f;
    
    struct Node
    {
        int v, p, rev, from;
    }A[1100000], B[1100000];
    int alen, blen;
    
    
    inline int read()
    {
       int s = 0, w = 1;
       char ch = getchar();
       while(ch < '0' || ch > '9') {if(ch == '-') w = -1; ch = getchar();}
       while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
       return s * w;
    }
    
    bool Sort(Node a, Node b)
    {
    	if(a.p != b.p) return a.p < b.p;
    	return a.v > b.v;
    }
    
    int Find_A(int v)
    {
        int l = 1, r = alen, ans = 0;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(A[mid].p <= v) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        return ans;
        
    }
    
    int Find_B(int v)
    {
        int l = 1, r = blen, ans = 0;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(B[mid].p <= v) ans = mid, l = mid + 1;
            else r = mid - 1;
        }
        return ans;
    }
    
    signed main()
    {
        int n, a, b, value, price, maxa = 0, maxb = 0, ans = 0;
        char s;
        n = read(), a = read(), b = read();
        for(int i = 1; i <= n; i ++)
        {
        	value = read(), price = read();
            scanf(" %c", &s);
            if(s == 'A')
            {
                A[++ alen].v = value, A[alen].p = price, A[alen].rev = value;
                if(price <= a) maxa = max(maxa, value);
            }
            else
            {
                B[++ blen].v = value, B[blen].p = price, B[blen].rev = value;
                if(price <= b) maxb = max(maxb, value);
            }
        }
    
        ans = maxa + maxb;
        sort(A + 1, A + 1 + alen, Sort);
        sort(B + 1, B + 1 + blen, Sort);
    
        A[0].v = B[0].v = -inf;
        for(int i = 1; i <= alen; i ++)
        {
            if(A[i - 1].v > A[i].v) A[i].v = A[i - 1].v, A[i].from = A[i - 1].from;
            else A[i].from = i;
        }
        for(int i = 1; i <= blen; i ++)
        {
            if(B[i - 1].v > B[i].v) B[i].v = B[i - 1].v, B[i].from = B[i - 1].from;
            else B[i].from = i;
        }
    
        for(int i = 1; i <= alen; i ++)
        {
            int t = Find_A(a - A[i].p);
            if(A[t].from != i) ans = max(ans, A[i].rev + A[t].v);
        }
        for(int i = 1; i <= blen; i ++)
        {
            int t = Find_B(b - B[i].p);
            if(B[t].from != i) ans = max(ans, B[i].rev + B[t].v);
        }
    
        cout << ans << endl;
    
        return 0;
    }
    
    • 2
      @ 2021-9-5 9:22:52

      题意

      nn 种商品,每个商品只有一件,每种商品有价格 pip_i 和价值 viv_i,其中每个商品指定只能用 A/B 货币中的一种购买。现在告诉你初始时你拥有的 A/B 货币数量,让你购买最多两个商品,问你能达到的最大价值和。

      2n106, p, v1092 \le n \le 10^6,~p,~|v|\le 10^9

      分析

      考虑选两个物品时的所有情况:

      1. 两个物品均只能用 A 货币购买。
      2. 两个物品均只能用 B 货币购买。
      3. 一个物品只能用 A 货币购买,另一个物品只能用 B 货币购买。

      对于第 3 种情况,可以发现两个货品的购买不会产生影响,只需要在只能用 A 货币购买的物品中找出能买的价值最高的物品,再找出只能用 B 货币购买的物品中能够买到的价值最高的物品,把他们的价值加起来即可。

      对于 1 情况和 2 情况,不难发现他们本质相同。以 1 情况举例,我们考虑将所有只能用 A 货币购买的物品按照价格排序,并对价值计算前缀最大值数组。然后我们维护一个双指针,第一个指针从低到高枚举每一个物品并将它钦定为我们购买的第一个物品,另一个指针实时维护当前状态能够购买到的价格最大的物品的位置。其中一个状态中的价值和即为第一个指针指向的物品的价值加上第二个指针指向的位置的前缀价值最大值。此处需注意特判防止同一个物品被选择两次。

      总时间复杂度 O(nlogn)O(n \log n),复杂度瓶颈在于 sort,如果换为 O(n)O(n) 的排序可以将总时间复杂度降为 O(n)O(n)

      代码

      /**
       * @author Macesuted (macesuted@outlook.com)
       * @copyright Copyright (c) 2021
       */
      
      #include <bits/stdc++.h>
      using namespace std;
      
      namespace io {
      #define SIZE (1 << 20)
      char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55];
      int f, qr;
      inline void flush(void) { return fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void(); }
      inline char getch(void) { return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); }
      inline void putch(char x) {
          *oS++ = x;
          if (oS == oT) flush();
          return;
      }
      string getstr(void) {
          queue<char> que;
          char c = getch();
          while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF) c = getch();
          while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF)) que.push(c), c = getch();
          string s;
          s.resize(que.size());
          for (register int i = 0; i < (int)s.size(); i++) s[i] = que.front(), que.pop();
          return s;
      }
      void putstr(string str, int begin = 0, int end = -1) {
          if (end == -1) end = str.size();
          for (register int i = begin; i < end; i++) putch(str[i]);
          return;
      }
      template <typename T>
      inline T read() {
          register T x = 0;
          for (f = 1, c = getch(); c < '0' || c > '9'; c = getch())
              if (c == '-') f = -1;
          for (x = 0; c <= '9' && c >= '0'; c = getch()) x = x * 10 + (c & 15);
          return x * f;
      }
      template <typename T>
      inline void write(const T& t) {
          register T x = t;
          if (!x) putch('0');
          if (x < 0) putch('-'), x = -x;
          while (x) qu[++qr] = x % 10 + '0', x /= 10;
          while (qr) putch(qu[qr--]);
          return;
      }
      struct Flusher_ {
          ~Flusher_() { flush(); }
      } io_flusher_;
      }  // namespace io
      using io::getch;
      using io::getstr;
      using io::putch;
      using io::putstr;
      using io::read;
      using io::write;
      
      #define maxn 1000005
      
      typedef pair<int, int> pii;
      
      pii pa[maxn], pb[maxn];
      int amax[maxn], bmax[maxn];
      int atail = 0, btail = 0;
      
      int main() {
          int n = read<int>(), a = read<int>(), b = read<int>();
          for (register int i = 1; i <= n; i++) {
              int v = read<int>(), p = read<int>();
              char c = getstr()[0];
              (c == 'A' ? pa[++atail] : pb[++btail]) = (pii){p, v};
          }
          sort(pa + 1, pa + atail + 1), sort(pb + 1, pb + btail + 1);
          for (register int i = 1; i <= atail; i++) amax[i] = max(amax[i - 1], pa[i].second);
          for (register int i = 1; i <= btail; i++) bmax[i] = max(bmax[i - 1], pb[i].second);
          int answer = amax[upper_bound(pa + 1, pa + atail + 1, (pii){a + 1, 0}) - pa - 1] +
                       bmax[upper_bound(pb + 1, pb + btail + 1, (pii){b + 1, 0}) - pb - 1];
          for (register int i = 1, j = atail; i <= atail; i++) {
              if (a - pa[i].first < 0) continue;
              while (j >= 1 && pa[j].first > a - pa[i].first) j--;
              answer = max(answer, pa[i].second + amax[min(i - 1, j)]);
          }
          for (register int i = 1, j = btail; i <= btail; i++) {
              if (b - pb[i].first < 0) continue;
              while (j >= 1 && pb[j].first > b - pb[i].first) j--;
              answer = max(answer, pb[i].second + bmax[min(i - 1, j)]);
          }
          write(answer), putch('\n');
          return 0;
      }
      
      • 1

      信息

      ID
      192
      时间
      1000ms
      内存
      512MiB
      难度
      3
      标签
      递交数
      193
      已通过
      31
      上传者