Thứ Hai, 9 tháng 5, 2022

CHATGO C++

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

long long m, maxx = -1, a[1000009],res=0;
int n;

void nhap()
{
    cin>>n>>m;
    for(int i=1 ;i<=n ;i++)
        cin>>a[i];
}

bool check(long long h)
{
    long long sum = 0;
    for(int i=1 ;i<=n ;i++)
    {
        if (a[i] > h)
            sum+=a[i] - h;
    }
    if (sum >= m) return true;
    else return false;
}

int tknp(int l,int r)
{
    int mid,h=0;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(check(mid)==true)
        {
            h=mid;
            l=mid+1;
        }
        else
            r=mid-1;
    }
    return h;
}

/*void find(int l, int r)
{
    if (l > r) return;
    ll mid = (l + r)/2;
    if (check(mid) == true)
    {
        res = max(res, mid);
        find(mid+1,r);
    }
    else
        find(l,mid-1);
}*/

void giai()
{
    int maxx=0;
    for(int i=1;i<=n;i++)
        if(a[i]>maxx) maxx=a[i];
    int h=tknp(0,maxx);
    cout<<h;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    freopen("chatgo.inp", "r", stdin);
    freopen("chatgo.out", "w", stdout);
    nhap();
    giai();

    //printf("%lld", res);
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.