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 ...
随机推荐
- spring aop 的一个思考
问题: spring aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...
- 基于SSM的在线考试系统
本系统功能非常完善,页面美观大方,技术新颖,选用主流数据库Mysql,表数量及结构适当,如果你需要做在线考试或者其它考试类系统,这个系统将非常有用. 其实,任何考试系统,无非试题不一样,所以如果你是做 ...
- Invalid bound statement(not found):cn.e3mall.mapper.TbItemMapper.selectByExample.....
1.出现如下问题: 说明mapper接口和mapper.xml映射文件没有在一个文件夹下面 2.解决方法: 在dao层的pom.xml中配置一下: <!-- 如果不添加此节点mybatis的ma ...
- FreeSql (五)插入数据
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...
- hbase shell命令及Java接口介绍
一. shell命令 1. 进入hbase命令行 ./hbase shell 2. 显示hbase中的表 list3. 创建user表,包含info.data两个列族create 'user', ...
- Day 16 软件管理
1.RPM基本概述 1.什么是rpm? RPM全称 RPM Package Manager 缩写,由红帽开发用于软件包的安装升级卸载与查询 2.如何获取rpm包 在我们刚开始学习rpm包,建议先使用本 ...
- C. Anadi and Domino
题目链接:http://codeforces.com/contest/1230/problem/C C. Anadi and Domino time limit per test: 2 seconds ...
- vue把链接转二维码
使用qrcodejs2插件 1. 安装qrcodejs2:npm install qrcodejs2 --save 2. 在组件里面引入: import QRCode from 'qrcodejs2' ...
- Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码
在文章:Mybatis源码解析,一步一步从浅入深(一):创建准备工程,中我们为了解析mybatis源码创建了一个mybatis的简单工程(源码已上传github,链接在文章末尾),并实现了一个查询功能 ...
- net core WebApi——定时任务Quartz
目录 前言 Quartz 测试 问题及解决方法 小结 前言 本来打算昨天都开始写这篇,就因为要把小团队的博客整理汇总,一看二哈的博客那么多,一个个复制粘贴肯定麻烦(其实是我自己觉得复制麻烦),所以穿插 ...