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 ...
随机推荐
- 3.python之文件操作
一.文件操作初识 f = open('文件路径', '编码方式', '操作方式') # 注意里面所有内容,需加引号 ” 打开一个文件需要知道的内容有: 文件路径:c:\文件.txt(绝对路径和相对路径 ...
- Day005_Linux基础之文件权限
test.sh 举例: [oldboy@luffy001 ~]$ ls -l test.sh -rw-r--r-- 1 oldboy ops 0 Nov 14 10:42 test.sh 该文件权 ...
- 【LeetCode】322-零钱兑换
题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...
- lambda表达式与匿名内部类与双冒号(::)
lambda表达式在只有一条代码时还可以引用其他方法或构造器并自动调用,可以省略参数传递,代码更加简洁,引用方法的语法需要使用::符号.lambda表达式提供了四种引用方法和构造器的方式: 引用对象的 ...
- Vue中的slot(占坑,预留位置)
Vue中的slot(占坑,预留位置) 子模板不使用slot 子模板使用slot 子模板使用使用name属性,且传递data 文件名:Slots.vue //slot组件 <template> ...
- 你以为反射真的无所不能?至少JDK8以后很强大
目录 反射操作方法 Spring的方法的优点 反射如何实现Spring的方法 Java字节码 高级反射注意点 javac的彩蛋 续点 每日一笑 上期答案 # 加入战队 微信公众号 之前我们已经介绍了J ...
- airflow + CeleryExecutor 环境搭建
airflow整合环境搭建 1. 整体结构 mysql -> 后端数据库 redis -> 用于broker CeleryExecutor -> 执行器 2. 环境安装 2.1,安装 ...
- js 手机号码正则表达式
var chenkPhone=/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/var username=$ ...
- ECharts使用总结归纳
UserNAME:你为什么写这篇文章? My:最近项目中有统计报表的需求,使用了ECharts,“度娘”过程中东查西找太麻烦,自己写一篇加深印象,方便以后查阅. 辅助文档------>ttps: ...
- SpringBoot整合redis缓存(一)
准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...