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:

  1. 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.
  2. The photo and words can't overlap, but can exist in same line.
  3. The relative order of words cannot be changed.
  4. Individual words need to be placed in a line.
  5. If two words are placed in a continuous position on the same line, then there is a space between them.
  6. 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的更多相关文章

  1. HDU 6107 - Typesetting | 2017 Multi-University Training Contest 6

    比赛的时候一直念叨链表怎么加速,比完赛吃饭路上突然想到倍增- - /* HDU 6107 - Typesetting [ 尺取法, 倍增 ] | 2017 Multi-University Train ...

  2. HDU 6107 Typesetting (倍增)

    Typesetting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. Typesetting HDU - 6107

    Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains aia ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. python中functools.singledispatch的使用

    from functools import singledispatch @singledispatch def show(obj): print (obj, type(obj), "obj ...

  2. python每日一函数 - divmod数字处理函数

    python每日一函数 - divmod数字处理函数 divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: ...

  3. python实现简单tftp(基于udp)

    tftp是基于udp的协议 实现简单的tftp,首先要有tftp的协议图. tftp默认接收端口为69,但每次有连接过来后,tftp会随机分配一个端口来专门为这个连接来服务. 操作码:1.上传 2.下 ...

  4. 《高级软件测试》11.14.安装和运行Jira

    今日任务完成情况如下: 小段:研究Jira在Linux的安装教程 小费:尝试在Ubuntu下安装Jira 小高:查阅了关于Jira软件的介绍和安装教程,下载准备明天安装,并学习使用 小王:注册Jira ...

  5. poj2029 Get Many Persimmon Trees

    http://poj.org/problem?id=2029 单点修改 矩阵查询 二维线段树 #include<cstdio> #include<cstring> #inclu ...

  6. linux下安装配置jdk(解压版)

    在linux下登录oracle官网,下载解压版jdk    传送门 系统默认下载到"下载"目录中 创建要将该文件解压的文件夹: 其中 -p 参数代表递归创建文件夹(可以创建多级目录 ...

  7. Mego开发文档 - 索引

    Mego 开发文档 Mego 快速概述 主要特性 获取Mego 使用流程 模型 查询 保存数据 入门 Mego 快速开始 创建项目 安装Nuget包 创建连接字符串 创建模型及数据上下文(添加引用) ...

  8. http缓存浅谈

    我们在访问百度首页的时候,会发现不管怎么刷新页面,静态资源基本都是返回 200(from cache): 随便点开一个静态资源是酱的: 哎哟有Response报头数据呢,看来服务器也正常返回了etag ...

  9. Spring Security 入门(1-9)国际化的使用

  10. HTML5的常用新特性你必须知道

    HTML5的常用新特性你必须知道 1 新的 声明 HTML 有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 HTML 页面.这就是 的用处. 不是 HTML ...