求助,按照我的写法为什么记录l行,记录ans就不行?
查看原帖
求助,按照我的写法为什么记录l行,记录ans就不行?
204619
wwhOvO楼主2020/5/31 15:41
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 1000050

using namespace std;

int n , m;
long long a[ MAXN ];
long long l = -9999999999 , r;

bool check( long long mid )
{
	int k = 0;
	long long now = 0;
	for( int i = 1 ; i <= n ; i++ )
	{
		if( now + a[i] <= mid ) now += a[i];
		else now = a[i] , k++;
	}
	return k >= m;
}

int main()
{
	cin >> n >> m;
	for( int i = 1 ; i <= n ; i++ ) 
	{
		cin >> a[i];
		l = max( l , a[i] );
		r += a[i];
	}
	long long ans = 0;
	while( l <= r )
	{
		long long mid = ( l + r ) / 2;
		if( check( mid ) ) ans = mid , l = mid + 1;
		else r = mid - 1;
	}
	cout << l << endl;
    // 这种不行:cout << ans << endl;
	return 0;
} 
2020/5/31 15:41
加载中...