您的位置:首页 > 其它

【codeforces 803D】Magazine Ad

2017-10-04 18:44 211 查看

【题目链接】: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;
char s
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: