UVA 714 题解

2017/10/19 OI 二分

UVA 714 题解

题目

链接

UVA 714

题面

    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. Thescripts 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 havembooks (numbered 1,2... m) that may have different number of pages (p1; p2; ... pm) and you want to make one copy of each of them. Your task is to divide these books among kscribes,k<=m. 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 0 = b0 < b1 < b2< ... < bk

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, $1 \le k \le m \le 500$. At the second line, there are integers $p_1, p_2, \dots p_m$ 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 $p_1, p_2, \dots p_m$ 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

题意

  将一个长度为n的数列分为k个子数列,问在什么情况小,所有子数列的最大值最小

题解

思路

  这题考的是二分。我们可以假设X为所有子数列中的最大值的最小值。用这个X去利用数据生成一系列的子数列,然后再判断这个子数列的个数是否合法(即子数列的个数是否等于k)即可。值得注意的是二分的范围是从数列中元素的最大值到数列中所有元素的和。

  本题对细节的考察比较恶心,在生成子数列时,要从后向前找,同时还要考虑到剩余的要分配的子数列等于未分配的数列元素的情况。且如果是从后向前扫的,在输出时还要将答案颠倒一下。就这样还没有TLE也是很迷。

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n,k;
vector<int> data;
vector<vector<int> > ans;

inline void print_ans()
{
    if(ans.empty())
        return ;
    ans=vector<vector<int> >(ans.rbegin(),ans.rend());
    for(int i=0;i<ans.size();i++)
        ans[i]=vector<int>(ans[i].rbegin(),ans[i].rend());
    for(int i=0;i<ans[0].size()-1;i++)
    {
        cout<<ans[0][i]<<" ";
    }
    cout<<ans[0][ans[0].size()-1];
    for(int i=1;i<ans.size();i++)
    {
        cout<<" /";
        for(int j=0;j<ans[i].size();j++)
            cout<<" "<<ans[i][j];
    }
    cout<<endl;
}

bool if_work(long long x,bool get_ans=true)
{
    vector<vector<int> > buff;
    long long sum=0;
    vector<int> buffer;
    for(int i=data.size()-1;i>=0;i--)
    {
        if((sum&&(k-buff.size()-1>=i+1))||(sum==0&&k-buff.size()>=i+1))
        {
            if(sum)
            {
                buff.push_back(buffer);
                buffer.clear();
                sum=0;
            }
            for(int j=i;j>=0;j--)
            {
                buff.push_back(vector<int>(1,data[j]));
            }
            break;
        }
        
        if(sum+data[i]<=x)
        {
            sum+=data[i];
            buffer.push_back(data[i]);
        }
        else
        {
            buff.push_back(buffer);
            buffer.clear();
            sum=0;
            i+=1;
        }
        if(buff.size()>k)
        {
            return false;
        }
    }
    if(buffer.size())
        buff.push_back(buffer);
    if(buff.size()>k)
        return false;
    else
    {
        if(get_ans)
            ans=buff;
        return true;
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        long long sum=0;
        int maxl=0;
        data.clear();
        ans.clear();
        cin>>n>>k;
        data.resize(n);
        for(int i=0;i<n;i++)
        {
            cin>>data[i];
            sum+=data[i];
            maxl=max(maxl,data[i]);
        }
        long long l=maxl,r=sum;
        long long mid=(l+r)/2;
        while(l<r)
        {
            if(if_work(mid))
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
            mid=(l+r)/2;
        }
        print_ans();
    }
    return 0;
}
Show Disqus Comments

Search

    welcome to visit my github

    creatorlxd's github

    Table of Contents