Magazine Ad CodeForces - 803D

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
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比较。大了说明要合并则不成立,假设值需要扩大;小了说明要再分,距离更加小于假设值,成立,假设值可以再尝试缩减。

#include"stdio.h"
#include"string.h"
#include"cstdio"
#include"algorithm"
using namespace std;
const int max_n=1e6+10;
char A[max_n];
int B[max_n];
int k,n,num;
int judge(int mid)
{
	int maxx=B[1],minn=0,l=0,r=0,len;
	B[0]=-1;B[num]=n-1;B[num+1]=0x3f3f3f3f;
	bool p=true;
	while(l<r||p)
	{//printf("l:%d r:%d mid:%d minn:%d\n",l,r,mid,minn);
		if(B[r+1]-B[l]>mid)
		{
			minn++;
			l=r;
		}
		while(B[r+1]-B[l]<=mid&&r<num)
		{
			//if(B[r+1]-B[l]==mid)
			maxx=max(maxx,B[r+1]-B[l]);r++;
		}
		p=false;
	}//printf("r:%d  maxx:%d  minn%d num:%d\n",r,maxx,minn,num);
	if(minn>k||r<num)
	return 0;
	return maxx;
}
int EF()
{
	int l=1,r=n,mid;
	mid=(l+r)>>1;
	while(l<=r)
	{
		if(judge(mid))
		r=mid-1;
		else
		l=mid+1;
		mid=(l+r)>>1;
	}//printf("r:%d judge(r):%d\n",r,judge(r));
	if(judge(r))
	return judge(r);
	else
	return judge(l);
}
int main()
{
	while(scanf("%d",&k)!=EOF)
	{
		getchar();
		gets(A);
		n=strlen(A);
		num=1;
		for(int i=0;i<n;i++)
		{
			if(A[i]==' '||A[i]=='-')
			B[num++]=i;
		}
		int ret=EF();
		printf("%d\n",ret);
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值