#include<bits/stdc++.h> #define N 50008 usingnamespacestd;
int a[N]; int m, n, l;
inlineboolvalid(int x){ int nowl = 0, cnt = 0; for(int i = 1; i <= n; ++i){ if(a[i] - nowl < x) ++cnt; else nowl = a[i]; } return cnt <= m; }
intmain(){ cin >> l >> n >> m; for(int i = 1; i <= n; ++i){ cin >> a[i]; } int le = 0, re = l; while(le < re){ int mid = (le + re + 1) / 2; if(valid(mid)) le = mid; else re = mid - 1; } cout << le << endl; return0; }
#include<bits/stdc++.h> #define N 100005 usingnamespacestd;
int a[N], n, m;
inlineboolwork(int x){ //每段的长度都小于等于x int cnt = 0, sum = 0; for(int i = 1; i <= n; ++i){ sum = a[i]; while(i < n && sum + a[i + 1] <= x) { i++; sum += a[i]; } cnt++; } return cnt <= m; }
intmain(){ cin >> n >> m; int i = 0, j = 0; for(int k = 1; k <= n; ++k){ cin >> a[k]; if(a[k] > i) i = a[k]; j += a[k]; } while(i < j){ int mid = (i + j)/2; if(work(mid)) j = mid; else i = mid + 1;; } cout << j; return0; }
Post title:二分
Post author:Eva.Q
Create time:2021-08-18 09:27:41
Post link:https://qyy/2021/08/18/Algorithm/ColinClass2/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.