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的更多相关文章

  1. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

  2. CodeForces 803D Magazine Ad

    二分. 首先把字符串处理成一个数组,二分答案,判断一下即可. #include <cstdio> #include <cmath> #include <set> # ...

  3. Magazine Ad CodeForces - 803D(二分 + 贪心,第一次写博客)

    Magazine Ad The main city magazine offers its readers an opportunity to publish their ads. The forma ...

  4. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  5. 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 ...

  6. codeforces803D. Magazine Ad

    D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...

  7. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  8. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. 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 = { ...

随机推荐

  1. PowerShell~发布你的mvc网站

    通过使用ps加上msbuild可以方便的编译你的.net应用程序,并且可以把它发布到你的磁盘上,部署非常方例! 我们在c盘添加一个hello网站,解决方案名是hello.sln,它的网站是hello. ...

  2. AJPFX关于java中的方法

    java中的方法和c语言中的函数类似,是新手入门面向对象之前的内容最大的难关如何写方法        1,明确返回值类型        2,明确参数列表 * 修饰符:目前就用 public stati ...

  3. 深入Docker 存储驱动 (转)

    参考: http://static.dockerone.com/ppt/filedriver.html#28

  4. SQLite – ORDER 子句

    SQLite - ORDER BY子句 The SQLite ORDER BY子句用于数据按升序或降序排序,基于一个或多个列. 语法: ORDER BY子句的基本语法如下: SELECT column ...

  5. docker 新手入门(docker的安装)

    docker的安装(在centos7下面) 1. 卸载在liunx下,先看有没有安装docker,docker version,如果有的话,可以先移除 yum remove ........ 可以使用 ...

  6. 汇编2.汇编版本的helloworld

    寻址方式 立即数寻址 寄存器寻址 存储器寻址 直接寻址 : mov ax, [ 01000h ]; 直接在[]内给出一个内存地址 寄存器间接寻址: mov ax ,[si]; 在[]以寄存器的值给出内 ...

  7. vue 组件名和方法名 重名了,报function错误

    vue 组件名和方法名 重名了,报function错误

  8. docker的安装及基础操作与镜像构建

    仓库配置及安装启动 [root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 [root@loca ...

  9. Kubernetes 架构(上)【转】

    Kubernetes Cluster 由 Master 和 Node 组成,节点上运行着若干 Kubernetes 服务. Master 节点 Master 是 Kubernetes Cluster ...

  10. Ioc 之 Unity的依赖注入

    Unity是微软官方提供的一个Ioc容器,用来实现依赖注入,减少代码之间的耦合程度.使用Unity实现Ioc方式有两种,一种是使用代码方式实现,一种是利用配置文件来实现. 我们先来看一下代码方式是如何 ...