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的更多相关文章

  1. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

  2. Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)

    Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...

  3. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  4. Magazine Ad CodeForces - 803D (二分+贪心)

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...

  5. 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 ...

  6. CodeForces 803D Magazine Ad

    二分. 首先把字符串处理成一个数组,二分答案,判断一下即可. #include <cstdio> #include <cmath> #include <set> # ...

  7. 【贪心+二分】codeforces D. Magazine Ad

    codeforces.com/contest/803/problem/D [题意] 给定一个字符串,字符串里可能有空格和连字符‘-’,空格和连字符的意义是一样的,都表示:能在那个位置把字符串分成两部分 ...

  8. 【codeforces 803D】Magazine Ad

    [题目链接]:http://codeforces.com/contest/803/problem/D [题意] 给你一个字符串; 其中的空格和连字符表示可以折叠的部分 (就是说能在那个位置把字符串分成 ...

  9. rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial

    路由及路由参数 <%= link_to 'My Blog', {controller: 'articles', demo: "lidsi"}, class: "bl ...

随机推荐

  1. [转载]三小时学会Kubernetes:容器编排详细指南

    原翻译by梁晓勇 原英文:Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers 我很奇怪,为什 ...

  2. 关于实现mybatis order by 排序传递参数实现 问题记录

    一    问题场景:本人项目纯纯的后端系统  并且项目前端采用纯纯的原生js 实现 1)表格  通过查询列表数据放入到域中  前段采用 for循环的方式实现遍历生成列表 2)分页实现本人是公司内部自定 ...

  3. Thymeleaf 常用th标签基础整理

    (一)Thymeleaf 是个什么?      简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...

  4. ora-12154 TNS:"无法处理服务名"的一个解决方法

    http://www.cnblogs.com/xh3/archive/2007/04/21/722217.html 很怪异的一个问题,在网络环境下配置客户端,竟然怎么也连不上主机了,看了不少帖子,大多 ...

  5. XStream轻松转换xml和java对象

    首先引入所需的jar: xstream-1.4.9.xpp3_min-1.1.4c.dom4j-1.6.1, 或用maven管理jar包时在pom.xml中添加: <!-- https://mv ...

  6. oracle 开启归档日志模式

    摘自:https://www.jianshu.com/p/f8c0e9309ce2 在默认情况下,oracle数据库是在非归日志档模式中创建的,在非归档日志模式中,进行日志切换时会直接重写redo l ...

  7. Django笔记 —— Admin(Django站点管理界面)

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  8. Vue一些重要的知识点

    vue sync修饰(1)双向数据绑定,父子组件之间信息的交互 1⃣️在自组件中使用this.emmit('toFather'),子组件产生一个tofather事件,然后在父组件中通过@进行监听,那么 ...

  9. SQL - SELECT COUNT用法

     SQL Server数据库  COUNT() 函数返回匹配指定条件的行数.   语法   SQL COUNT(column_name) 语法   COUNT(column_name) 函数返回指定列 ...

  10. CSP201604-1:折点计数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...