原題:
D. Magazine Ad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
Output minimal width of the ad.
Examples
Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10
Note
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
中文:
想給你一個整數k,然後給你一個帶有空格和’-‘的字串,如果有空格和’-‘可以進行換行(要求空格和’-‘必須在換到上一行的字串的結尾)。現在讓你最多換k行,問你在滿足上面換行條件下,換行後的一列字串中,最長字串最短。
//自己寫儲存中間結果的代碼#include<bits/stdc++.h>using namespace std;typedef long long ll;vector<string> vs;int main(){ ios::sync_with_stdio(false); int n; string s; while(cin>>n) { cin.ignore(); getline(cin,s); string tmp; vs.clear(); for(int i=0;i<s.size();i++) { if(s[i]!=' '&&s[i]!='-') tmp+=s[i]; else { tmp+='.'; vs.push_back(tmp); tmp.clear(); } } vs.push_back(tmp); int L=1,R=s.size(),mid; int ans,res=0; while(R-L>1) { mid=(L+R)/2; // cout<<L<<" "<<mid<<" "<<R<<endl; // mid=8; ans=1;// res=0; for(int i=0;i<vs.size();) { if(res+vs[i].size()<=mid) { res+=vs[i].size(); i++; } else { if(res==0) break; ans++; res=0; } } // for(int i=0;i<ans.size();i++) // cout<<ans[i]<<endl; if(res==0) L=mid; else { if(ans<=n) R=mid; else L=mid; } } cout<<R<<endl; } return 0;}
//別人的代碼#include <iostream>using namespace std;const int INF = (int)1e9;int n,k,r,l;string s;int solve(int w){ int ans = 0; int l = 0; while(l < n) { ans++; int r = l + w; if (r >= n) break; while(r > l && s[r - 1] != ' ' && s[r - 1] != '-') r--; if (r == l) return INF; l = r; } return ans;}int main(){ cin >> k; getline(cin, s); getline(cin, s); n = s.length(); int l = 0, r = n; while(r - l > 1) { int m = (l + r) / 2; if (solve(m) <= k)//小於等於 r = m; else l = m; } cout << r << endl;//輸出右端點 return 0;}
思路:
最大值最小,最小值最大等等問題是典型的二分法問題。
此題也不例外
大概思路如下:
二分最長字串的長度上界mid,然後按照長度mid劃分字串換行。如果換行的個數大於k,那說明二分的長度上界太短了,分出的行數過多,否則分出的行數如果小於等於k,那就說明還可以把字串拉長一些。按照二分得到的字串長度上界,貪心累加字串長度,如此下來,時間複雜度為 二分 * 字串長度O(M*logN)