803D - Magazine Ad

 

思路:

  二分答案+贪心;

 

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 1000006

int k,ai[maxn],cnt,num,len,maxlen;

char ch[maxn];

bool check(int lit)
{
    int pos=0,times=0;
    for(int i=1;i<=num;i++)
    {
        if(pos+ai[i]<=lit) pos+=ai[i];
        else times++,pos=ai[i];
    }
    if(times>=k) return false;
    return true;
}

int main()
{
    scanf("%d",&k);getchar();
    gets(ch);
    for(int i=0;true;i++)
    {
        cnt++;
        if(ch[i]=='-'||ch[i]==' ') ai[++num]=cnt,cnt=0;
        if(ch[i+1]==0&&ch[i+2]==0)
        {
            len=i+1;
            ai[++num]=cnt;
            break;
        }
    }
    for(int i=1;i<=num;i++) maxlen=max(maxlen,ai[i]);
    int l=maxlen,r=len,ans;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    cout<<ans;
    return 0;
}

 

相关文章:

  • 2021-05-25
  • 2021-11-17
  • 2022-03-03
  • 2021-06-06
  • 2021-11-11
  • 2021-12-05
  • 2021-10-11
  • 2022-03-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2021-08-25
  • 2021-09-11
  • 2022-01-22
  • 2022-02-06
相关资源
相似解决方案