题目链接

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.

 
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with four integers N, W, pw, dw : the number of words, page width, picture width and left margin.
The next line contains N integers ai, indicates i-th word consists of ai characters.
The third line contains one integer Q. 
Then Q lines follow, each line contains the values of xi and hi, indicates the starting line and the image height of the image.

Limits
T≤10
1≤N,W,Q≤105
1≤pw,ai≤W
0≤dw≤W−pw

 
Output
For each query, output one integer denotes the minimum number of rows.
 
Sample Input
2
2 7 4 3
1 3
3
1 2
2 2
5 2
3 8 2 3
1 1 3
1
1 1
 
Sample Output
2
3
3
1
 
 
题意:现在有一篇文章由 n 个单词构成,其中要放入一张图片,单词与单词之间要空一格,图片上不能放单词,单词之间的顺序不能变化,现在规定图片宽为pw,文章宽为w,图片左边距文章左边线宽为dw,现在Q次询问,每次给定图片高h和图片在文章中的起始位置(距离文章上边界)x,求最少多少行能放下图片与单词?
 
思路:先计算出固定宽为w、dw、w-dw-pw时以每个单词开头时下一行第一个单词的下标,根据dw和w-dw-pw可以计算出有图片占据行时以每个单词开始时的下一行第一个单词的下标,然后根据已求出的有图片和无图片占据行时以每个单词开始的下一行第一个单词的下标信息,进行倍增求出以每个单词开始时,向下2^i行的第一个单词的下标。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1e5+;
int n,w,pw,dw;
int a[N];
int lto[N],rto[N],to[N];
int s[N][],t[N][]; void cal(int *c,int W)
{
int len=-;
for(int i=,j=;i<=n;i++)
{
while(len+a[j]+<=W) len+=a[j]+,j++;
c[i]=j;
len-=a[i]+;
}
}
void process()
{
cal(lto,w);
for(int i=;i<=n;i++) s[i][]=lto[i];
to[n]=;
for(int i=n-;i>=;i--) to[i]=to[lto[i]]+;
for(int i=;i<;i++)
for(int j=;j<=n;j++)
s[j][i]=s[s[j][i-]][i-];
cal(lto,dw);
cal(rto,w-dw-pw);
for(int i=;i<=n;i++) t[i][]=rto[lto[i]];
for(int i=;i<;i++)
for(int j=;j<=n;j++)
t[j][i]=t[t[j][i-]][i-];
}
int main()
{
int T; cin>>T;
while(T--)
{
scanf("%d%d%d%d",&n,&w,&pw,&dw);
for(int i=;i<n;i++) scanf("%d",&a[i]);
a[n]=w+;
process();
int Q; scanf("%d",&Q);
while(Q--)
{
int x,h,ans,tot=; scanf("%d%d",&x,&h);
ans=min(--x,to[]);
for(int i=,tmp=ans;i<;i++)
{
if(tmp&) tot=s[tot][i];
tmp>>=;
}
for(int i=,tmp=h;i<;i++)
{
if(tmp&) tot=t[tot][i];
tmp>>=;
}
ans+=h;
ans+=to[tot];
printf("%d\n",ans);
}
}
return ;
}
 
 

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. HDU 6107 Typesetting

    Problem Description Yellowstar is writing an article that contains N words and 1 picture, and the i- ...

  4. Typesetting HDU - 6107

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

  5. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...

  6. HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...

  7. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  9. HDU - 6394 Tree(树分块+倍增)

    http://acm.hdu.edu.cn/showproblem.php?pid=6394 题意 给出一棵树,然后每个节点有一个权值,代表这个点可以往上面跳多远,问最少需要多少次可以跳出这颗树 分析 ...

  10. hdu 6394 Tree (2018 Multi-University Training Contest 7 1009) (树分块+倍增)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6394 思路:用dfs序处理下树,在用分块,我们只需要维护当前这个点要跳出这个块需要的步数和他跳出这个块去 ...

随机推荐

  1. rsync工作机制(翻译)

    本篇为rsync官方推荐文章How Rsync Works的翻译,主要内容是Rsync术语说明和简单版的rsync工作原理.本篇没有通篇都进行翻译,前言直接跳过了,但为了文章的完整性,前言部分的原文还 ...

  2. Linux常用命令和常见问题解决<------>第一章

    查看文件下面所有的隐藏目录:ls -al ~ ls -al ~ls -a -l ~可以发现三条命令执行结果是一致的,原因:因为ls为命令 后面的参数要以空格来区分,不论几个空格 shell都会视为一体 ...

  3. vue项目优化之按需加载组件-使用webpack require.ensure

    require-ensure和require-amd的区别: require-amd 说明: 同AMD规范的require函数,使用时传递一个模块数组和回调函数,模块都被下载下来且都被执行后才执行回调 ...

  4. ASP.NET在母版页或内容页上获取控件ID

    原本想给一个button添加一个confirm,不同的分数提示不同的信息(大于80合格,小于80不合格,提示是否提交),最开始用了button.Atribute.Add();但是它每次获取到的是lab ...

  5. Git时光机穿梭之版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  6. Zabbix监控nginx性能

    编辑nginx的配置文件nging #配置ngx_status location /nginx_status{ stub_status on; access_log off; } #重启nginx # ...

  7. 三菱Q系列PLC基本指令讲解

    1.数据传送指令MOV和MOVP,格式为 MOV    SRC1    DES1     表示条件接通,将SRC1的值传送到DES1寄存器中,带P的表示只在条件接通的上升沿指令执行一个扫描周期,不带P ...

  8. [stm32F429-DISCO-HAL] 1.先说说关于stm32Cube的一些事情。然后,Start with it...

    目前,我觉得STM32CUBE最大的方便在于,可以使用STM32CubeMX软件来图形化配置外设.首先贴出官网的PDF,Getting started with STM32CubeF4 firmwar ...

  9. python_求相邻数

    什么是相邻数? 比如5,相邻数为4和6,和5相差1的数,连续相差为1的一组数 需求: 遍历inputList 所有数字,取出所有数字,判断是否有相邻数, 不相邻数字 和 相邻数字 都以 “数组”形式 ...

  10. shell运算符之 关系运算符,算数运算符,布尔运算符,字符串运算符和文件测试运算符

    shell运算符有很多,关系运算符,算数运算符,布尔运算符,字符串运算符和文件测试运算符 1,算术运算符 原声bash 不支持简单的算术运算,可以使用expr 工具 两点注意: 表达式和运算符之间要有 ...