【codeforces 803D】Magazine Ad
【题目链接】:http://codeforces.com/contest/803/problem/D
【题意】
给你一个字符串;
其中的空格和连字符表示可以折叠的部分
(就是说能在那个位置把字符串分成两部分,且和两部分分到两行去);
这个操作能够使得原字符串不断变小;
问你最后获得的所有字符串(可能分裂成了多个,所以是”所有”)中最长的那个最短能够是多少;
(这个操作最多只能操作k次)
【题解】
二分答案;
枚举最后那个最长的长度是多少x;
现在,相当于让你最多切k-1刀;
使得k个部分,每个部分的长度都小于等于x;
问你可不可行;
这个模拟可不可行的部分贪心就能写;(加上这一段之后如果还能小于等于x,就让它们在同一段,否则到另外一段(其实也就对应了在不同的行));
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+100;
int k,nex[N],n;
char s[N];
bool ok(LL x)
{
LL now = 0,tot = 0;
for (int i = 1;i<=n; )
{
if (nex[i]-i+1>x) return false;
if (now+nex[i]-i+1<=x)
{
now+=nex[i]-i+1;
i = nex[i]+1;
}
else
{
now = 0,tot++;
}
}
if (now) tot++;
return tot<=k;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
// ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> k;char t = getchar();
gets(s+1);
n = strlen(s+1);
int be = n;
rep2(i,n,1)
{
if (isupper(s[i])||islower(s[i]))
continue;
nex[i+1] = be;
be = i;
}
nex[1] = be;
LL l = 1,r = n,ans;
while (l<=r)
{
LL mid = (l+r)>>1;
if (ok(mid))
{
ans = mid,r = mid-1;
}
else
l = mid+1;
}
cout << ans << endl;
return 0;
}
【codeforces 803D】Magazine Ad的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 787A】The Monster
[题目链接]:http://codeforces.com/contest/787/problem/A [题意] 把b一直加a->得到x 把d一直加c->得到y 然后问你x和y可不可能有相同 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- UVA - 10029 Edit Step Ladders (二分+hash)
Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...
- fragment.setMenuVisibility setUserVisibleHint
[Android]Fragment真正意义上的onResume和onPause 前言 Fragment虽然有onResume和onPause的,但是这两个方法是Activity的方法,调用时机也是与A ...
- (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...
- 写一个类似淘宝的ios app需要用到哪些技术?
写一个类似淘宝的ios app需要用到哪些技术? 让我想起了有人私信我,说不缺钱,做个类似知乎的东西,包括加运营,需要多少钱. 扯淡结束,正好最近看了一点这方面的东西,也许对题主来说有点帮助. 手机淘 ...
- 第19章 Redis的一些常用技术
19.1 Redis的基础事务 图19-1 Redis命令执行事务的过程 19-1:在Spring中使用Redis ...
- selenium 最大化浏览器是解决浏览器和驱动不匹配的方法如下
那么要想selenium成功的操作chrome浏览器需要经历如下步骤: 1.下载ChromeDriver驱动包(下载地址: http://chromedriver.storage.googleapis ...
- BeautifulSoup 库的使用记录
BeautifulSoup 有何用途 如果我们需要通过脚本来抓取网络中的数据时,使用传统的字符解析等方法时是非常低效的,而BeautifulSoup则可以方便的通过接口来获取标签中所想要得到的数据.主 ...
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'
ALTER TABLE table1 ADD COLUMN column1 VARCHAR(400) DEFAULT NULL; ERROR 1062 (23000): Duplicate entry ...
- python安装Django需要环境
Django==1.10.3 -e git+https://github.com/duoshuo/duoshuo-python-sdk.git#egg=duoshuo-python-sdk djang ...
- preparedStatement平台:
public class cs{ public static void main(String[] args){ try{ class.forName("com.mysql.jdbc.Dri ...