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
inputCopy
4
garage for sa-le
outputCopy
7
inputCopy
4
Edu-ca-tion-al Ro-unds are so fun
outputCopy
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和一个字符串s,最多可以把s分成k行,分割位置只能是-或者空格,求分割后最长一行长度的最小值
题解:二分最后一行的长度的最小值,每次二分的时候检查一遍,每次检查的时候从0扫到最后即可
代码如下:

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int k,n;
string str;
int C(int w){
int ans=;
int l=;
//从左到右扫
while(l<n){
ans++;
//最右端
int r=l+w;
//最右段过了长度,找到一个大的了 就break返回答案了 r=mid 继续找更小的
if(r>=n) break;
//如果没有到这个分段的边界 就--
while(r>l&&str[r-]!='-'&&str[r-]!=' ') r--;
//如果减到这个分段的边界了,就证明二分到的这个答案小了 l=mid
if(r==l) return INF;
l=r;
}
return ans;
} int main(){
cin>>k;
getchar();
getline(cin,str); n=str.length();
int l=,r=n;
//二分答案找小长度
while(r-l>){
int mid=(l+r)/;
if(C(mid)<=k) r=mid;
else l=mid;
}
cout<<r<<endl;
}

最新文章

  1. JavaScript parseInt() 函数
  2. android开发中scrollview添加自定义view的滑动显示问题
  3. DMA-330(一)
  4. 开启所有PHP错误!无论在任何时候
  5. Spring源码学习之:spring注解@Transactional
  6. 区间DP+next求循环节 uva 6876
  7. CSS3新增Hsl、Hsla、Rgba色彩模式以及透明属性(转)
  8. C# 对XML基本操作总结
  9. quarze的工作原理
  10. 如何自学成为一个WEB前端
  11. Linux设备驱动框架设计
  12. linux 下修改etc/profile文件
  13. BZOJ.5467.[PKUWC2018]Slay the Spire(DP)
  14. URL.createObjectURL() 实现本地上传图片 并预览功能
  15. TortoiseGit拉取或推送,输入账号密码后提示 HTTP Basic: Access denied fatal: Authentication failed 解决方案
  16. 遇到的一个移动端从下往上过渡的弹框,在Android下过渡动画的优化问题。
  17. laravel启动过程简单解析
  18. Mysql innodb_fast_shutdown
  19. 洛谷.4180.[模板]次小生成树Tree(Kruskal LCA 倍增)
  20. ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context

热门文章

  1. 预防跨站脚本(xss)
  2. mysql学习第四天(高级查询)
  3. 单调队列优化dp
  4. mybatis-generator自定义注释生成
  5. c/c++容器操作
  6. 【数据库】 SQLite 介绍
  7. Django笔记 —— Admin(Django站点管理界面)
  8. [转]使用Gradle管理你的Android Studio工程
  9. 【紫书】(UVa12563)Jin Ge Jin Qu hao
  10. 【app.json】配置说明,不断更新中