I-beautiful spacing
Time limit:8000 ms
Memory limit:65536kb
64bit Io format:% LLD & % llusubmit status
Description
Text is a sequence of words, and a word consists of characters. Your task is to put words into a gridWColumns and sufficiently restricted lines. for the beauty of the layout, the following conditions have to be satisfied.
- The words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "This is a pen" into 11 columns.
|
|
Figure I .1: A good layout. |
Figure I .2: Bad | Do not reorder words. |
- Between two words adjacent In the same line, You must place at least one space character. you sometimes have to put more than one space in order to meet other conditions.
Figure I .3: Bad | words must be separated by spaces.
- A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces.
Figure I .4: Bad | characters in a single word must be contiguous.
- The text must be justified to the both sides. that is, the first word of a line must startfrom the first column of the line, and should t the last line, the last word of a line must end at the last column.
Figure I .5: Bad | lines must be Justi ed to both the left and the right sides.
The text is the most beautifully laid out when there is no unnecessarily long spaces. for instance, the layout in Figure I .6 has at most 2 contiguous spaces, which is more beautiful than that in Figure I .1, having 3 contiguous spaces. given an input text and the number of columns, please find a layout such that the length of the longest contiguous spaces between words is minimum.
Figure I .6: A good and the most beautiful layout.
Input
The input consists of multiple datasets, each in the following format.
W n
X1X2...XN
W,N, AndXIAre All integers.WIs the number of columns (3 ≤W≤ 80,000 ).NIs the number of words (2 ≤N≤ 50,000 ).XIIs the number of characters inI-Th word (1 ≤XI≤ (W? 1)/2). Note that the upper bound onXIAssures that there always exists a layout satisfying the conditions.
The last dataset is followed by a line containing two zeros.
Output
For each dataset, print the smallest possible number of the longest contiguous spaces between words.
Sample Input
11 44 2 1 35 71 1 1 2 2 1 211 73 1 3 1 3 3 4100 330 30 3930 32 5 30 0
Output for the sample input
212401
Question: How to minimize the maximum space by adding a text containing N words to a rectangle W in width according to some rules. Idea: the answer is monotonous. The answer is a binary answer, and then DP is used to test whether the DP [I] indicates whether the I-th word can end. It is easy to think of the N ^ 2 test, but it will certainly be TLE and needs optimization. It can be found that if I-j can be placed in one row, but the maximum space will exceed mid, then I + 1-j will not work. If I is used for pushing, it can be pushed to [S, T, then we can start from t + 1 next time. Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 50005#define MAXN 200005#define mod 1000000007#define INF 0x3f3f3f3f#define eps 1e-6const double pi=acos(-1.0);typedef long long ll;using namespace std;int n,w;int len[maxn],sum[maxn];bool dp[maxn];bool isok(int mid){ int i,j,last=0; memset(dp,0,sizeof(dp)); dp[0]=1; if(sum[n]+n-1<=w) return true ; for(i=0; i<n-1; i++) { if(!dp[i]) continue ; for(j=max(i+2,last+1); j<=n; j++) { if(w<sum[j]-sum[i]+j-i-1) break ; if(w>sum[j]-sum[i]+ll(j-i-1)*mid) continue ; last=j; dp[j]=1; if(sum[n]-sum[j]+n-j-1<=w) return true ; } } return false ;}void solve(){ int i,j,le=1,ri=w,mid,ans; while(le<=ri) { mid=(le+ri)>>1; if(isok(mid)) { ans=mid; ri=mid-1; } else le=mid+1; } printf("%d\n",ans);}int main(){ int i,j; while(~scanf("%d%d",&w,&n)) { if(w==0&&n==0) break ; sum[0]=0; for(i=1; i<=n; i++) { scanf("%d",&len[i]); sum[i]=sum[i-1]+len[i]; } solve(); } return 0;}
Ultraviolet A live 6190 beautiful spacing (two-point + dp test optimized based on specific properties)