CF803D:Magazine Ad(二分)
来源:互联网 发布:河南网络电视台直播 编辑:程序博客网 时间:2024/06/08 04:29
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.
When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.
The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.
You should write a program that will find minimal width of the ad.
The first line contains number k (1 ≤ k ≤ 105).
The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed 106 characters.
Output minimal width of the ad.
4garage for sa-le
7
4Edu-ca-tion-al Ro-unds are so fun
10
Here all spaces are replaced with dots.
In the first example one of possible results after all word wraps looks like this:
garage.for.sa-le
The second example:
Edu-ca-tion-al.Ro-unds.are.so.fun
思路:典型的最大值最小问题,预处理出每个分割点的位置,及其子串的长度,二分答案即可。
# include <bits/stdc++.h>using namespace std;const int maxn = 1e6+3;char s[maxn];int k, cnt, a[maxn];bool judge(int mid){ int icount = 1, sum = 0; for(int i=0; i<cnt; ++i) { sum += a[i]; if(sum > mid) { ++icount; sum = a[i]; if(icount > k) return false; } } return true;}int main(){ int i, p, imin=0; scanf("%d",&k); getchar(); gets(s); p = -1; for(i=0; s[i]; ++i) if(s[i]=='-' || s[i]==' ') imin = max(imin, i-p), a[cnt++] = i - p, p = i; a[cnt++] = i-p-1; imin = max(imin, i-p-1); int l=imin, r=strlen(s); while(l<=r) { int mid = l+r>>1; if(judge(mid)) r = mid - 1; else l = mid + 1; } printf("%d\n",l); return 0;}
- CF803D:Magazine Ad(二分)
- Codeforces 803D Magazine Ad【二分+贪心】
- Codeforces803D Magazine Ad
- Magazine Ad CodeForces
- codeforces 803D Magazine Ad
- CodeForces 803D Magazine Ad
- codeforces 803D Magazine Ad
- Codeforces 803D Magazine Ad 题解
- CF Educational Codeforces Round 20 D. Magazine Ad
- 「codeforces 」教育场20-D.Magazine Ad
- CSS3 (Smashing Magazine) 设计比赛 获奖作品
- IEEE magazine和journal的区别(转)
- poj 1695 Magazine Delivery(记忆化/dp)
- AD帳號解鎖(C#)
- ad
- ad
- AD
- ad
- 使用mybatis 通用Mapper类的笔记
- Windows下在某个目录下的所有文件中查找特定字符串
- 小白题解 Codeforces 785A
- socket 网络编程教程
- 算法设计与应用基础
- CF803D:Magazine Ad(二分)
- 使用防抖动(debounce)优化页面滚动
- Python: PS 图像调整--颜色梯度
- netdiscover
- 页面预加载loading动画,再载入内容
- 【如何快速的开发一个完整的iOS直播app】(采集篇)
- java abstract class vs interface
- 布隆过滤器 Bloom filter
- Struts2_Action中result的各种转发类型