[Template] [P1182] half the number of columns segments II-- answer

The meaning of problems: given a number, m is divided into segments, each segment so that the maximum and minimum.

 

  Consider two points and the minimum segment size, the answer is clearly monotone. Accumulating the number of column elements can be determined whether the sum of the current group size within each of the check. Since the sequence elements are non-negative integers, the prefix and the value of the array satisfy the non-strictly monotonic increasing, and then the prefix can be re-set to a half of the accumulated force optimization process.

  Complexity I do not know how to optimize later analysis, anyway, it ran much faster

Code:

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #define maxn 100010   
  4. using namespace std;  
  5. int a[maxn], n, m;  
  6. long long s[maxn];  
  7. bool div(int sum) {  
  8.     int cnt = 0;  
  9.     for (int i = 1; i <= n; ) {  
  10.         if (a[i] > sum) return false;  
  11.         int l = i, r = n;  
  12.         while (l < r) {  
  13.             int mid = (l + r + 1) >> 1;  
  14.             s[mid] - s[i - 1] <= sum ? l = mid : r = mid - 1;  
  15.         }  
  16.         ++cnt, i = l + 1;  
  17.     }  
  18.     return cnt <= m;  
  19. }  
  20. int main () {  
  21. //  freopen("testdata-10.in", "r", stdin);  
  22.     ios::sync_with_stdio(0);  
  23.     cin >> n >> m;  
  24.     you l = 0, r = 0;  
  25.     for (int i = 1; i <= n; ++i)  
  26.         cin >> a[i], s[i] = a[i] + s[i - 1], l = max(l, a[i]);  
  27.     r = s[n];  
  28.     while (l < r) {  
  29.         long long mid = (l + r) >> 1;  
  30.         if (div(mid))  
  31.             r = mid;  
  32.         else l = mid + 1;  
  33.     }  
  34.     cout << l;  
  35.     return 0;  
  36. }  

 

Guess you like

Origin www.cnblogs.com/TY02/p/11366562.html