8.18 B组 T2 工资

题目大意:

有一个长度为n的数列,把他们分成m段使得每段的和的最大值最小

解题思路:

二分答案

A c c e p t e d   c o d e : Accepted\ code: Accepted code:

#include
#define int long long

using namespace std;

int n, m;
int v[100005];

int check(int x) {
     
	int f = 1, owo = 0;
	for (int i = 1; i <= n; ++i) {
     
		if (owo + v[i] > x) {
     
			owo = v[i];
			++f;
		} else owo += v[i];
		if (f > m || owo > x) return 0; 
	}
	return (f <= m);
}

signed main() {
     
	scanf("%d %d", &n, &m);
	int l = 0, r = 0;
	for (int i = 1; i <= n; ++i)
		scanf("%d", &v[i]), r += v[i];
	while (l < r) {
     
		int mid = l + r >> 1;
		if (check(mid)) r = mid;
		else l = mid + 1;
	}
	printf("%lld", l);
}

你可能感兴趣的:(二分)