Original question:
D. Magazine Ad
Time limit per test
1 second
Memory limit per test
Megabytes
Input
Standard input
Output
Standard output
The main city magazine offers its readers a opportunity to publish their ads. The format of the ad should is like this:
There is space-separated non-empty words of lowercase and uppercase Latin letters.
There is hyphen characters '-' in some words, their positions set word wrapping points. Word can include more than one hyphen.
It is guaranteed this there is no adjacent spaces and no adjacent hyphens. No Hyphen is adjacent to space. There is no spaces and no hyphens before the first word and after the last word.
When the word was 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 to the next line. You can also put-line break between-words, in the-case the space-stays on-line. Check notes for better understanding.
The ad can occupy no more, that k lines and should has 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'll 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
Output
10
Note
Here all spaces is 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
English:
Want to give you an integer k, and then give you a string with a space and '-', if there are spaces and '-' can be wrapped (requires a space and '-' must be at the end of the string to the previous line). Now let you change the most K line, ask you to meet the above line break conditions, after the line wrapped in a column string, the longest string of the shortest.
Write your own code to save intermediate results #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;
}
//someone else's code #include <iostream> using namespace std; const int INF = (int) 1e9; int n,k,r,l; str
ing 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)///less than or equal to r = m;
else L = m;
} cout << r << endl;//output right endpoint return 0; }
idea:
Maximum value, minimum value, etc. the problem is a typical dichotomy problem.
This is no exception
The idea is the following:
the length of the longest string is upper bound mid, and then the string is broken down by the length mid. If the number of lines is greater than k, that means that the length of the upper bound is too short, the number of rows out too many, or if the number of rows separated by equal to K, then it is also possible to lengthen the string. The upper bound of the string length obtained by the dichotomy, greedy to accumulate the string length, so down, the time complexity of two * string length O (m*logn)