3 solutions

  • 10
    @ 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;
    }
    
    • 0
      @ 2026-6-30 20:06:36
      #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;
      }
      
      • -3
        @ 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
        97
        Accepted
        35
        Uploaded By