二分。

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

#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. 前端PHP入门-012-回调函数[慎入]

    尽力而为,对于WEB前端和美工同学,比较难了!但是你们都学过JS的闭包等操作,那么这里也一定没有问题! 回调函数,可以配合匿名函数和变量函数实现更加优美.复杂的一种函数结构. 回调函数,就是在处理一个 ...

  2. 为VSCODE添加右键菜单

    参考:https://blog.csdn.net/GreekMrzzJ/article/details/82194913 1.创建一个名为vscode.reg的空文本文件,填入下列内容 Windows ...

  3. POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

    ---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...

  4. GridControl详解(八)菜单

    菜单控件 拖入窗口中 显示如下 设置popupMenu 设置barManager 设置controller 增加菜单项 弹出配置窗口 一般菜单项设置 对应属性如下: 对应事件: 选择菜单项设置 事件同 ...

  5. Configure Always On Availability Group for SQL Server on Ubuntu——Ubuntu上配置SQL Server Always On Availability Group

    下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...

  6. VueJS 轻松支持 JSX 配置

    使用: babel-preset-vue-app TODO

  7. pythonif语句和循环语句

    1.if语句用法 # if语句用法(缩进相同的成为一个代码块) score=90 if score>=60: print("合格") print("OK" ...

  8. OGG相关操作

    参数文件详解: 1)truncate ogg 进程: Manager进程:manager进程是配置在源端和目标端 Extract(抽取)进程:部署在源端,用于捕获数据表或者日志中的数据文件: Pump ...

  9. kernel随机生成MAC地址的接口

    /**  * eth_random_addr - Generate software assigned random Ethernet address * @addr: Pointer to a si ...

  10. 微信小程序开发定制

    上海软件定制专家:http://www.dzonly.com/?from=timeline