Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <map>
- using namespace std;
- #define ll long long int
- #define INF (((ll) 1)<<((ll) 60 ))
- ll vals[1005];
- ll num_chips;
- ll count_min_three(ll num_one, ll num_two, ll target) {
- // Try to make largest among one and two that's target mod 3
- ll largest_same = -1;
- for(int i = 0; i <= num_one; i++) {
- for(int j = 0; j <= num_two; j++) {
- if(((i+2*j) % 3) == (target%3)) {
- if(i+2*j > largest_same) {
- largest_same = i+2*j;
- }
- }
- }
- }
- ll ans;
- if(largest_same == -1) {
- ans = 1000000001;
- } else {
- if (target < largest_same) {
- ans= 0;
- } else {
- ans= (target - largest_same) / 3;
- }
- }
- return ans;
- }
- ll solve_case(ll num_one, ll num_two) {
- ll cur_max_three = 0;
- for(int i = 0; i < num_chips; i++) {
- ll cur_three = count_min_three(num_one, num_two, vals[i]);
- if(cur_three > cur_max_three) cur_max_three = cur_three;
- }
- return cur_max_three+num_one+num_two;
- }
- void solve() {
- ll cur_min = INF;
- for(int i = 0; i < 3; i++) {
- for(int j = 0; j < 3; j++) {
- ll cur_ans = solve_case(i, j);
- if(cur_ans < cur_min) {
- cur_min = cur_ans;
- }
- }
- }
- cout << cur_min << endl;
- }
- int main() {
- int Z; cin >> Z;
- while(Z--) {
- cin >> num_chips;
- for(int i = 0; i < num_chips; i++) {
- cin >> vals[i];
- }
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment