https://ac.nowcoder.com/acm/contest/22353/1003
经典最小值最大,考虑二分,注意 check 函数里对最后一天的判断。
#include<bits/stdc++.h>
#define endl '\n'
#define pii pair<int,int>
using namespace std;
using ll = long long;
const int maxn = 5e4+3;
int h[maxn];
int n,d;
int loc[maxn];
bool check(ll x)
{
int cnt=0;
ll sum=0;
for(int i=1;i<=d;i++)
{
if(cnt>n) return false;
while(cnt<=n)
{
if(sum>=x) break;
sum+=h[++cnt];
loc[cnt]=i;
}
if(sum<x) return false;
sum/=2;
}
return true;
}
void solve()
{
cin>>n>>d;
for(int i=1;i<=n;i++)
cin>>h[i];
// check(24);
ll l=1,r=1e18;
ll ans;
while(l<=r)
{
ll mid=l+((r-l)>>1);
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
memset(loc,0,sizeof(loc));
check(ans);
cout<<ans<<endl;
for(int i=1;i<=n;i++)
{
if(loc[i])
cout<<loc[i]<<endl;
else cout<<d<<endl;
}
}
int main()
{
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T; T=1;
while(T--)
solve();
return 0;
}