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.

题目大意:

有N个单词,每一个单词长度为\(a_i\),现在存在一页纸,宽度为W,和一张需要放进纸中的照片,照片的宽度为pw,距离纸的左边x,距离右边W-pw-x,有Q组询问,(x,y)表示照片从第x行开始,长度为y,求把单词和照片都放进纸中,需要占用多少行

解题报告:

用时:1h,1WA

比较简单,直接预处理出:

\(f[i][j]\)表示在没有照片的行中,从第\(i\)个单词开始,占用\(2^j\)行可以放下的单词数

\(g[i][j]\)表示在有照片的行中,从第\(i\)个单词开始,占用\(2^j\)行可以放下的单词数

我们可以先用二分处理出\(f[i][0]\)和\(g[i][0]\)

显然:

\(f[i][j]=f[i][j-1]+f[i+f[i][j-1]][j-1]\)

\(g[i][j]=g[i][j-1]+g[i+g[i][j-1]][j-1]\)

最后再分照片前的部分,照片的部分,和照片后的三个部分处理即可

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #define RG register
  8. #define il inline
  9. #define iter iterator
  10. #define Max(a,b) ((a)>(b)?(a):(b))
  11. #define Min(a,b) ((a)<(b)?(a):(b))
  12. using namespace std;
  13. const int N=1e5+5;
  14. int n,m,W,pw,lw,rw,a[N],sum[N],f[N][20];
  15. int g[N][20],maxlen;
  16. int midit(int sta,int lim){
  17. if(sta>n || !lim)return 0;
  18. int l=sta,r=n,mid,ret=l-1;
  19. while(l<=r){
  20. mid=(l+r)>>1;
  21. if(sum[mid]-sum[sta-1]+mid-sta<=lim)
  22. ret=mid,l=mid+1;
  23. else r=mid-1;
  24. }
  25. return ret-sta+1;
  26. }
  27. void prework(){
  28. int x,y;
  29. for(int i=1;i<=n;i++){
  30. f[i][0]=midit(i,W);
  31. x=midit(i,lw);y=midit(i+x,rw);
  32. g[i][0]=x+y;
  33. }
  34. for(int j=1;j<=maxlen;j++){
  35. for(int i=1;i<=n;i++){
  36. if(i+f[i][j-1]<=n)
  37. f[i][j]=f[i][j-1]+f[i+f[i][j-1]][j-1];
  38. else f[i][j]=N;
  39. if(i+g[i][j-1]<=n)
  40. g[i][j]=g[i][j-1]+g[i+g[i][j-1]][j-1];
  41. else g[i][j]=N;
  42. if(i+f[i][j]-1>n)f[i][j]=N;
  43. if(i+g[i][j]-1>n)g[i][j]=N;
  44. }
  45. }
  46. }
  47. int solve(int s,int d){
  48. int res=n,x=1,ans=0,tot=s-1;
  49. for(int i=maxlen;i>=0;i--){
  50. if(tot>=(1<<i) && res>=f[x][i] && x<=n)
  51. tot-=(1<<i),res-=f[x][i],x+=f[x][i],ans+=(1<<i);
  52. }
  53. int pre=ans+d;
  54. if(!res)return pre;
  55. tot=d;
  56. for(int i=maxlen;i>=0;i--){
  57. if(tot>=(1<<i) && res>=g[x][i] && x<=n)
  58. tot-=(1<<i),res-=g[x][i],x+=g[x][i],ans+=(1<<i);
  59. }
  60. ans=Max(pre,ans);
  61. if(!res)return ans;
  62. for(int i=maxlen;i>=0;i--){
  63. if(res>=f[x][i] && x<=n)
  64. res-=f[x][i],x+=f[x][i],ans+=(1<<i);
  65. }
  66. return ans;
  67. }
  68. void work()
  69. {
  70. scanf("%d%d%d%d",&n,&W,&pw,&lw);
  71. maxlen=log(n)/log(2)+1;rw=W-pw-lw;
  72. for(int i=1;i<=n;i++)
  73. scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
  74. prework();
  75. int Q,x,y;cin>>Q;
  76. while(Q--){
  77. scanf("%d%d",&x,&y);
  78. printf("%d\n",solve(x,y));
  79. }
  80. }
  81. int main()
  82. {
  83. int T;cin>>T;
  84. while(T--)work();
  85. return 0;
  86. }

HDU 6107 Typesetting的更多相关文章

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

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

  2. HDU 6107 Typesetting (倍增)

    Typesetting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. Typesetting HDU - 6107

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

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. (转)如何在Eclipse中查看JDK类库的源代码

    在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...

  2. 最短路算法模板SPFA、disjkstra、Floyd

    朴素SPFA(链表建边) #include <iostream> #include <cstdio> #include <cstring> #include < ...

  3. datetimepicker.js 使用笔记

    1.官网地址 官网传送门 2.属性及使用示例 2.1调用 html: <input  type="text"  readonly class="date" ...

  4. Dictionary导致CPU暴涨

    中午吃完饭回来,刚想眯一会,突然发现公司预警群报警,某台机器CPU100%,连续三次报警,心里咯噔一下,我新开发的程序就在这上面,是不是我的程序导致的?立马远程,oh my god,果然是. 二话不说 ...

  5. Celery 分布式任务队列快速入门

    Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 定时任务 与django结合 通过django配置cel ...

  6. 道可道,非常道——详解promise

    promise 出来已久,以前一直使用,没有仔细剖析原理,最近在复习es6的知识,写一下自己对于promise的理解. promise是es6的一种异步编程解决方案,避免频繁的回调函数,增强代码的可阅 ...

  7. Vue项目结构说明

    简单介绍目录结构 http://blog.csdn.net/u013778905/article/details/53864289 (别人家的链接,留给我自己看的)

  8. 告知服务器意图的http方法

    1.GET 用来获取资源,返回已有的结果 2.POST 传输实体主体,返回处理过后的结果 3.PUT 向服务器传输文件,返回是否成功的状态码 4.DELETE 删除服务器文件,返回是否成功的状态码 5 ...

  9. 设置python爬虫IP代理(urllib/requests模块)

    urllib模块设置代理 如果我们频繁用一个IP去爬取同一个网站的内容,很可能会被网站封杀IP.其中一种比较常见的方式就是设置代理IP from urllib import request proxy ...

  10. 关于myeclipse启动报错:An internal error has occurred. java.lang.NullPointerException解决办法

    启动myeclipse报错,百度了一下网友处理方式,对比日志,发现现在已有的教程真的是巨人坑: 如果出现了上述的错误按照如下的3个步骤解决:1.首先关闭MyEclipse工作空间.2.然后删除工作空间 ...