当前位置:网站首页>Educational Codeforces Round 20 D. Magazine Ad

Educational Codeforces Round 20 D. Magazine Ad

2022-06-09 05:56:00 No toast

#include<bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))

typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long uLL;

const int maxn = 1e6+2;
const int INF  = 1<<30;
const int mod  = 1e9+7;

char ch[maxn];int len=0,k;

std::vector<int> v;

bool check(int x){
    
    int now=0,cnt=1;
    for(int i=0;i<v.size();i++){
    
        now+=v[i];
        if(now>x){
    
            ++cnt;
            now=v[i];
        }
    }
    return cnt<=k;
}

void solve(){
    
    cin>>k;
    getchar();
    while(1){
    
        char c=getchar();
        if(c=='\n') break;
        ch[++len]=c;
    }
    int pre=0;
    for(int i=1;i<=len;i++){
    
        if(ch[i]==' '||ch[i]=='-'){
    
            v.push_back(i-pre);
            pre=i;
        }
    }
    v.push_back(len-pre);
    // for(auto it:v) cout<<it<<endl;
    int l=0,r=len;for(auto it:v) l=max(l,it);
    while(l<r){
    
        int mid=(l+r)/2;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<l<<endl;
    return;
}

int main()
{
    
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// ios::sync_with_stdio(false);
    int t = 1;
    //scanf("%d",&t);
    while(t--){
    
    // printf("Case %d: ",cas++);
        solve();
    }
    return 0;
}
原网站

版权声明
本文为[No toast]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090555059193.html