pb_jiang

CF2166E WA

Nov 17th, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. // Problem: E. Binary Wine
  2. // Contest: Codeforces - Codeforces Round 1064 (Div. 2)
  3. // URL: https://codeforces.com/contest/2166/problem/E
  4. // Memory Limit: 512 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using vl = vector<ll>;
  21.  
  22. void solve()
  23. {
  24.     ll n, q;
  25.     cin >> n >> q;
  26.     vl a(n);
  27.     for (auto &x : a)
  28.         cin >> x;
  29.     priority_queue<a2l> pq;
  30.     for (auto x : a)
  31.         pq.push({x, 1});
  32.     // priority_queue<ll> pq(a.begin(), a.end());
  33.     multiset<ll> add;
  34.     multiset<a2l> del;
  35.     for (ll i = 0, c; i < q; ++i) {
  36.         cin >> c;
  37.         for (auto x : add)
  38.             pq.push({x, 1});
  39.         add.clear();
  40.         dbg(c);
  41.         bool done = false;
  42.         ll cost = 0;
  43.         for (ll j = 30; j >= 0 && !done; --j) {
  44.             auto [val, ori] = pq.top();
  45.  
  46.             while (del.contains({val, ori})) {
  47.                 pq.pop();
  48.                 del.extract({val, ori});
  49.                 std::tie(val, ori) = pq.top();
  50.                 // val = pq.top()[0], ori = pq.top()[1];
  51.             }
  52.  
  53.             if (c <= val) {
  54.                 done = true;
  55.                 cout << cost << '\n';
  56.                 break;
  57.             }
  58.             ll b = (c >> j) % 2;
  59.             if (b) {
  60.                 ll tg = 1ll << j;
  61.                 dbg(cost, tg, val);
  62.                 ll dc = 0;
  63.                 del.insert({val, ori});
  64.  
  65.                 if (tg >= val) {
  66.                     dc = tg - val;
  67.                     pq.push({0, 0});
  68.                     if (ori)
  69.                         add.insert(val);
  70.                 } else {
  71.                     dc = 0;
  72.                     pq.push({min(tg - 1, val - tg), 0});
  73.                     if (ori)
  74.                         add.insert(val);
  75.                 }
  76.                 dbg(tg, val, dc);
  77.                 cost += dc;
  78.                 c -= tg;
  79.             }
  80.         }
  81.         if (!done)
  82.             cout << cost << '\n';
  83.     }
  84. }
  85.  
  86. int main(int argc, char **argv)
  87. {
  88.     std::ios::sync_with_stdio(false);
  89.     std::cin.tie(nullptr);
  90.  
  91.     ll t = 1;
  92.     cin >> t;
  93.     while (t--)
  94.         solve();
  95.  
  96.     return 0;
  97. };
  98.  
Advertisement
Add Comment
Please, Sign In to add comment