Educational Codeforces Round 20 D. Magazine Ad

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

4garage for sa-le

output

7

input

4Edu-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;
}

时间: 2024-10-10 17:18:46

Educational Codeforces Round 20 D. Magazine Ad的相关文章

Educational Codeforces Round 20解(bu)题记录

A. Maximal Binary Matrix 解法:暴力模拟+贪心 #include<bits/stdc++.h> using namespace std; int a[110][110]; int main(){ int n,k,cmp,f=0;scanf("%d%d",&n,&k); if(k>n*n){puts("-1");return 0;}cmp=n; int r=1; while(k>=2*cmp-1){ k-

Educational Codeforces Round 20 C(math)

題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的gcd盡量大... 思路: 顯然題目的要求是求 n = a1*cnt + a2*cnt + a3*cnt..+ak*cnt 其中a1<a2<a3..<ak 並且要求cnt盡量大: 要使cnt盡量大,那麼顯然a1...ak要盡量小, 那麼可以令a1...ak=1, 2, 3, ..k, 令key

Educational Codeforces Round 20 B

Description You are given the array of integer numbers a0,?a1,?...,?an?-?1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line cont

Educational Codeforces Round 20 A

Description You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the botto

Educational Codeforces Round 20 C

Description You are given positive integer number n. You should create such strictly increasing sequence of k positive numbersa1,?a2,?...,?ak, that their sum is equal to n and greatest common divisor is maximal. Greatest common divisor of sequence is

Educational Codeforces Round 20 A. Maximal Binary Matrix(模拟)

题意:给你一个n*n的全是0的矩阵,和k个数字"1",让你把这k个数字1按照从上到下,从左到右的顺序构建出来 思路:模拟即可 代码: #include <iostream> #include <cstring> using namespace std; int main() { int n,k; int data[105][105]; while(cin>>n>>k) { memset(data,0,sizeof(data)); if(k

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(