Advertisement
OIQ

Untitled

OIQ
Jan 24th, 2022
1,068
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. int countWires(const std::vector<int>& wires, int len) {
  6.     int count = 0;
  7.     for (int i = 0; i < wires.size(); ++i) {
  8.         count += wires[i] / len;
  9.     }
  10.  
  11.     return count;
  12. }
  13.  
  14. void search(const std::vector<int>& wires, int count) {
  15.     int left = 0;
  16.     int right = 1e7 + 1;
  17.  
  18.     int result = 0;
  19.  
  20.     while (left < right - 1) {
  21.         int mid = (left + right) / 2;
  22.         if (mid == 0) {
  23.             left = mid + 1;
  24.         }
  25.         if (countWires(wires, mid) < count) {
  26.             right = mid;
  27.         } else {
  28.             left = mid;
  29.             result = std::max(result, mid);
  30.         }
  31.     }
  32.     std::cout << result;
  33. }
  34.  
  35. int main() {
  36.     int size, count;
  37.     std::cin >> size >> count;
  38.  
  39.     std::vector<int> wires(size);
  40.  
  41.     for (int i = 0; i < size; ++i) {
  42.         std::cin >> wires[i];
  43.     }
  44.  
  45.     search(wires, count);
  46.  
  47.     return 0;
  48. }
  49.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement