3675: [Apio2014] Sequence Segmentation
Time limit:40 Sec Memory limit:128 MB
submit:1538 solved:637
[Submit] [Status] [Discuss]
Description
Little H has recently hooked up to a game of delimited sequences. 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 for K-Times:
1. Small h first selects a sequence with a length of more than 1 (the first small h has only one 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 Input
7 3
4 1 3 4 0 2 3
Sample Output
108
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).
title link : http://www.lydsy.com/JudgeOnline/problem.php?id=3675
Ideas :
1. First, the order of the same place is found to have no effect:
Certificate: ab,cd--"A,B,CD (AB+AC+AD+BC+BD)
With a,bcd--"A,B,CD (AB+AC+AD+BC+BD)
2. Easy to think of the bare dp equation:
DP "I" "J" =DP "I-1" "K" + (sum "J"-sum "K") *sum "K";
3.tle-> Slope Optimization:
Refer to (self-lazy). )
Maintenance of a monotonous queue when and only when Cal (Q[h],q[h+1]) >=sum[i] is updated;
Code :
#include <iostream>#include <stdio.h>#include <string.h>Using namespace Std;long long g[100005],f[100005],sum[100005];inta[100005];intNintKintH,t;int q[100005];d ouble Cal (intJintK) {return(double) (Sum[k]*sum[K]-g[k]-sum[j]*sum[J]+g[j])/(double) (Sum[k]-sum[j]);} void Solve () { for(intI=1; i<=k;i++) {h=1, t=0; for(intj=i;j<=n;j++) { while(H<t&&cal (Q[t], J-1) <cal (Q[t-1],Q[t])) t--;Q[++t]=j-1; while(H<t&&cal (Q[h],Q[h+1]) <sum[j]) h++;inttmp=Q[h]; f[j]=g[tmp]+ (Sum[j]-sum[tmp])*sum[TMP];//cout<" "<<t<<endl; } for(intj=i;j<=n;j++) Swap (g[j],f[j]); }}intMain () {scanf ("%d%d", &n,&k); for(intI=1; i<=n;i++) scanf ("%d", &a[i]);inttot=0; for(intI=1; i<=n;i++)if(A[i]) a[++tot]=a[i]; N=tot; for(intI=1; i<=n;i++) sum[i]=sum[i-1]+a[i]; Solve ();printf("%lld\ n", G[n]);
[bzoj3675] [Apio2014] sequence split split the sequence dp+ slope optimization