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 ... 
随机推荐
- 归并排序算法Matlab实现
			Matlab一段时间不用发现有些生疏了,就用归并排序来练手吧.代码没啥说的,百度有很多.写篇博客,主要是记下matlab语法,以后备查. 测试代码 srcData = [1,3,2,4,6,5,8 ... 
- sqoop简单介绍
			一简介 Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如 : MySQL ,Oracle ,Postgres等)中的数据导进到Hadoop的HDFS ... 
- 【转】Web服务器之Nginx详解(理论部分)
			大纲 一.前言 二.Web服务器提供服务的方式 三.多进程.多线程.异步模式的对比 四.Web 服务请求过程 五.Linux I/O 模型 六.Linux I/O 模型具体说明 七.Linux I/O ... 
- idea整个项目乱码解决办法
			idea前几天还好好的,这两天一直出现乱码.不止一个文件,是整个项目都有这个问题.想想这几天什么也没做啊... 上网搜了下 解决. 经过多次排查寻找,终于,在项目的目录下有个.idea的文件夹,这个文 ... 
- idea maven 依赖问题
			今天发现,原来idea引用maven依赖的时候,优先依赖的是本地.但是有个问题,如果我删除了一个packet,如果刚刚好本地另外一个项目里面也有这个对于的packet,idea的智能提示 是 将其导入 ... 
- RunDll32.exe 详解及[Windows批处理]清除IE缓存
			Rundll32命令详解 文件作用:执行32位DLL文件中的内部函数 位置:X:(当前系统分区)\windows\system32 命令语法: Rundll32.exe DLLname,Functio ... 
- https填坑之旅
			Boss说,我们买了个权威证书,不如做全站式的https吧,让用户打开主页就能看到受信任的绿标.于是我们就开始了填坑之旅. [只上主域好不好?] 不好...console会报出一大堆warning因为 ... 
- linux shell每天一阅 -- 安装nginx以及apache
			当然这个博客原代码是转载大神的... 自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本mynginx.sh #!/bin/sh ###nginx install ... 
- msvcr100.dll丢失原因及解决方法
			msvcr100.dll为Visual Studio 2010的一个动态链接库,如果某程序是用它开发出来的,那么该程序的运行就有可能需要此动态链接库.有些程序直接将其打包到了安装目录,并注册,就不会出 ... 
- 迷宫问题求解——C++
			迷宫问题思路 根据昨天的博客,有如下几种解决方案 克鲁斯卡尔 ,为避免死循环,需要设定优化路径的次数. Prim,为避免死循环,需要设定优化路径的次数,暂定200次. BFS , 实现简单,无死循环. ... 
