[贪心&&二分]uva714 Copying Books



  Copying Books 

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.


Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered ) that may have different number of pages ( ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers  such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.

Input 

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k,. At the second line, there are integers  separated by spaces. All these values are positive and less than 10000000.

Output 

For each case, print exactly one line. The line must contain the input succession  divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.


If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.

Sample Input 

2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100

Sample Output 

100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100


Miguel A. Revilla 
2000-02-15

题意:给出m个数字,一个数字k,把这m个数字划分为k个区间使得这k个区间中的所有的数相加的最大值最小,k个区间中的数字必须按照原来的顺序。

思路:典型的最大值最小化,这是刘汝佳的算法竞赛经典入门中的讲解,然后使用贪心从后往前找。

[贪心&&二分]uva714 Copying Books_第1张图片

#include<iostream>

using namespace std;

int arry[550],vis[550];
int m,n;

int judge(long long pos,int len)
{
    int i=0,tag=0;
    long long sum=0;
    while(len>0&&i<m)
    {
        if(sum+arry[i]<pos)
        {
            sum=sum+arry[i];
            i++;
        }
        else if(sum+arry[i]==pos)
        {
            i++;
            sum=0;
            tag=1;
            len--;
        }
        else if(sum+arry[i]>pos)
        {
            sum=0;
            len--;
        }
    }
    if(i<m) return -1;
    else if(i==m&&tag==1) return 0;
    else return 1;
}

void output(long long pos,int len)
{
    int i;
    long long sum=0;
    for(i=m-1;i>0;i--)
    {
        if(len>i)
        {
            vis[i]=1;
            len--;
        }
        else if(sum+arry[i]<=pos&&sum+arry[i]+arry[i-1]>pos)
        {
            sum=0;
            vis[i]=1;
            len--;
        }
        else
        {
            sum=sum+arry[i];
        }
    }
    for(i=0;i<m;i++)
    {
        if(vis[i]) cout<<"/ ";
        if(i!=m-1)cout<<arry[i]<<" ";
        else cout<<arry[i];
    }
    cout<<endl;
}

int main()
{
    int num;
    cin>>num;
    while(num--)
    {
        int i;
        long long sum=0;
        cin>>m>>n;
        for(i=0;i<m;i++)
        {
            cin>>arry[i];
            sum=sum+arry[i];
            vis[i]=0;
        }
        long long left=1,right=sum,mid;
        mid=(left+right)/2;
        int tag=judge(mid,n);
        while(tag!=0)
        {
            if(tag==-1)
            {
                left=mid+1;
                mid=(left+right)/2;
                tag=judge(mid,n);
            }
            else if(tag==1)
            {
                right=mid-1;
                mid=(right+left)/2;
                tag=judge(mid,n);
            }
        }
        output(mid,n);
        //cout<<mid<<endl;
    }
    return 0;
}

你可能感兴趣的:(二分查找,贪心)