1221A - 2048 Game - CodeForces Solution


brute force greedy math *1000

Please click on ads to support us..

Python Code:



turn = int(input())
count = 0
def game(x):
    x.sort()
    while len(x)>1 and 2048 not in x:
        if x.count(min(x)) <2:
            del x[0]
        else:
            for i in range(x.count(min(x))//2):
                x.append(2*min(x))
            x.sort()
            del x[0:x.count(min(x))//2*2]
    return x
while count<turn:
    count+=1
    n = int(input())
    test = [int(i) for i in input().split()]
    if 2048 in game(test):
        print('YES')
    else:
        print('NO')
          

C++ Code:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll mod = 1e9 + 7;

ll a[200200], b[200200];

void sol() {
    ll i, j = 0, k = 0, p = 1e9, q = 0, m = 0, r = 0, w = 0, e = 0, l, n, ans = 0;
    string s;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> q;
        if (q <= 2048)
            w += q;
    }
    if (w >= 2048)
        cout << "YES\n";
    else
        cout << "NO\n";
}

int main() {
    ios;
    //    freopen("input.txt", "rt", stdin);
    //    freopen("output.txt", "wt", stdout);
    ll n, m;
    cin >> n;
    for (int i = 0; i < n; i++)
        sol();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction