Educational Codeforces Round 20 D. Magazine Ad

本文介绍了一个解决广告排版问题的算法,通过二分查找确定最小宽度以确保广告能在限定的行数内显示,并保持最窄的宽度。文章提供了一段C++代码实现,该算法适用于包含换行符和空格的文本格式。

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

 官方题解说的意思其实不就是二分么' '和'_'是一样的

Firstly notice that there is no difference between space and hyphen, you can replace them with the same character, if you want.

Let's run binary search on answer. Fix width and greedily construct ad — wrap word only if you don't option to continue on the same line. Then check if number of lines doesn't exceed k.

#include <iostream>
using namespace std;
const int INF = (int)1e9;
int n,k,r,l;
string s;
int solve(int w)
{
    int ans = 0;
    int l = 0;
    while(l < n)
    {
        ans++;
        int r = l + w;
        if (r >= n) break;
        while(r > l && s[r - 1] != ' ' && s[r - 1] != '-') r--;
        if (r == l) return INF;
        l = r;
    }
    return ans;
}

int main()
{
    cin >> k;
    getline(cin, s);
    getline(cin, s);
    n = s.length();
    int l = 0, r = n;
    while(r - l > 1)
    {
        int m = (l + r) / 2;
        if (solve(m) <= k)
            r = m;
        else
            l = m;
    }
    cout << r << endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/BobHuang/p/6801167.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值