Thanks again for the advice of the big guy in the area.
Order \ (f[i][j]\) indicates the number of first I to select the maximum value of J,\ (g[i][j]\) indicates the number of first I selected J segment, and the first paragraph must be selected to the maximum value of the first position (this is not mandatory to select the number of i\)
As for the transfer, enumeration J, and then from the go after enumeration \ (i\), can be transferred from \ (f[i-1][j]\) , you can also choose another paragraph.
This records a prefix maximum \ (Ma=max (F[l][j-1]-pre[l]) (l<i,pre\) is the prefix and \ () \), which is transferred from \ (ma+pre[i]\) .
Because \ (Ma+pre[i]=max (F[l][j-1]-pre[l]) +pre[i]=max (F[l][j-1]-pre[l]+pre[i]) =max (F[l][j-1]+sum[l+1][i]) (l<i) \)
Preprocessing, \ (\forall i\ f[i][0]=0\) and \ (g[i][1]\) Take the maximum prefix and
luogu-judger-enable-o2#include<algorithm> #include <iostream> #include <cstring> #include < complex> #include <cstdio> #include <vector> #include <cmath> #include <ctime> #include < queue> #include <map> #define LL long long#define il inline#define re registerusing namespace Std;const LL mod=1000 000007;il LL Rd () {re LL x=0,w=1;re char ch; while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') W=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<3) + (x<<1) + (ch^48); Ch=getchar ();} return x*w;} int A[100010],hd,tl,n,k;int F[100010][60],g[100010][60],ans,mi;int Main () {n=rd (), K=rd (); for (int i=1;i<=n;i++) a[i]=rd () +a[i-1]; memset (F,-0x3f3f3f,sizeof (f)); memset (G,-0x3f3f3f,sizeof (g)); MI=F[0][0]; for (int i=0;i<=n;i++) f[i][0]=0; for (int j=1;j<=k;j++) {int ma=mi; for (int i=j;i<=n;i++) {F[i][j]=max (f[i-1][j],ma+a[i]); Ma=max (Ma,f[i][j-1]-a[i]); }} Ans=f[n][k];for (int i=1;i<=n;i++) G[i][1]=max (G[i-1][1],a[i]); for (int j=2;j<=k;j++) {int ma=mi; for (int i=j;i<=n;i++) {G[i][j]=max (g[i-1][j],ma+a[i]); Ma=max (Ma,g[i][j-1]-a[i]); }} for (int i=k;i<=n;i++) Ans=max (Ans,g[i][k]+a[n]-a[i]); printf ("%d\n", ans); return 0;}
Luogu P4744 [Wind Festival]iron mans