[2011山东ACM省赛] Sequence (动态规划)
Sequence
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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.
输入
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.
输出
cost of splitting the sequence into exactly M succesive parts.
示例输入
1
5 2
1 3 2 4 5
示例输出
117
提示
来源
解题思路:
代码:
#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 (动态规划)的更多相关文章
- [2011山东ACM省赛] Identifiers(模拟)
Identifiers Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Identifier is an important ...
- [2011山东ACM省赛] Mathman Bank(模拟题)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sr19930829/article/details/24187925 Mathman Bank ni ...
- [2011山东ACM省赛] Binomial Coeffcients(求组合数)
Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- [2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...
- [2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描写叙述 Once ago, there is a mystery yard which ...
- 山东ACM省赛历届入口
山东省第一届ACM大学生程序设计竞赛 山东省第二届ACM大学生程序设计竞赛 山东省第三届ACM大学生程序设计竞赛 山东省第四届ACM大学生程序设计竞赛 山东省第五届ACM大学生程序设计竞赛 山东省第六 ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- 2019山东ACM省赛L题题解(FLOYD传递闭包的变形)
本题地址 https://cn.vjudge.net/contest/302014#problem/L Median Time Limit: 1 Second Memory Limit: 6 ...
随机推荐
- 初识UML
最近的学习中,遇到几次UML图,很是迷糊,确切的说,看不太懂.查阅UML相关资料,基本解决了这个问题.UML看起来还是相当深奥,这里只提一下解决问题的部分知识.(以下知识来自网络) Unified M ...
- python3内置函数详解
内置函数 注:查看详细猛击这里 abs() 对传入参数取绝对值 bool() 对传入参数取布尔值, None, 0, "",[],{},() 这些参数传入bool后,返回False ...
- Flume 远程写HDFS
现在的需求是在一台Flume采集机器上,往Hadoop集群上写HDFS,该机器没有安装Hadoop. 这里的Flume版本是1.6.0,Hadoop版本是2.7.1. 把Hadoop集群的hdfs-s ...
- linux网络完全与防护
7.1 网络封包联机进入主机的流程 7.1.1 封包进入主机的流程 1.经过防火墙的分析 iptables 主要功能是封包过滤 主要分析TCP/IP的封包表头来进行过滤的机制 分析的是OSI的第二 ...
- docker-compose安装使用
Docker Compose的工作原理 Docker Compose将所管理的容器分为三层,工程(project),服务(service)以及容器(contaienr).Docker Compose运 ...
- wamp密码设置
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
- 英语语法 It all started the summer before second grade when our moving van pulled into her neighborhood
It all started the summer before second grade when our moving van pulled into herneighborhood It all ...
- eval 函数的应用 (去除包装在列表外面的引号)
a="[u'ANDROID-5a9ac5c22ad94e26b2fa24e296787a35', u'0', 0, 0, 0, 1]" 此时的a是一个字符串,目的是要去掉a上面的引 ...
- fastJson顺序遍历JSON字段
fastJson在把json格式的字符串转换成JSONObject的时候,使用的是HashMap,所以排序规则是根据HASH值排序的,如果想要按照字符串顺序遍历JSON属性,需要在转换的时候指定使用L ...
- 基于阿里云服务器的git服务器搭建
使用阿里云Ubuntu 12.0.4 64位操作系统做git服务器. 首先git服务器有两种访问方式可以选择:http方式和ssh的方式,http方式更容易使用. 1.http方式的git服务器搭建以 ...