二分答案——坑有好几处 P2985 [USACO10FEB]吃巧克力Chocolate Eating

#include
#include
#include
#include
using namespace std;
long long n,m,h[1000002],d[50002], l,r,mid;
long long now, t;

void read(long long &x)
{
	x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9') ch =getchar();
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	} 
}

bool check(long long x)
{
	now = 0, t = 0;
	for (long long i=1; i<=m; i++){
		now /= 2;
		while (now < x && t+1 <= n) {
		    t++;
			now += h[t];
			d[t] = i;	
		}
		if (now < x) return false;
	}
	for (long long i=t+1; i<=n; i++) d[i] = m;    // 所有的都要吃且保证字典序最小
	return true;       
}

int main()
{
	read(n); read(m);
	for (long long i=1; i<=n; i++) read(h[i]);
	for (long long i=1; i<=n; i++) r += h[i];
	while (l < r){
		mid = (l + r + 1) >> 1;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	cout<

你可能感兴趣的:(二分答案——坑有好几处 P2985 [USACO10FEB]吃巧克力Chocolate Eating)