Time limit:40 Sec Memory limit:128 MB
submit:2898 solved:1170
Description Little H has recently been fascinated by a separate sequence of games. In this game, small h needs to divide a series of non-negative integers of length n into k+1 non-empty subsequence sequences. In order to get the k+1 subsequence, the small H needs to repeat the following steps of K: 1. Small h first selects a sequence longer than 1 (at the beginning of the small h only a sequence of length n-that is, the entire sequence obtained at the beginning) ; 2. Select a location and divide the sequence into successive two non-empty new sequences through this position. After each of these steps, small h will get a certain score. This fraction is the product of the elements and in two new sequences. Small h wants to choose the best way to divide, so that after K-wheel, the total score of small h is the largest. Input
Enter the first line containing two integers n,k (k+1≤n).
The second line contains n nonnegative integers a1,a2,...,an (0≤ai≤10^4), which represents the sequence at the beginning of the small H obtained. Output
The output first line contains an integer, which is the maximum fraction that can be obtained for small H.
Sample Input7 3
4 1 3 4 0 2 3
Sample Output108
HINT
"Sample description"
In the example, small H can get 108 points with the following 3-wheel operation:
1. -Start small H has a sequence (4,1,3,4,0,2,3). Small H selects the position after the 1th number
Divide the sequence into two parts and get the 4x (1+3+4+0+2+3) =52 points.
2. At the beginning of this round, small H has two sequences: (4), (1,3,4,0,2,3). Small h selection in 3rd number
The position after the word divides the second sequence into two parts and gets (1+3) x (4+0+2+
3) =36 points.
3. At the beginning of this round, small H has three sequences: (4), (1,3), (4,0,2,3). Small h selection in 5th
The position after the number divides the third sequence into two parts and gets (4+0) x (2+3) =
20 points.
After the three-wheeled operation, the small H will get four sub-sequences: (4), (1,3), (4,0), (2,3) and a total of 52+36+20=108 points.
"Data size and scoring"
: Data meets 2≤n≤100000,1≤k≤min (N-1,200).
Source
Dynamic planning slope Optimization DP
First you need to find a property:
$ A * (b+c) +b*c = AB+AC+BC $
$ (a+b) *c+a*b = AB+AC+BC $
This means that the optimal score is independent of the cut order.
Then we can cut from left to right, which is obviously a DP problem.
Slope optimization can be.
The first push is $ f[i][k]=max{f[j][k-1]+ (Sum[n]-sum[i]) * (Sum[i]-sum[j])} $
Run out like this 26+s, why so slow ah? Is it the case that the equation is not suitable for slope optimization? (How foolish to come to this conclusion)
Then replaced by $ f[i][k]=max{f[j][k-1]+ (Sum[i]-sum[j]) *sum[j]} $
Run out like this, 30+s, Meow meow?
Silent constant optimization, in addition to the type of split into the same type, card to 13s
A satisfying (not)
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6 #defineLL Long Long7 using namespacestd;8 Const intmxn=100010;9 intRead () {Ten intx=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the intn,k; - intA[mxn],cnt=0; - intQ[mxn],hd,tl; - LL SMM[MXN]; + LL F[MXN],G[MXN]; - DoubleCalcintJintk) { + return(g[j]-g[k]-(LL) smm[n]* (Smm[j]-smm[k])/(Double) (smm[k]-smm[j]); A } at intMain () { - //freopen ("In.txt", "R", stdin); - inti,j; -N=read (); k=read (); - for(i=1; i<=n;i++) {a[i]=read ();if(A[i]) a[++cnt]=a[i];} -n=cnt; K=min (n1, K); in for(i=1; i<=n;i++) smm[i]=smm[i-1]+A[i]; - for(i=1; i<=k;i++){ to swap (f,g); +Hd=1; tl=0; - for(j=1; j<=n;j++){ the while(Hd<tl && Calc (q[tl-1],Q[TL]) >calc (q[tl],j-1)) tl--; *q[++tl]=j-1; $ while(Hd<tl && Calc (q[hd],q[hd+1]) <smm[j]) hd++;Panax Notoginseng intt=Q[HD]; - if(HD<=TL) f[j]=g[t]+ (LL) smm[n]*smm[j]-smm[j]*smm[j]-smm[n]*smm[t]+smm[j]*Smm[t]; the } + } ALL ans=0; the for(i=1; i<=n;i++) ans=Max (ans,f[i]); +printf"%lld\n", ans); - return 0; $}
First Number Machine
Complete Body:
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6 #defineLL Long Long7 using namespacestd;8 Const intmxn=100015;9 intRead () {Ten intx=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the intn,k; - intA[mxn],cnt=0; - intQ[mxn],hd,tl; - LL SMM[MXN],SMM2[MXN]; + LL F[MXN],G[MXN]; -Inline LL calc_up (intJintk) { + returnsmm2[k]-smm2[j]+g[j]-G[k]; A } atInline LL Calc_down (intJintk) { - returnsmm[k]-Smm[j]; - } - intMain () { - //freopen ("In.txt", "R", stdin); - inti,j; inN=read (); k=read (); - for(i=1; i<=n;i++) {a[i]=read ();if(A[i]) a[++cnt]=a[i];} ton=CNT; + for(i=1; i<=n;i++) smm[i]=smm[i-1]+a[i],smm2[i]=smm[i]*Smm[i]; - for(i=1; i<=k;i++){ the swap (f,g); *Hd=1; tl=0; $ for(j=i;j<=n;j++){Panax Notoginseng while(Hd<tl && calc_up (q[tl-1],Q[TL]) *calc_down (q[tl],j-1) >=calc_up (q[tl],j-1) *calc_down (q[tl-1],Q[TL]) tl--; -q[++tl]=j-1; the while(Hd<tl && calc_up (q[hd],q[hd+1]) <smm[j]*calc_down (q[hd],q[hd+1])) hd++; + intt=Q[HD]; Af[j]=g[t]+smm[t]* (LL) smm[j]-smm[t]); the } + } -printf"%lld\n", F[n]); $ return 0; $}
Bzoj3675 [Apio2014] Sequence Segmentation