[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 ...
随机推荐
- 第一波实习的前端笔记(2)——js.md
1.如何解决移动端点透问题? $('xx').on('touchend', function(event){ event.preventDefault(); }) 但是,存在滑动页面会触发问题.期待更 ...
- Eclipse JSP/Servlet 环境搭建
Eclipse JSP/Servlet 环境搭建 本文假定你已安装了 JDK 环境,如未安装,可参阅 Java 开发环境配置. 我们可以使用 Eclipse 来搭建 JSP 开发环境,首先我们分别下载 ...
- 【原创】No matching distribution found for Twisted>=10.0.0 (from scrapy)
系统 Ubuntu14.04 python 2.7.11 运行 pip install scrapy 报错: No matching distribution found for Twiste ...
- singleTask, singleInstance使用心得
1. singleTask, singleInstance 共同点: 1) 一旦入栈,都为于栈底. 2) 全栈有且只有一个activity实例对象. 2. singleTask, singleIns ...
- javascript组件化
http://purplebamboo.github.io/2015/03/16/javascript-component/#%E5%BC%95%E5%85%A5%E4%BA%8B%E4%BB%B6% ...
- Ajax readystate 5种状态
Status 说明 0(Uninitialized) XMLHttpRequest 对象已经创建,但没调用 open 方法. 1(Loading) 调用 open 方法,但没调用 send 方法.(尚 ...
- memcpy code
memcpy #include <stddef.h> //#include <stdint.h> //uintptr_t is quoted.#include "st ...
- 读取jar包资源(转)
可能有不少初学者会有这样的困惑:在你的代码里调用了一些资源文件,如图片,音乐等,在调试环境或单独运行的时候可以正常显示或播放,而一旦打包到jar文件中,这些东东就再也出不来了,除非把这个jar放到原来 ...
- VC++ 将资源位图画到窗口上去的方法
第一种方法最简单:直接看代码 CImage img; img.LoadFromResource(AfxGetInstanceHandle(), IDB_BITMAP1); img.Draw(*pDc, ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(十五 ) CommsThreadPool自定义线程池
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...