SDUT 2169

Sequence

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Given an integer number sequence A of length N (1<=N<=1000), we define f(i,j)=(A[i]+A[i+1]+...+A[j])^2 (i<=j). Now you can split the sequence into exactly M (1<=M<= N) succesive parts, and the cost of a part from A[i] to A[j] is f(i,j). The totle cost is the sum of the cost of each part. Please split the sequence with the minimal cost.

输入

At the first of the input comes an integer t indicates the number of cases to follow. Every case starts with a line containing N ans M. The following N lines are A[1], A[2]...A[N], respectively. 0<=A[i]<=100 for every 1<=i<=N.

输出

For each testcase, output one line containing an integer number denoting the  minimal cost of splitting the sequence into exactly M succesive parts.

示例输入

1
5 2
1 3 2 4 5

示例输出

117

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
int a[1010];
long long sum[1010];
long long int dp[1010][1010];
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        memset(dp,INF,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            dp[0][i]=0;
            dp[i][0]=0;
            dp[1][i]=sum[i]*sum[i];
        }
        for(int i=2;i<=m;i++)
        {
            for(int j=i;j<=n-m+i;j++)
            {
                for(int k=i-1;k<=j-1;k++)
                {
                    dp[i][j]=min(dp[i][j],dp[i-1][k]+(sum[j]-sum[k])*(sum[j]-sum[k]));
                }
            }
        }
        printf("%lld\n",dp[m][n]);
    }
    return 0;
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值