sdut2169:Sequence(dp)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2169
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; ll n,m; ll sum; ll w[1010],num[1010],dp[1010]; int main() { int T; scanf("%d",&T); while(T--) { sum=0; scanf("%lld%lld",&n,&m); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); num[i]=num[i-1]+w[i]; dp[i]=num[i]*num[i]; sum+=w[i]; } if(m==1) { sum=sum*sum; printf("%lld\n",sum); continue; } bool F=false; for(int j=2;j<=m;j++) { for(int i=n-m+j;i>=j;i--) { for(int k=j-1;k<i;k++) { dp[i]=min(dp[i],(dp[k]+(num[i]-num[k])*(num[i]-num[k]))); if(j==m&&k==i-1) { F=true; break; } } if(F) break; } } printf("%lld\n",dp[n]); } return 0; }