<span>Magazine Ad CodeForces - 803D (二分+贪心)</span>

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


题目链接:https://vjudge.net/problem/CodeForces-803D#author=0
题意:将一个带空格和分隔符的字符串以空格或者分隔符为分割点,分割成多行字符串,并且行数小于给定的k值,
而且每一行的字符串长度尽可能的大,问最大的长度是多少?

思路:显然可知:如果一个最大长度x满足条件,x+1,x+2等等也一定满足,即为单调的,那么我们就可以快乐的二分了
注意下二分的区间是有限制的,总字符串中可以分割成每一个小段,这些小段中最长的那个即为区间的左端点,因为比他还短一定不能存在的。
区间的右端点即为字符串的总长度。
然后check函数即只需要贪心的求以mid为最长字串长度去分割要产生多少行,如果行数小于k就满足,反而反之。
  博主的博客地址:https://www.cnblogs.com/qieqiemin/
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int k;
string s;
vector<int> v;
int cnt;
bool check(int mid)
{
    int x;
//    int last=0;
    int m=0;
    int num=0;
    for(int i=0;i<cnt;i++)
    {
        x=v[i];
        m+=x;
        if(m>mid)
        {
            m=x;
            num++;
        }
    }
    num++;
    return num<=k;
}
int main()
{
    cin>>k;
    getchar();
    getline(cin,s);
    int len=s.length();
    int st=0;
    int pos=-1;
    for(int i=0;i<len;i++)
    {
        if(s[i]==' '||s[i]=='-')
        {
            v.push_back(i-pos);

            st=max(st,i-pos);
            pos=i;
        }
    }
    st=max(st,len-pos-1);
    v.push_back(len-pos-1);
    cnt=v.size();
//    for(int i=0;i<cnt;i)
    int l=st;
    int r=len;
    int mid;
    int ans;
    while(l<=r)
    {
        mid=(l+r)>>1;

        if(check(mid))
        {
            r=mid-1;
            ans=mid;
        }else
        {
            l=mid+1;
        }
    }
    cout<<ans<<endl;
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 

 
全部评论

相关推荐

不知道怎么取名字_:玩游戏都写到简历上了啊
点赞 评论 收藏
分享
2025-11-21 13:44
重庆科技大学 Java
程序员花海:实习太简单了 学历可以的 实习描述应该是先介绍业务 再介绍技术 技术咋推动业务的 做到了啥收益 有没有做实验 实验组和对照组有什么不同 你最后学到了什么 有没有参与处理过线上问题 有没有参与过公司的code review 有没有参与过技术分享 这些都是可以在实习描述中写的 并且实习和项目不一样不会撞车 应该放在最前面 放在教育背景下面 另外项目有点烂大街 可以看下我主页的简历优化案例
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务