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 ...
随机推荐
- 可以使用QT给龙芯开发软件
直接apt-get install libqt5core5a就有了,也许是一个很好的小众市场机会呢 至于系统,可以使用debian mips https://www.debian.org/devel/ ...
- SSH25个命令 + 深入SSH端口转发细节
OpenSSH是SSH连接工具的免费版本.telnet,rlogin和ftp用户可能还没意识到他们在互联网上传输的密码是未加密的,但SSH是加密的,OpenSSH加密所有通信(包括密码),有效消除了窃 ...
- react-native学习笔记——ViewStack组件
今天来写一个组件,相信很多人都会用到的——ViewStack. ViewStack组件无疑是UI中很重要的一个组件,可惜react-native并没有内嵌进去,需要开发者自己去实现. 实现原理很简单, ...
- poj3589---判断两个数有多接近
#include <stdio.h> #include <stdlib.h> int main() { ],s2[]; int a,b,i,j,n; scanf("% ...
- opennebula auth module ldap
1,安装net-ldap addon ruby library for openldap
- struts 2 三目运算
<s:set id="styleList" value="{'default','success','warning'}"/> <s:iter ...
- 04737_C++程序设计_第1章_认识C++的对象
例1.1 演示使用结构对象的示例程序. //功能:将结构对象的两个域值相加,乘以2再加50 #include <iostream>//包含头文件 using namespace std;/ ...
- php的引用
<?php $var1 = 1; $var2 = 2; function foo(){ global $var1,$var2; $var2 = &$var1; } function te ...
- Cube(规律)
Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 我的小前端 (1)—— 安卓机和ios机的区别
没有什么特别新技术,就是记录我做移动端遇到的问题 2016-02-16 微信,支付宝和APP都会遇到这些问题 一.安卓机和ios机的区别 1.常用 <head> <me ...