UVA - 714 Copying Books (抄书)(二分+贪心)
题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列)。设第i个序列的各数之和为S(i),你的任务是让所有的S(i)的最大值尽量小。如果有多解,S(1)应尽量小,如果仍有多解,S(2)应尽量小,依此类推。
分析:
1、二分最小值x。
2、判断当前x是否满足条件时,从右往左尽量划分,若cnt<k,则从0开始依次标为分界点,这样可满足S(1),S(2),……,尽量小。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 500 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL a[MAXN];
int m, k;
int cnt;
int vis[MAXN];//分界点
bool judge(LL x){
memset(vis, 0, sizeof vis);
cnt = 0;
int pos = m - 1;
while(pos >= 0){
int num = 0;//当前序列中的元素
LL sum = 0;
while(pos >= 0 && sum + a[pos] <= x){
sum += a[pos];
++num;
--pos;
}
if(!num) return false;//若当前序列中没有元素,说明x太小
if(pos >= 0) vis[pos] = 1;
++cnt;
}
if(cnt > k) return false;
return true;
}
void solve(){
LL l = 0, r = 1e15;
while(l < r){
LL mid = l + (r - l) / 2;
if(judge(mid)) r = mid;
else l = mid + 1;
}
if(judge(r)){
for(int i = 0; i < m - 1 && cnt < k; ++i){
if(!vis[i]){
vis[i] = 1;
++cnt;
}
}
for(int i = 0; i < m; ++i){
if(i) printf(" ");
printf("%lld", a[i]);
if(vis[i]) printf(" /");
}
printf("\n");
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &m, &k);
for(int i = 0; i < m; ++i){
scanf("%lld", &a[i]);
}
solve();
}
return 0;
}
UVA - 714 Copying Books (抄书)(二分+贪心)的更多相关文章
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 714 Copying Books 抄书 (二分)
题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...
- 【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents h ...
- uva 714 Copying Books(二分法求最大值最小化)
题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...
- 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 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- uva 714 - Copying Books(贪心 最大值最小化 二分)
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...
- UVa 714 Copying books 贪心+二分 最大值最小化
题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...
- UVa 714 Copying Books - 二分答案
求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...
随机推荐
- kd-tree理论以及在PCL 中的代码的实现(转载)
该文转自:https://www.cnblogs.com/li-yao7758258/p/6437440.html kd-tree理论以及在PCL 中的代码的实现 (小技巧记录:博客园编辑的网页界 ...
- LIS问题
LIS定义LIS(Longest Increasing Subsequence)最长上升子序列 .一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的. ...
- iplimage 转HBITMAP
HBITMAP IplImage2hBitmap(IplImage* pImg) { cvFlip(pImg, NULL); BYTE tmp[]; BITMAPINFO *bmi = (BITMAP ...
- mysql5.7修改账户密码
一.首次登录时,修改root账户的密码: vim /etc/my.cnf 在末尾添加 skip-grant-tables ,保存. service mysqld restart 再次登录时,不需要密码 ...
- idea 将部分class文件打包成jar使用
工作中有时候有太多模块堆放一块比较混乱,将某个功能(例如:三方支付)所需要的模块打包成jar使用起来会方便点. 步骤如下: 选择 Empty,然后为自己打的jar起个名字 然后在myjar上面右键 创 ...
- wdcp升级php5.8到php7.1.12后安装redis
一.安装redis a.下载redis: redis最新稳定版下载http://www.redis.io/download wget http://download.redis.io/releases ...
- 002、创建第一个Java程序HelloWord
代码如下: package TIANPAN; public class TestDemo { public static void main(String args[]) { System.out.p ...
- [题解] LuoguP2257 YY的GCD
传送门 给\(n,m\),让你求 \[ \sum\limits_{i=1}^n \sum\limits_{j=1}^m [\gcd(i,j) \in prime] \] 有\(T\)组询问\((T \ ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-time
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- P1048 数字加密
P1048 数字加密 转跳点: