Solving: slope optimization, maintenance on convex hull, similar to upper right half circle
Scrolling array optimization space, DP time record decision point
Note: note Sum[i]-sum[j] may ==0
The guy gave 32 points to the QWQ.
In fact, this code has a bug but the data is not card
For removing the 0 element directly then DP may make the sequence less than M
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn=100009; const int Maxm=209;typedef long long lint;int n,m; Lint S[maxn];int REF[MAXN]; Lint F[MAXN]; Lint g[maxn];int P[maxn][maxm];int Q[MAXN]; Lint getk (int i) {return g[i]-s[i]*s[i];} void Putans (int x,int y) {if (y==0) Return;putans (p[x][y],y-1);p rintf ("%d", ref[x]);} int main () {scanf ("%d%d", &n,&m), ++m;int cnt=0;for (int i=1;i<=n;++i) {Lint x;scanf ("%lld", &x); if (x==0) Continue;++cnt;s[cnt]=s[cnt-1]+x;ref[cnt]=i;} N=cnt;int h,t;for (int j=2;j<=m;++j) {q[h=t=1]=j-1;for (int i=j;i<=n;++i) {while (h<t) && (GETK (q[h+1 ])-GETK (Q[h])) >-s[i]* (S[q[h+1]]-s[q[h])) ++h;//printf ("%d%d%d\n", J,i,q[h]) f[i]=g[q[h]]+ (S[I]-S[Q[H)] [H]]; P[i][j]=q[h];while (h<t) {int a=q[t-1],b=q[t];if (1.0* (GETK (b)-getk (a))/(S[b]-s[a]) <1.0* (GETK (i)-GETK (b))/(s [I]-s[b])--t;else break;} Q[++t]=i;} for (int i=1;i<=n;++i) {g[i]=f[i];f[i]=0;}} printf ("%lld\n", G[n]);//putans (p[n][m],m-1);//printf ("\ n "); return 0;}
Bzoj 3675 [Apio2014] Sequence Segmentation