Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)
Magazine Ad
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
4
garage for sa-le
7
4
Edu-ca-tion-al Ro-unds are so fun
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段,求分割后的最小宽度(宽度就是分割后最长的字符串的长度)。
思路:博主是个菜鸡,一开始没啥思路,学姐讲题的时候才想到用二分。。。。。。。。
经过不断地啃博客(正经脸),算是明白一点如何从 二分 入手这道题。
把每一段字符串 按照一个长度 x 进行分割,得到的字符串长度不能超过 x ,看最后得到的行数是否小于k;
如果按长度x进行分割得到的行数小于k 那么按长度 x+1 进行分割所得到的行数必定小于k。
注意 最小宽度 定义,这个时候我们就可以用二分,不断地去逼近这个最小宽度,就可以得到答案。
最后注意的就是二分的上下界,上界就是初始字符串的长度s.size(),下界可以是s.size()/k(这个博主就不解释了)。
AC代码:
#include<bits/stdc++.h>
using namespace std;
vector<int>v;
string s;
int k;
bool check(int x){
int line = ;// 记录以x切割时可以得到的行数
int len = ;
for(int i = ; i < v.size(); i++){
if(v[i] > x){
return ;// 如果v[i] > x 说明存在v[i]让x无法表示(说明x取小了)
}
if(v[i] + len > x){
line++;
len = v[i];
}else if(v[i] + len == x){
line++;
len = ;
}else if(v[i] + len < x){
len += v[i];
}// 将字符串 以不大于x 的长度分割
// line 记录进行分割后得到的总行数
}
if(len > ) line++;// 注意分割最后的一小段字符串是否忽略
return line <= k;// 根据x分割所得到的总行数不能大于k
} int main(){
while(~scanf("%d",&k)){
v.clear();
getchar();
getline(cin,s);
int j = ;
for(int i = ; i < s.size(); i++){
if(s[i] == ' ' || s[i] == '-'){
v.push_back(j + );
j = ;
}else j++;
}
v.push_back(j);// 不要忘记末尾的字符串长度
int l = s.size()/k, r = s.size();
while(r - l > ){
int mid = (r + l)/ ;
if(check(mid)){
r = mid;
}else{
l = mid + ;
}
}
printf("%d\n",l);
}
return ;
}
Magazine Ad
一个从很久以前就在做的梦。
Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)的更多相关文章
- 第一次写博客Poj1044
Date bugs Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3005 Accepted: 889 Descript ...
- 第一次写博客,关于前端开发deMVC在js中的应用
对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...
- HDU 2064 菜鸡第一次写博客
果然集训就是学长学姐天天传授水铜的动态规划和搜索,今天讲DP由于困意加上面瘫学长"听不懂就是你不行"的呵呵传授,全程梦游.最后面对连入门都算不上的几道动态规划,我的内心一片宁静,甚 ...
- Magazine Ad CodeForces - 803D (二分+贪心)
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- 第一次写博客,就写如何向外行介绍自己做的是什么,那个我是做web的
如果想外行问你是做什么的,改如何回答.和内行说java后台就可以了,但外行听不懂,我们该如何描述呢? 我的方法是:我做的是java web开发,不是内外的外,是个英文单词web,全名叫world wi ...
- iOS controller解耦探究实现——第一次写博客
大学时曾经做过android的开发,目前的工作是iOS的开发.之前自己记录东西都是通过自己比较喜欢的笔记类的应用记录下了.直到前段时一个哥们拉着我注册了一个博客.现在终于想明白了,博客这个东西受众会稍 ...
- 谈谈自己对C语言中函数指针的一些理解 (第一次写博客,有点小兴奋哈)
1.函数指针声明的格式及简单的使用 (1)格式:(返回值)(*函数指针名)(参数列表) 例如:声明一个无参数无返回值的函数指针(void)(*p)(void). (2)将函数指针指向某个无参数无 ...
- sikuli+eclipse对于安卓app自动化测试的应用(第一次写博客,有些语言还不太专业,望海涵)
Sikuli是什么? 下面是来自于官网的介绍:Sikuli is a visual technology to automate and test graphical user interfaces ...
随机推荐
- jQuery Callback函数的用法
在动画100%完成后,调用callback函数 语法如下 $(selector).hide(speed, callback); <!-- html部分 --> <button> ...
- 【Python】多重赋值之值互换
右边的值先确定,然后再开始向左赋值 s = 1 t = 2 s,t = t,s print s print t >>> 2 >>> 1 区分 s = t t = s ...
- B+/-Tree原理(mysql索引数据结构)
B+/-Tree原理 B-Tree介绍 B-Tree是一种多路搜索树(并不是二叉的): 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M ...
- 基于bootstrap的单选(radio)或者多选(checkbox)的选择框组
完成的效果如下图所示: html代码如下: <!-- 两行组 --> <div class="box"> <ul class="list-g ...
- Data Flow ->> Source ->> Error Output ->> Error & Truncation: Ignore Failure, Redirect Now, Fail Component
Ignore Failure: 当该字段遇到错误时,字段值被设为NULL Redirect Now: 把该行输出到SSIS的Source组件的红色输出线,这时红色输出线应该连接一个可以接受结果集的组件 ...
- Jenkins安装 CentOS 7上安装Jenkins
CentOS 7上安装Jenkins Jenkins 安装 只安装不介绍 步骤1:更新CentOS 7 Linux系统管理员的最佳做法之一是使系统保持最新.安装最新的稳定包,然后重新启动. 1 2 ...
- Simotion 绝对值编码器使用外部开关回零
问题来源: 西门子的1FK7二代电机,目前已经没有增量编码器.标准的编码器选项是单圈绝对值,或多圈绝对值. 在某些应用中,如印刷机的版辊.模切轴.飞剪电机等,需要使用外部开关来回零.下文描述了使用外部 ...
- ZT 设计模式六大原则(6):开闭原则
ZT 设计模式六大原则(6):开闭原则 分类: 设计模式 2012-02-27 08:48 24870人阅读 评论(72) 收藏 举报 设计模式扩展框架编程测试 定义:一个软件实体如类.模块和函数应该 ...
- mongodb 备份、还原、导入、导出
mongodump备份数据库 常用的备份命令格式 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 如果想导出所有数据库,可以去掉-d - ...
- Telnet配置
一.环境 路由 IP:192.168.56.2 本地云 IP:192.168.56.1 二.认证模式 AAA模式 认证 授权 计费的安全技术 当配置用户界面的认证方式为AAA时, 用户登录设备时需要首 ...