UVA1471-Copying Books(二分答案)
Accept: 2669 Submit: 22797
Time Limit: 3000 mSec
Problem 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. Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies. 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.
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, 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.
Output
Sample Input
Sample Output
100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100
题解:最大化最小值,目前遇到的题不是贪心就是二分,这个题明显二分答案。输出格式需要注意一下,因为要字典序最小所以尽量让后面的数凑在一起,这样前面的数就能尽量分散。如果当前剩余分块数等于剩余数字个数,直接break输出即可。
#include <bits/stdc++.h> using namespace std;
typedef long long LL; const int maxn = + ;
const LL INF = 1e15; int n, m;
LL num[maxn]; int Check(LL x) {
LL tmp = ;
int cnt = ;
for (int i = ; i < n; i++) {
if (tmp + num[i] <= x) tmp += num[i];
else tmp = num[i], cnt++;
} return cnt;
} bool should_put[maxn]; void output(LL x) {
memset(should_put, false, sizeof(should_put));
LL tmp = ;
int i, cnt = ;
for (i = n - ; i >= ; i--) {
if (tmp + num[i] <= x) tmp += num[i];
else {
should_put[i] = true;
tmp = num[i];
cnt++;
}
if (i == m - cnt) break;
} if (i != -) {
for (int j = ; j < i; j++) {
should_put[j] = true;
}
}
for (int i = ; i < n - ; i++) {
printf("%lld ", num[i]);
if (should_put[i]) printf("/ ");
}
printf("%lld\n", num[n - ]);
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d%d", &n, &m);
LL sum = , Max = -INF;
for (int i = ; i < n; i++) {
scanf("%lld", &num[i]);
sum += num[i];
Max = Max > num[i] ? Max : num[i];
}
LL ans = ;
LL l = Max, r = sum;
while (l <= r) {
LL mid = (l + r) / ;
if (Check(mid) <= m) {
ans = mid;
r = mid - ;
}
else l = mid + ;
} //printf("%lld\n", l); output(l);
}
return ;
}
UVA1471-Copying Books(二分答案)的更多相关文章
- UVa 714 Copying Books - 二分答案
求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- 2019杭电多校第三场hdu6606 Distribution of books(二分答案+dp+权值线段树)
Distribution of books 题目传送门 解题思路 求最大值的最小值,可以想到用二分答案. 对于二分出的每个mid,要找到是否存在前缀可以份为小于等于mid的k份.先求出这n个数的前缀和 ...
- ZOJ 2002 Copying Books 二分 贪心
传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- Copying Books
Copying Books 给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的 ...
- CH Round #72树洞[二分答案 DFS&&BFS]
树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...
- [CF752E]Santa Claus and Tangerines(二分答案,dp)
题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...
随机推荐
- Java学习笔记之——final关键字
最终的,最后的 可以修饰:变量.方法.类 (1) 修饰变量 不可改变,即常量,只能赋值一次,赋值之后不可改变 a. 修饰属性:private final int num; 要求属性要显式赋值 通常在属 ...
- JavaWeb-BeginTomcat
上手Tomcat 1.Ubuntu 18.04 下载/安装Tomcat 以下内容参考链接 安装JDK sudo apt-get update sudo apt-get install default- ...
- Fundebug累计处理1000万条错误事件!
摘要上线半年时间,Fundebug累计处理1000万条错误事件.10000000啊! 自从去年双11[正式上线](https://blog.fundebug.com/2016/11/11/fundeb ...
- 记录使用Redis和nginx 实现一个简单的负载均衡(FB)
这两年在博客园看了不少大牛的分享,一直打算能写点什么东西. 之前偶然看见一个利用Redis 当作 Session数据宿主的demo,出处我已经找不到了.后来没事看了看nginx相关的东西.其中负载均衡 ...
- idea代码快捷
idea代码快捷:main函数快捷:psvmfor循环快捷:fori.foreach系统输出快捷:sout.serr 更多的提示可以按Ctrl+ J 进行查看 更改快捷:File-->Setti ...
- js控制随机数生成概率代码实例
基本思路:把Math.random()js随机数生成的数看着百分比,然后定义每个整数值取值范围. 具体内容如下,供大家参考 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- js替换字符串中特殊字符
var reg=/\\|\/|\?|\?|\*|\"|\“|\”|\'|\‘|\’|\<|\>|\{|\}|\[|\]|\[|\]|\:|\:|\.|\^|\$|\!|\~|\` ...
- 获取请求的ip工具类
package com.example.util; import javax.servlet.http.HttpServletRequest; /** * get remote msg * 获取访问的 ...
- getprop从哪获取属性
Android SystemProperties设置/取得系统属性的用法总结 通过调查得知,Android系统中取得/设置系统属性的用法参考以下3篇文章就足够了. 1.Android SystemPr ...
- Team Services的打包管理
Team Services的打包管理 概述 Package Management (打包管理)是一种扩展,可以更容易地发现.安装和发布包. 它与Team Services中心如构建功能深度集成,这样打 ...