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 ...
随机推荐
- 关于zepto 选择特定值的input 报错问题
zepto 选择特定值的input 时,需要用单引号或双引号引用这个特定值 否则 报错
- Apache服务器运维笔记(5)----容器的处理顺序
容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的. ...
- Windows API 编程-----Windows NT 环境下禁止任务切换
函数原型: BOOL WINAPI SystemParametersInfo( _In_ UINT uiAction, _In_ UINT uiParam, _Inout_ PVOID pvParam ...
- 安卓app应用开发资料
android 配置文件画图 http://blog.csdn.net/loongggdroid/article/details/46687589 android下拉刷新控件 https://gith ...
- Windows ->> Windows下一代文件系统 -- Resilient file system(ReFS)
Comming soon!!! 参考文献: Building the next generation file system for Windows: ReFS ReFS: What you need ...
- viirtualBox显示不了Ip并且无法上网的解决方式
首先描述下我自己遇到的问题:就是在virtualBox下的ubuntu系统下,输入ifconfig,没有显示出ip,显示出了eth3,lo的相关信息.在网上也找了相关信息还是无法解决,终于在老大的 ...
- MySQL案例01:Last_SQL_Errno: 1755 Cannot execute the current event group in the parallel mode
周五同事监控报警,有个MySQL从库复制状态异常,让我帮忙排查下,经过排查发现是MySQL5.6并行复制的一个Bug所致,具体处理过程如下: 一.错误信息 登录mysql从库服务器,检查复制状态 my ...
- VS断点不生效
工程属性页中“配置属性”->“C/C++”->“常规”->“调试信息格式”,选择“用于“编辑并继承”的程序数据库(/ZI)”. 在“配置属性”->“链接器”->“调试”- ...
- IE浏览器兼容问题(下)——IE6的常见问题
IE6常见兼容性问题 1.盒模型问题 (1)DTD问题 DTD:文档定义类型,规定了要遵循的书写规范. 如果不写DTD,高级浏览器还是可以正常加载,IE6会以怪异模式进行加载. 盒模型:正常应该是外扩 ...
- css笔记 2
定义一个类选择器.center {text-align: center} h1 有 center 类.这意味将遵守 ".center" 选择器中的规则.<h1 class=& ...