l为什么不能从0开始二分???
查看原帖
l为什么不能从0开始二分???
795766
Lemonwxy楼主2023/8/13 14:42

#include using namespace std;

const int maxn = 100005; int n, m, l, r; int a[maxn];

inline bool check(int x){ int tot = 0, cnt = 0; for(int i = 1; i <= n; ++i){ if(tot + a[i] <= x) tot += a[i]; else tot = a[i], ++cnt; } return cnt >= m; }

int main(){ cin >> n >> m; for(int i = 1; i <= n; ++i) cin >> a[i], l = max(l, a[i]), r += a[i]; //l为何不能从0开始二分??? while(l <= r){ int mid = l + r >> 1; if(check(mid)) l = mid + 1; else r = mid - 1; } cout << l; return 0; }

2023/8/13 14:42
加载中...