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

Submit Status

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

解题思路:

题意:按顺序给你N个数,将这N个数分成连续的M段,使得这M段每段的和中的最大值最小,输出最小值(1<=N<=100000,1<=M<=N,每个数在1到10000之间),如果有多种可能的话,尽量在前面进行划分。

思路:

1、由于函数具有单调性的特征,因此可以用二分枚举的办法去实现它,但这里不需要排序。

2、输出的时候需要用到贪心的思想,既尽量往前划分。

3、大概的思路就是二分枚举求得满足题意的最大值之后,然后以这个最大值通过从后往前的方式划分成段,如果剩余可划分段与i+1的值相等(尽量靠前),则将剩余的段往前划分,具体实现可以用一个标记数组表示是否划分。

5、注意要用long long 来存。

解题思路借鉴了大神的,感觉这个思路比较清晰、易懂,希望能够帮助到博友们

程序代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxm = + ;
int m, k, p[maxm];
int solve(long long maxp)
{
long long done = ;
int ans = ;
for(int i = ; i < m; i++)
{
if(done + p[i] <= maxp) done += p[i];
else { ans++; done = p[i]; }
}
return ans;
}
int last[maxm];
void print(long long ans)
{
long long done = ;
memset(last, , sizeof(last));
int remain = k;
for(int i = m-; i >= ; i--)
{
if(done + p[i] > ans || i+ < remain)
{ last[i] = ; remain--; done = p[i]; }
else
done += p[i];
}
for(int i = ; i < m-; i++)
{
printf("%d ", p[i]);
if(last[i]) printf("/ ");
}
printf("%d\n", p[m-]);
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &m, &k);
long long tot = ;
int maxp = -;
for(int i = ; i < m; i++)
{
scanf("%d", &p[i]);
tot += p[i];
maxp = max(maxp, p[i]);
}
long long L = maxp, R = tot;
while(L < R)
{
long long M = L + (R-L)/;
if(solve(M) <= k) R = M; else L = M+;
}
print(L);
}
return ;
}

高效算法——B 抄书 copying books,uva714的更多相关文章

  1. 抄书 Copying Books UVa 714

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

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

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

  3. UVa 714 Copying Books(二分)

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

  4. UVA 714 Copying Books 二分

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

  5. poj 1505 Copying Books

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

  6. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

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

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

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

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

  9. Copying Books

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

随机推荐

  1. IK分词算法设计总结

    IK分词算法设计思考 加载词典 IK分词算法初始化时加载了“敏感词”.“主词典”.“停词”.“量词”,如果这些词语的数量很多,怎么保证加载的时候内存不溢出 分词缓冲区 在分词缓冲区中进行分词操作,怎么 ...

  2. SQL数据库安装

    安装过程中经常出现失败或者提示,那么久要清楚干净所有的数据在重新安装,步骤如下. SQL2008卸载 一.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 卸载与 ...

  3. webServices 执行流程,(我是菜鸟,我怕谁,仅代表个人理解,欢迎各位大神们指导,不和您的胃口,请默默离开!!)

    二.上图仅仅代表个人理解,下面以代码方式解释一下. (1) strtus.xml <?xml version="1.0" encoding="UTF-8" ...

  4. IOS应用程序生命周期&启动周期函数

    —程序的生命周期         a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程         b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...

  5. gulp安装

    1. npm install gulp -g    全局安装  npm install gulp --save-dev  安装文件内,纪录于package.json     接著安装插件,完成下列任务 ...

  6. Java 基础(一)

    Java不只是一种语言,更是一个完整的平台,有一个庞大的库,其中包含了很多可重用的代码和一个提供诸如安全性.跨操作系统的可移植性以及自动垃圾收集等服务的执行环境. javaSE: 整个java技术的核 ...

  7. Linux下JDK环境变量配置

    JDK官方下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 我的下载路 ...

  8. 【POJ2155】【二维树状数组】Matrix

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  9. jQuery慢慢啃之属性(三)

    1.attr(name|properties|key,value|fn)设置或返回被选元素的属性值. $("img").attr("src");//获取属性 $ ...

  10. ExtJS4 动态加载

    由于有人说不要每次都调用ext-all.js,会影响性能,所以有考虑动态加载,动态加载时页面调用ext.js(4.0.7在调试时可考虑用ext-dev.js),然后在onReady之前调用 Ext.L ...