二分。

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

#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. [吴恩达机器学习笔记]12支持向量机3SVM大间距分类的数学解释

    12.支持向量机 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考资料 斯坦福大学 2014 机器学习教程中文笔记 by 黄海广 12.3 大间距分类背后的数学原理- Mathematic ...

  2. codeblocks 设置代码自动补全

    熟悉使用一些开发类IDE的朋友对代码自动补全一定印象深刻,如Visual studio,eclipse等,我们在程序中定义的那一个个超长的变量函数名只需打出几个字母就可自动补全,但是在codebloc ...

  3. CSS居中之美

    关于居中,你会想到什么? div{margin: auto;} 常见的居中方法 水平居中 .demo{ text-align: center; margin: auto; position: abso ...

  4. node、npm及node_modules中依赖的版本更新

    好久没用node了,想重新拾起来发现node还有相关模块的版本都太低了,使用npm install全是报版本低的警告. 这里记录一下,版本管理和node_modules更新的方法. 我用的是Windo ...

  5. JSON与JS的区别以及转换

    JSON是什么?(JSON和JavaScript对象有什么区别?)如何把JS对象转化为JSON字符串,又如何把JSON字符串转化为JavaScript对象? JSON (JavaScript Obje ...

  6. Linux内核多线程实现方法 —— kthread_create函数【转】

    转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...

  7. utsrelease.h 包含svn信息

    utsrelease.h是一个自动生成的文件,没有办法修改,但这个数据是根据Makefile和.config的内容进行生成的,通过修改这两个文件的内容,可以改变!/usr/src/linux/Make ...

  8. Python 生成随机数

    import random x = int(input('Enter a number for x: '))  --随机数最小值y = int(input('Enter a number for y: ...

  9. Codeforces Round #502

    Codeforces Round #502 C. The Phone Number 题目描述:求一个\(n\)排列,满足\(LIS+LDS\)最小 solution 枚举\(LIS\),可证明\(LD ...

  10. spring中的任务调度Quartz

    Spring 整合 Quartz 任务调度 主要有两种方式. Quartz的官网:http://www.quartz-scheduler.org/ 这两种只是一些配置文件简单配置就OK了,但是根本无法 ...