hdu 6107--Typesetting(倍增)
2018-06-17 21:58:23来源:未知 阅读 ()
题目链接
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+5; int n,w,pw,dw; int a[N]; int lto[N],rto[N],to[N]; int s[N][20],t[N][20]; void cal(int *c,int W) { int len=-1; for(int i=0,j=0;i<=n;i++) { while(len+a[j]+1<=W) len+=a[j]+1,j++; c[i]=j; len-=a[i]+1; } } void process() { cal(lto,w); for(int i=0;i<=n;i++) s[i][0]=lto[i]; to[n]=0; for(int i=n-1;i>=0;i--) to[i]=to[lto[i]]+1; for(int i=1;i<18;i++) for(int j=0;j<=n;j++) s[j][i]=s[s[j][i-1]][i-1]; cal(lto,dw); cal(rto,w-dw-pw); for(int i=0;i<=n;i++) t[i][0]=rto[lto[i]]; for(int i=1;i<18;i++) for(int j=0;j<=n;j++) t[j][i]=t[t[j][i-1]][i-1]; } int main() { int T; cin>>T; while(T--) { scanf("%d%d%d%d",&n,&w,&pw,&dw); for(int i=0;i<n;i++) scanf("%d",&a[i]); a[n]=w+5; process(); int Q; scanf("%d",&Q); while(Q--) { int x,h,ans,tot=0; scanf("%d%d",&x,&h); ans=min(--x,to[0]); for(int i=0,tmp=ans;i<18;i++) { if(tmp&1) tot=s[tot][i]; tmp>>=1; } for(int i=0,tmp=h;i<18;i++) { if(tmp&1) tot=t[tot][i]; tmp>>=1; } ans+=h; ans+=to[tot]; printf("%d\n",ans); } } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:lintcode.46 主元素
下一篇:lintcode.82 落单的数
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash