HDU 6107 Typesetting
Problem Description
Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains ai characters.
The page width is fixed to W characters. In order to make the article look more beautiful, Yellowstar has made some rules:
- The fixed width of the picture is pw. The distance from the left side of the page to the left side of the photo fixed to dw, in other words, the left margin is dw, and the right margin is W - pw - dw.
- The photo and words can't overlap, but can exist in same line.
- The relative order of words cannot be changed.
- Individual words need to be placed in a line.
- If two words are placed in a continuous position on the same line, then there is a space between them.
- Minimize the number of rows occupied by the article according to the location and height of the image.
However, Yellowstar has not yet determined the location of the picture and the height of the picture, he would like to try Q different locations and different heights to get the best look. Yellowstar tries too many times, he wants to quickly know the number of rows each time, so he asked for your help. It should be noted that when a row contains characters or pictures, the line was considered to be occupied.
题目大意:
有N个单词,每一个单词长度为\(a_i\),现在存在一页纸,宽度为W,和一张需要放进纸中的照片,照片的宽度为pw,距离纸的左边x,距离右边W-pw-x,有Q组询问,(x,y)表示照片从第x行开始,长度为y,求把单词和照片都放进纸中,需要占用多少行
解题报告:
用时:1h,1WA
比较简单,直接预处理出:
\(f[i][j]\)表示在没有照片的行中,从第\(i\)个单词开始,占用\(2^j\)行可以放下的单词数
\(g[i][j]\)表示在有照片的行中,从第\(i\)个单词开始,占用\(2^j\)行可以放下的单词数
我们可以先用二分处理出\(f[i][0]\)和\(g[i][0]\)
显然:
\(f[i][j]=f[i][j-1]+f[i+f[i][j-1]][j-1]\)
\(g[i][j]=g[i][j-1]+g[i+g[i][j-1]][j-1]\)
最后再分照片前的部分,照片的部分,和照片后的三个部分处理即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1e5+5;
int n,m,W,pw,lw,rw,a[N],sum[N],f[N][20];
int g[N][20],maxlen;
int midit(int sta,int lim){
if(sta>n || !lim)return 0;
int l=sta,r=n,mid,ret=l-1;
while(l<=r){
mid=(l+r)>>1;
if(sum[mid]-sum[sta-1]+mid-sta<=lim)
ret=mid,l=mid+1;
else r=mid-1;
}
return ret-sta+1;
}
void prework(){
int x,y;
for(int i=1;i<=n;i++){
f[i][0]=midit(i,W);
x=midit(i,lw);y=midit(i+x,rw);
g[i][0]=x+y;
}
for(int j=1;j<=maxlen;j++){
for(int i=1;i<=n;i++){
if(i+f[i][j-1]<=n)
f[i][j]=f[i][j-1]+f[i+f[i][j-1]][j-1];
else f[i][j]=N;
if(i+g[i][j-1]<=n)
g[i][j]=g[i][j-1]+g[i+g[i][j-1]][j-1];
else g[i][j]=N;
if(i+f[i][j]-1>n)f[i][j]=N;
if(i+g[i][j]-1>n)g[i][j]=N;
}
}
}
int solve(int s,int d){
int res=n,x=1,ans=0,tot=s-1;
for(int i=maxlen;i>=0;i--){
if(tot>=(1<<i) && res>=f[x][i] && x<=n)
tot-=(1<<i),res-=f[x][i],x+=f[x][i],ans+=(1<<i);
}
int pre=ans+d;
if(!res)return pre;
tot=d;
for(int i=maxlen;i>=0;i--){
if(tot>=(1<<i) && res>=g[x][i] && x<=n)
tot-=(1<<i),res-=g[x][i],x+=g[x][i],ans+=(1<<i);
}
ans=Max(pre,ans);
if(!res)return ans;
for(int i=maxlen;i>=0;i--){
if(res>=f[x][i] && x<=n)
res-=f[x][i],x+=f[x][i],ans+=(1<<i);
}
return ans;
}
void work()
{
scanf("%d%d%d%d",&n,&W,&pw,&lw);
maxlen=log(n)/log(2)+1;rw=W-pw-lw;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
prework();
int Q,x,y;cin>>Q;
while(Q--){
scanf("%d%d",&x,&y);
printf("%d\n",solve(x,y));
}
}
int main()
{
int T;cin>>T;
while(T--)work();
return 0;
}
HDU 6107 Typesetting的更多相关文章
- HDU 6107 - Typesetting | 2017 Multi-University Training Contest 6
比赛的时候一直念叨链表怎么加速,比完赛吃饭路上突然想到倍增- - /* HDU 6107 - Typesetting [ 尺取法, 倍增 ] | 2017 Multi-University Train ...
- HDU 6107 Typesetting (倍增)
Typesetting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- Typesetting HDU - 6107
Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains aia ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- vim配置强悍来袭
vim 这个关键字,我不想再过多的解释,相信看到这里的同仁,对vim都有十七八分的理解,如果你还不知道vim是什么,自己找个黑屋子... 废话不多说,今天在这里主要说vim的,不带插件的配置,也就 ...
- 用python实现与小米网关通讯
python 与小米网关通讯的三块内容: 以下内容的理解需要配合<绿米网关局域网通讯协议>使用 1.监听网关发出的组播信息:(有网关及连接设备的生命信号,事件信息) 2.读取需要获得的信息 ...
- 分贝块---dBblock
分贝,用英语来表达的话,是decibel,是量度两个相同单位之数量比例的计量单位,主要用于度量声音强度,常用dB表示. 块,block,在百度百科中,指数据库中的最小存储和处理单位,包含块本身的头信息 ...
- HttpWebRequest,HttpWebResponse C# 代码调用webservice,参数为xml
先上调用代码 public static string PostMoths(string url, string Json) { System.Net.HttpWebRequest request; ...
- Python-函数-Day4
1.函数 1.1.集合 主要作用: 去重 关系测试, 交集\差集\并集\反向(对称)差集 a = {1,2,3,4} b ={3,4,5,6} a {1, 2, 3, 4} type(a) <c ...
- kubernetes入门(08)kubernetes单机版的安装和使用
kubectl get - 类似于 docker ps ,查询资源列表 kubectl describe - 类似于 docker inspect ,获取资源的详细信息 kubectl logs - ...
- maven入门(8)maven的依赖管理
我们项目中用到的jar包可以通过依赖的方式引入,构建项目的时候从Maven仓库下载即可. 1. 依赖配置 依赖可以声明如下: <project> ... <dependenci ...
- python常用运算符
1. / 浮点除法,就算分子分母都是int类型,也返回float类型,比如我们用4/2,返回2.0 2. // 整数除法,根据分子分母的不同组合,返回的值有差异. 正数//正数,取整,比如5//3,返 ...
- 集智robot微信公众号开发笔记
开发流程 公众号基本配置(首先得有公众平台账号) 在开发菜单的基本配置中填写好基本配置项 首先配置服务器地址.Token.和消息加密密钥(地址为开发者为微信验证留的接口.token可以随便填写,只要在 ...
- linux ubunt 安装软件的前期准备——更新源的更换
如果是高手,请翻到页面最下方,更换更新源的总结,直接操作即可 可能会优点啰嗦,但是认真看,一定能解决问题~~希望对大家有帮助~ 最近在熟悉linux环境,自己安装了一个ubuntu虚拟机. 很多朋友问 ...