2 solutions

  • 4
    @ 2024-3-31 14:51:18
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    int a[N];
    int n,m;
    bool check(int x)
    {
        long long res=0; //总合 
        for(int i=1;i<=n;i++)
        {
            res=res+max(0,a[i]-x);  
        }
        return res>=m; //当前长度是否可以符合要求 
    } 
    int main()
    {
        cin>>n>>m;
        int l=0,r=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            r=max(r,a[i]);
        }
        while(l<r)
        {
            int mid=l+r+1>>1;
            if(check(mid)) l=mid;
            else r=mid-1;
        }
        cout<<l;
        return 0;
    }
    
    • -2
      @ 2024-7-16 9:09:37
      • 无注释版本
      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e6+10;
      int a[N];
      int n,m;
      bool check(int x)
      {
          long long res=0;
          for(int i=1;i<=n;i++)
          {
              res=res+max(0,a[i]-x);  
          }
          return res>=m;
      } 
      int main() 
      {
          cin>>n>>m;
          int l=0,r=0;
          for(int i=1;i<=n;i++)
          {
              cin>>a[i];
              r=max(r,a[i]);
          }
          while(l<r)
          {
              int mid=l+r+1>>1;
              if(check(mid)) l=mid;
              else r=mid-1;
          }
          cout<<l;
          return 0;
      }
      
      • 1

      Information

      ID
      952
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      3
      Tags
      (None)
      # Submissions
      70
      Accepted
      26
      Uploaded By