1710A - Color the Picture - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

def canColor(n, m, colors):
    colors.sort(reverse=True)
    n, m = _n, _m = sorted([n, m])
    cando = 0
    mfirst = colors[0]//n
    nfirst = colors[0]//m
    for c in colors:
        if c//n>1:
            if _m==1:
                if mfirst>2:
                    _m = 0 
            else:
                _m -= c//n 
        if c//m>1:
            if _n==1:
                if nfirst>2:
                    _n = 0
            else:
                _n -= c//m 
    if _m<=0 or _n<=0: cando = 1
    return "Yes" if cando else "No"
    

def main():
    t = int(input())
    for _ in range(t):
        n, m, k = map(int, input().split())
        colors = list(map(int, input().split()))
        print(canColor(n, m, colors))

main()

C++ Code:

#include<bits/stdc++.h>

using namespace std;

//Ordered set
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;

#define rep(i, x, n) for(ll i = (x); i < (n); i++)
#define ll long long
#define endl "\n"
#define boost ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define vll vector<ll>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define mp make_pair
#define ckmin(a,b) a = min(a,b)
#define ckmax(a,b) a = max(a,b)
#define inf ( (1e18) + 10)
#define mod 1000000007
#define endll cout<<endl
#define minPQ priority_queue <ll, vector<ll>, greater<ll>>
//getline(cin, s) to take whole input line with spaces
ll power(ll x, ll y)//power using mod
{
    x %= mod, y %= mod - 1;
    ll res = 1;
    while (y)
    {
        if (y & 1LL)
            res = (res * x) % mod;
        y >>= 1;
        x = (x * x) % mod;
    }
    return res % mod;
}

//Sieve of erothrosiene - to find Primes and smallest prime factor(SPF)

// ll range =1e5+7;

// vll primes(range, 1);

// for(int i = 0; i < range; i++)primes[i] = i;

// for(int i = 2; i*i < range; i++){
//     if(primes[i] == i){
//         for(int j = i*i; j < range; j+=i){
//             if(primes[j] == j)
//             primes[j] = i;
//         }
//     }
// }

/* ------------------------------------------------------------------------------------------------*/

void solve(){
    ll n, m, k;
    cin>>n>>m>>k;
    vll a(k);
    rep(i, 0, k)cin>>a[i];

    ll cnt = 0, od = 0, ev = 0;
    for(int i = 0; i < k; i++){
        ll o = a[i]/n;

        if(o != 1){
            cnt += o;
        }

        if(o > 2)od = 1;
    }

    if(!(m&1)){
        if(cnt >= m){
            cout<<"YES"<<endl;
            return;
        }
    }else{
        if(cnt >= m && od){
            cout<<"YES"<<endl;
            return;
        }
    }

    
    cnt = 0; od = 0;
    for(int i = 0; i < k; i++){
        ll o = a[i]/m;

        if(o!=1){
            cnt += o;
        }

        if(o > 2)od = 1;
    }

    if(!(n&1)){
        if(cnt >= n){
            cout<<"YES"<<endl;
            return;
        }
    }else{
        if(cnt >= n && od){
            cout<<"YES"<<endl;
            return;
        }
    }

    cout<<"NO"<<endl;
    
}

int main (){
    boost
    //To take input from files
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout); 

    ll t;
    cin>>t;
    while(t--)
    solve();

}


Comments

Submit
0 Comments
More Questions

41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)