1622C - Set or Decrease - CodeForces Solution


binary search brute force greedy sortings *1600

Please click on ads to support us..

Python Code:

from math import ceil


def main():
    t = int(input())
    for _ in range(t):
        n, k = map(int, input().split())

        array  = sorted(list(map(int, input().split())))
        current_sum = sum(array)

        if current_sum <= k:
            print(0)
            continue
        elif n == 1:
            print(array[0]-k)
            continue

        result = current_sum - k
        new_result = 0
        for i in range(n-1, 0, -1):
            new_result = n-i

            if current_sum - array[i] + array[0] > k:
                new_result += ceil((current_sum-array[i]+array[0]-k)/(n-i+1))

            result = new_result if new_result < result else result
            current_sum -= array[i]-array[0]
        print(result)


main()

C++ Code:

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define ff              first
#define ss              second
#define ll             long long int
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int>>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
//mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i,a,b)          for(ll i = a; i < b; ++i)
// #define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
ll lstbt(ll val) { ll msk = val & (val - 1); return log2(val ^ msk);}
void c_p_c()
{
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
#endif
}
ll countDigit(ll n) {
  return floor(log10(n) + 1);
}
ll power(ll base, ll n) {
	ll ans = 1;
	while (n != 0) {
		if (n % 2) {
			ans = (ans * base);
			n = n - 1;
		}
		else {
			base = (base * base);
			n = n / 2;

		}

	}
	return ans;
}
bool comp(pair<pair<ll,ll>,ll>p1,pair<pair<ll,ll>,ll>p2)
{
	if(p1.first.first==p2.first.first){
		return p1.first.second>p2.first.second;
	}
	else{
		return p1.first.first<p2.first.first;
	}
}
ll findsum(ll n){
	ll s=0;
	while(n>0){
		s+=n%10;
		n/=10;
	}
	return s;
}
	vector<ll>primes;

ll func(ll n ){
    ll cnt = 0 ;
    
    while(n % 2 == 0){
        n = n/2 ;
        primes.push_back(2);
        cnt++;
    }
    
    for(int i=3 ; i<= sqrt(n) ; ++i){
        
        while(n % i == 0){
            n = n/i ;
            primes.push_back(i);
            cnt++;
        }
        
    }
    
    if(n > 1) {
        primes.push_back(n);
        cnt++;
    }
    
    return cnt ;
}
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	c_p_c();
	w(t){
		ll n,k;
		cin>>n>>k;
		ll arr[n+1];
		rep(i,0,n){
			cin>>arr[i];
		}
		sort(arr,arr+n);
		ll sum=0;
		ll pre[n+1]={0};
		for(ll i=n-1;i>=0;i--){
			sum+=arr[i];
			if(i!=n-1){
				pre[i]=pre[i+1]+arr[i];
			}
			else{
				pre[i]=arr[i];
			}
		}
		ll cnt=0;
		ll dif=sum-k;
		if(n==1){
			if(dif<=0){
				cout<<"0"<<endl;
			}else{
				cout<<dif<<endl;
			}
		}
		else if(dif<=0){
			cout<<"0"<<endl;
		}
		else{
			ll mini=dif;
			// rep(i,0,n){
			// 	cout<<arr[i]<<" ";
			// }
			// cout<<endl;
	for(int i=n-1;i>0;i--)
    {
        ll temp = n-i;
        // cout<<"temp "<<temp<<endl;
        // cout<<"dif "<<dif<<endl;
        // cout<<"pre[i] "<<pre[i]<<endl;
        // cout<<"upper "<<(dif - pre[i] + arr[0]*(temp))<<endl;
        ll temp2 = ceil((dif - pre[i] + arr[0]*(temp))*1.00/(temp+1));
        // cout<<"temp2 "<<temp2<<endl;
        if(temp2>0)
        mini = min(temp+temp2,mini);
        else
        mini = min(temp,mini);
    }
    cout<<mini<<endl;
		}


	}


}


Comments

Submit
0 Comments
More Questions

221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers