4518: [Sdoi2016] Journey time limit:10 Sec Memory limit:256 MB
submit:230 solved:156
[Submit] [Status] [Discuss] Descriptionpine began the journey from the s to the land of T. 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
Source
Acknowledgement Menci Upload
Solution
DP + slope optimization
Start by simplifying the things you ask for:
That is, the final result is $m*\sum_{i=1}^{m}x_{i}^{2}-\sum_{i=1}^{m}x_{i}$ where $x_{i}$ represents the sum of the sections of the $i$ Day walk
Obviously there are $\sum_{i=1}^{m}x_{i}$ for the fixed value, then only need to minimize the $\sum_{i=1}^{m}x_{i}^{2}$
Then consider $f[i][j]$ said $i$ days together took the $j$ section of the best
There can be a day of stagnation, it is easy to find that, to the best, it is impossible to stagnate
Can get Transfer $dp[i][j]=min (dp[i][j],dp[i-1][j-k]+ (Sum[j]-sum[j-k]) ^{2}), k=1~j$
After fixing the $i$, it is clear that the $j$ is a slope-optimized
Then you can get $\frac{dp[i-1][k_{1}]+sum[k_{1}]^{2}-dp[i-1][k_{2}]-sum[k_{2}]^{2}}{sum[k_{1}]-sum[k_{2}]}<2*sum[j by simplifying it. ]$
Then the slope optimization, you also hit a scrolling array, but seemingly meaningless
Code
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 3010intN,m;intDis[maxn],que[maxn],l,r;Long Longdp[2][MAXN],SUM[MAXN];Long LongpfLong Longx) {returnx*x;} InlineDoubleSlopeintTintIintj) { return(Double) (DP[T][I]+PF (Sum[i])-DP[T][J]-PF (Sum[j])/(Double) (sum[i]-sum[j]);}intMain () {n=read (), m=read (); for(intI=1; i<=n; i++) Dis[i]=read (), sum[i]=sum[i-1]+Dis[i]; Memset (DP, the,sizeof(DP)); dp[0][0]=0; for(intt=1, i=1; i<=m; i++,t^=1, l=r=0) for(inttmp,j=1; j<=n; J + +) { while(L<r && Slope (t^1, que[l],que[l+1]) < (sum[j]<<1)) l++; TMP=Que[l]; DP[T][J]=PF (Sum[j]-sum[tmp]) +dp[t^1][tmp]; while(L<r && Slope (t^1, que[r],que[r-1]) >slope (t^1, que[r],j)) r--; que[++r]=J; } printf ("%lld\n",m*dp[m&1][n]-PF (sum[n])); return 0;}
In the BZOJ seems not high ah .... If I could have so many slope-optimised exercise reserves before the exam, it would be different.
"BZOJ-4518" Journey DP + slope optimization