题目链接

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.

Input

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

Output

Print one integer — number of lines l, printed by "dir -C" command.

Examples
input
11 20
1 3 7 4 1 2 1 1 1 1 4
output
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+;
int a[MAXN],m[][MAXN];
int n;
LL w; int main()
{
while(scanf("%d%I64d",&n,&w)!=EOF)
{
memset(m,,sizeof(m));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
m[][i]=a[i];
}
for(int i=;i<=(int)log(n)/log();i++)
{
for(int j=;j+(<<i)-<=n;j++)
m[i][j]=max(m[i-][j],m[i-][j+(<<(i-))]);
} for(int i=;i<=n;i++)
{
int k=(int)log(i);
long long sum=;
for(int j=;j<n/i;j++)
{
sum+=(long long)max(m[k][j*i+],m[k][i*(j+)-(<<k)+])+;
}
if(n%i!=) {
k=(int)log(n%i);
sum+=(long long)max(m[k][n-n%i+],m[k][n-(<<k)+])+;
}
if(sum-<=w){
printf("%d\n",i);
break;
}
}
}
return ;
}

Gym 100917J---dir -C(RMQ--ST)的更多相关文章

  1. poj3368(RMQ——ST)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16543   Accepted: 5985 ...

  2. [NOI2010]超级钢琴(RMQ+堆)

    小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中Ai可正可负 ...

  3. codeforces gym #102082C Emergency Evacuation(贪心Orz)

    题目链接: https://codeforces.com/gym/102082 题意: 在一个客车里面有$r$排座位,每排座位有$2s$个座位,中间一条走廊 有$p$个人在车内,求出所有人走出客车的最 ...

  4. HDU 5726 GCD(RMQ+二分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...

  5. BZOJ 1067:[SCOI2007]降雨量(RMQ+思维)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1067 题意:…… 思路:首先我们开一个数组记录年份,一个记录降雨量,因为年份是按升序排列的,所以我们 ...

  6. POJ 3419 Difference Is Beautiful(RMQ变形)

    题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...

  7. 2015 多校联赛 ——HDU5289(二分+ST)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  8. (RMQ版)LCA注意要点

    inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...

  9. 【BZOJ 2006】2006: [NOI2010]超级钢琴(RMQ+优先队列)

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2792  Solved: 1388 Description 小 ...

  10. UVA - 1618 Weak Key(RMQ算法)

    题目: 给出k个互不相同的证书组成的序列Ni,判断是否存在4个证书Np.Nq.Nr.Ns(1≤p<q<r<s≤k)使得Nq>Ns>Np>Nr或者Nq<Ns&l ...

随机推荐

  1. JS中的宽高(基础知识很重要)

    IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.docu ...

  2. Redis 监控

    redis 监控有 redis-live 和 redis-stat Redis-Live是一个用来监控redis实例,分析查询语句并且有web界面的监控工具,使用python编写. redis-sta ...

  3. salesforce 零基础学习(十九)Permission sets 讲解及设置

    Permission sets以及Profile是常见的设置访问权限的方式. Profile规则为'who see what'.通过Profile可以将一类的用户设置相同的访问权限.对于有着相同Pro ...

  4. hadoop本地库与系统版本不一致引起的错误解决方法

    hadoop本地库与系统版本不一致引起的错误解决方法 部署hadoop的集群环境为 操作系统 centos 5.8 hadoop版本为cloudera   hadoop-0.20.2-cdh3u3 集 ...

  5. js选择目录

    找了好久,终于找到了! function browseFolder(path) {//打开本地目录(目录选择功能) try { var Message = "\u8bf7\u9009\u62 ...

  6. Mina、Netty、Twisted一起学(六):session

    开发过Web应用的同学应该都会使用session.由于HTTP协议本身是无状态的,所以一个客户端多次访问这个web应用的多个页面,服务器无法判断多次访问的客户端是否是同一个客户端.有了session就 ...

  7. SQL Server优化器特性-动态检索

    前段时间我写的文章SQL Server 隐式转换引发的躺枪死锁 中有的朋友评论回复说在SQL2008R2测试时并未出现死锁,自己一测果然如此,因此给大家带来的疑惑表示抱歉,这里我就解释下其原因. 回顾 ...

  8. JavaScript作用域原理(二)——预编译

    JavaScript是一种脚本语言, 它的执行过程, 是一种翻译执行的过程.并且JavaScript是有预编译过程的,在执行每一段脚本代码之前, 都会首先处理var关键字和function定义式(函数 ...

  9. SQL Server 2014新特性——基数评估(白皮书阅读笔记)

    基数评估 目录 基数评估 说明 基数评估准确的重要性 模型假设 启用新的基数评估 验证基数评估的版本 在迁移到新的基数评估前要测试 校验基数评估 偏差问题 需要手动处理的变化 避免因为新的CE造成性能 ...

  10. GDB 和 windbg 命令对照(转载)

    From:http://blog.csdn.net/joeleechj/article/details/10020501 命令                                      ...