|洛谷|二分|P1182 数列分段Section II

http://www.luogu.org/problem/show?pid=1182

用二分答案,check即可

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int n,m;
int a[100005];
bool check(int x)//大于m return 1;
{
	int ans = 1;
	int tot = 0;
	for (int i=1;i<=n;i++)
	{
		if (tot+a[i]<=x) tot+=a[i];
		else {ans++; tot = a[i];}
	}
	if (ans>m) return 1;else return 0;
}
int main()
{
	scanf("%d%d", &n ,&m);
	int l = 0,r = 0;
	for (int i=1;i<=n;i++) {scanf("%d", &a[i]); r+=a[i]; l = max(l, a[i]);}
	while (l


你可能感兴趣的:(二分/分治,洛谷)