Copying Books
给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的序列的字典序最小),\(m,k\leq 500\)。
解
显然最大值的最小值想到二分,其实dp也可以,因为区间划分问题有可递推性,而且它能求出答案。
二分一个东西就等于换时间复杂度增加个\(log(n)\)增加了一个已知条件,虽然前提是单调性,但不妨以后缺少条件,先考虑二分再找单调性。
我们二分\(x\)表示区间和的最大值不超过\(x\),于是二分以后再考虑贪心(说是在的,二分后面的check要么是贪心要么是dp),那么从左往右扫描,处理出一个区间时,不停地在它的右边增加数字,当刚好要超过的时候,就把该个数字划分到下一个区间,显然这样我们得到了一个数字,意思即满足当前条件下最少的区间数。
其实可以看成一个函数\(f(x)\),显然随着x的增大,最少区间数会减少,而x又恰恰对应一个范围\([f(x),n]\),即区间数的范围。
因此当k在这个范围外的时候,显然\(f(x)\)需要减少,即x增加,反之(应该写的是\(lower\_bound\)二分),于是我们就得到了一个最小的x,正好框住k,显然当取比x大的值,同样满足条件,但是结果不优秀,取比x小的又不满足条件,于是x就是答案的"前提"。
于是按照之前的贪心方法,可以得到一个方案,字典序最小,只要从右往左贪心即可,但它是最少的区间数的方案,于是还得随便砍掉一些区间,显然可以随便砍,又要保证字典序最小,于是从左往右扫描,能砍就砍,最终时间复杂度O(mlog(m))。
参考代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define Size 550
using namespace std;
bool b[Size];
int a[Size],m;
il void read(int&);
il int fen(int);
int main(){
//freopen("in","r",stdin);
int lsy;read(lsy);
while(lsy--){
memset(b,0,sizeof(b));
int k,l(0),r(0);read(m),read(k);
for(int i(1);i<=m;++i)
read(a[i]),r+=a[i],l=max(l,a[i]);
int mid;while(l<=r){
mid=l+r>>1;
if(fen(mid)>k)l=mid+1;
else r=mid-1;
}int tot(1);
for(int i(m),j(0);i;--i)
if(j+a[i]<=l)j+=a[i];
else j=a[i],b[i]=1,++tot;
tot=k-tot;
for(int i(1);i<=m;++i)
if(tot&&!b[i])b[i]=1,--tot;
for(int i(1);i<=m;++i){
printf("%d ",a[i]);
if(b[i])printf("/ ");
}
putchar('\n');
}
return 0;
}
il int fen(int x){int ans(1);
for(int i(1),j(0);i<=m;++i)
if(j+a[i]<=x)j+=a[i];
else j=a[i],++ans;
return ans;
}
il void read(int &x){
x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}
Copying Books的更多相关文章
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- 抄书 Copying Books UVa 714
Copying Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- poj 1505 Copying Books
http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submiss ...
- 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 ...
- POJ1505&&UVa714 Copying Books(DP)
Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...
- 【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 ...
- 高效算法——B 抄书 copying books,uva714
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Description ...
随机推荐
- org.apache.http.client.HttpClient使用方法
一.org.apache.commons.httpclient和org.apache.http.client区别(转) 官网说明: http://hc.apache.org/httpclient- ...
- ICU lirary DownLoad
{ //https://github.com/unicode-org/icu }
- linux-usb软件系统架构
1.软件系统架构 USB主控制器,芯片里面自带了得.为了让USB主控制器运行,所有有USB主控制器驱动. USB核心,内核提供好的USB协议之类的.USB设备驱动是针对插到接口的设备去工作的软件. 主 ...
- centos7 安装VMware tools 出现The path "" is not a valid path to the 3.10.0-514.el7.x86_64 kernel headers
执行:yum install "kernel-devel-uname-r == $(uname -r)"
- 判断系统是否安装了flash插件
方法1: uses comobj; procedure TForm1.Button1Click(Sender: TObject); var v:variant; begin v:=CreateOleO ...
- 【Java架构:基础技术】一篇文章搞掂:Linux
基于CentOS 一.安装[暂略] 二.使用和登录[赞略] 三.使用yum CentOS自带yum,这里暂时不介绍安装方式 四.使用yum安装JDK 1.检查系统是否有安装open-jdk rpm - ...
- 前端工程师技能图谱skill-map
# 前端工程师技能图谱 ## 浏览器 - IE6/7/8/9/10/11 (Trident) - Firefox (Gecko) - Chrome/Chromium (Blink) - Safari ...
- oracle 中和mysql的group_concat有同样作用的写法
所有版本的oracle都可以使用select wm_concat(name) as name from user;但如果是oracle11g,使用select listagg(name, ',') w ...
- 201⑨湘潭邀请赛 Chika and Friendly Pairs(HDU6534)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6534 题意: 给你一个数组,对于第i个数来说,如果存在一个位置j,使得j>i并且a[j]-k&l ...
- 在VMware下创建windows2008虚拟机
1.创建新的虚拟机 打开VMware软件,点击主页内创建新的虚拟机 2.进入新建虚拟机向导 点击典型,点击下一步 3.在下一步中单击稍后安装操作系统 点击下一步 4.选择操作系统类型 客户机操作系统选 ...