求调试#5, 有偿
  • 板块学术版
  • 楼主Nemuri
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/7/21 15:57
  • 上次更新2024/7/21 16:46:04
查看原帖
求调试#5, 有偿
1089862
Nemuri楼主2024/7/21 15:57
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 1e5+6;
const int INF = 0x7fffffff;
typedef long long int llint;
int N,group;
llint a[MAX];
inline bool judge_l(llint mid)
{
	int total=0;
	llint sum=mid;
	for(int pre=1,next=2;next<=N+1;pre++,next++)
	{
		if(a[pre]<=sum and a[next]>sum)
		{
			sum=a[pre];
			sum+=mid;
			total++;
		}
		else if(a[pre]>sum)
		{
			return true;
		}
	}
	return total>group;
}
int main()
{
	freopen("../IO/P1182_5.in","r",stdin);
	cin>>N>>group;
	for(int i=1;i<=N;i++)
	{
		llint tmp;
		cin>>tmp;
		a[i]=a[i-1]+tmp;
	}
	a[N+1]=INF;
	
	llint l=1,r=a[N];
	while(l<r)
	{
		llint m=(l+r)/2;
		if(judge_l(m))
		{
			l=m+1;
		}
		else
		{
			r=m;
		}
	}
	
	cout<<l<<endl;
	
	fclose(stdin);
	return 0;
}

2024/7/21 15:57
加载中...