"Bzoj" "1044" "HAOI2008" stick split

Source: Internet
Author: User

Two minutes/DP

That's a good question!

The first question is very simple two points ...

The second question at the beginning I want to be greedy, actually should be DP = =

Then did not notice ... And the MLE and the tle ... This topic to the DP for space-time optimization!!

The following: (by Joefan)

Use the prefix and, to make sum[i] the length of the top I stick and.

F[I][J] for the first I-stick medium-cut J-knife, and meet the maximum length of the number of programs not exceeding J, then:

State transfer equation: F[i][j] =σf[k][j-1] ((1 <= k <= i-1) && (Sum[i]-sum[k] <= Len))

This spatial complexity is O (nm), with a time complexity of O (n^2 m). are clearly beyond the limits.

Below we consider the DP optimization.

1) for space optimization.

Obviously, because the current F[][J] is only related to f[][j-1], it can be implemented with a scrolling array.

F[i][now] Instead of f[i][j], f[i][now^1] instead of f[i][j-1]. For convenience, we call f[][now^1] f[][last].

This space complexity is O (n). Meet space constraints.

2) for the optimization of time.

Consider the process of optimizing state transitions.

For F[i][now], in fact is f[mink][last]...f[i-1][last] This section f[k][last] and, mink is to meet sum[i]-sum[k] <= Len the smallest k, then, for from 1 to n I, the relative mink also must be non-diminishing (because Sum[i] is incremented). We record the f[1][last]...f[i-1][last] and SUMF, Mink is initially set to 1, each time for I will mink backwards, while passing the discarded p corresponding F[p][last] from the sumf minus. Then F[i][now] is the value of SUMF.

This is the time complexity of O (NM). Time limit is met.

1 /**************************************************************2 problem:10443 User:tunix4 language:c++5 result:accepted6 time:4152 Ms7 memory:4396 KB8 ****************************************************************/9  Ten //Bzoj 1044 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at     intv=0, sign=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -     returnv*Sign ; - } - Const intn=1e5+Ten, inf=~0u>>2, p=10007; intypedefLong LongLL; - /******************tamplate*********************/ to intn,m,ans,a[n],f[n][2],pos[n],sumf[n][2]; + LL S[n]; - BOOLCheckintLen) { the     intCnt=0, sum=0; *F (I,1, N) { $         if(A[i]>len)return 0;Panax Notoginseng         if(sum+a[i]>Len) { -cnt++; sum=A[i]; the}Elsesum+=A[i]; +     } A     returncnt<=m; the } + intMain () { - #ifndef Online_judge $Freopen ("1044.in","R", stdin); $Freopen ("1044.out","W", stdout); - #endif -N=getint (); m=getint (); theF (I,1, n) {a[i]=getint (); s[i]=s[i-1]+a[i];} -      Wuyi     intL=0, r=S[n],mid; the      while(l<=R) { -Mid=l+r>>1; Wu         if(Check (mid)) ans=mid,r=mid-1; -         ElseL=mid+1; About     } $printf"%d", ans); -F (I,0, N) sumf[i][0]=1; -     intway=0; -F (J,1, m+1){ A         intnow=j&1;  +sumf[0][now]=0; theF (I,1, N) { -             if(!Pos[i]) $pos[i]=pos[i-1]; while(S[i]-s[pos[i]]>ans) pos[i]++; theF[i][now]= (sumf[i-1][now^1]-sumf[pos[i]-1][now^1]+P)%P; theSumf[i][now]= (sumf[i-1][now]+f[i][now])%P; the         } theWay= (Way+f[n][now])%P; -     } inprintf"%d\n", the "the"); the     return 0; the}
View Code 1044: [HAOI2008] Stick split time limit:10 Sec Memory limit:162 MB
submit:2008 solved:725
[Submit] [Status] [Discuss] Description

There are n sticks, and the length of the root of the Li,n sticks is connected together in succession, with a total of n-1 joints. Now allows you to cut the maximum m connections, after chopping the N sticks are divided into a number of segments, required to meet the total length of the largest section of the length of the smallest, and the output of how many ways to cut the total length of the largest part of the smallest length. And will result mod 10007 ...

Input

The first line of the input file has 2 number n,m. The next n rows are a positive integer li for each line, indicating the length of the root I stick.

Output

The output has 2 numbers, the first number is the minimum length of the largest segment of the total length, and the second number is how many methods of chopping are used to satisfy the condition.

Sample Input3 2
1
1
TenSample OutputTen 2HINT

Two ways of Chopping: (1) (1) (10) and (1 1) (10)

Data range

n<=50000, 0<=m<=min (n-1,1000).

1<=li<=1000.

Source [Submit] [Status] [Discuss]

"Bzoj" "1044" "HAOI2008" stick split

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.