CF803D:Magazine Ad(二分)

来源:互联网 发布:河南网络电视台直播 编辑:程序博客网 时间:2024/06/08 04:29

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
4garage for sa-le
output
7
input
4Edu-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;}



原创粉丝点击