Ultraviolet A live 6190 Beautiful Spacing (two points + dp test optimized based on specific properties), 6190 spacing

Source: Internet
Author: User

Ultraviolet A live 6190 Beautiful Spacing (two points + dp test optimized based on specific properties), 6190 spacing
I-Beautiful SpacingTime Limit:8000 MSMemory Limit:65536KB64bit 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 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
X
1X2...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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.