Topic Links:
4518: [Sdoi2016] Journey Descriptionpine began the journey from S to t land. The road from S to T can be divided into n segments, and the dividing point of the adjacent two sections is provided with a rest station. Pine plans to reach T-land with M-day. In addition to the outer m, every night pine must stay overnight at the rest station. Therefore, a certain road must be completed on the same day. Pine wants to walk the length of the road as closely as possible every day, so he wants the variance of the length of the road to go every day as small as possible. Help Pine to find out what the minimum variance is. Setting the variance is V, which proves that vxm^2 is an integer. In order to avoid accuracy error, output vxm^2 when output result. Input first row two number n, M. Second row n number, indicating the length of the N-segment output
A number, the minimum variance multiplied by the value of m^2
Sample Input5 2
1 2 5) 8 6Sample Output $HINT
1≤n≤3000, ensure that the total distance from S to T is not more than 30000
Idea: Again a slope optimization dp, with the number of n divided into m block, the sum of squares and the minimum equivalent, and then is the slope optimization dp; AC Code:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int Maxn=3e3+4;int n,m,q[maxn],head=1,tail = 1; LL A[maxn],sum[maxn],dp[maxn][maxn];inline double slope (int l,int r,int dep) {double ratio; Ratio= (Double) (dp[r][dep]+sum[r]*sum[r]-dp[l][dep]-sum[l]*sum[l])/(double) (sum[r]-sum[l]); return ratio;} int main () {scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) scanf ("%lld", &a[i]), sum[i]=sum[i-1]+a[i]; for (int i=1;i<=n;i++) dp[i][1]=sum[i]*sum[i]; for (int j=2;j<=m;j++) {head=tail=1; for (int i=1;i<=n;i++) {while (Head<tail&&slope (q[head],q[head+1],j-1) < (double) 2*sum[i]) head++;//because Sum[i] is monotonically increasing; dp[i][j]=dp[q[head]][j-1]+ (Sum[i]-sum[q[head]) * (Sum[i]-sum[q[head]]); while (Head<tail&&slope (q[tail-1],q[tail],j-1) >slope (q[tail],i,j-1)) tail--; Q[++tail]=i; }} printf ("%lld\n", M*dp[n][m]-sum[n]*sum[n]); return 0;}
bzoj-4518 4518: [Sdoi2016] Journey (Slope optimized DP)