aojiaoluolisaiban @ 2024-03-09 17:53:49
采用的是二分答案,借助样例1和2检查了judge函数,二分部分的思路是如果满足,争取找到更大的,所以先将ans=mid,但是样例3和5死活过不去
#include <iostream>
#include <algorithm>
using namespace std;
int n,m;
int a[100005];
int ans,l,r;
int mjudge(int x)
{
int cnt = 0;
int sum = 0;
int i = 1;
while(i <= n)
{
if(i == n && sum == 0)
{
cnt++;
break;
}
if(sum + a[i] <= x)
{
sum += a[i];
i++;
}
else
{
cnt++;
sum = 0;
}
}
return cnt;
}
void msearch()
{
while(l <= r)
{
int mid = (l + r) / 2;
if(mjudge(mid) > m)
{
l = mid + 1;
}
else
{
ans = mid;
r = mid - 1;
}
}
}
int main()
{
cin >> n >> m;
for(int i=1;i<=n;i++)
{
cin >> a[i];
l = max(l,a[i]);
r+=a[i];
}
msearch();
cout << ans << endl;
return 0;
}
我真的母鸡哇