A division of DP, but because of the number of partitions arbitrarily, only one-dimensional array can be
Set Dp[i] Indicates the minimum cost of the first I box (any case)
Dp[i]=min (Dp[u]+cost (u+1,i))
But the complexity of the n<=50000,n side is obviously unacceptable.
Set Choice[i] Array save for each I value, the enumeration of the maximum value of the U-f[i]
Hit the table, found choice[] is monotonous does not descend
Each time for a new I, you only need to start from choice[i-1] when enumerating u
Code:
#include <iostream>#include<cstring>#defineSize 50005using namespacestd;intN;intC[size];Long LongSum[size];Long LongL;Long LongDp[size];intChoice[size];Long LongCostintLintR) { Long Longx=sum[r]-sum[l-1] + RM; return(x-l) * (X-L);}intMain () {Freopen ("1319.in","R", stdin); Memset (DP,0x7f,sizeof(DP)); CIN>>n>>m; for(intI=1; i<=n;i++) {cin>>C[i]; Sum[i]=sum[i-1]+C[i]; } dp[0]=0; choice[0]=0; for(intI=1; i<=n;i++){ for(intu=choice[i-1];u<=i-1; u++){ if(Dp[u]+cost (u+1, i) <=Dp[i]) {Dp[i]=dp[u]+cost (u+1, i); Choice[i]=T; } }} cout<<Dp[n]; Fclose (stdin); return 0;}
As for better nlgn optimization and n slope optimization, let's say ... sleeping
code1319 Toy Packing