抄书 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应该为所有正整数 ...
随机推荐
- 如何更快的删除String中的空格[未完]
背景:此文章主要源于网址[1]所描述的,文中大部分方法亦是[1]中实现的. 下面介绍集中删除空格的方法: 方法1:按空格分割后再拼接 /// <summary> /// 按空格分割后拼 ...
- 优秀的API接口设计原则及方法(转)
一旦API发生变化,就可能对相关的调用者带来巨大的代价,用户需要排查所有调用的代码,需要调整所有与之相关的部分,这些工作对他们来说都是额外的.如果辛辛苦苦完成这些以后,还发现了相关的bug,那对用户的 ...
- MapKit的使用显示当前位置
1.添加MapKit.framework框架 ,在plist中添加字段,用于,获取用户当前位置设置 NSLocationAlwaysUsageDescription 2.代码 #import &quo ...
- 简单解释Windows如何使用FS段寄存器
详见附件 jpg改rar
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件 我杀过 w3wp.exe和asp.net_state的进程,重启 iis admin的服务,都还是不行. 只是把 ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- JS操作select下拉框动态变动(创建/删除/获取)
1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select"); m ...
- 分享Kali Linux 2016.2第48周镜像文件
分享Kali Linux 2016.2第48周镜像文件Kali Linux官方于11月27日发布Kali Linux 2016.2的第48周镜像.这次延续以往规律,仍然是11个镜像文件.默认的Gnom ...
- 将MapReduce的结果输出至Mysql数据库
package com.sun.mysql;import java.io.DataInput;import java.io.DataOutput;import java.io.IOException; ...
- HDU5754 Life Winner Bo(博弈)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5754 Description Bo is a "Life Winner" ...