UVa 714 Copying Books(二分)

时间:2023-03-08 22:42:55
UVa 714 Copying Books(二分)

题目链接: 传送门

Copying Books

Time Limit: 3000MS     Memory Limit: 32768 KB

Description

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. Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies. 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 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 k scribes, 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−1 ≤ bk = m 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, 1 ≤ k ≤ m ≤ 500. At the second line, there are integers p1,p2,...,pm 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 p1,p2,...pm 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. Ifthereismorethanonesolution,printtheonethatminimizestheworkassignedtothefirstscriber, 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

思路:

题目大意:给m个数分成k个区间,使区间中数的和最小。如果有多种情况,尽量在从前面开始划分。

最大值最小化问题,二分找出满足题意的最大值,以这个最大值从后面开始划分,划分后不足k段,从前面未细分的继续划分至k段。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
LL M,K;
int ans[505];

bool OK(LL x)
{
    LL sum = 0,cnt = 0;
    for (int i = 0;i < M;i++)
    {
        sum += ans[i];
        if (sum > x)
        {
            sum = ans[i];
            cnt++;
        }
    }
    cnt++;
    return cnt > K;
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        LL sum = 0,cnt = 0,maxx = 0;
        bool flag[505];
        memset(flag,false,sizeof(flag));
        memset(ans,0,sizeof(ans));
        scanf("%I64d%I64d",&M,&K);
        for (int i = 0;i < M;i++)
        {
            scanf("%d",&ans[i]);
            sum += ans[i];
            if (maxx < ans[i])
            {
                maxx = ans[i];
            }
        }
        LL left = maxx,right = sum;
        while (left < right)
        {
            LL mid = left + ((right-left)>>1);
            if (OK(mid))
            {
                left = mid+1;
            }
            else
            {
                right = mid;
            }
        }
        //cout << left << " " << right << endl;
        sum = 0;
        for (int i = M - 1;i >= 0;i--)
        {
            sum += ans[i];
            if (sum > right)
            {
                cnt++;
                sum = ans[i];
                flag[i] = true;
            }
        }
        for (int i = 0;i < M && cnt < K - 1;i++)
        {
            if (!flag[i])
            {
                flag[i] = true;
                cnt++;
            }
        }
        for (int i = 0;i < M - 1;i++)
        {
            printf("%d ",ans[i]);
            if (flag[i])
            {
                printf("/ ");
            }
        }
        printf("%d\n",ans[M-1]);
    }
    return 0;
} 

UVA如果用64位的整型输出会格式错误,真是奇怪,另外在二分查找的时候LL left = maxx,right = sum;LL left = maxx换成LL left = -1(0)却得到了wrong answer真是想不通啊。
在汉犇犇的指导下,终于搞明白这个下限改掉wrong answer是怎么回事了。看一组样例,左边是下线max{ f[i] },右边是下限为-1,二分查找出来的值可能比max{ f[i] }小,导致没有尽量往前分割。UVa 714 Copying Books(二分)