CF - 803D. Magazine Ad - 二分+贪心

1.题目描述:

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
2.题意概述:

给你一行文字,其中有空格和连接字符‘-’,都可以进行换行,但是要求换行以后空格和连接字符都留在当前行,输入n表示字符串长度,k表示最多可以换k行。问你这种要求下每行的最小宽度是多少?

3.解题思路:

显然长度具有单调性,因为如果当前mid宽度可以符合条件,则大于mid宽度的肯定符合条件。考虑二分长度然后贪心地构造。假设当前长度为x,则最后一个字符肯定是空格或者‘-’,如果不是空格则贪心地找本行最后一个空格或‘-’,如果找不到说明这行不合法,这样每judge一次就更新一次ans值。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 1000100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
char str[maxn];
bool check(int x, int len, int k)
{
	int cnt = 0, pre = 0;
	while (pre < len)
	{
		cnt++;
		int cur = pre + x;
		if (cur >= len)
			break;
		while (cur > pre && str[cur - 1] != ' ' && str[cur - 1] != '-')
			--cur;
		if (cur == pre)
			return 0;
		pre = cur;
	}
	return cnt <= k;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int k;
	while (~scanf("%d\n", &k))
	{
		gets(str);
		int len = strlen(str);
		int l = 0, r = len, ans = len;
		while (l < r)
		{
			int mid = l + r >> 1;
			if (check(mid, len, k))
			{
				ans = mid;
				r = mid;
			}
			else
				l = mid + 1;
		}
		printf("%d\n", ans);
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值