Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=3675
http://uoj.ac/problem/104
Exercises
At that time guess when the next point in the past to better.
It turned out to be the same. The contribution can be split to analyze.
So we can get a contribution =σ every two blocks of the product.
Then we can get a contribution =σ and * (The and of all the blocks in front)
Then there is dp:f[i,j] that represents to block I, the selected number is A[1]...a[j] contribution max.
Then the direct DP has 50 points.
# include <stdio.h># include<string.h># include<algorithm>//# include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;typedefLong Doubleld;typedef unsignedLong Longull;Const intM = 1e5 +Ten, N = About;Const intMoD = 1e9+7; # define RG register# define STStaticintN, K, A[m];ll s[m], f[n][m];intPre[n][m];inlinevoidPrtintIintj) {if(i = =1)return; PRT (i-1, Pre[i][j]); printf ("%d", Pre[i][j]);}intMain () {scanf ("%d%d", &n, &K); for(intI=1; i<=n; ++i) scanf ("%d", &a[i]), s[i] = s[i-1] +A[i]; for(intI=2; i<=k+1; ++i) for(intj=1; j<=n; ++j) {F[i][j]= -1e18; for(intk=1; k<j; ++k) {if(F[i][j] < f[i-1][k] + (s[j]-s[k]) *S[k]) {F[i][j]= f[i-1][k] + (s[j]-s[k]) *S[k]; PRE[I][J]=K; }}} printf ("%lld\n", f[k+1][n]); PRT (K+1, N); return 0;}
View Code
Definitely not directly DP, need to optimize
The slope is obviously optimized.
directly on the can.
Uoj version (output scheme, unlimited space)
# include <stdio.h># include<string.h># include<algorithm>//# include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;typedefLong Doubleld;typedef unsignedLong Longull;Const intM = 1e5 +Ten, N = About;Const intMoD = 1e9+7; # define RG register# define STStaticintN, K, A[m];ll s[m], f[n][m];intPre[n][m];intQ[m], head, Tail;inlinevoidPrtintIintj) {if(i = =1)return; PRT (i-1, Pre[i][j]); printf ("%d", Pre[i][j]);} inline ll Slop (intIintK1,intK2) { returnf[i-1][k1]-f[i-1][k2]+s[k2]*s[k2]-s[k1]*S[K1];} inline ll GETDP (intIintJintx) {returnf[i-1][x]+ (S[j]-s[x]) *s[x];}intMain () {scanf ("%d%d", &n, &K); for(intI=1; i<=n; ++i) scanf ("%d", &a[i]), s[i] = s[i-1] +A[i]; for(intI=2; i<=k+1; ++i) {head=1, tail =0; for(intj=1; j<=n; ++j) { while(Head<tail && Slop (i, Q[head], q[head+1]) <= (s[q[head+1]]-S[q[head]]) + +Head; F[I][J]=GETDP (i, J, Q[head]); PRE[I][J]=Q[head]; while(Head<tail && Slop (i, q[tail-1], Q[tail]) * (S[j]-s[q[tail]) >= slop (i, Q[tail], j) * (S[q[tail]]-s[q[tail-1]])) --tail; q[++tail] =J; }} printf ("%lld\n", f[k+1][n]); PRT (K+1, N); return 0;}
View Code
Bzoj version (no output scheme, space limit)
# include <stdio.h># include<string.h># include<algorithm>//# include <bits/stdc++.h>using namespaceStd;typedefLong LongLl;typedefLong Doubleld;typedef unsignedLong Longull;Const intM = 1e5 +Ten, N = About;Const intMoD = 1e9+7; # define RG register# define STStaticintN, K, A[m];ll s[m], f[2][m];intQ[m], head, tail;intPre, Cur;inline ll slop (intK1,intK2) { returnf[pre][k1]-f[pre][k2]+s[k2]*s[k2]-s[k1]*S[K1];} inline ll GETDP (intJintx) {returnf[pre][x]+ (S[j]-s[x]) *s[x];}intMain () {scanf ("%d%d", &n, &K); for(intI=1; i<=n; ++i) scanf ("%d", &a[i]), s[i] = s[i-1] +A[i]; Pre=0, cur =1; for(intI=2; i<=k+1; ++i) {head=1, tail =0; for(intj=1; j<=n; ++j) { while(Head<tail && Slop (Q[head], q[head+1]) <= (s[q[head+1]]-S[q[head]]) + +Head; F[CUR][J]=GETDP (J, Q[head]); while(Head<tail && Slop (q[tail-1], Q[tail]) * (S[j]-s[q[tail]) >= slop (Q[tail], j) * (S[q[tail])-s[q[tail-1]])) --tail; q[++tail] =J; } swap (pre, cur); } printf ("%lld\n", F[pre][n]); return 0;}
View Code
bzoj3675 [Apio2014] Sequence Segmentation