CF803D: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,表示可以最多将这个字符串分成k行line[1],line[2]......line[k],分割的位置只能是'-'和空格的位置,问如何分割可以使max_strlen( line[1~k] )最短。

思路:典型的最大值最小问题,预处理出每个分割点的位置,及其子串的长度,二分答案即可。

# include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6+3;
char s[maxn];
int k, cnt, a[maxn];

bool judge(int mid)
{
    int icount = 1, sum = 0;
    for(int i=0; i<cnt; ++i)
    {
        sum += a[i];
        if(sum > mid)
        {
            ++icount;
            sum = a[i];
            if(icount > k)
                return false;
        }
    }
    return true;
}
int main()
{
    int i, p, imin=0;
    scanf("%d",&k);
    getchar();
    gets(s);
    p = -1;
    for(i=0; s[i]; ++i)
        if(s[i]=='-' || s[i]==' ')
            imin = max(imin, i-p), a[cnt++] = i - p, p = i;
    a[cnt++] = i-p-1;
    imin = max(imin, i-p-1);
    int l=imin, r=strlen(s);
    while(l<=r)
    {
        int mid = l+r>>1;
        if(judge(mid))
            r = mid - 1;
        else
            l = mid + 1;
    }
    printf("%d\n",l);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值