Educational Codeforces Round 20 D. Magazine Ad

原创 2018年04月17日 18:23:41

1.题目链接

http://codeforces.com/contest/803/problem/D

2.题面

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
Copy
4
garage for sa-le
output
Copy
7
input
Copy
4
Edu-ca-tion-al Ro-unds are so fun
output
Copy
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

3.题意

大概就是给你一段字符串,中间有空格和-,对于每个空格和-,都可以让之后的字符串转到下一行,问你将该字符串转为k行的最小宽度。

4.思路

很明显,答案具有二分的性质,如果长度n是允许的,那么大于n的一定是可行的,所以我们可以采取二分+验证答案的方式来nlogn解决这道题。

5.代码实现

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

const int maxn = 1e6+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=2.718281828459045235360287471352662;
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back

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()
{
	int k;
	cin>>k;
	getchar();
    gets(str);
    int len = strlen(str);
    int l=0,r=len,mid;//找不到返回r+1
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(check(mid, len, k)) r=mid-1;
        else l=mid+1;
    }
    if(l==len+1)
        cout<<len<<endl;
    else
        cout<<l<<endl;
	return 0;
}

6.反思

看到nlogn的数据范围量,就要联想是否可以二分+验证答案。

Educational Codeforces Round 39 (Rated for Div. 2) D Timetable

题目链接:点击打开链接题意:某大学一周有n天,一天有m小时工作/上课时间。给定n行m列0 和 1组成的字符,当第i行第j列为1时表示第i天第j小时有课。主角每天在学校的时间为第一节课到最后一节课之间(...
  • Lfhase
  • Lfhase
  • 2018-03-07 02:10:44
  • 83

Educational Codeforces Round 34 (Rated for Div. 2)

这是我这个弱渣第一次正儿八经打codeforce的比赛 只水出来A题一道签到题,B题和C题只是有思路 但是并没有写出来 至于D E F就根本没有看 可能没有挑战的欲望吧 以后就算做不出来也会想一想思路...
  • hdsdogge
  • hdsdogge
  • 2017-12-13 16:57:49
  • 92

CF - 813D. Two Melodies - dp

1.题目描述: D. Two Melodies time limit per test 2 seconds memory limit per test 256 megabytes in...
  • dragon60066
  • dragon60066
  • 2017-06-22 21:41:47
  • 370

Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable (背包)

题意:    一个周期有n天,每天有m节课,一个周期的时间可以逃课k次。问至少要在学校里待多久。(从早上去的第一节课到最后一节课,如果没有课就不去)分析:    每天有x节课,我们需要考虑旷课[0,x...
  • lj130lj
  • lj130lj
  • 2018-03-08 20:20:23
  • 17

Educational Codeforces Round 39-D-Timetable(背包DP)

题意:给你n天,每天有m节课,你最多可以逃k节课,并且对于某一天,在你决定上的第一节课和最后一节课之间就算没课,也要待在学校,问你可以在学校呆的最少时间是多少? 题解:对于每天,我们暴力出上x节课需...
  • haut_ykc
  • haut_ykc
  • 2018-03-08 10:45:10
  • 56

Educational Codeforces Round 39: D. Timetable(DP)

D. Timetabletime limit per test 2 secondsmemory limit per test 256 megabytesinput standard inputoutp...
  • Jaihk662
  • Jaihk662
  • 2018-03-08 17:02:14
  • 118

Educational Codeforces Round 34 (Rated for Div. 2) D

D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes ...
  • ACM_e
  • ACM_e
  • 2017-12-14 13:44:15
  • 142

解题报告:Educational Codeforces Round 24 D,E,F

D、题目链接 题意: 给定一个长度为n的序列,给定一个数a,询问是否存在数b满足从前往后遍历序列,数b出现的次数始终大于等于数a出现的次数。 思路: 把每两个数a之间看做一个筛选阶段,每次筛选掉之...
  • qq_32570675
  • qq_32570675
  • 2017-07-01 14:07:43
  • 287

Educational Codeforces Round 36 (Rated for Div. 2) 题解

先总结一波 第一次打cf,感觉还不错,题目做得挺顺手。虽然开始30min才想起来有这么个比赛来着。。 纪念一下第一次的rank,话说题真是水 这是大概还剩下5min的时候截的,实际可能会...
  • jpwang8
  • jpwang8
  • 2018-01-13 23:16:24
  • 366
收藏助手
不良信息举报
您举报文章:Educational Codeforces Round 20 D. Magazine Ad
举报原因:
原因补充:

(最多只允许输入30个字)