2 条题解

  • 4
    @ 2022-12-25 18:54:46
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int N=1000005;
    ll n,m,a[N];
    ll maxn=-1;
    ll check(ll x)
    {
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]>x)
            ans+=(a[i]-x);
        }
        return ans;
    }
    int main()
    {
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            maxn=max(maxn,a[i]);
        }
        ll l=1,r=maxn;
        for(int i=0;i<100;i++)
        {
            ll mid=(l+r)>>1;
            if(check(mid)<m) r=mid-1;
            else l=mid+1;            
        }
        printf("%lld\n",r);
        return 0;
    }
    
    • 0
      @ 2023-7-26 10:29:48
      #include <iostream>
      using namespace std;
      long long a[1000005], N, M, l, r, mid, ans;
      bool check(long long x)
      {
          long long m = 0;
          for (long long i = 1; i <= N; i++)
          {
              if (a[i] > x)
              {
                  m += a[i] - x;
              }
          }
          return m >= M;
      }
      int main()
      {
          cin >> N >> M;
          for (long long i = 1; i <= N; i++)
          {
              cin >> a[i];
              r = max(r, a[i]);
          }
          l = 0;
          while (l <= r)
          {
              mid = (l + r) / 2;
              if (check(mid))
              {
                  l = mid + 1;
                  ans = mid;
              }
              else
              {
                  r = mid - 1;
              }
          }
          cout << ans;
          return 0;
      }
      
      • 1

      信息

      ID
      904
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      200
      已通过
      63
      上传者