抄书 Copying Books UVa 714
Copying Books
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B
题目:
Description
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的更多相关文章
- 高效算法——B 抄书 copying books,uva714
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Description ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- uva 714 Copying Books(二分法求最大值最小化)
题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- poj 1505 Copying Books
http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submiss ...
- POJ1505&&UVa714 Copying Books(DP)
Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...
- Copying Books
Copying Books 给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的 ...
- UVA 714 Copying Books 抄书 (二分)
题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...
随机推荐
- WPF 创建自定义窗体
在前面的一篇博客"WPF 自定义Metro Style窗体",展示了如何创建一个类似于Metro Style的Window,并在程序中使用.但是这个窗体不能够自由的改变大小.今天的 ...
- pythonchallenge之C++学习篇-03
提示说一个小写字母两面精确地被大写字母包围,应该指的是周围没有四个而仅仅这两个像这样的:xXXXxXXXx的中间的那个应该是符合条件的 好了标题是re,提示该是使用正则表达式,网页源码里有待处理的字符 ...
- jQuery基础知识点(DOM操作)
1.样式属性操作 1)设置样式属性操作 ①设置单个样式: // 第一个参数表示:样式属性名称 // 第二个参数表示:样式属性值 $(selector).css(“color”, ...
- 使用while代替for循环的几个习题
1:兔子问题 2:100以内质数的和 3:单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元.求刚好花完150元,有多少种买法,没种买法都是各买几样? 总结:wh ...
- SQL作业的操作全
--定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...
- winmail服务器启动失败 无法启动
1.解决句柄问题:打开命令行:开始 -> 运行-> 输入 cmd -> 确定.切换命令目录至winmail的服务目录,我的是:E:\htdocs\Winmail\server\> ...
- Liferay 6.2 改造系列之十六:关闭OpenID模式的单点登录
在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to enable OpenId au ...
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...
- Android实现全屏的三种方式
一.通过代码 requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏 getWindow().setFlags(WindowManager.Lay ...
- iOS KVO 学习笔记
//// //// main.m //// TestBasis //// //// Created by ficow on 16/1/14. //// Copyright © 2016年 ficow. ...