UVA 714(p244)----Copying Books

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define debu
using namespace std;
const int maxn=1e7+50;
typedef long long LL;
int v[maxn];
LL sum,maxx;
LL n,m,a[maxn],l,r,mid;
int check(LL limit)
{
    int i=0,num=1;
    LL sum=0;
    while(i<n)
    {
        LL tmp=sum+a[i];
        if(tmp<=limit) sum+=a[i];
        else
        {
            num++;
            sum=a[i];
        }
        i++;
    }
   return num;
}
void solve()
{
     l=maxx,r=sum;
     LL mid;
    while(l<r)
    {
        mid=l+(r-l)/2;
        if(check(mid)<=m) r=mid;
        else l=mid+1;
    }
}
void output()
{
    sum=0;
    for(int i=n-1;i>=0;i--)
    {
        LL tmp=sum+a[i];
        if(tmp>l||(i+1<m))
        {
            sum=a[i];
            v[i]=1;
            m--;
        }
        else sum+=a[i];
    }
    for(int i=0;i<n-1;i++)
    {
        printf("%lld ",a[i]);
        if(v[i]) printf("/ ");
    }
    printf("%lld\n",a[n-1]);
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    int t;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;maxx=0;
        memset(v,0,sizeof(v));
        scanf("%lld%lld",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=a[i];
            maxx=max(a[i],maxx);
        }
        solve();
        output();
    }
    return 0;
}
题目地址: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=655

你可能感兴趣的:(UVA 714(p244)----Copying Books)