Dmaxiya

[蓝桥杯 2026 省 A] 切割木材 参考代码

Apr 11th, 2026
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 1000000 + 100;
  8. int n, k;
  9. int a[maxn];
  10.  
  11. bool judge(int L) {
  12.     LL cnt = 0;
  13.     for (int i = 1; i <= n; ++i) {
  14.         cnt += (a[i] + L - 1) / L - 1;
  15.     }
  16.     return cnt <= k;
  17. }
  18.  
  19. int main() {
  20.     ios::sync_with_stdio(false);
  21.  
  22.     cin >> n >> k;
  23.     for (int i = 1; i <= n; ++i) {
  24.         cin >> a[i];
  25.     }
  26.     int low = 0;
  27.     int high = 1000000000;
  28.     int mid;
  29.     while (high - low > 1) {
  30.         mid = (low + high) >> 1;
  31.         if (judge(mid)) {
  32.             high = mid;
  33.         } else {
  34.             low = mid;
  35.         }
  36.     }
  37.     cout << high << endl;
  38.  
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment