Gym 100917J---dir -C(RMQ--ST)
2018-06-17 23:43:10来源:未知 阅读 ()
题目链接
http://codeforces.com/gym/100917/problem/D
problem description
Famous Berland coder and IT manager Linus Gates announced his next proprietary open-source system "Winux 10.04 LTS"
In this system command "dir -C" prints list of all files in the current catalog in multicolumn mode.
Lets define the multicolumn mode for number of lines l. Assume that filenames are already sorted lexicographically.
- We split list of filenames into several continuous blocks such as all blocks except for maybe last one consist of l filenames, and last block consists of no more than l filenames, then blocks are printed as columns.
- Width of each column wi is defined as maximal length of the filename in appropriate block.
- Columns are separated by 1 × l column of spaces.
- So, width of the output is calculated as , i.e. sum of widths of each column plus number of columns minus one.
Example of multi-column output:
a accd e t
aba b f wtrt
abacaba db k
In the example above width of output is equal to 19.
"dir -C" command selects minimal l, such that width of the output does not exceed width of screen w.
Given information about filename lengths and width of screen, calculate number of lines l printed by "dir -C" command.
First line of the input contains two integers n and w — number of files in the list and width of screen (1 ≤ n ≤ 105, 1 ≤ w ≤ 109).
Second line contains n integers fi — lengths of filenames. i-th of those integers represents length of i-th filename in the lexicographically ordered list (1 ≤ fi ≤ w).
Print one integer — number of lines l, printed by "dir -C" command.
11 20
1 3 7 4 1 2 1 1 1 1 4
3
题意:有n个目录名字符串,长度为a[1]~a[n] 屏幕宽为w ,现在要按照已经给的目录循序一列一列的放,每一列放x个,最后一列放<=x个 要求每一列目录名左端对整齐 ,形成一个长方形的块 ,且块与块之间空一格,且不能超过屏幕的宽度,求最小的行数;
思路:先对输入长度处理,用ST算出每个区间的最大值,然后枚举行数x 从1 ~ n;
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <cmath> using namespace std; typedef long long LL; const int MAXN = 1e5+5; int a[MAXN],m[30][MAXN]; int n; LL w; int main() { while(scanf("%d%I64d",&n,&w)!=EOF) { memset(m,0,sizeof(m)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); m[0][i]=a[i]; } for(int i=1;i<=(int)log(n)/log(2);i++) { for(int j=1;j+(1<<i)-1<=n;j++) m[i][j]=max(m[i-1][j],m[i-1][j+(1<<(i-1))]); } for(int i=1;i<=n;i++) { int k=(int)log(i); long long sum=0; for(int j=0;j<n/i;j++) { sum+=(long long)max(m[k][j*i+1],m[k][i*(j+1)-(1<<k)+1])+1; } if(n%i!=0) { k=(int)log(n%i); sum+=(long long)max(m[k][n-n%i+1],m[k][n-(1<<k)+1])+1; } if(sum-1<=w){ printf("%d\n",i); break; } } } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 【题解】Building Strings Gym - 102152E 2020-03-31
- CodeForces Gym 100213F Counterfeit Money 2020-02-13
- 2018-10-06 Gym-101864F丨STL爽题丨想法 2018-10-08
- Gym 100703I---Endeavor for perfection(尺取) 2018-06-27
- Gym 100703G---Game of numbers(DP) 2018-06-17
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