2 条题解

  • 0
    @ 2026-7-20 1:42:40

    P2440 木材加工——题解

    解题思路

    二分木段长度 xx。一根长度 LiL_i 的原木可贡献 Li/xfloor\lfloor L_i/x floor 段,总段数至少为 kk 时说明 xx 可行,且所有更小长度也可行;否则需减小。记录最大的可行值。

    复杂度分析

    每次检查 O(n)O(n),二分次数 O(logmaxLi)O(\log\max L_i),总时间 O(nlogmaxLi)O(n\log\max L_i),空间 O(n)O(n)

    C++17 参考代码

    #include <bits/stdc++.h>
    using namespace std;
    int main(){
        ios::sync_with_stdio(false);cin.tie(nullptr);
        int n;long long k;cin>>n>>k;vector<int>a(n);int hi=0;for(int&i:a){cin>>i;hi=max(hi,i);}int lo=1,ans=0;
        while(lo<=hi){int mid=lo+(hi-lo)/2;long long cnt=0;for(int x:a){cnt+=x/mid;if(cnt>=k)break;}if(cnt>=k)ans=mid,lo=mid+1;else hi=mid-1;}
        cout<<ans<<'\n';return 0;
    }
    
    • 0
      @ 2026-7-20 1:42:40

      #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false);cin.tie(nullptr); int n;long long k;cin>>n>>k;vectora(n);int hi=0;for(int&i:a){cin>>i;hi=max(hi,i);}int lo=1,ans=0; while(lo<=hi){int mid=lo+(hi-lo)/2;long long cnt=0;for(int x:a){cnt+=x/mid;if(cnt>=k)break;}if(cnt>=k)ans=mid,lo=mid+1;else hi=mid-1;} cout<<ans<<'\n';return 0; }

      • 1

      信息

      ID
      4935
      时间
      2000ms
      内存
      256MiB
      难度
      10
      标签
      递交数
      1
      已通过
      1
      上传者