hdu 6107--Typesetting(倍增)
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.
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
#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(倍增)的更多相关文章
- 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- ...
- Typesetting HDU - 6107
Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains aia ...
- hdu 5726 GCD 倍增+ 二分
题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...
- HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...
- 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 ...
- hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...
- HDU - 6394 Tree(树分块+倍增)
http://acm.hdu.edu.cn/showproblem.php?pid=6394 题意 给出一棵树,然后每个节点有一个权值,代表这个点可以往上面跳多远,问最少需要多少次可以跳出这颗树 分析 ...
- hdu 6394 Tree (2018 Multi-University Training Contest 7 1009) (树分块+倍增)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=6394 思路:用dfs序处理下树,在用分块,我们只需要维护当前这个点要跳出这个块需要的步数和他跳出这个块去 ...
随机推荐
- Jenkins中的一些问题解决(~~不断更新~~)
请使用ctrl+F,查询页面中你需要查找的错误信息(注意空格的输入) 1.错误"error fetching remote repo origin" 本地有多个sshkey,导致构 ...
- 【SignalR学习系列】3. SignalR实时高刷新率程序
创建项目 创建一个空的 Web 项目,并在 Nuget 里面添加 SignalR,jQuery UI 包,添加以后项目里包含了 jQuery,jQuery.UI ,和 SignalR 的脚本. 创建基 ...
- [COGS 2051] 王者之剑
Saber大法吼 2051. 王者之剑 ★★★☆ 输入文件:Excalibur.in 输出文件:Excalibur.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述 ...
- 微信小程序开发-新闻列表之新闻列表绑定
微信小程序开发-新闻列表之新闻列表绑定开发教程: 1.效果图预览 2.准备工作 在拿到效果图后不要先急着去写代码,而是要去分析一下页面的整体结构,用什么方式定位和布局.小程序里建议使用flex布局,因 ...
- Canvas 实现灵动的红鲤鱼动画(上)
前言 上一篇文章<Canvas 仿百度贴吧客户端 loading 小球>实现了百度贴吧客户端的 loading 小球效果,同时还留下了一个任务:实现灵动的红鲤鱼动画. 这个动画效果实现起来 ...
- 有关cxf与安卓客户端或者C客户端的中文乱码问题
前段时间在与C的客户端和安卓的客户端进行联调,首先我的方法接收的是C客户端所传递的数据,但是传递到方法内的中文就变成了乱码(我的方法的编码是utf8),最后通过String a = new Strin ...
- iOS上机题(附个人见解)
##机试题目如下 用命令行创建一个以CocoaPods管理的项目[Test-你的姓名拼音],新建3个ViewController,完成以下题目 将下面的问题在一个UITabView里面列出所有问题,单 ...
- H5投放在朋友圈广告做压力测试
一.环境 MacOS Sierra 二.背景 朋友圈广告投放的H5需要做ab压测,这里不赘述. 具体官方文档如下:http://ad.weixin.qq.com/learn/n10 三.正文 (1)别 ...
- 万能头文件#include
#include<bits/stdc++.h>包含了目前c++所包含的所有头文件!!!! 测试结果POJ不支持HDU,NYOJ支持
- mybatis 详解(六)------通过mapper接口加载映射文件
通过 mapper 接口加载映射文件,这对于后面 ssm三大框架 的整合是非常重要的.那么什么是通过 mapper 接口加载映射文件呢? 我们首先看以前的做法,在全局配置文件 mybatis-conf ...