4 条题解

  • 1
    @ 2024-4-10 11:04:26
    #include<bits/stdc++.h>
    using namespace std;
    using ll=long long;
    int n;
    const int N=1e6+5;
    ll m,a[N];
    
    bool check(ll mid){
        ll sum=0;
        for(int i=1;i<=n;i++){
            if(a[i]-mid<=0){//如果该树高<=砍树高度,直接跳过
                continue;
            }
            sum+=(a[i]-mid);
        }
        if(sum>=m){//当sum>=m说明砍树的高度要<=该mid
            return true;
        }
        return false;
    }
    
    int main(){
        cin>>n>>m;
        ll maxx=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            maxx=max(maxx,a[i]);
        }
        sort(a+1,a+1+n);//先排序
        ll l=1,r=maxx,ans=0;
        while (r - l > 1) // 当右边界与左边界相差大于1时
    	{
    		int mid = (l + r) >> 1; // 取中间位置
    		if (check(mid)){ // 如果满足条件
                ans=mid;//最后得到的mid就是答案
    			l = mid; // 更新左边界为mid
            }
    		else{
    			r = mid; // 否则更新右边界为mid
    	    }
        }
        cout<<ans<<endl;
        return 0;
    }
    
    • 0
      @ 2024-5-25 12:19:20
      def get_wood_amount(trees, cut_height):
          # 计算砍下的木材总长度
          wood = sum(tree - cut_height for tree in trees if tree > cut_height)
          return wood
      
      def find_max_height(trees, m):
          low, high = 0, max(trees)
          result = 0
      
          while low <= high:
              mid = (low + high) // 2
              wood = get_wood_amount(trees, mid)
      
              if wood >= m:
                  result = mid
                  low = mid + 1
              else:
                  high = mid - 1
      
          return result
      
      # 读取输入
      n, m = map(int, input().split())
      trees = list(map(int, input().split()))
      
      # 计算结果
      max_cut_height = find_max_height(trees, m)
      
      # 输出结果
      print(max_cut_height)
      
      
      • 0
        @ 2024-4-10 0:30:08

        普普通通二分题 我叫你开longlong你二龙嘛

        #define _CRT_SECURE_NO_WARNINGS 1
        /**
         * @brief 打开宏开关
         * */
        #define _USE_MATH_DEFINES
        #include <cmath>
        #include <bits/stdc++.h>
        using namespace std;
        #define ll long long
        
        const int N = 1e6 + 5;
        ll n, m;
        ll a[N];
        bool bisearch(ll h)
        {
        	ll sum = 0;
        	for (int i = 1; i <= n; i++)
        	{
        		if (a[i] > h) sum += a[i] - h;
        	}
        	return sum >= m;
        }
        int main()
        {
        	cin >> n >> m;
        	for (int i = 1; i <= n; i++)
        		cin >> a[i];
        	ll l = 1, r = 1e9;
        	while (l + 1 < r)
        	{
        		ll m = l + r >> 1;
        		if (bisearch(m)) l = m;
        		else r = m;
        	}
        	if (bisearch(l)) cout << l;
        	else cout << r;
        	return 0;
        }
        
        • 0
          @ 2024-4-9 13:42:37

          二分答案即可

          #include <bits/stdc++.h>
          using namespace std;
          const int N = 1000010;
          int a[N];
          int n,m;
          int mx = -1;
          bool check(int x){
              long long sum = 0;
              for (int i = 1; i <= n; ++i) {
                  if (a[i] > x) {
                      sum += (a[i] - x);
                  }else{
                      continue;
                  }
              }
              if (sum < m) return true;
              return false;
          }
          int main(){
              scanf("%d %d",&n,&m);
              for (int i = 1; i <= n; ++i) {
                  scanf("%d",&a[i]);
                  mx = max(mx,a[i]);
              }
              int l = 1,r = mx;
              while (l + 1 < r) {
                  int mid = (l + r)/2;
                  if (check(mid)) {
                      r = mid;
                  }else {
                      l = mid;
                  }
              }
              printf("%d\n",l);
              return 0;
          }
          
          • 1

          信息

          ID
          89
          时间
          1000ms
          内存
          256MiB
          难度
          7
          标签
          递交数
          935
          已通过
          208
          上传者