二分。

首先把字符串处理成一个数组,二分答案,判断一下即可。

#include <cstdio>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std; int p;
int a[1000010],sz;
char s[1000010]; void work()
{
int sum=0;
for(int i=0;s[i];i++)
{
sum++;
if(s[i]==' ')
{
a[sz++] = sum;
sum=0;
}
else if(s[i]=='-')
{
a[sz++] = sum;
sum=0;
}
}
if(sum)
{
a[sz++] = sum;
}
} bool check(int x)
{
int sum=0;
int y=0; for(int i=0;i<sz;i++)
{
if(a[i]>x) return 0;
} for(int i=0;i<sz;i++)
{
if(sum+a[i]<=x)
{
sum=sum+a[i];
}
else
{
sum = a[i];
y++;
}
} if(sum) y++; if(y<=p) return 1;
return 0;
} int main()
{
scanf("%d",&p);
getchar();
gets(s);
work(); int ans,L=1,R=1000000; while(L<=R)
{
int mid = (L+R)/2;
if(check(mid)) ans = mid,R = mid-1;
else L = mid+1;
} printf("%d\n",ans); return 0;
}

CodeForces 803D Magazine Ad的更多相关文章

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

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

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

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

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

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

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

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

  5. codeforces803D. Magazine Ad

    D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...

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

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

  7. 【codeforces 803D】Magazine Ad

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

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

  9. codeforces 576 div2 A-D题解

    A题 Description 题目链接: https://codeforces.com/contest/1199/problem/A 题意: 给定长度为n(1≤n≤100000)的一个序列a,以及两个 ...

随机推荐

  1. python---Celery分布式任务队列了解

    linux下定时器了解 Celery 框架学习笔记(不错哟) Celery 分布式任务队列快速入门 Celery的最佳实践 一.Celery介绍 Celery 是一个 基于python开发的分布式异步 ...

  2. 使用FormData提交表单及上传文件

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  3. 每个Web开发者都需要具备的9个软技能

    对于一份工作,你可能专注于修炼自己的内功,会在不自觉中忽视软技能.硬技能决定你是否能得到工作,而软技能能够表明你是否适合这份工作和适应工作环境等.所有的公司都有属于自己的文化,并努力将这些文化传承下去 ...

  4. 【CodeForces】578 C. Weakness and Poorness

    [题目]C. Weakness and Poorness [题意]给定含n个整数的序列ai,定义新序列为ai-x,要使新序列的最大子段和绝对值最小,求实数x.n<=2*10^5. [算法]二分| ...

  5. python模块之StringIO/cStringIO(内存文件)

    1. StringIO/cStringIO是什么 这个模块提供了一个类,这个类的实例就像是一个文件一样可以读写,实际上读写的是一个字符串缓存,也可以称之为内存文件. StringIO和文件对象拥有共同 ...

  6. JWT 拓展

    JWT适用场景 https://www.jianshu.com/p/af8360b83a9f 适用于一次性操作的认证,颁布一个很短过期时间的JWT给浏览器. 无状态的JWT无法实现精确的在线人数统计. ...

  7. 使用Sass预定义一些常用的样式,非常方便

    CSS预处理技术现在已经非常成熟,比较流行的有Less,Sass,Stylus,在开发过程中提升我们的工作效率,缩短开发时间,方便管理和维护代码,可以根据自己的喜好选择一款自己喜欢的工具开发,使用很接 ...

  8. isolation forest进行异常点检测

    一.简介 孤立森林(Isolation Forest)是另外一种高效的异常检测算法,它和随机森林类似,但每次选择划分属性和划分点(值)时都是随机的,而不是根据信息增益或者基尼指数来选择.在建树过程中, ...

  9. Machine Learning系列--判别式模型与生成式模型

    监督学习的任务就是学习一个模型,应用这一模型,对给定的输入预测相应的输出.这个模型的一般形式为决策函数:$$ Y=f(X) $$或者条件概率分布:$$ P(Y|X) $$监督学习方法又可以分为生成方法 ...

  10. Python 中的闭包与装饰器

    闭包(closure)是函数式编程的重要的语法结构.闭包也是一种组织代码的结构,它同样提高了代码的可重复使用性. 如果在一个内嵌函数里,对在外部函数内(但不是在全局作用域)的变量进行引用,那么内嵌函数 ...