codeforces 803D Magazine Ad

来源:互联网 发布:小影怎么添加网络音乐 编辑:程序博客网 时间:2024/06/10 20:44

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.

Example
.input, .output {border: 1px solid #888888;} .output {margin-bottom:1em;position:relative;top:-1px;} .output pre,.input pre {background-color:#EFEFEF;line-height:1.25em;margin:0;padding:0.25em;} .title {background-color:#FFFFFF;border-bottom: 1px solid #888888;font-family:arial;font-weight:bold;padding:0.25em;}
Input
4garage for sa-le
Output
7
Input
4Edu-ca-tion-al Ro-unds are so fun
Output
10
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <set>#include <map>#define N 1000002#define maxn 2000using namespace std;typedef long long LL;int num,k;int len;char str[N];int calculate(int m){    int cnt=0;    int x,j;    for(int i=0; i<len;)    {        int flag=0;        for(j=i; j<m+i&&j<len; j++)            if(str[j]==' '||str[j]=='-')            {                flag=1;                x=j;            }        if(flag)        {            cnt++;            if(j==len)            {                i=j;            }            else                i=x+1;        }        else        {            if(j==len)            {                cnt++;                i=j;            }            else                return false;        }    }    if(cnt<=k)        return true;    return false;}int  erfen(){    int low=0,high= len,mid;    while(low<=high)    {        mid=(high+low)/2;        if(calculate(mid))high=mid-1;        else low=mid+1;    }    return low;}int main(){    num=0;    cin>>k;    getchar();    gets(str);    len=strlen(str);    cout<<erfen()<<endl;    return 0;}

 
原创粉丝点击