714 - Copying Books

描述:采用二分+贪心策略,找出范围值,然后从后往前找就行“/”的位置判断
#include <cstdio>
#include <cstdlib>
int num[510],score[510];
int main()
{
    //freopen("a.txt","r",stdin);
    int n,m,t,k;
    long long v,sum,max,count;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        sum=v=0;
        int i=0;
        for(; i<n; i++)
        {
            scanf("%d",&num[i]);
            v=v<num[i] ? num[i] : v ;
            sum+=num[i];
        }
        while(1)
        {
            max=(v+sum)/2;
            if(max==v) break;
            for(i=count=k=0; i<n; i++)
            {
                if(count+num[i]>max)
                {
                    k++;
                    if(k==m) break;
                    count=0;
                }
                count+=num[i];
            }
            if(i==n) sum=max;
            else v=max;
        }
        v=0;
        for(count=0,k=m-1,i=n-1; i>=0; i--)
        {
            if(count+num[i]>sum||i==k-1)
            {
                --k;
                score[v++]=i;
                count=0;
            }
            count+=num[i];
        }
        for(v=v-1,i=0; i<n; i++)
        {
            if(!i) printf("%d",num[i]);
            else printf(" %d",num[i]);
            if(v>=0&&score[v]==i)
            {
                printf(" /");
                v--;
            }
        }
        printf("\n");
    }
    return 0;
}


posted @ 2013-03-24 11:09  javawebsoa  Views(247)  Comments(0Edit  收藏  举报