hdu3280Equal Sum Partitions (区间DP)
2 5 1 3 3 7
may be grouped as:
(2 5) (1 3 3) (7)
to yield an equal sum of 7.
Note: The partition that puts all the numbers in a single group is an equal sum partition with the sum equal to the sum of all the numbers in the sequence.
For this problem, you will write a program that takes as input a sequence of positive integers and returns the smallest sum for an equal sum partition of the sequence.
P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by a decimal integer
M, (1 ≤ M ≤ 10000), giving the total number of integers in the sequence. The remaining line(s) in the dataset consist of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than
10 values.
3
1 6
2 5 1 3 3 7
2 6
1 2 3 4 5 6
3 20
1 1 2 1 1 2 1 1 2 1
1 2 1 1 2 1 1 2 1 1
1 7
2 21
3 2题意:给出一个序列,假设能把该序列分成若干段,使每段和相等,求最小和,若不能分则和为这一整序列。#include<stdio.h>
int dp[7000][7000];
int min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int a[10005],ans[10005],t,c,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&c,&m); ans[0]=0;
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
ans[i]=ans[i-1]+a[i];
}
for(int r=0;r<m;r++)
for(int i=1;i<=m-r;i++)
{
int j=i+r;
dp[i][j]=ans[j]-ans[i-1];
for(int k=i;k<j;k++)
{
if(ans[k]-ans[i-1]==dp[k+1][j])
dp[i][j]=min(dp[i][j],dp[k+1][j]);
if(dp[i][k]==ans[j]-ans[k])
dp[i][j]=min(dp[i][j],dp[i][k]);
if(dp[i][k]==dp[k+1][j])
dp[i][j]=min(dp[i][j],dp[i][k]);
}
}
printf("%d %d\n",c,dp[1][m]);
}
}
hdu3280Equal Sum Partitions (区间DP)的更多相关文章
- UVA - 10891 Game of Sum (区间dp)
题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva10891 Game of Sum(博弈+区间dp+优化)
题目:点击打开链接 题意:两个人做游戏,共有n个数,每个人可以任选一端取任意多连续的数,问两个人都想拿最多的情况下,先手最多比后手多拿多少分数. 思路:这题一开始想到的是用dp[i][j]表示区间[i ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 【UVA】10891 Game of Sum(区间dp)
题目 传送门:QWQ 分析 大力dp.用$ dp[i][j] $表示$ [i,j] $A能得到的最高分 我看到博弈论就怂... 代码 #include <bits/stdc++.h> us ...
- UVA - 10891 Game of Sum 区间DP
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461 Game of sum Description This ...
- E - Max Sum Plus Plus Plus HDU - 1244 (线性区间DP)
题目大意: 值得注意的一点是题目要求的是这些子段之间的最大整数和.注意和Max Sum Plus Plus这个题目的区别. 题解: 线性区间DP,对每一段考虑取或者不取.定义状态dp[i][j]指的 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
- 区间dp专题练习
区间dp专题练习 题意 1.Equal Sum Partitions ? 这嘛东西,\(n^2\)自己写去 \[\ \] \[\ \] 2.You Are the One 感觉自己智力被吊打 \(dp ...
随机推荐
- linux----ln
1.格式 ln source_file_path target_file_path 2.执行ln 命令的用户要对source_file_path有写权限,才可以创建软连接. 3.souce_file这 ...
- Linux06--Shell程序设计02 数据流重定向与管道
包含3种数据流: •标准输入(stdin):代码为0,符号为<或<<; •标准输出(stdout):代码为1,符号为>或>>; •标准错误输出(stderr):代码 ...
- java.lang.OutOfMemoryError: PermGen space 解决方案
只需两步: 将值改为512或者1024,然后CTRL+S,重启tomcat 和eclipse即可.
- javascript时间处理方法收集
首先收集到的是一个给某一个时间对象增加一段时间的方法, 例如2026-05-11增加一个月的时间,增加后时间为2026-05-11, 代码如下: function DateAdd(interval,n ...
- C# DataTable的用法
建表DataTable dtP = new DataTable("Man");加列DataColumn dcP;dcP = new DataColumn("Name&qu ...
- Web 应用配置Log4Net
1.第一步:在web.config文件添加如下代码: [sourcecode language="csharp"] <configSections> <secti ...
- KeystoneJS+mongo搭建简易博客
KeystoneJS 是一款基于 Express 和 MongoDB 的开源免费 Node.js CMS 网站开发框架. 一. 安装node.js,mongodb 二. 命令行安装KeystoneJS ...
- JAVA 堆设置
JAVA 堆设置 第四节 堆已经讲得差不多啦,这章我们以一个例子来说说如何设置以及当发生堆溢出的时候怎么排查问题.先看一小段代码: 代码中使用了一个无限循环来为list添加对象,如果采 ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- php5.3 PHP5.4 PHP5.5 新特性/使用PHP5.5要注意的
1.PHP 5.3中的新特性 1.1 PHP 5.3中的新特性 1.1.1. 支持命名空间 (Namespace) 毫无疑问,命名空间是PHP5.3所带来的最重要的新特性. 在PHP5.3中,则只 ...