Typesetting HDU - 6107
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.
InputThe 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 aiai, indicates i-th word consists of aiai 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≤10T≤10
1≤N,W,Q≤1051≤N,W,Q≤105
1≤pw,ai≤W1≤pw,ai≤W
0≤dw≤W−pw0≤dw≤W−pwOutputFor 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 题解:
这个题目,的确,第一眼看上去是在倍增什么呢?可以倍增字节,但是复杂度还是不对,那么还有什么量可以倍增呢?就只剩下行数了,好久没有写倍增了,什么边界处理什么的都忘了,所以讲一下实现吧。
dp[i][j]表示在没有图的情况下以第i个单词为开头,覆盖2^j次方单词包括i在内需要几个,这个初始状态就是对于每个单词,暴力匹配他还需要多少单词,付给dp[i][0],然后有一个显然的状态转移dp[i][j]=dp[i][j-1]+dp[i+dp[i][j-1]][j-1];
这里处理一下边界,如果dp[i][j-1]||dp[i+dp[i][j-1]][j-1]==0就不处理,保证之后单词数不够的情况下,dp[i][j]=0。
然后再次设一个状态dp2[i][j]表示在有图的情况下以第i个单词为开头,覆盖2^j次方单词包括i在内需要几个,初始值和状态转移都是一样的。求的时候因为不可能超过20,就从20for到0,能跳就跳就可以了。
具体就是没到图的开头时用dp1跳到图的开头,然后用dp2,跳过图中,最后如果还剩下单词就继续用dp跳,有细节,推荐看一下代码。 代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 101000
using namespace std;
int dp[MAXN][],dp2[MAXN][];
int hi[MAXN],star[MAXN];
int a[MAXN];
int n,w,pw,dw,q; void init(){
scanf("%d%d%d%d",&n,&w,&pw,&dw);
memset(a,,sizeof(a));
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&q);
memset(star,,sizeof(star));
memset(hi,,sizeof(hi));
for(int i=;i<=q;i++) scanf("%d%d",&star[i],&hi[i]);
} void pre(){
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
int sum=a[i],j=i+;
while(sum+a[j]+<=w&&j<=n) sum+=a[j++]+;
dp[i][]=j-i;
}
for(int j=;j<=;j++)
for(int i=;i<=n;i++){
if(!dp[i][j-]||!dp[i+dp[i][j-]][j-]) continue;
dp[i][j]=dp[i][j-]+dp[i+dp[i][j-]][j-];
}
memset(dp2,,sizeof(dp2));
for(int i=;i<=n;i++){
int j=i,flag=,sum=;
while(sum+a[j]+flag<=dw&&j<=n) sum+=a[j++]+flag,flag=;
int k=j;
sum=,flag=;
while(sum+a[k]+flag<=w-pw-dw&&k<=n) sum+=a[k++]+flag,flag=;
dp2[i][]=k-i;
}
for(int j=;j<=;j++)
for(int i=;i<=n;i++){
if(!dp2[i][j-]||!dp2[i+dp2[i][j-]][j-]) continue;
dp2[i][j]=dp2[i][j-]+dp2[i+dp2[i][j-]][j-];
}
} int jump1(int now){
int ans=;
for(int j=;j>=;j--){
if(!dp[now][j]) continue;
if(now+dp[now][j]-<=n){
now+=dp[now][j];
ans+=<<j;
}
}
return ans;
} int jump2(int now,int cen){
for(int j=;j>=;j--){
if(!dp[now][j]) continue;
if((<<j)<=cen){
cen-=(<<j);
now+=dp[now][j];
}
}
return now;
} int jump3(int now,int cen){
for(int j=;j>=;j--){
if(!dp2[now][j]) continue;
if((<<j)<=cen){
cen-=(<<j);
now+=dp2[now][j];
}
}
return now;
} void work(){
for(int i=;i<=q;i++){
int x=star[i],h=hi[i],tmp=jump1();
if(tmp<=x-){
printf("%d\n",tmp+h);
continue;
}
int ans=x+h-,now=jump2(,x-);
now=jump3(now,h);
if(now<=n) ans+=jump1(now);
printf("%d\n",ans);
}
} int main()
{
int t;cin>>t;
while(t--){
init();
pre();
work();
}
return ;
}
Typesetting HDU - 6107的更多相关文章
- 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 ...
- HDU 6107 Typesetting
Problem Description Yellowstar is writing an article that contains N words and 1 picture, and the i- ...
- 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 ...
随机推荐
- python读取大文件只能读取部分的问题
最近准备重新研究一下推荐系统的东西,用到的数据集是Audioscrobbler音乐数据集.我用python处理数据集中artist_data.txt这个文件的时候,先读取每一行然后进行处理: with ...
- CVE-2014-6271 Shellshock 破壳漏洞 复现
补坑. 什么是shellshock ShellShock是一个BashShell漏洞(据说不仅仅是Bash,其他shell也可能有这个漏洞). 一般情况来说,系统里面的Shell是有严格的权限控制的, ...
- IDEA中创建maven web项目
本文将带你一路从IDEA中maven的配置到创建maven web项目,掌握IDEA中maven的使用. 一.IDEA中配置maven 开发中一般我们使用自己下载的maven,不使用IDEA工具自带的 ...
- SpringBoot自定义过滤器的两种方式及过滤器执行顺序
第一种 @WebFilter + @ServletComponentScan 注解 1.首先自定义过滤器 如下自定义过滤器 ReqResFilter 必须实现 javax.servlet.Filte ...
- Java的初始化块及执行过程详解
问题:Java对象初始化方式主要有哪几种?分别是什么?针对上面的问题,想必大家脑海中首先浮现出的答案是构造器,没错,构造器是Java中常用的对象初始化方式. 还有一种与构造器作用非常相似的是初始化块, ...
- centos 7 ifconfig无法找到命令的方法
场景:新安装centos 没有安装. centos7.2的mini版没有安装这个东东,所以我们就直接安装就好了,在终端里面输入: yum -y install net-tools
- 利用maven命令将外部jar包导进maven仓库
命令如下:mvn install:install-file -DgroupId=com.zebra -DartifactId=ZSDK_API -Dversion=v2.12.3782 -Dpacka ...
- Java8之熟透Optional
一.使用Optional引言 1.1.代码问题引出 在写程序的时候一般都遇到过 NullPointerException,所以经常会对程序进行非空的判断: User user = getUserByI ...
- 增删改查——PreparedStatement接口
1.添加 package pers.Pre.add; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...
- HTML-css样式引用方式
1.使用行内样式表 语法:在标签内部写入一个style属性. 优点:没有样式表文件,在某些时候可以提高效率: 优先级高. 缺点:多个页面难以共享样式,不利于代码复用: HTML和CSS代码混杂,不利于 ...