Advertisement
markim00

Untitled

Aug 5th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. void solve()
  8. {
  9.     ll n;
  10.     cin >> n;
  11.     vector<ll> arr(n);
  12.     for (auto &e : arr)
  13.     {
  14.         cin >> e;
  15.     }
  16.  
  17.     ll ans = 0;
  18.  
  19.     priority_queue<ll> oddHeap;
  20.     priority_queue<ll, vector<ll>, greater<ll>> evenHeap;
  21.  
  22.     for (auto e : arr)
  23.     {
  24.         if (e & 1)
  25.         {
  26.             oddHeap.push(e);
  27.         }
  28.         else
  29.         {
  30.             evenHeap.push(e);
  31.         }
  32.     }
  33.  
  34.     while (!oddHeap.empty() && !evenHeap.empty())
  35.     {
  36.         ll odd = oddHeap.top();
  37.         oddHeap.pop();
  38.         ll even = evenHeap.top();
  39.         evenHeap.pop();
  40.  
  41.         if (odd < even)
  42.         {
  43.             oddHeap.push(odd + even);
  44.             evenHeap.push(even);
  45.         }
  46.         else
  47.         {
  48.             oddHeap.push(odd);
  49.             oddHeap.push(odd + even);
  50.         }
  51.  
  52.         ++ans;
  53.     }
  54.  
  55.     cout << ans << '\n';
  56. }
  57.  
  58. int main()
  59. {
  60.     ll t;
  61.     cin >> t;
  62.     for (int it = 1; it <= t; it++)
  63.     {
  64.         solve();
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement