题目链接: 传送门

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(二分)的更多相关文章

  1. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

  2. UVa 714 Copying Books - 二分答案

    求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...

  3. uva 714 Copying Books(二分法求最大值最小化)

    题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...

  4. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

  5. 【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx

    Before the invention of book-printing, it was very hard to make a copy of a book. All the contents h ...

  6. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

  7. UVa 714 Copying books 贪心+二分 最大值最小化

    题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...

  8. UVA 714 Copying Books 抄书 (二分)

    题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...

  9. UVA - 714 Copying Books (抄书)(二分+贪心)

    题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列).设第i个序列的 ...

随机推荐

  1. 拥抱HTML5 — Page Visibility(页面可见性) API介绍

    H5 提供了很多简单实用的 API,Page Visibility API 就是其中之一. 不知道用户是不是在与页面交互,这是困扰广大 Web 开发人员的一个主要问题.如果 页面最小化了 或者 隐藏在 ...

  2. GPS围栏两个多边形相交问题的奇葩解法

    前言 GPS测量仪测量的产地面积,然后提交到系统中,系统需要校验这块产地和其他产地是否有重叠,重叠超过10%就要提出警告这块产地已经被XXX登记入库了.GPS测量仪测量出来的数据是连续的经纬度坐标数据 ...

  3. 我的 GitHub 100 连击

    终于达成 gayhub 的第一个100连击了,感觉自己整个人颜色都不一样了,完全蜕变了. PS: GitHub 汉化插件 52cik/github-hans 感兴趣的赶紧 get 起来吧. 遇到瓶颈 ...

  4. 隐马尔可夫模型(Hidden Markov Model,HMM)

    介绍 崔晓源 翻译 我们通常都习惯寻找一个事物在一段时间里的变化规律.在很多领域我们都希望找到这个规律,比如计算机中的指令顺序,句子中的词顺序和语音中的词顺序等等.一个最适用的例子就是天气的预测. 首 ...

  5. JavaScript学习笔记- 正则表达式常用验证

    <div> <h1>一.判断中国邮政编码匹配</h1> <p>分析:中国邮政编码都是6位,且为纯数字</p> <div>邮政编码 ...

  6. MVC中使用Ajax提交数据 Jquery Ajax方法传值到action

    Jquery Ajax方法传值到action <script type="text/javascript"> $(document).ready(function(){ ...

  7. 东大OJ-一元三次方程解的个数

    1043: Fixed Point 时间限制: 5 Sec  内存限制: 128 MB 提交: 26  解决: 5 [提交][状态][讨论版] 题目描述 In mathematics, a fixed ...

  8. 1104关于优化mysql服务器几个参数和思路

    转自http://www.cnblogs.com/AloneSword/p/3207697.html 按照从大到小,从主要到次要的形式,分析 mysql 性能优化点,达到最终优化的效果. 利用 min ...

  9. springMvc静态资源拦截问题

    测试的时候发现:如果直接访问web项目的html等静态资源,不能访问 原因如下: 当web.xml中url-pattern配置为"/"时,会导致系统中的静态资源被拦截 如何解决: ...

  10. Java网络编程——IP

    类:InetAdrress 该类主要用于表示互联网协议(IP对象)地址,且无构造方法 主要方法: public static InetAddress getLocalHost()-->返回本地主 ...