1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<bits/stdc++.h> #include <ostream>
using namespace std; typedef long long ll; #define endl '\n' typedef pair<int, int> PII; #define debug() cout.flush() #define for0(i, a) for (int i = 0; i < a; ++i) #define REP(i, a, b) for (int i = a; i < b; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define REPC(i, a, b, c) for (ll i = a; i < b && i < c; ++i) #define RREP(i, a, b) for (int i = a; i >= b; --i) const ll MOD = 1e9 + 7; const ll mod = 998244353; const int INF = 0x3f3f3f3f; const int MAXN = 3e5 + 5e2;
inline void init() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
int n; ll h[MAXN];
inline ll check(ll maxn) { ll res = 0; ll odd = 0, even = 0; for0 (i, n) { if (maxn - h[i] & 1) { odd++; } even += (maxn - h[i]) / 2; } if (even > odd) { ll sub = (even - odd + 1) / 3; odd += sub * 2; even -= sub; } ll sub = min(even, odd); res = sub * 2; odd -= sub; even -= sub; if (odd) res += odd * 2 - 1; else if (even) res += even * 2; return res; }
inline void solve() { int maxn = -1; for0 (i, n) { cin >> h[i]; maxn = maxn > h[i] ? maxn : h[i]; } cout << min(check(maxn), check(maxn + 1)) << endl; }
signed main() { init(); int _; cin >> _; while (_--) { cin >> n; solve(); } return 0; }
|