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 ...
随机推荐
- NGUI_基础入门学习
目录 1. Control Widgets 控制部件2. Anchors 锚3. Interaction 交互4. Menu 菜单5. Controller Input 控制器的输入6. Lights ...
- StringBulider类
StringBulider类创建的字符串同样可以对字符串进行修改: public class StringBuliderDemo { public static void main(String[] ...
- 实现一个正则表达式引擎in Python(三)
项目地址:Regex in Python 前两篇已经完成的写了一个基于NFA的正则表达式引擎了,下面要做的就是更近一步,把NFA转换为DFA,并对DFA最小化 DFA的定义 对于NFA转换为DFA的算 ...
- 单线程Redis性能为何如此之高?
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 实际项目开发中现在无法逃避的一个问题就是缓存问题,而缓存问题也是面试必问知识点之一,如果面试官好一点可能会简单 ...
- Java匹马行天下之C国程序员的秃头原因
Java帝国的崛起 前言: 分享技术之前先请允许我分享一下黄永玉老先生说过的话:“明确的爱,直接的厌恶,真诚的喜欢.站在太阳下的坦荡,大声无愧地称赞自己.” <编程常识知多少> <走 ...
- 【linux】【FastDFS】FastDFS数据迁移
后来同步的时候发现有的没有同步过来,应该是没有同步完成我就停止服务了. 最后尝试直接把fastdfs storage的data文件迁移过去即可. 1.在新的storage server服务器上停止所有 ...
- 【linux】【FastDFS】FastDFS安装
前言 FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存 ...
- 【linux】【mysql】mysql主从数据库
系统环境:Centos7 主:192.168.8.162 从:192.168.8.127 前提条件 a.关闭防火墙 systemctl stop firewalld 关闭防火墙开机自启 system ...
- centos下U盘重装windows
当前机器使用的都是centos系统,后来想使用windows,这时候就需要在centos下去安装windows系统啦. 当然是采用U盘安装啦,先准备U盘,U盘数据先备份以免丢失. 第一步:制作U盘启动 ...
- tomcat下的路径问题
在tomcat下 如果是根据类装载器获得某个需要修改的文件路径 就有可能在web项目部署的时候存在问题 比如这里有一个测试 package Junit.test; public class test ...