P1182 数列分段 Section II - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
int n, m, a[100005],l,r;
long long s[100005];
inline void read()
{
cin >> n >> m;
for (int i = 1; i <= n;i++)
{
cin >> a[i];
l = max(a[i], l);
r += a[i];
}
}
inline bool C(int x)
{
int tot=0,num=0;
for(int i=1;i<=n;i++)
{
if(tot+a[i]<=x)tot+=a[i];
else tot=a[i],num++;
}
return num>=m;
}
int main()
{
read();
while(l<=r)
{
int mid=(l+r)>>1;
if(C(mid))
{
l = mid+1;
}
else
{
r = mid-1;
}
}
cout << l << endl;
return 0;
}
P2440 木材加工 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010;
ll a[N];
ll n, m;
ll check(ll x) {
ll cnt = 0;
for (int i = 0; i < n; i++) {
cnt += a[i] / x;
if (cnt >= m) return true;
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
ll l = 0, r = 0;
for (int i = 0; i < n; i++) r += a[i];
while (l < r) {
ll mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l << endl;
return 0;
}