Guest User

Untitled

a guest
Dec 18th, 2021
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. #define ll long long int
  7. #define INF (((ll) 1)<<((ll) 60 ))
  8.  
  9. ll vals[1005];
  10. ll num_chips;
  11.  
  12. ll count_min_three(ll num_one, ll num_two, ll target) {
  13.     // Try to make largest among one and two that's target mod 3
  14.     ll largest_same = -1;
  15.     for(int i = 0; i <= num_one; i++) {
  16.         for(int j = 0; j <= num_two; j++) {
  17.             if(((i+2*j) % 3) == (target%3)) {
  18.                 if(i+2*j > largest_same) {
  19.                     largest_same = i+2*j;
  20.                 }
  21.             }
  22.         }
  23.  
  24.     }
  25.  
  26.     ll ans;
  27.  
  28.     if(largest_same == -1) {
  29.         ans = 1000000001;
  30.     } else {
  31.         if (target < largest_same) {
  32.             ans= 0;
  33.         } else {
  34.             ans= (target - largest_same) / 3;
  35.         }
  36.     }
  37.     return ans;
  38. }
  39.  
  40. ll solve_case(ll num_one, ll num_two) {
  41.     ll cur_max_three = 0;
  42.     for(int i = 0; i < num_chips; i++) {
  43.         ll cur_three = count_min_three(num_one, num_two, vals[i]);
  44.         if(cur_three > cur_max_three) cur_max_three = cur_three;
  45.     }
  46.     return cur_max_three+num_one+num_two;
  47. }
  48.  
  49. void solve() {
  50.     ll cur_min = INF;
  51.     for(int i = 0; i < 3; i++) {
  52.         for(int j = 0; j < 3; j++) {
  53.             ll cur_ans = solve_case(i, j);
  54.             if(cur_ans < cur_min) {
  55.                 cur_min = cur_ans;
  56.             }
  57.         }
  58.     }
  59.     cout << cur_min << endl;
  60. }
  61.  
  62. int main() {
  63.     int Z; cin >> Z;
  64.     while(Z--) {
  65.         cin >> num_chips;
  66.         for(int i = 0; i < num_chips; i++) {
  67.             cin >> vals[i];
  68.         }
  69.         solve();
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment