codeforces 803D Magazine Ad

8 篇文章 0 订阅
4 篇文章 0 订阅
D. Magazine Ad
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

题意:给你一段字符串,可以有空格和‘-’来分割,让你确定在分组数目小于等于k的情况下, 最小的分组宽度思路:二分+贪心判断;二分的时候我们直接分答案,假定最长就可以分mid,然后利用贪心的思想来验证,这样的分组数是否小于等于k,如果分组数大于k说明结果太小low=mid+1,否则说明结果太大high=mid-1;这样最后的low即为结果,贪心的时候我们需要判断连续的mid的字母中是否包含‘ ’和‘-’,如果包含,那么分组数+1,如果不含并且还没达到字符串结尾,那么这种长度太短了,返回false,具体实现看代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>

using namespace std;

const int max_n = 5;
int k;
char c[1000006];
int numa,numb;
int len;
bool judge(int mid) {
    int cnt = 0;
    int t = 0;
    int j;
    for(int i = 0; i < len;) {
        int flag = 0;
        for( j=i; j < i+mid && j < len; ++j) {
            if(c[j] ==' ' || c[j] == '-') {
                flag = 1;
                t = j;
            }
        }
        if(flag) {
            cnt++;
            if(j == len){
                i = j;
            }else{
                i = t+1;
            }
        }else {
            if(j == len){
                cnt++;
                i = j;
            }else{
                return false;
            }
        }
    }
    if(cnt <= k) {
        return true;
    }
    return false;
}

int main() {
    cin >> k;
    getchar();
    gets(c);
    len = strlen(c);
    int low = 0;
    int high = len;
    int mid;
    while(low <= high) {
        mid = (low+high)/2;
        if(judge(mid)) {
            high = mid-1;
        } else {
            low = mid+1;
        }
    }
    printf("%d\n",low);
    return 0;
}

注意:输出的是low而不是mid

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值