嘟嘟嘟


这题就比较水了,毕竟只评了个蓝。


想一下发现满足单调性,所以可以二分找最大值。
但是最小值怎么办?刚开始我很zz的以为只要把判断条件从大于等于改成小于等于就行了,后来发现根本不对。
想了想因为最小值和最大值之间一定是一段答案为\(k\)的区间,所以可以二分找最小值:如果当前答案不等于\(m\),就向右二分,否则向左二分。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
	ll ans = 0;
	char ch = getchar(), last = ' ';
	while(!isdigit(ch)) last = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(last == '-') ans = -ans;
	return ans;
}
inline void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}

int n, m;
ll a[maxn], Max;
In bool judge1(ll x)
{
	ll cnt = 0; ll tot = 0;
	for(int i = 1; i <= n; ++i)
	{
		tot = max(1LL * 0, tot + a[i]);
		if(tot >= x) ++cnt, tot = 0;
	}
	return cnt >= m;
}
In bool judge2(ll x)
{
	ll cnt = 0; ll tot = 0;
	for(int i = 1; i <= n; ++i)
	{
		tot = max(1LL * 0, tot + a[i]);
		if(tot >= x) ++cnt, tot = 0;
	}
	return cnt == m;
}

int main()
{
//	freopen("1.in", "r", stdin);
	n = read(), m = read();
	for(int i = 1; i <= n; ++i) a[i] = read();
	ll L = 0, R = 1e14;
	while(L < R)
	{
		ll mid = (L + R + 1) >> 1;
		if(judge1(mid)) L = mid;
		else R = mid - 1;
	}
	if(!L || !judge2(L)) {puts("-1"); return 0;}
	Max = L;
	L = 1, R = Max;
	while(L < R)
	{
		ll mid = (L + R) >> 1;
		if(judge2(mid)) R = mid;
		else L = mid + 1;
	}
	write(L), space, write(Max), enter;
	return 0;
}