【二分】工资

D e s c r i p t i o n Description Description

【二分】工资_第1张图片

I n p u t Input Input

O u t p u t Output Output

S a m p l e Sample Sample I n p u t Input Input
7 5
100
400
300
100
500
101
400
S a m p l e Sample Sample O u t p u t Output Output
500

H i n t Hint Hint


T r a i n Train Train o f of of T h o u g h t Thought Thought

水题吧
就二分答案嗯

#include
#include
#include
#include
#define ll long long
using namespace std;

int A[100005];
int n, m;
ll Sum;

bool Check(ll k)
{
	int l = 1, t = m;
	ll s = 0;
	while(l <= n && t > 0)
	{
		s = 0;
		while(s + (ll)A[l] <= k && l <= n)
			s += (ll)A[l++];
		t--;
	}
	if(l <= n)return 0;
	else return 1;
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i)
		scanf("%d", &A[i]), Sum += A[i];
	ll l = 1, r = Sum;
	while(l < r)
	{
		ll mid = (l + r) / 2;
		if(Check(mid))r = mid;
		else l = mid + 1;
	}
	printf("%d", r);
	return 0;
} 

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