UVa 714 (二分) Copying Books
首先通过二分来确定这种最大值最小的问题。
假设每个区间的和的最大值为x,那么只要判断的时候只要贪心即可。
也就是如果和不超过x就一直往区间里放数,否则就开辟一个新的区间,这样来判断是否k个区间容得下这些数。
还有就是输出也挺麻烦的,借鉴了一下lrj的代码,感觉也是十分巧妙。
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int maxn = + ;
LL a[maxn];
bool last[maxn];
int n, k; bool check(LL x)
{
int cnt = ;
for(int i = , j; i < n; i = j)
{
if(cnt > k) return false;
LL s = ;
for(j = i; j < n; j++)
{
if(a[j] > x) return false;
if(s + a[j] <= x) s += a[j];
else break;
}
cnt++;
}
return true;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); int T; scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &k);
LL L = , R = ;
for(int i = ; i < n; i++) { scanf("%lld", &a[i]); R += a[i]; }
while(L < R)
{
LL mid = (L + R) / ;
if(check(mid)) R = mid;
else L = mid + ;
} memset(last, false, sizeof(last));
LL s = ; int r = k;
for(int i = n-; i >= ; i--)
{
if(s + a[i] > L || i + < r)
{
last[i] = true;
s = a[i];
r--;
}
else s += a[i];
}
for(int i = ; i < n-; i++)
{
printf("%lld ", a[i]);
if(last[i]) printf("/ ");
}
printf("%lld\n", a[n-]);
} return ;
}
代码君
UVa 714 (二分) Copying Books的更多相关文章
- 【uva 714】Copying Books(算法效率--二分+贪心)
题意:将1个含N个正整数的序列划分成K个连续的子序列,使每段的和的最大值尽量小,问字典序最小的划分方案. 解法:由于是连续的数的"最大值最小",便可想到二分每段的最大值,若这时可分 ...
- 【例题 8-10 UVA - 714】 Copying Books
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分最后的最大值的最小值. 得到ans 然后从后往前尽量划分. 如果发现不够分成k个. 那么就从第一个开始接着分restk个(每隔1 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- uva 714 Copying Books(二分法求最大值最小化)
题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...
- 抄书 Copying Books UVa 714
Copying Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...
- Copying Books
Copying Books 给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的 ...
- poj 1505 Copying Books
http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submiss ...
随机推荐
- git shell 中文
alias ls="ls --show-control-chars" alias ll="ls -l"
- javascript的Function 和其 Arguments
http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...
- iOS正则匹配手机号
#pragma 正则匹配手机号 + (BOOL)validateMobile:(NSString *)mobileNum { /** * 手机号码 * 移动:134[0-8 ...
- Freebie: Material Design UI Kit
点这里 Following the guidelines laid out by Google, this free UI kit has been designed so that you can ...
- POJ 2140
#include<iostream> #include<stdio.h> using namespace std; int main() { int num; int i; i ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- github研究
一个程序猿一定会用git,但是我还没怎么用过,平时真是懒啊,学习之!...
- JAVA IO 类库详解
JAVA IO类库详解 一.InputStream类 1.表示字节输入流的所有类的超类,是一个抽象类. 2.类的方法 方法 参数 功能详述 InputStream 构造方法 available 如果用 ...
- VS2013试用期结束后如何激活
在激活框中输入密钥:BWG7X-J98B3-W34RT-33B3R-JVYW9
- How to say "no"?
How to say "no"?7招教你如何拒绝别人 Do you have a hard time saying no to others? Do you say “y ...