codeforces 803D Magazine Ad

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

你可能感兴趣的:(Greedy,codeforces,二分)