231C - To Add or Not to Add - CodeForces Solution


binary search sortings two pointers *1600

Please click on ads to support us..

Python Code:



from cmath import inf


n,k = list(map(int, input().split(' '))) 
nums = list(map(int, input().split(' ')))


nums.sort(reverse=True)
first = nums[0]
        
li = []
count = 0
i = 0
j = 0
ans_val = []

while j < len(nums):

    diff = nums[i] - nums[j]

    if diff <= k:
        k-=diff
        j+=1

    elif diff > k:
        
        count = j-i
        val = nums[i]
        i+=1
        k += ((j-i)*(nums[i-1] - nums[i]))
        if len(li)==0:
            li.append(count)
            ans_val.append(val)
        else:
            if count>=li[-1]:
                li.append(count) 
                ans_val.append(val)

count = j-i
val = nums[i]
i+=1
if len(li)==0:
    li.append(count)
    ans_val.append(val)
else:
    if count>=li[-1]:
        li.append(count) 
        ans_val.append(val)

if li==[]:
    li.append(j-i)
    ans_val.append(nums[i])
elif (j-i)>li[-1]:
    li.append(j-i)
    ans_val.append(nums[i])

print(li[-1], ans_val[-1]) 

C++ Code:

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

bool good (vector<ll> &sums, ll num, ll k, ll l, ll r) {
    return k >= num - (sums[r] - sums[l]); 
}

ll solve (vector<ll> &sums, ll num, ll k, ll j) {
    ll l = -1, r = j;

    while (l + 1 < r) {
        ll m = (l + r) / 2; 
        ll len = j - m + 1;
        if (good(sums, num * (len - 1), k, j - len, j - 1)) r = m;
        else l = m;  
    }

    return j - r + 1;
}

int main () {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    ll n, k; cin >> n >> k;

    vector<ll> nums(n + 1, -1e12), sums(n + 1);

    for (int i = 1; i <= n; i++) {
        cin >> nums[i];
        
    }

    sort(nums.begin(), nums.end());

    for (int i = 1; i <= n; i++) {
        sums[i] = nums[i] + sums[i - 1];
    }


    ll cnt = 0, num = 0;

    for (ll i = 1; i <= n; i++) {
        ll now = solve(sums, nums[i], k, i);
        if (now > cnt) {
            cnt = now, num = nums[i];
        }
    }

    cout << cnt << " " << num;

    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST