题干:

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和一个字符串s,最多可以把s分成k行,分割位置只能是-或者空格,求分割后最长一行长度的最小值。

解题报告:

以前做过一个类似的,直接二分枚举答案就可以了,注意fit中直接让cnt=1,表示,当前在凑第cnt个,这样设计cnt是为了表示最后一个点,需要多一个,所以干脆直接cnt=1好了,而且也有比较合理的解释。

AC代码:

#include<bits/stdc++.h>using namespace std;
char s[1000005];
int ok[1000005];
int len,tot,k;
bool fit(int x) {int cur = -1,cnt = 1;for(int i = 1; i<=tot; i++) {if(ok[i] - cur > x) {i--;cnt++;cur = ok[i];} if(cnt > k) return 0;}return 1;
}
int main()
{int maxx = 0;cin>>k;getchar(); gets(s);len = strlen(s);ok[0] = -1;for(int i = 0; i<len; i++) {if(s[i] == ' ' ||s[i] == '-') {ok[++tot] = i;maxx = max(maxx,ok[tot] - ok[tot-1]);}}ok[++tot] = len-1;int l = maxx,r = len;int mid = (l + r) / 2;while(l < r) {mid = (l + r ) / 2 ;if(fit(mid)) r = mid;else l = mid + 1;} printf("%d\n",l);return 0 ;} 

总结:l=maxx开始,因为这样就避免了fit中需要先循环判断一次是否有一个串的长度也不符合的,r从len开始,其实从哪开始都可以,刚开始写的是len/k +1 不知道自己是怎么想的。。。肯定是错的反正。

【CodeForces - 803D】Magazine Ad(二分答案)相关推荐

  1. 2018.12.08 codeforces 939E. Maximize!(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...

  2. Match Points CodeForces 1156C 二分答案

    CodeForces 1156C Match Points 传送门:https://codeforces.com/problemset/problem/1156/C You are given a s ...

  3. Magic Powder - 2 CodeForces - 670D2(二分答案)

    先贴一篇关于二分边界问题的博客,看来自己还是对二分边界的理解不够深入 题目:今天我们要来造房子.造这个房子需要n种原料,每造一个房子需要第i种原料ai个.现在你有第i种原料bi个.此外,你还有一种特殊 ...

  4. CodeForces - 51C 修信号站_思维+二分答案

    题目 给你x轴上的n个点,可以用三段相同长度的区间进行覆盖,问你这三个区间的最小长度是多少,并输出三段区间的中点. 求解 前半个问题应该不难,直接二分答案即可,后半个问题则有些思维成分.另外,这题让我 ...

  5. 【CodeForces 1100E】二分答案 | 拓扑排序 | E

    这是一道很美妙的题- 1100E. Andrew and Taxi time limit per test: 2 seconds memory limit per test: 256 megabyte ...

  6. Codeforces 685C Optimal Point (二分、不同类型距离的相互转换)

    题目链接 https://codeforces.com/contest/685/problem/C 题解 我怎么又还差最后一步的时候放弃了然后往别的方向上想了一小时才发现这个思路能做-- 首先二分答案 ...

  7. UVA1396 Most Distant Point from the Sea(AM - ICPC - Tokyo - 2007)(计算几何,半平面交 + 二分答案)

    整理的算法模板合集: ACM模板 题目传送门 见<训练指南>P279 很明显就是一个二分答案,它问的是最远的点,直接枚举因为这里都是double类型的数所以有无限个点,我们可以直接二分. ...

  8. UVA1146 / LA3211(ACM-ICPC 2004 Europe - Southwestern) Now or later(2-SAT问题 + 二分答案)

    题目要求为 最大化最小值,很明显就是二分答案. 题目中每个飞机 要么是一种状态(早),要么是另一种状态(晚),考虑 2-SAT. 我们二分答案,二分着陆时间间隔的最小值 x. 枚举每两个飞机 p , ...

  9. 解题报告:luoguP2868 Sightseeing Cows G(最优比率环,负环判定,二分答案)

    根据题意,我们要环上各点权值之和除以各边权值之和最大. 求最大答案,很明显可以使用二分答案.那么我们假设当前答案为 x,如果有更大的答案,那么方程就可以按下图转换: 也就是说如果有更大的答案,则有一个 ...

  10. P2759 奇怪的函数(二分答案,数学运算)

    P2759 奇怪的函数 范围2e92e92e9,直接枚举肯定超时,正着直接求答案求不出来,那么运用逆向思维,直接二分答案判断即可.这道题涉及简单的数学运算. 要xx>=nx^x>=nxx& ...

最新文章

  1. Tomcat怎样将配置文件放在外部
  2. C++中的文件读写操作(1)
  3. 本地环境用eclipse搭建spring源码环境
  4. Python中如何把一个UTC时间转换为本地时间
  5. 不想加班你敢说出来吗?华为员工公开说我不想加班
  6. android imageview图片崩溃,android - setImageResource导致应用程序崩溃 - 堆栈内存溢出...
  7. 专科python应届生工资多少-Python这么火热,本科应届生薪资这么高?
  8. 职场三剑客,助你从容闯职场
  9. 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)
  10. 配电室综合监控系统企业标准(试行)
  11. 软件测试基础知识大全
  12. 基于非负矩阵分解的盲信号分离方法研究–Matlab
  13. Lexicon Enhanced Chinese Sequence Labelling Using BERT Adapter 论文解读
  14. 那些测绘工作中让你事半功倍的小工具软件,我不推荐你轻易安装
  15. 五年磨一剑:滴滴顺风车服务端之稳定性规范
  16. 软件设计师教程(九)计算机系统知识-软件工程基础知识
  17. 猪呀,羊呀,送到哪里去?
  18. SQL server 获取一周前到两周后之间的所有日期
  19. 通过ASP.NET Ajax技术模拟实现NBA比赛文字直播功能
  20. 零时科技 || BEGO Token 攻击事件分析

热门文章

  1. java实现坐标图进行拖拉拽放_js实现限定区域范围拖拉拽效果
  2. mysql 导入unl文件_Informix和Oracel数据库导入UNL数据
  3. 2.5d generator 2.0_ps插件【2.5D插件】
  4. Ubuntu apt-get 卸载命令
  5. 禅道——需要我们斟酌
  6. 如何使用autotools工具
  7. Windows Embedded CE 6.0开发初体验(三)设置Boot-loader
  8. Android判断view在屏幕可见,如何检查TextView是否在Android可见屏幕内
  9. linux查询服务器的dns,如何查看Linux系统中DNS服务器的运行状况
  10. 交通与计算机杂志社,交通信息与安全