Codeforces 803D Magazine Ad【二分+贪心】

本文介绍了一个关于杂志广告排版的问题,需要将包含特殊字符的字符串合理分组,以确保每组不超过指定行数且宽度最小。通过二分查找确定最优解,并采用贪心策略进行检查。

D. Magazine Ad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.


思路:


很显然有单调性,如果长度len可以满足条件,那么长度len+1也一定满足条件,同理len+2更满足条件。

所以我们二分答案,然后贪心check一下即可。

尽可能将一个单词分到一组中。


注意一些细节,因为上界是1e6.而分组数最大可以到1e5.如果我们判定mid*kk和n的大小的时候,我们如果两个int相乘会爆int.

所以不妨将所有数据都设定为LL.稳妥更多。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值