uVa 714 (二分法)
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.
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
Miguel A. Revilla
2000-02-15
程序分析:题目大意是要抄N本书,编号为1,2,3……N,每本书有1<=x<=10000000页,把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的,每个抄写员的速度是相同的,求所有书抄完所用最少时间的分配方案。此题不但用到的二分法,同时在输出的时候也用到的贪心算法,还有一个值得注意的地方就是如果把X,Y,MID定义成int型会导致数据的溢出所以我们应该使用long long型。
程序代码:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = ;
int a[MAXN], use[MAXN];
long long low_bound, high_bound;
int n, m;
void init()
{
low_bound = -;
high_bound = ;
memset(use, , sizeof(use));
} int solve(int mid)
{
int sum = , group = ;
for(int i = n-; i >= ; i--)
{
if(sum + a[i] > mid)
{
sum = a[i];
group++;
if(group > m) return ;
}
else sum += a[i];
}
return ;
} void print(int high_bound)
{
int group = , sum = ;
for(int i = n-; i >= ; i--)
{
if(sum + a[i] > high_bound)
{
use[i] = ;
sum = a[i];
group++;
}
else sum += a[i];
if(m-group == i+)
{
for(int j = ; j <= i; j++)
use[j] = ;
break;
}
}
for(int z = ; z< n-; z++)
{
printf("%d ", a[z]);
if(use[z]) printf("/ ");
}
printf("%d\n", a[n-]);
} int main()
{
int T;
scanf("%d%*c", &T);
while(T--)
{
init();
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
if(low_bound < a[i]) low_bound = a[i];
high_bound += a[i];
}
long long x = low_bound, y = high_bound;
while(x <= y)
{
long long mid = x+(y-x)/;
if(solve(mid)) y = mid-;
else x = mid+;
}
print(x);
}
return ;
}
uVa 714 (二分法)的更多相关文章
- uva 714 Copying Books(二分法求最大值最小化)
题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- 抄书 Copying Books UVa 714
Copying Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...
- uva 714 - Copying Books(贪心 最大值最小化 二分)
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...
- UVa 714 Copying Books - 二分答案
求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...
- UVa 714 抄书(贪心+二分)
https://vjudge.net/problem/UVA-714 题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列.设第i个序列的各数之和为S(i),你的 ...
- UVa 714 Copying books 贪心+二分 最大值最小化
题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...
- 【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 ...
- UVa 714 (二分) Copying Books
首先通过二分来确定这种最大值最小的问题. 假设每个区间的和的最大值为x,那么只要判断的时候只要贪心即可. 也就是如果和不超过x就一直往区间里放数,否则就开辟一个新的区间,这样来判断是否k个区间容得下这 ...
随机推荐
- LNMP 基于域名的虚拟主机配置 (Centos5.6)
. . server { listen ; #listen [::]: default_server ipv6only=on; server_name www.blog.com; index inde ...
- 创建理想的SEQUENCE和自增长的trigger
SEQUENCE CREATE SEQUENCE TEST_SEQ START 1 --从1开始,第一个一定是NEXTVAL,因为第一个CURRVAL不好使,返回值会是1,第一个NEXTVAL相当于从 ...
- Linux串口编程详解(转)
串口本身,标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口曾经被广泛用于连接计算机和终端设备和各种外部设备.虽然以太网接口和USB接口也是以一个串行流进行数据传送的,但是串口连接 ...
- 【转】Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)
在vs2010下配置opencv是件痛苦的事情,一点点错误可能就会导致莫名其妙的报错,各种error让人郁闷不已,这里提供给大家一篇vs2010下配置opencv2.4.4的方法,我是64位的win7 ...
- RII K25A 语音空中飞鼠 红外学习步骤
1.按住多功能遥控器上的SET按键,超过4秒不要放手,LED指示灯会闪一次,然后长亮.2.将多功能遥控器的红外口对准你原来的遥控器的红外口,然后按RII多功能遥控器面上任何按钮,上面灯将会闪动,闪动过 ...
- 射频识别技术漫谈(14)——Mifare S50与S70的存取控制
存取控制指符合什么条件才能对卡片进行操作. S50和S70的块分为数据块和控制块,对数据块的操作有“读”.“写”.“加值”.“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种. S50 ...
- 菜农群课笔记之ICP与ISP----20110412(整理版)
耗时一上午时间对HOT大叔昨晚的群课内容进行温故并整理,现将其上传,若想看直播可到下面链接处下载:http://bbs.21ic.com/icview-229746-1-1.html 成 ...
- JavaEE Tutorials (11) - 使用Criteria API创建查询
11.1Criteria和Metamodel API概述16811.2使用Metamodel API为实体类建模170 11.2.1使用元模型类17011.3使用Criteria API和Metamo ...
- Python 参数传递
python中的变量: 一个变量是局部还是全局,在编译函数的时候就已经决定,因此读变量值的时候也不会逐层向外查找.变量是全局还是局域,根据如下3条: 1. 如果函数内部有global语句,那么它声明的 ...
- wpf全局异常
在App.xaml文件中 添加DispatcherUnhandledExceptionEventArgs 新增对应事件