uva 714 Copying Books (二分)
来源:互联网 发布:linux 文件夹删除 编辑:程序博客网 时间:2024/05/17 02:18
uva 714 Copying Books
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
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,
Output
For each case, print exactly one line. The line must contain the input succession
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个数字,将其分成M段,要求在N个数字中加入M-1个分隔符,使得每一段的数的和 中的最大值最小。
解题思路:最大值的最小化用二分。先用二分法找到,最小的最大值,然后通过这个最大值去找到分隔符的位置。
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>typedef long long ll;using namespace std;ll n, m, num[505], rec[505], sum;int check(ll x) { ll s = 0; int cnt = 0; for (int i = 0; i < n; i++) { s += num[i]; if (num[i] > x) return 0; if (s > x) { s = num[i]; cnt++; } } if (cnt >= m) { return 0; } return 1;}int main() { int T; sum = 0; scanf("%d", &T); while (T--) { memset(rec, 0, sizeof(rec)); scanf("%lld %lld", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld", &num[i]); sum += num[i]; } ll l, r, mid; l = 0, r = sum; while (l < r) { mid = (l + r) / 2; if (!check(mid)) l = mid + 1; else r = mid; } sum = 0; int cnt2 = 0; for (int i = n - 1; i >= 0; i--) { sum += num[i]; if (sum > l || m - cnt2 - 1 == i + 1) { sum = num[i]; rec[i] = 1; cnt2++; } } printf("%lld", num[0]); if (rec[0]) printf(" /"); for (int i = 1 ; i < n; i++) { printf(" %lld", num[i]); if (rec[i]) printf(" /"); } printf("\n"); } return 0;}
- uva 714 - Copying Books(二分+贪心)
- UVA - 714 Copying Books(二分+贪心)
- uva 714 Copying Books (二分)
- UVa 714 - Copying Books(二分查找)
- 二分,贪心(Copying Books,uva 714)
- UVA 714 Copying Books(二分+贪心)
- UVA 714 Copying Books (二分)
- 714 - Copying Books(二分)
- UVA 714 Copying Books(二分查找)(未a)
- UVa 714 Copying Books 二分 + 贪心 (最大值最小化问题)
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
- uva 714 - Copying Books(贪心 最大值最小化 二分)
- UVa 714 Copying Books(贪心 二分)
- UVA - 714 Copying Books 抄书 贪心+二分
- Copying Books +uva+二分+贪心
- Copying Books--(二分)
- Copying Books (二分)
- uva 714 Copying Books
- Android中通过Intent传递对象、集合问题
- 【入门】Android创建数据库
- 2015-03-09---原型模式
- compile qemu for windows
- TVS学习
- uva 714 Copying Books (二分)
- JSP内置对象_1
- 游戏人物跟随鼠标移动的实现
- Android基础笔记(五)-网络编程
- 最小表示法
- java基础巩固之IO(三)
- 找球号(一)
- 【Plan 第五场】
- 好吧,十年之后