【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 ...
随机推荐
- Android 5.0 怎样正确启用isLoggable(二)__原理分析
前置文章 <Android 5.0 怎样正确启用isLoggable(一)__使用具体解释> 概要 在上文<Android 5.0 怎样正确启用isLoggable(一)__使用具体 ...
- URL重写及ASP.NET路由、Http处理模块、程序等
这段时间在学习ASP.NET路由.HTTP处理等内容,了解了一些,但又未完全弄懂,似是而非,不管如何,作一总结,供日后借鉴和修改. 一.IIS6和IIS7经典模式和集成模式 在IIS6及IIS7经典模 ...
- ETL (数据仓库技术)
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL一词较常用在数 ...
- Build website project by roslyn through devenv.com
1.fetch the source code2.compile controls project3.copy files under bin folder of controls to bin fo ...
- [PHP] Ecstore 开发学习资源
一.ECOS框架图 二.学习资源链接 PHP之窗 http://www.phpwindow.com/ ECOS百科大全 http://www.ec-os.net/doc.html
- php 判断过去离现在几年的函数
function gettime($worktime){ $time=time(); $amount=date("Y",$time)-date("Y",strt ...
- 【NOIP2011 Day 1】选择客栈
[问题描述] 丽江河边有n家客栈,客栈按照其位置顺序从1到n编号.每家客栈都按照某一种色调进行装饰(总共k种,用整数0 ~ k-1表示),且每家客栈都设有一家咖啡店,每家咖啡店均有各自的最低消费.两位 ...
- Xcode7.3 使用NSURLSession发送HTTP请求报错
控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it i ...
- A - I Wanna Be the Guy
Problem description There is a game called "I Wanna Be the Guy", consisting of n levels. L ...
- java8-3-LambdaMapReduce例子
public class LambdaMapReduce { private static List<User> users = Arrays.asList( new User(1, &q ...