题目链接:

题目

Copying Books

Time limit: 3.000 seconds

问题描述

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

1, 2, . . . , m) that may have different number of pages (p1, p2, . . . , pm) and you want to make one copy of

each of them. Your task is to divide these books among k scribes, k ≤ m. 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 0 = b0 < b1 < b2, . . . < bk−1 ≤ bk = m 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.

输入

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,

1 ≤ k ≤ m ≤ 500. At the second line, there are integers p1, p2, . . . , pm separated by spaces. All these

values are positive and less than 10000000.

输出

For each case, print exactly one line. The line must contain the input succession p1, p2, . . . pm 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.

样例

input

2

9 3

100 200 300 400 500 600 700 800 900

5 4

100 100 100 100 100

output

100 200 300 400 500 / 600 700 / 800 900

100 / 100 / 100 / 100 100

题意

给你n本书,现在让你把它们分为m堆,每一堆的书编号要连续,并且使得页数最多的那一堆的页数值最小。

题解

最大值最小,考虑二分。

二分最大堆的书页数小于等于x,然后只要贪心线性扫一遍,如果能分出少于m堆,则继续扩大答案,否则缩小。

由于多解的时候要让越前面的堆的页数越小。所以考虑从后面贪心往前面扫,因为后面的堆越大前面的就自然越小了,如果扫一遍发现分出的堆数不够,那就从前往后把没有分割的给分割开,直到分出m堆。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M L+(R-L)/2
using namespace std; typedef long long LL;
const int maxn=555;
const LL INF=(LL)5e9; int m,k;
int arr[maxn],tag[maxn];
int Ma; bool ok(LL ma){
if(ma<Ma) return false;
LL sum=0;
int cnt=0;
for(int i=0;i<m;i++){
sum+=arr[i];
if(sum>ma){
sum=arr[i];
cnt++;
}
}
cnt++;
return cnt<=k;
} int main(){
int tc;
scanf("%d",&tc);
while(tc--){
Ma=-1;
memset(tag,0,sizeof(tag));
scanf("%d%d",&m,&k);
for(int i=0;i<m;i++){
scanf("%d",&arr[i]);
Ma=max(Ma,arr[i]);
}
LL l=Ma-1,r=INF;
while(l+1<r){
LL mid=l+(r-l)/2;
if(ok(mid)) r=mid;
else l=mid;
}
LL sum=0; int cnt=1;
for(int i=m-1;i>=0;i--){
sum+=arr[i];
if(sum>r){
sum=arr[i];
cnt++;
tag[i]=1;
}
}
for(int i=0;i<m-1&&cnt<k;i++){
if(!tag[i]){
tag[i]=1; cnt++;
}
}
for(int i=0;i<m-1;i++){
printf("%d ",arr[i]);
if(tag[i]) printf("/ ");
}
printf("%d\n",arr[m-1]);
}
return 0;
}

最新文章

  1. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
  2. Java知识结构
  3. 35-less 简明笔记
  4. FFRPC应用之Client/Server
  5. Web 项目可能会用到的20款优秀的开源工具
  6. 结对编程——关于Fault、Error、Failure程序设计
  7. float与double的范围和精度
  8. OpensStack instance debug
  9. ios01
  10. [学习笔记] 模拟退火 (Simulated Annealing)
  11. Spring事务隔离级别和传播性
  12. MyBatis学习笔记2--配置环境详解
  13. 集成maven和Spring boot的profile 专题
  14. FastJson(阿里巴巴)基础
  15. python3与mysql交互:pymysql
  16. 【大数据笔记】白话详解Zookeeper的一致性
  17. java类的成员变量和局部变量的区别
  18. iOS笔记之常用工具
  19. IO,File对象-构造函数和常用方法
  20. freeradius防止用户异常断开无法重新链接上

热门文章

  1. Oracle数据库对象_同义词
  2. 第一个Cocos2d-JS游戏
  3. div 显示与隐藏
  4. pancake的排序- 1.3 一摞烙饼的排序 《编程之美》读书笔记03
  5. Linux驱动编程--基于I2C子系统的I2C驱动
  6. 游戏对象的变换-Transform
  7. Ubuntu下MySQL忘记root密码重置
  8. 基础学习总结(七)--子线程及Handler
  9. CenterOS中安装Redis及开机启动设置
  10. 七天学会NodeJS-学习笔记