1749C - Number Game - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

from collections import defaultdict
first = -1
def possible(arr, n, k):
    d = defaultdict(int)
    for i in arr:
        d[i] += 1
    ans = 0
    if d[1] == 0:
        return 0
    st = 0
    K = k
    while True:
        k = K - st 
        while k > 0 and d[k] == 0:
            k -= 1
        
        if k == 0:
            return 0
        if k==1 and d[k] > 0 and st == K-1:
            return 1
        d[k] -= 1
        
        d[1] -= 1
        st += 1
        if st == K:
            return 1
        if d[1] <= 0:
            return 0
        k -= 1
        
        

for t in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    
    if t == 0 and first == -1:
        first = n
    ans = 0
    for i in range(n):
        if possible(arr, n, i+1):
            ans = i+1
    
    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ctz __builtin_ctz
#define ctzll __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define setbits __builtin_popcount
#define setbitsll __builtin_popcountll
#define endl "\n"
#define input(v)                        \
    for (auto i = 0; i < v.size(); i++) \
    {                                   \
        cin >> v[i];                    \
    }
#define output(v)         \
    for (auto k : v)      \
    {                     \
        cout << k << " "; \
    }                     \
    cout << "\n";
typedef long long ll;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> pi;
typedef vector<pair<ll, ll>> vp;

void solve();
ll max(ll a, ll b)
{
    if (a >= b)
        return a;
    else
        return b;
}
ll min(ll a, ll b)
{
    if (a >= b)
        return b;
    else
        return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("error.txt", "w", stderr);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }

    cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
    return 0;
}
void solve()
{
    ll n;
    cin >> n;
    vector<ll> arr(n);
    map<ll, ll> mp;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        mp[arr[i]]++;
    }
    sort(arr.begin(), arr.end());
    ll ans = 0;
    for (ll i = n; i >= 0; i--)
    {
        map<ll, ll> m = mp;
        ll flag = 0;
        for (ll j = 1; j <= i; j++)
        {
            ll val = i - j + 1;
            auto it = m.upper_bound(val);
            if (it == m.begin())
            {
                flag = 1;
                break;
            }
            it--;
            it->second--;
            if (it->second == 0)
            {
                m.erase(it->first);
            }
            ll x = m.begin()->first;
            m.begin()->second--;
            if (m.begin()->second == 0)
            {
                m.erase(m.begin()->first);
            }
            m[x + val]++;
        }
        if (!flag)
        {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
}
//  ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤


Comments

Submit
0 Comments
More Questions

489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years