【贪心+二分】codeforces D. Magazine Ad
codeforces.com/contest/803/problem/D
【题意】
给定一个字符串,字符串里可能有空格和连字符‘-’,空格和连字符的意义是一样的,都表示:能在那个位置把字符串分成两部分,且两部分分到两行去,空格或连字符留在当前行。 这个分裂操作能够使得原字符串不断变短;
问你最后获得的所有字符串(可能分裂成了多行,所以是”所有”)中最长的那个最短能够是多少;
分裂操作最多只能操作k次;
【思路】
这种题已经做过多次了,要找最长的那个最短的,直接二分查找最短值,每次贪心划分,看总行数是不是小于等于k。
时间复杂度O(|s|log|s|),|s|是字符串的长度。
【注意】
给定的字符串有空格,所以用
getchar();
gets(c);
读取有空格字符串
【Accepted】
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
int n;
const int maxn=1e6+;
char c[maxn];
int len;
bool judge(int mid)
{
int cnt=;
int l=;
while(l<len)
{
if(l+mid>=len)
{
cnt++;
break;
}
if(c[l+mid-]=='-'||c[l+mid-]==' ')
{
cnt++;
l=l+mid;
}
else
{
int index=-;
for(int i=l;i<l+mid;i++)
{
if(c[i]=='-'||c[i]==' ')
{
index=i;
}
}
if(index==-)
{
return false;
}
cnt++;
l=index+;
}
}
if(cnt<=n)
{
return true;
}
return false;
}
int main()
{
while(~scanf("%d",&n))
{
getchar();
gets(c);
len=strlen(c);
int l=;
int r=len;
while(l<=r)
{
int mid=(l+r)>>;
if(judge(mid))
{
r=mid-;
}
else
{
l=mid+;
}
}
printf("%d\n",l);
}
return ;
}
【贪心+二分】codeforces D. Magazine Ad的更多相关文章
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- CodeForces 803D Magazine Ad
二分. 首先把字符串处理成一个数组,二分答案,判断一下即可. #include <cstdio> #include <cmath> #include <set> # ...
- Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)
Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- codeforces803D. Magazine Ad
D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- Card Game Cheater(贪心+二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
随机推荐
- wp跳转到评价界面代码
wp跳转到评价界面代码(仅适用于wp8.0) MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask(); ma ...
- 基于udp协议的套接字及udp协议粘包问题
udp协议的套接字 udp协议传输 服务端和客户端没有建立连接一说. import socket # 总结一下基础工作流程:服务端生成套接字并绑定ip_port,进入数据传输循环,服务端接受客户端发 ...
- 6 Specialzed layers 特殊层 第二部分 读书笔记
CAGradientLayer CAGradientLayer is used to generate a smooth gradient between two or more colors. ...
- array_keys
<?php$array = array(0 => 100, "color" => "red");print_r(array_keys($arr ...
- Oracle体系结构总览
第一篇 Oracle架构总览 先让我们来看一张图 这张就是Oracle 9i的架构全图.看上去,很繁杂.是的,是这样的.现在让我们来梳理一下: 一.数据库.表空间.数据文件 1.数据库 数据库是数 ...
- 【C++】双边滤波器(bilateral filter)
Bilateral Filtering for Gray and Color Images 双边滤波器:保留边界的平滑滤波器. 在局部上,就是在灰度值差异不大的区域平滑,在灰度值差异比较大的边界地区保 ...
- DFS、BFS和Backtracking模板
区别与联系 区别 DFS多用于连通性问题因为其运行思想与人脑的思维很相似,故解决连通性问题更自然,采用递归,编写简便(但我个人不这样觉得...) DFS的常数时间开销会较少.所以对于一些能用DFS就能 ...
- 时钟Demo
其实是一个很简单的Demo,可以编译了拿NSIS打包.最近在做富文本编辑器和补C++不记得的东西吧,项目遥遥无期. //clock.pro #----------------------------- ...
- Shell数值比较
Shell数值比较 比较 描述 n1 -eq n2 检查n1是否与n2相等 n1 -ge n2 检查n1是否大于或等于n2 n1 -gt n2 检查n1是否大于n2 n1 -le n2 检查n1是否小 ...
- 小程序之Button组件,函数的调用与简单的逻辑
我们要实现一个简单的功能,在界面上放置一张图片,设置重新加载按钮,能更新图片. WXML代码: <!--index.wxml--> <view clas="index&qu ...