codeforces803D. 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
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;
}
codeforces803D. Magazine Ad的更多相关文章
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)
Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- Magazine Ad CodeForces - 803D (二分+贪心)
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- Educational Codeforces Round 20 D. Magazine Ad
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- CodeForces 803D Magazine Ad
二分. 首先把字符串处理成一个数组,二分答案,判断一下即可. #include <cstdio> #include <cmath> #include <set> # ...
- 【贪心+二分】codeforces D. Magazine Ad
codeforces.com/contest/803/problem/D [题意] 给定一个字符串,字符串里可能有空格和连字符‘-’,空格和连字符的意义是一样的,都表示:能在那个位置把字符串分成两部分 ...
- 【codeforces 803D】Magazine Ad
[题目链接]:http://codeforces.com/contest/803/problem/D [题意] 给你一个字符串; 其中的空格和连字符表示可以折叠的部分 (就是说能在那个位置把字符串分成 ...
- rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial
路由及路由参数 <%= link_to 'My Blog', {controller: 'articles', demo: "lidsi"}, class: "bl ...
随机推荐
- struts2架构网站漏洞修复详情与利用漏洞修复方案
struts2从开发出来到现在,很多互联网企业,公司,平台都在使用apache struts2系统来开发网站,以及应用系统,这几年来因为使用较多,被攻击者挖掘出来的struts2漏洞也越来越,从最一开 ...
- maven之package与install的区别
mvn clean package 先看命令的执行过程 mvn clean install 同样先看执行过程 mvn clean package依次执行了clean.resources.compile ...
- thrift服务端到客户端开发简单示例
(1)首先我们在服务器端写个helloworld.thrift文件,如下所示: service HelloWorld{ string ping(1: string name), string getp ...
- ExtJs工具篇(2)——Aptana Studio 3 汉化
本身用的是中文版本的,但是输入一些中文后,竟然有乱码,所以就想把它汉化.在网上搜索了一下,把步骤记录如下: 首先到这个网站去 http://aptana.com/support 选择View Docu ...
- 深入浅出 Webpack
深入浅出 Webpack 评价 Webpack 凭借强大的功能与良好的使用体验,已经成为目前最流行,社区最活跃的打包工具,是现代 Web 开发必须掌握的技能之一.作者结合自身的实战经验,介绍了 Web ...
- Python|花了一天,为大家整理的一套来自外国大佬的密码速查表
简单的HTTPS服务器 检查证书信息 输出 生成自签名证书 输出 准备一个签名注册请求 输出 生成无密码的RSA秘钥文件 用一个私钥给文件签名 输出 从签名验证一个文件 输出 通过pem文件做RSA加 ...
- windows下网络命令----Tracert命令详解
现在网络四通八达,网线光纤基站卫星,只要运营商能收费的地方,就有网络,覆盖了全世界所有的区域.彻底改变了以前通讯基本靠吼的情况.那么宽广的网络世界,超过100米就得需要中继放大信号的网线,即使现在的光 ...
- Cross Entropy in Machine Learning
整理摘自:https://blog.csdn.net/tsyccnh/article/details/79163834 信息论 Outline 1. 信息量与信息熵 2. 相对熵(KL散度) 3. 交 ...
- Servlet入门(2)
1.url_pattern匹配模式 2.servlet生命周期 3.servlet线程问题 一.url_pattern 1.定义: 当浏览器发起一个url请求后,该请求发送到servlet容器的时候, ...
- 文本太长,用省略号显示的css样式
——html代码 <divid="d1" title="鼠标放上显示的文字"></div> ——css代码 #d1{ width:300 ...