Magazine Ad

The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:

There are space-separated non-empty words of lowercase and uppercase Latin letters.

There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.

It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.

When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.

The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.

You should write a program that will find minimal width of the ad.

Input

The first line contains number k (1 ≤ k ≤ 105).

The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters.

Output

Output minimal width of the ad.

Examples

Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10

Note

Here all spaces are replaced with dots.

In the first example one of possible results after all word wraps looks like this:

garage.
for.
sa-
le

The second example:

Edu-ca-
tion-al.
Ro-unds.
are.so.fun 题目大意:有一组字符串,用连字符‘-’以及空格‘ ’进行分割,要求分割不超过k段,求分割后的最小宽度(宽度就是分割后最长的字符串的长度)
思路:博主是个菜鸡,一开始没啥思路,学姐讲题的时候才想到用二分。。。。。。。。
  经过不断地啃博客(正经脸),算是明白一点如何从 二分 入手这道题。
  把每一段字符串 按照一个长度 x 进行分割,得到的字符串长度不能超过 x ,看最后得到的行数是否小于k;
  如果按长度x进行分割得到的行数小于k 那么按长度 x+1 进行分割所得到的行数必定小于k。
  注意 最小宽度 定义,这个时候我们就可以用二分,不断地去逼近这个最小宽度,就可以得到答案。
  最后注意的就是二分的上下界,上界就是初始字符串的长度s.size(),下界可以是s.size()/k(这个博主就不解释了)。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
vector<int>v;
string s;
int k;
bool check(int x){
int line = ;// 记录以x切割时可以得到的行数
int len = ;
for(int i = ; i < v.size(); i++){
if(v[i] > x){
return ;// 如果v[i] > x 说明存在v[i]让x无法表示(说明x取小了)
}
if(v[i] + len > x){
line++;
len = v[i];
}else if(v[i] + len == x){
line++;
len = ;
}else if(v[i] + len < x){
len += v[i];
}// 将字符串 以不大于x 的长度分割
// line 记录进行分割后得到的总行数
}
if(len > ) line++;// 注意分割最后的一小段字符串是否忽略
return line <= k;// 根据x分割所得到的总行数不能大于k
} int main(){
while(~scanf("%d",&k)){
v.clear();
getchar();
getline(cin,s);
int j = ;
for(int i = ; i < s.size(); i++){
if(s[i] == ' ' || s[i] == '-'){
v.push_back(j + );
j = ;
}else j++;
}
v.push_back(j);// 不要忘记末尾的字符串长度
int l = s.size()/k, r = s.size();
while(r - l > ){
int mid = (r + l)/ ;
if(check(mid)){
r = mid;
}else{
l = mid + ;
}
}
printf("%d\n",l);
}
return ;
}

Magazine Ad

一个从很久以前就在做的梦。

 

Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)的更多相关文章

  1. 第一次写博客Poj1044

    Date bugs Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3005   Accepted: 889 Descript ...

  2. 第一次写博客,关于前端开发deMVC在js中的应用

    对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...

  3. HDU 2064 菜鸡第一次写博客

    果然集训就是学长学姐天天传授水铜的动态规划和搜索,今天讲DP由于困意加上面瘫学长"听不懂就是你不行"的呵呵传授,全程梦游.最后面对连入门都算不上的几道动态规划,我的内心一片宁静,甚 ...

  4. Magazine Ad CodeForces - 803D (二分+贪心)

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...

  5. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  6. 第一次写博客,就写如何向外行介绍自己做的是什么,那个我是做web的

    如果想外行问你是做什么的,改如何回答.和内行说java后台就可以了,但外行听不懂,我们该如何描述呢? 我的方法是:我做的是java web开发,不是内外的外,是个英文单词web,全名叫world wi ...

  7. iOS controller解耦探究实现——第一次写博客

    大学时曾经做过android的开发,目前的工作是iOS的开发.之前自己记录东西都是通过自己比较喜欢的笔记类的应用记录下了.直到前段时一个哥们拉着我注册了一个博客.现在终于想明白了,博客这个东西受众会稍 ...

  8. 谈谈自己对C语言中函数指针的一些理解 (第一次写博客,有点小兴奋哈)

    1.函数指针声明的格式及简单的使用 (1)格式:(返回值)(*函数指针名)(参数列表)    例如:声明一个无参数无返回值的函数指针(void)(*p)(void). (2)将函数指针指向某个无参数无 ...

  9. sikuli+eclipse对于安卓app自动化测试的应用(第一次写博客,有些语言还不太专业,望海涵)

    Sikuli是什么? 下面是来自于官网的介绍:Sikuli is a visual technology to automate and test graphical user interfaces ...

随机推荐

  1. Python基础-继承与派生

    一.继承 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. python中类的继承分为:单继承和多继承 class P ...

  2. 如何在一台机器上运行2个Tomcat

    比如:有两个版本的tomcat,一个5.*,一个6.*,此时由于两个工程分别部署在两个 版本的tomcat下,需要同时启动两个tomcat,以下是方法: 1.特别要注意:不要设置CATALINA_HO ...

  3. OpenCV 小图重叠至大图指定位置

    Android OpenCV Java: Codes: smallImg.copyTo( bigImg.submat( y, smallImg.rows(), x, smallImg.cols() ) ...

  4. Hush Framework框架配置(续) 转自《Android和PHP最佳实践》官方站

    图书资源下载 Xampp 开发环境下载:http://pan.baidu.com/share/link?shareid=531771&uk=773037279 微博实例完整源码包下载:http ...

  5. 部署node.js的开发环境

    1.进入Node.js的官方网站下载安装包: http:nodejs.org 2.安装后打开cmd的dos窗口(在path环境变量中查看到有nodejs说明安装成功): 3.运行node.

  6. qt 使用qtxlsx 读写excel

    https://github.com/dbzhang800/QtXlsxWriter 下载qtxlsx地址 QtXlsx is a library that can read and write Ex ...

  7. 信用卡精养卡POS机方案

    所谓的精养卡,就是模仿有钱人的一种方式,提额难吗!真心不难,难就难在养卡消费 ,信用卡都有,但是不同费率的POS机你有吗,没有POS机难道你真的要去花费去消费吗,你消费的起吗?所以我们这个行业就出现了 ...

  8. IIS7下设置上传大小的限制

    一.找到修改大小的配置文件和配置节点 打开你系统盘(我是C盘),找到 C:\Windows\System32\inetsrv\config\schema目录,该目录下有一个IIS_schema.xml ...

  9. IE浏览器 div或者其他容器的height属性无效 滚动条问题解决办法

    1.height设置定值是功能好使的  但是如果在不同分辨率的电脑上运行程序 会出现样式上的偏差 2.height的百分比是根据父级来的  所以将父级的height设置好(如果当前容器上方有很多父级 ...

  10. mongoDb 命令

    1.显示MongoDB的服务器统计:db.stats() 2.创建数据库:use dbname 3.删除数据库:db.dropDatabase() 4.检查当前选择的数据库:db 5.检查数据库列表: ...