Typesetting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 144    Accepted Submission(s): 72

Problem Description
Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains ai characters.
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.

 
Input
The 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 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
Output
For 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
【题意】一张纸上写了些单词,每个单词处于同一行,不被分开,且两个单词之间有一个空格或换行。现在要在照片中 插入一张宽度固定的图片。Q次询问,问你当照片插进单词中,单词总共占据多少行。
【分析】先对于三种宽度w,dw,w-dw-pw,预处理 当第i个单词放在当前宽度最左端时,下一行第一个单词是哪个,h[i]表示第i个单词放在首端时剩下的需要占据多少行。然后倍增,s[i][j]表示在没有图片区域第i个单词放在最左端时,往下2^j行的第一个单词是哪个,然后t数组表示插入单词的区域。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = 1e9+;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,w,q,pw,dw;
int a[N],lto[N],rto[N];
int h[N],s[N][M],t[N][M];
void solve(int len,int to[]){
for(int i=,j=,x=-;i<=n;i++){
while(x+a[j]+<=len)x+=a[j++]+;
to[i]=j;x-=a[i]+;
}
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&w,&pw,&dw);
rep(i,,n-)scanf("%d",&a[i]);
a[n]=w+;
solve(w,lto);
rep(i,,n)s[i][]=lto[i];
rep(j,,M-)rep(i,,n)s[i][j]=s[s[i][j-]][j-];
h[n]=;
for(int i=n-;i>=;i--)h[i]=h[lto[i]]+;
solve(dw,lto);solve(w-dw-pw,rto);
rep(i,,n)t[i][]=rto[lto[i]];
rep(j,,M-)rep(i,,n)t[i][j]=t[t[i][j-]][j-];
scanf("%d",&q);
while(q--){
int x,hh,res=;
scanf("%d%d",&x,&hh);
int ans=;
ans+=min(--x,h[]);
rep(j,,M-)if(x>>j & )res=s[res][j];
rep(j,,M-)if(hh>>j & )res=t[res][j];
ans+=hh+h[res];
printf("%d\n",ans);
}
}
return ;
}

HDU 6107 Typesetting (倍增)的更多相关文章

  1. HDU 6107 - Typesetting | 2017 Multi-University Training Contest 6

    比赛的时候一直念叨链表怎么加速,比完赛吃饭路上突然想到倍增- - /* HDU 6107 - Typesetting [ 尺取法, 倍增 ] | 2017 Multi-University Train ...

  2. HDU 6107 Typesetting

    Problem Description Yellowstar is writing an article that contains N words and 1 picture, and the i- ...

  3. Typesetting HDU - 6107

    Yellowstar is writing an article that contains N words and 1 picture, and the i-th word contains aia ...

  4. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...

  5. HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...

  6. 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 ...

  7. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  8. HDU - 6394 Tree(树分块+倍增)

    http://acm.hdu.edu.cn/showproblem.php?pid=6394 题意 给出一棵树,然后每个节点有一个权值,代表这个点可以往上面跳多远,问最少需要多少次可以跳出这颗树 分析 ...

  9. hdu 6394 Tree (2018 Multi-University Training Contest 7 1009) (树分块+倍增)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6394 思路:用dfs序处理下树,在用分块,我们只需要维护当前这个点要跳出这个块需要的步数和他跳出这个块去 ...

随机推荐

  1. 元类编程-- metaclass

    #类也是对象,type创建类的类 def create_class(name): if name == "user": class User: def __str__(self): ...

  2. Robot Framework Chrome

    1. 下载对应版本的chromedriver, 好像都是windows32位的,不过没关系,可以用即可. 2. 将chromedriver放入到chrome的安装路径下,然后将chromrdriver ...

  3. Item 1----------考虑用静态工厂方法代替构造器

    读书,有时候,我感觉总是有点绕和不具体.我阅读了代码,理解代码后,才有一种理解和把握的感觉. 优点三.   把某个对象的构建放给客户端来实现. 比如下面的实现,客户端Test,获取Service的实例 ...

  4. hdu 1253 胜利大逃亡(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目大意:在所给的时间能顺利离开城堡. #include <iostream> #i ...

  5. linux加载指定目录的so文件

    linux加载指定目录的so文件 http://blog.csdn.net/win_lin/article/details/8286125 download urlhttp://download.ch ...

  6. JAVA常见的集合类

    关系的介绍: Set(集):集合中的元素不按特定方式排序,并且没有重复对象.他的有些实现类能对集合中的对象按特定方式排序. List(列表):集合中的元素按索引位置排序,可以有重复对象,允许按照对象在 ...

  7. JavaScript中数组对象详解

    Array对象即数组对象用于在单个变量中存储多个值,JS的数组是弱类型的,所以允许数组中含有不同类型的元素,数组元素甚至可以是对象或者其他数组. 创建数组的语法 1.Array构造器 1.var li ...

  8. 使用IDEA从github中下载fastdfs-client-java

    由于在pom文件中加入依赖坐标无法将fastdfs-client-java下载下来,后来通过查资料,发现在中央仓库中没有定义该坐标.为此,使用idea从github下载fastdfs-client-j ...

  9. 查看linux 下进程运行时间(转)

    原文地址:http://blog.csdn.net/tspangle/article/details/11731317 可通过ps 来查看,通过参数 -o 来查看 如: ps -eo pid,tty, ...

  10. Cent os FTP配置

    原文:http://www.aicoffees.com/itshare/412261137.html