1760F - Quests - CodeForces Solution


binary search greedy sortings *1500

Please click on ads to support us..

Python Code:

T = int(input())

for hasbulla in range(T):
	n,c,d = [int(x) for x in map(int, input().split())]
	t = list(map(int, input().split()))
	t.sort(reverse = True)
	if(t[0]*d < c):
		print("Impossible")
		continue
	l = 0
	r = d+9
	ans = 0
	while l != r:
		mid = (l + r) // 2
		essa = 0
		for i in range(d):
			if i % mid < n:
				essa += t[i % mid]
		if essa >= c:
			l = mid+1
			ans = mid
		else:
			r = mid
	if(ans == d+8):
		print("Infinity")
		continue
	print(ans-1)

'''

6
2 5 4
1 2
2 20 10
100 10
3 100 3
7 2 6
4 20 3
4 5 6 7
4 100000000000 2022
8217734 927368 26389746 627896974
2 20 4
5 1

'''

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define vpl vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define fr(n) for(ll i=0;i<(n);i++)
#define frr(k, n) for(ll j=(k); j<(n); j++)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define lz(x) 31-__builtin_clz(x)
#define endl "\n"
#define ff first
#define ss second
using namespace std;
mt19937 rng((ll)std::chrono::steady_clock::now().time_since_epoch().count());
const ll N=2e5+5, mod=1e9+7;

void error(){
    ll n, c, d; cin>>n>>c>>d;
    vll a(n); fr(n){cin>>a[i];}

    sort(all(a), greater<ll>());

    ll r=0;
    for(ll i=0; i<n && i<d; i++){
        r+=a[i];
    }
    if(a[0]*d < c){
        cout<<"Impossible"<<endl;
        return;
    }
    if(r>=c){
        cout<<"Infinity"<<endl;
        return;
    }
    ll l=0, h=d;
    while(l<=h){
        ll mid=(l+h)/2;
        ll rem=0;
        for(ll i=0; i<n&&i<=mid; i++){
            rem+=a[i];
        }
        ll r_rem=(d/(mid+1)), g=(d%(mid+1));
        rem*=r_rem;
        for(ll i=0; i<n&&i<g; i++){
            rem+=a[i];
        }
        if(rem<c){
            h=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    cout<<l-1<<endl;
}

int main(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
while(t--){error();}
return 0;
}


Comments

Submit
0 Comments
More Questions

450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers