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

提示

 

来源

山东省第二届ACM大学生程序设计竞赛

解题思路:

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=1010;
int num[maxn];
long long sum[maxn];
long long dp[maxn]; int min(int a,int b)
{
return a>b?b:a;
} int main()
{
int t;cin>>t;
int n,m;
while(t--)
{
memset(sum,0,sizeof(sum));
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>num[i];
sum[i]=sum[i-1]+num[i];
dp[i]=sum[i]*sum[i];//求前i个数和的平方,这一句也是为什么dp数组不用初始化为最大值的原因
}
for(int i=2;i<=m;i++)//划分为几部分
{
for(int j=n-m+i;j>=i;j--)//从dp[n-m+i]出开始更新,从后往前,要构成更多的划分,因此dp[n-m+i]后面的dp[]不用更新,更新了也用不到
{
for(int k=i-1;k<j;k++)//从哪里到哪里的划分,上一次的dp[]划分加上新的划分区间和,枚举,取最小值
{
dp[j]=min(dp[j],dp[k]+(sum[j]-sum[k])*(sum[j]-sum[k]));
if(i==m&&k==j-1)//计算到划分为m部分,且枚举完毕
goto label;
}
}
}
label:
cout<<dp[n]<<endl;
}
return 0;
}

[2011山东ACM省赛] Sequence (动态规划)的更多相关文章

  1. [2011山东ACM省赛] Identifiers(模拟)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述  Identifier is an important ...

  2. [2011山东ACM省赛] Mathman Bank(模拟题)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sr19930829/article/details/24187925 Mathman Bank ni ...

  3. [2011山东ACM省赛] Binomial Coeffcients(求组合数)

    Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  4. [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...

  5. [2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)

    Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描写叙述 Once ago, there is a mystery yard which ...

  6. 山东ACM省赛历届入口

    山东省第一届ACM大学生程序设计竞赛 山东省第二届ACM大学生程序设计竞赛 山东省第三届ACM大学生程序设计竞赛 山东省第四届ACM大学生程序设计竞赛 山东省第五届ACM大学生程序设计竞赛 山东省第六 ...

  7. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  8. 第八届山东ACM省赛F题-quadratic equation

    这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...

  9. 2019山东ACM省赛L题题解(FLOYD传递闭包的变形)

    本题地址 https://cn.vjudge.net/contest/302014#problem/L Median Time Limit: 1 Second      Memory Limit: 6 ...

随机推荐

  1. Linux 组群账户管理

    一.Linux组群账户配置文件 1./etc/group文件 /etc/group文件是用户组群的配置文件,内容包括用户和用户组群,并且能显示出用户是归属哪个用户组群或哪几个用户组群.一个用户可以归属 ...

  2. setTimeout

    setTimeout(function () { $('#successTip').hide(); location = location; }, 3000);

  3. PHP&MySQL 语法

    PHP操作MySQL数据库 第一步:连接数据库 $dbhost = 'localhost:3306'; //mysql服务器主机地址 $dbuser = 'guest'; //mysql用户名 $db ...

  4. XidianOJ 1176 ship

    题目描述 The members of XDU-ACM group went camp this summer holiday. They came across a river one day. T ...

  5. 使用SftpDrive+SourceInsight阅读开源代码

    在虚拟机环境下使用Linux编写和阅读代码,我之前一直是通过Xshell利用ssh登录至虚拟机从而在命令行下使用vim来进行的.目前有阅读开源代码的需要,虽然vim+ctags+cscope可以完成这 ...

  6. 程序的删除kill、killall

    发送信号给程序,或者列出信号集 SYNOPSIS        kill [-s SIGNAL | -SIGNAL] PID...        kill -l [SIGNAL]...   1.信号操 ...

  7. 【原】JS正则表达式里的控制符

    正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...

  8. cannot access the system temp folder

    cannot access the system temp folder. please, make sure your application have full control rights on ...

  9. LC.exe已退出,代码为-1错误

    因为证书的原因,把项目中“properties”目录下的“license.licx”文件删除,再编译就成功了.如图:

  10. 深入理解js——隐式原型

    每个函数都有一个prototye(原型),而每个对象都有一个_proto_,可成为隐式原型. _proto_是一个隐藏的属性,javascript不希望开发者用到这个属性值,有的低版本浏览器甚至不支持 ...