1 条题解

  • 0
    @ 2026-10-2 2:21:45

    二分最小的最大抄写页数,并从后向前划分且为前面每个人保留至少一本,覆盖 500 本书和 k=m。

    #include <bits/stdc++.h>
    using namespace std;
    int main(){int n,k;cin>>n>>k;vector<long long>a(n);long long low=0,high=0;for(auto&x:a){cin>>x;low=max(low,x);high+=x;}while(low<high){long long mid=(low+high)/2,total=0;int count=1;for(long long x:a){if(total+x>mid){count++;total=0;}total+=x;}if(count<=k)high=mid;else low=mid+1;}vector<pair<int,int>>parts;int end=n;for(int remaining=k;remaining>0;remaining--){int start=end-1;long long sum=a[start];while(start>=remaining&&sum+a[start-1]<=low){start--;sum+=a[start];}parts.push_back({start+1,end});end=start;}reverse(parts.begin(),parts.end());for(auto[l,r]:parts)cout<<l<<' '<<r<<"\n";}
    
    • 1

    信息

    ID
    3953
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者