【贪心+二分】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 = { ...
随机推荐
- PDO相关函数
(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0) PDO::__construct — 创建一个表示数据库连接的 PDO 实例 说明 PDO::__co ...
- A8ERP管理系统(采购单管理)
花了一个星期的时间终于把采购模块完成了. 最近新开发的采购单管理,供大家参考学习,软件一步一步来.
- websocket 加layim实现在线聊天系统
实现流程: 1.浏览器连接服务器时保存所有用户id以及对应的唯一session(session用户用户消息推送). 1.1:判断登录用户是否有离线消息(个人消息以及群消息),有则将离线消息进行推送给登 ...
- IOS 根据身份证号码获取 年龄 生日 性别
/** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...
- centos系统iptables使用帮助
#如果只是想屏蔽IP的话“开放指定的端口”可以直接跳过.#屏蔽单个IP的命令是iptables -I INPUT -s 123.45.6.7 -j DROP#封整个段即从123.0.0.1到123.2 ...
- java将一个List赋值给另一个List的4种方法
编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...
- CortexA7工业级迅为-iMX6UL开发板硬件和资料介绍
商业级核心板 ARM Cortex-A7架构 主频高达528 MHz 核心板512M DDR内存 8G EMMC 存储 运行温度:-20℃ ~ +80℃ CPU集成电源管理 核心板尺寸仅:42mm*3 ...
- centos6上安装mysql8.0版本
本博客是采用yum源的方式安装,非常的方便和快捷.(redhat 与centos7 等操作系统都可以采用此方法,步骤大体一致) mysql官网地址: https://dev.mysql.com 开 ...
- find、filter、map的区别
1.find 查询数组中符合条件的第一个元素,如果没有符合条件的元素则返回空数组 ,,,,,,] ) var men=[ {name:',sex:'女'}, {name:',sex:'nan'}, { ...
- AspNetCore容器化(Docker)部署(一) —— 入门
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...