二分。

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

#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. Resharper报“Possible multiple enumeration of IEnumerable”

    问题描述:在IEnumerable使用时显示警告 分析:如果对IEnumerable多次读取操作,会有因数据源改变导致前后两次枚举项不固定的风险,最突出例子是读取数据库的时候,第二次foreach时恰 ...

  2. struts标签错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"

    今天使用eclipse开发ssh,出现Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/co ...

  3. Java enum的用法详解 (转)

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...

  4. bzoj 2502 清理雪道 (有源汇上下界最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  5. Spring Boot 使用IntelliJ IDEA创建一个web开发实例(一)

    .新建项目File-->New-->Project-->Spring Initializr 点击Finish,一个Spring Boot web应用就创建好了.

  6. Struts2版本更新报错:>>> ActionContextCleanUp <<< is deprecated! Please use the new filters!

    因低版本Struts2存在漏洞,更新为较新的版本.启动时,报如下警告信息: ************************************************************** ...

  7. 【51nod】1222 最小公倍数计数 莫比乌斯反演+组合计数

    [题意]给定a和b,求满足a<=lcm(x,y)<=b && x<y的数对(x,y)个数.a,b<=10^11. [算法]莫比乌斯反演+组合计数 [题解]★具体 ...

  8. 配置node,sass,淘宝镜像环境

    由于最近由于刚到手一台新的thinkpad(哈哈,宝宝是个小穷B,木有小苹果),所以工作开发中所用到的环境就需要重新安装一下啦,这里的话,我就把我目前所用到的进行总结一下,其余的会在以后的开发过程中, ...

  9. 8、V模型、W模型、H模型

    软件测试&软件工程 ·软件测试与软件工程息息相关,软件测试是软件工程组成中不可或缺的一部分.·在软件工程.项目管理.质量管理得到规范化应用的企业,软件测试也会进行得比较顺利,软件测试发挥的价值 ...

  10. CRF原理解读

    概率有向图又称为贝叶斯网络,概率无向图又称为马尔科夫网络.具体地,他们的核心差异表现在如何求  ,即怎么表示  这个的联合概率. 概率图模型的优点: 提供了一个简单的方式将概率模型的结构可视化. 通过 ...