loading...

Daily Coding Challenge #31

wingkwong profile image wkw ・2 min read

About

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.


/*
Educational Codeforces Round 85 (Rated for Div. 2) - A. Level Statistics
https://codeforces.com/contest/1353/problem/A
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int t,n,x,y;
    cin >> t;
    while(t--){
        cin >> n;
        int ans=1,px=0,py=0;
        for(int i=0;i<n;i++){
            cin >> x >> y;
            if(
                x<px || // the current number of plays cannot be smaller than the previous number of plays
                y<py || // the current number of clears cannot be smaller than the previous number of clears 
                y-py > x-px  // number of clears cannot be smaller than the number of plays for one jump
            ) {
                ans=0;
            }

            px=x; py=y;
        }
        if(ans) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
} 

Enter fullscreen mode Exit fullscreen mode

/*
Educational Codeforces Round 85 (Rated for Div. 2) - B. Middle Class
https://codeforces.com/contest/1353/problem/B
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int n,x,t;
    cin >> t;
    while(t--){
        cin >> n >> x;
        vector<int> a(n);
        for(int i=0;i<n;i++) cin >>a[i];
        // sort by descending order
        sort(a.rbegin(),a.rend());
        long long sum=0;
        int idx=0;
        for(int i=0;i<n;i++){
            // if a[i] can be deducted, deduct it
            if(a[i]>=x){
                sum+=a[i]-x;
            }else{
                // if the largest number is smaller than x, set idx=-1
                // if not, idx is the last idx of the number that can be deducted
                idx=(i==0?-1:i);
                break;
            }
        }

        // the largest number is smaller than x
        if(idx==-1){
            cout << 0 << "\n";
            continue;
        }
        // all numbers are greater than / equal to x
        if(idx==0) {
            cout << n << "\n";
            continue;
        }
        int idx2=0;
        // start from idx
        for(int i=idx;i<n;i++){
            // if the number can be added, add it up to x
            if(sum>=x-a[i]) {
                idx2++;
                sum-=x-a[i];
            }else{
                break;
            }
        }
        cout << idx+idx2 << "\n";
    }
    return 0;
} 

Enter fullscreen mode Exit fullscreen mode

The source code is available in corresponding repo below. Star and watch for timely updates!

GitHub logo wingkwong / leetcode

Migrated to https://github.com/wingkwong/competitive-programming

GitHub logo wingkwong / hackerrank

Migrated to https://github.com/wingkwong/competitive-programming

GitHub logo wingkwong / codeforces

Migrated to https://github.com/wingkwong/competitive-programming

GitHub logo wingkwong / atcoder

Migrated to https://github.com/wingkwong/competitive-programming

Discussion

pic
Editor guide