比较简单的DP题吧,不多解释了,直接上代码
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,K,i,j,k,s[605][605],g[605][605]; int ans,x,y,f[605][55],p[605][55]; void print(int x,int y){ if (y==0) return; print(p[x][y],y-1); if (y==K) printf("%d\n",x); else printf("%d ",x); } int main(){ scanf("%d%d",&n,&K); for (i=1;i<n;i++){ for (j=i+1;j<=n;j++) scanf("%d",&s[i][j]); for (j=n-1;j>i;j--) s[i][j]+=s[i][j+1]; } for (i=0;i<=n;i++) for (j=i+1;j<=n;j++) for (k=i+1;k<=j;k++) g[i][j]+=s[k][j+1]; memset(f,-1,sizeof(f)); f[0][0]=0; for (i=0;i<=n;i++) for (j=0;j<K;j++) if (f[i][j]>=0){ for (k=i+1;k<=n;k++) if (f[k][j+1]<f[i][j]+g[i][k]){ f[k][j+1]=f[i][j]+g[i][k]; p[k][j+1]=i; } } for (i=1;i<=n;i++) if (ans<f[i][K]){ ans=f[i][K]; x=i; } print(x,K); return 0; }