【贪心+二分】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 = { ...
随机推荐
- MVC:html动态追加行及取值
先一个button id=addRow 点击事件进行添加 $("#addRow").bind("click", function () { var addH ...
- iOS 根据屏幕宽度, 高度判断手机设备
#define iPhone_5 [UIScreen mainScreen].bounds.size.width == 320.0 #define iPhone_6 [UIScreen mainScr ...
- webuploader上传工具
http://fex.baidu.com/webuploader/getting-started.html#显示用户选择 Html部分 首先准备dom结构,包含存放文件信息的容器.选择按钮和上传按钮三 ...
- centos系统iptables使用帮助
#如果只是想屏蔽IP的话“开放指定的端口”可以直接跳过.#屏蔽单个IP的命令是iptables -I INPUT -s 123.45.6.7 -j DROP#封整个段即从123.0.0.1到123.2 ...
- ETH Dapp 体验报告
Dapp 体验报告 Dapp是分散式的应用程序.DApp运行在去中心化的网络上,也就是区块链网络中.网络中不存在中心化的节点可以完整的控制DApp. 必须依赖合约部署,没有一个中心化的服务器托管. 对 ...
- .netcore中使用EFCore连接SQL Server并部署至Ubuntu
前面一篇记录了如何在windows下开发asp.net core程序,并部署至ubuntu系统中.但仅仅是建立了一个demo项目,项目本身并没有实现多少功能.多数时候,我们的项目是要和数据库打交道.E ...
- UVA 10288 Coupons 彩票 (数学期望)
题意:一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最简的分数形式表示答案.n<=33. 思路:这题实在好人,n<=33.用longlong ...
- 版本号对比方案及参考代码(Objective-C,Java,JavaScript)
常用版本号 如 2.0.1 与 2.0.2 相比 2.0.2是比2.0.1要新的 那么该如何对这个版本号进行对比 这里有一个比较简单的实现方案 2.0.1 这种格式可以拆分为多个部分 如这里的2是大 ...
- jquery命名冲突
nodeName是jquery的关键字
- Linux-RedHat7.2 安装nginx托管.net core2.0
1.安装依赖包 yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel 2.下载安装包 wge ...