Magazine Ad CodeForces - 803D (二分+贪心)
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 题目链接:https://vjudge.net/problem/CodeForces-803D#author=0
题意:将一个带空格和分隔符的字符串以空格或者分隔符为分割点,分割成多行字符串,并且行数小于给定的k值,
而且每一行的字符串长度尽可能的大,问最大的长度是多少? 思路:显然可知:如果一个最大长度x满足条件,x+1,x+2等等也一定满足,即为单调的,那么我们就可以快乐的二分了
注意下二分的区间是有限制的,总字符串中可以分割成每一个小段,这些小段中最长的那个即为区间的左端点,因为比他还短一定不能存在的。
区间的右端点即为字符串的总长度。
然后check函数即只需要贪心的求以mid为最长字串长度去分割要产生多少行,如果行数小于k就满足,反而反之。
博主的博客地址:https://www.cnblogs.com/qieqiemin/
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int k;
string s;
vector<int> v;
int cnt;
bool check(int mid)
{
int x;
// int last=0;
int m=;
int num=;
for(int i=;i<cnt;i++)
{
x=v[i];
m+=x;
if(m>mid)
{
m=x;
num++;
}
}
num++;
return num<=k;
}
int main()
{
cin>>k;
getchar();
getline(cin,s);
int len=s.length();
int st=;
int pos=-;
for(int i=;i<len;i++)
{
if(s[i]==' '||s[i]=='-')
{
v.push_back(i-pos); st=max(st,i-pos);
pos=i;
}
}
st=max(st,len-pos-);
v.push_back(len-pos-);
cnt=v.size();
// for(int i=0;i<cnt;i)
int l=st;
int r=len;
int mid;
int ans;
while(l<=r)
{
mid=(l+r)>>; if(check(mid))
{
r=mid-;
ans=mid;
}else
{
l=mid+;
}
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Magazine Ad CodeForces - 803D (二分+贪心)的更多相关文章
- 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 ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- CodeForces - 551C 二分+贪心
题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...
- Codeforces 825D 二分贪心
题意:给一个 s 串和 t 串, s 串中有若干问号,问如何填充问号使得 s 串中字母可以组成最多的 t 串.输出填充后的 s 串. 思路:想了下感觉直接怼有点麻烦,要分情况:先处理已经可以组成 t ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- codeforces803D. Magazine Ad
D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...
随机推荐
- SpringMVC中与Spring相关的@注解
一.Spring的常用组件类注解 @Component 被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Repository.在 ...
- DevExpress gridcontrol gridView主从表折叠/展开显示
在使用报表的时候,有很多需要主从表一起显示,从表不需要另外弹窗显示明细,反而直接显示在主表下方.如图所示: 第一次做这个功能,主从表显示,从表列隐藏,从表单元格点击事件这三个功能点花费了很多时间,在网 ...
- swift函数的调用约定
The convention of the function, indicated by the attribute. This is similar to the language-level @c ...
- Qt 编程指南 4 按钮
1按钮类的控件 逐个解释一下各个用途:(1)按压按钮 QPushButton最基本的按钮,点击该按钮通常是通知程序进行一个操作,比如弹个窗.下一步.保存.退出等等,这是经常用到的,操作系统里的对话框里 ...
- 使用cmd导出mysql数据到excel
windows环境 (有时候复制的不好使,最好可以手动输入一次试试) 1.windows + R 输入cmd弹出命令框 2.cd C:\Program Files\MySQL\MySQL Serve ...
- Jquery弹窗组件
下面是写的简单的Jquery弹窗组件 暂不支持animate,只能满足一般的弹窗显示隐藏需求,更多功能后续会完善!网上及jquery组件很多这样的弹窗,但是用别人的感觉心里过不去,所以就随便写写,当做 ...
- 20175105 2018-2019-2 《Java程序设计》第八周学习总结
20175105 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章主要内容有:泛型.链表.堆栈.散列映射.树集以及树映射. 泛型:可以使用class名 ...
- android java.lang.NoClassDefFoundError: cn.yw.lib.viewpagerfragment.ViewPagerFragmentActivity
假如你判断你的项目没有异常,并且该注册的Activity也注册了.那么解决这个问题的方法就是:右击项目->properties->Java Build Path->Order and ...
- 创建http.Server实例,端口占用就换个端口
/** * Created by Sorrow.X on 2017/10/25. */ const http = require('http'); const url = require('url') ...
- jquery $.each()遍历json数组
使用jQuery的$.each()方法来遍历一个数组对象 var json=[ {"id":"1","tagName":"appl ...