fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll solve(ll a, ll b, ll c, ll d){
  6. ll B = a + b, G = c + d;
  7. ll R = min(B, G), p = (B & 1);
  8.  
  9. auto clamp = [&](ll z){ return min(R, max(0LL, z)); };
  10.  
  11. auto add = [&](ll z, vector<ll>& v){
  12. if (0 <= z && z <= R && (z & 1) == p) v.push_back(z);
  13. };
  14.  
  15. auto addBothParity = [&](ll v, vector<ll>& s){
  16. ll z = clamp(v);
  17. ll z1 = z - ((z & 1) != p);
  18. ll z2 = z + ((z & 1) != p);
  19. add(z1, s);
  20. add(z2, s);
  21. };
  22.  
  23. vector<ll> cand;
  24. addBothParity(0, cand);
  25. addBothParity(R, cand);
  26. addBothParity(b, cand);
  27. addBothParity(d, cand);
  28. addBothParity(B - a, cand);
  29. addBothParity(G - c, cand);
  30.  
  31. sort(cand.begin(), cand.end());
  32. cand.erase(unique(cand.begin(), cand.end()), cand.end());
  33.  
  34. auto score = [&](ll z){
  35. return min(B - z, a) + min(G - z, c) + min(z, b) + min(z, d);
  36. };
  37.  
  38. ll ans = 0;
  39. for (ll z : cand) ans = max(ans, score(z));
  40. return ans;
  41. }
  42.  
  43. int main(){
  44. ios::sync_with_stdio(false);
  45. cin.tie(nullptr);
  46.  
  47. if (FILE* f = fopen("pair.inp", "r")) {
  48. freopen("pair.inp", "r", stdin);
  49. freopen("pair.out", "w", stdout);
  50. fclose(f);
  51. }
  52.  
  53. int t;
  54. if(!(cin >> t)) return 0;
  55. for (int tc = 1; tc <= t; ++tc){
  56. ll a,b,c,d;
  57. cin >> a >> b >> c >> d;
  58. cout << solve(a,b,c,d);
  59. if (tc < t) cout << ' ';
  60. }
  61. cout << '\n';
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0.01s 5316KB
stdin
2
1 1 1 1
2 1 2 1
stdout
2 6