【贪心+二分】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 = { ...
随机推荐
- Linux下用matplotlib画决策树
1.trees = {'no surfacing': { 0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}} 2.从我的文件trees.txt里读的决策树, ...
- P3372 【模板】线段树 1 区间查询与区间修改
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...
- virtualbox没有64位选项
今天安装的virtualbox想安装一下sql server 测试一下 在安装系统的时候发现没有64位系统的选项,在网上找了一下 发现是 在BIOS里面有一个选项没有开启, 是 Intel virt ...
- android和IOS长连接区别
http://blog.csdn.net/zhangzeyuaaa/article/details/39028369 首先我们必须知道,所有的推送功能必须有一个客户端和服务器的长连接,因为推送是由服务 ...
- liunx 修改IP地址
1.安装centos系统,这里就不详细说明了. 2.进入到 vi /etc/sysconfig/network-scripts/ifcfg-eth0 后面的名称有些可能不同 其中,有些可能没 ...
- pip install python-igraph 报错,C core of igraph 没有安装。
(一)问题描述 Centos7 安装python-igraph时,pip install python-igraph 报错,C core of igraph 没有安装. failure: repoda ...
- myBatis的binding错误:Invalid bound statement (not found)
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误这个问题我找了好久,终于找到了正确的写 ...
- 自定义分隔符|/i|/x|/xs|需要转译
小骆驼 第八章 用正则表达式进行匹配 #!/usr/bin/envperl use strict; use warnings; $_ ="#adchbehnyhme3534f\nvdh5ej ...
- 如何让线程A等待B执行结束后执行?
1. 使用条件变量 判断是否任务B已经做完,然后再执行任务A. 测试代码可看:https://blog.csdn.net/guochao6531/article/details/78075882 2. ...
- FFT NTT 模板
NTT: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; # ...