二分。

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

#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. vue-router路由原理

    Vue-router路由原理 目前实现路由的方式有两中,vue通过参数mode来设置,默认是hash模式. 利用URL中的hash(‘#’)来实现 利用History interface在HTML5中 ...

  2. NAIPC2018-K-Zoning Houses

    题目描述 Given a registry of all houses in your state or province, you would like to know the minimum si ...

  3. 「LibreOJ β Round #4」子集

    https://loj.ac/problem/526 题目描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两个元素 i ...

  4. 您是哪个等级的CSS开发人员?

    我们在不断的学习,追求进步与提高,到底学到什么程度了,到底是 不是真的了解CSS,是哪个层次了呢.我们来对照一下. 第0级:CSS?那不是一个多人射击游戏吗?  CSS? Isn't that a m ...

  5. 【CodeForces】585 E. Present for Vitalik the Philatelist

    [题目]E. Present for Vitalik the Philatelist [题意]给定n个数字,定义一种合法方案为选择一个数字Aa,选择另外一些数字Abi,令g=gcd(Ab1...Abx ...

  6. 【STSRM13】绵津见

    [算法]扫描线:差分+树状数组 [题意]转化模型后:求每个矩形覆盖多少点和每个点被多少矩形覆盖.n<=10^5. [题解]经典的扫描线问题(二维偏序,二维数点). 数点问题 将所有询问离线并离散 ...

  7. 微信小程序开发(四)线程架构和开发步骤

    线程架构 从前面的章节我们可以知道,.js文件是页面逻辑处理层.我们可以按需在app.js和page.js中添加程序在生命周期的每个阶段相应的事件.如在页面的onLoad时进行数据的下载,onShow ...

  8. 七牛云 PHP SDK服务器鉴权失败!参数解释

    昨天搞了一下午,用7牛官方的SDK demo 1.上传凭证 $policy = array( 'callbackUrl' => 'http://api.example.com/qiniu/upl ...

  9. matlab实现cart(回归分类树)

    作为机器学习的小白和matlab的小白自己参照 python的 <机器学习实战> 写了一下分类回归树,这里记录一下. 关于决策树的基础概念就不过多介绍了,至于是分类还是回归..我说不清楚. ...

  10. 【译】Attacking XML with XML External Entity Injection (XXE)

    原文链接:Attacking XML with XML External Entity Injection (XXE) XXE:使用XML外部实体注入攻击XML 在XML中,有一种注入外部文件的方式. ...