Copying  Books

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B

题目:

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 calledscribers. 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 题意:
把一个包含m个正整数的序列划分成k个非空的连续子序列,使得这k个子序列中和的最大值最小。
如果有多解和最大的尽量在后面。(序列中的元素位置不能发生改变) 分析:
用二分法求出最小值,
把0设为l,sum设为r,mid=(l+r)/2;
如果以mid为最小值可以划分成k个或者小于k个子序列,说明最小值比mid小或者等于mid,相反最小值比mid大。
注意值的数据类型
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
long long a[maxn],b[maxn],sum;
int m,k;
int bj( long long x)
{
long long sum = ;
int t = k;
for(int i = ; i < m; i ++)
{
sum += a[i];
if(sum > x)
{
i --;
sum = ;
t --;
}
if(!t)
{
if(i != m - ) return ;
else return ;
}
}
return ;
}
void slove()
{
int i;
memset(b,,sizeof(b));
long long l=,r=sum,mid;
while(r>l)
{
mid=(l+r)/;
if(bj(mid)) r=mid;
else l=mid+;
}
int sum1=;
for( i=m-;i>=;i--) //标记需要划分的地方,由于和大的尽量往后所以从序列后面开始划分
{
sum1+=a[i];
if(sum1>r)
{ i++;
sum1=;
b[i]=;
k--; }
} while(k >) //如果子序列小于k就在还没划分的地方划分直到等于k
{
for(i = ;i < m; i ++ )
{
if(!b[i])
{
b[i] = ;
k --;
break;
}
}
}
return;
}
int main()
{
int n,i;
cin>>n;
while(n--)
{
sum=;
cin>>m>>k;
for(i=;i<m;i++)
{
cin>>a[i];
sum+=a[i];
}
slove();
printf("%lld",a[]);
for( i = ; i < m; i ++)
{
if(b[i]) printf(" /");
printf(" %lld", a[i]);
}
printf("\n");
}
return ;
}


抄书 Copying Books UVa 714的更多相关文章

  1. 高效算法——B 抄书 copying books,uva714

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description ...

  2. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

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

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

  4. UVA 714 Copying Books 二分

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

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

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

  6. poj 1505 Copying Books

    http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS   Memory Limit: 10000K Total Submiss ...

  7. POJ1505&amp;&amp;UVa714 Copying Books(DP)

    Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...

  8. Copying Books

    Copying Books 给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的 ...

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

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

随机推荐

  1. [Outlook] Outlook2013能收但无法发送邮件-0x800CCC13, 0x800CCC0B, 0x8004210B

    [20140704更新],在公司收邮件的时候,问题再次出现,错误码:0x800ccc13,按照以下方法测试成功: 1. 按照以前办法,反复重启,失败 2. 按照以下参考连接A中的步骤 a. Click ...

  2. C# 使用 NPOI 库读写 Excel 文件

    NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...

  3. 命令模式/command模式/行为型模式

    举个栗子 指挥官向士兵下达命令,士兵执行 实现代码如下: class Soldier { public void exe() { System.out.println("执行命令" ...

  4. 在Salesforce中实现对Object的增删改查操作

    1): Insert Statement    http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=ap ...

  5. UI中 frame 与 transform的用法与总结

    在iOS中,我们是不可以直接访问控件中frame的结构体的成员的,因此我们需要分三步来改变一个UI控件的位置,大小 一.frame用法 frame的结构体类型为: struct CGRect { CG ...

  6. 【工具】Git

    1.安装好Git以后,在开始菜单里找到Git->Git Bash,弹出一个命令窗口 2.设置邮箱 . 3.创建文件夹 4.创建版本库 5.将文件添加到缓存区中去 6.提交文件 7.检查是否还有文 ...

  7. 简单区分VMware的三种网络连接模式(bridged、NAT、host-only)

    艺搜简介 VMware在安装时默认安装了两块虚拟网卡,VMnet1和VMnet8,另外还有VMnet0.这些虚拟网卡的配置都是由Vmware虚拟机自动生成的,一般来说不需要用户自行设置. Vmware ...

  8. SpringMyBatis解析3-MapperFactoryBean

    在使用mybatis的时候,我们获取dao的方式一般是这样: SqlSession session=sessionFactory.openSession(); PersonDao personDao= ...

  9. SpringMVC解析2-ContextLoaderListener

    对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编 ...

  10. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...