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个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...
随机推荐
- 解决MyEclipse中install new software问题
eclipse中点击help可以直接找到install new software选项进行安装插件,但是在Myeclipse中help没有这个选项,如下提供几种解决方法 Windows-preferen ...
- js数据类型有哪些,js属性和方法的归属,
1.js的数据类型有哪些? 2.全局方法和全局属性? 一 1.js的本质就是处理数据,数据来自后台的数据库.所以变量就起到一个临时存储数据的作用. ECMAScript 制定了js的数据类型. 数据类 ...
- Java 原生网络编程.
一.概念 Java 语言从其诞生开始,就和网络紧密联系在一起.在 1995 年的 Sun World 大会上,当时占浏览器市场份额绝对领先的网景公司宣布在浏览器中支持Java,从而引起一系列的公司产品 ...
- Android Studio 学习(六)内容提供器
运行时权限 使用ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PHONE)!=Package ...
- Http(s)与后台交互方式
前言 Http(s)是前后端交互的主要方式之一,交互技术主要有:Ajax(XMLHttpRequest).Fetch.地址跳转(window.open.location.href).Http(s)与后 ...
- 前端入门6-JavaScript客户端api&jQuery
本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 声明 本系列文章内容全部梳理自以下四个来源: <HTML5权威指南> <JavaScript权威指南> MD ...
- Powershell中显示隐藏文件
PS> Get-ChildItem -Path $home -Force PS> Get-ChildItem -Path $home -Hidden
- K8S 部署 ingress-nginx (二) 部署后端为 tomcat
在上面已经部署了 ingress-nginx, https://www.cnblogs.com/klvchen/p/9903480.html 创建 service 和 pods cd vi tomca ...
- Web前端开发必备
前端学习相关书籍 关于书籍 HTML.CSS 类别书籍,都是大同小异,在当当网.卓越网搜索一下很多推荐.如果感觉学的差不多了,可以关注一下<CSS禅意花园>,这个很有影响力. Javasc ...
- Android为TV端助力 Intent匹配action,category和data原则
1.当你在androidmanifest里面定义了一个或多个action时 你使用隐式意图其他activity或者service时,规定你隐式里面的action必须匹配XML中定义的action,可以 ...