http://acm.hdu.edu.cn/showproblem.php?pid=3280

用了简单的枚举。

Equal Sum Partitions

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 453    Accepted Submission(s): 337

Problem Description
An equal sum partition of a sequence of numbers is a grouping of the numbers (in the same order as the original sequence) in such a way that each group has the same sum. For example, the sequence: 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.
Input
The first line of input contains a single integer 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.
Output
For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the smallest sum for an equal sum partition of the sequence.
Sample Input
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

Sample Output
1 7
2 21
3 2
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[10005];
int main()
{int i,j,t,n,m,sum,cursum,flag ,ans;scanf("%d",&t);while(t--){flag=0;memset(a,0,sizeof(a));scanf("%d%d",&n,&m);for(i=0;i<m;i++)scanf("%d",&a[i]);for(i=0;i<m;i++){sum=0;for(j=0;j<=i;j++)sum+=a[j];cursum=0;while(j<m){cursum+=a[j];if(cursum>sum)break;else if(cursum==sum){j++;if(j==m){printf("%d %d\n",n,sum);flag=1;}cursum=0;}elsej++;if(flag)break;}if(flag)break;}if(i==m)printf("%d %d\n",n,sum);}return 0;
}
/*
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
*/

区间dp

#include<iostream>
#include<cstdio>
using namespace std;
int dp[10005][10005],ans[10005];
int main()
{int t,n,m,i,j,k,g,a[10005];cin>>t;while(t--){cin>>n>>m;ans[0]=0;for(i=1;i<=m;i++){cin>>a[i];ans[i]=ans[i-1]+a[i];}for(k=0;k<m;k++)//k不能从1-m,虽然同样个数相同,但是j=2开始,就会使区间减少了一层,{               //比如i=1,j=2就没有这个区间。for(i=1;i<=m-k;i++){j=i+k;dp[i][j]=ans[j]-ans[i-1];//初始化dp,求出每个区间的和。for(g=i;g<j;g++){//三者的顺序可以随便调换。if((ans[g]-ans[i-1])==dp[g+1][j])dp[i][j]=min(dp[i][j],dp[g+1][j]);if(dp[i][g]==ans[j]-ans[g])dp[i][j]=min(dp[i][j],dp[i][g]); if(dp[i][g]==dp[g+1][j])dp[i][j]=min(dp[i][j],dp[i][g]);}}}printf("%d %d\n",n,dp[1][m]);}}
/*
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
*/

转载于:https://www.cnblogs.com/cancangood/p/3859488.html

HDU-3280 Equal Sum Partitions相关推荐

  1. hdu 4961 Boring Sum(高效)

    题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; 构造bi, k=max(j|0<j<i,aj%ai=0), bi=ak; 构造ci, k=min(j|i< ...

  2. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  3. HDU.1003 Max Sum

    原题 HDU.1003 Max Sum 分类 动态规划 题意 计算从一个序列中最大连续子序列和.对应的起始元素和终止元素的位置. 输入/输出 要求与格式 样例数的确定 最开始一行开始输入样例数 每个样 ...

  4. hdu 1003 Max Sum 解题报告

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem Description Given a sequence a[1],a[2],a[3 ...

  5. HDOJ(HDU) 1977 Consecutive sum II(推导、、)

    Problem Description Consecutive sum come again. Are you ready? Go ~~ 1 = 0 + 1 2+3+4 = 1 + 8 5+6+7+8 ...

  6. HDU - 5381 The sum of gcd(莫队/线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,再给出 mmm 次询问,每次询问需要回答区间 [L,R][L,R][L,R] 内所有子区间的 gcdgcdgcd 之和.更具体的,对于询问 ...

  7. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. HDU - 6955 Xor sum tire树 + 贪心

    传送门 文章目录 题意: 思路: 题意: 给你一个数列aaa,你需要找出来一个长度最小且左端点最靠前的区间,使其异或和≥k\ge k≥k. n≤1e5,0≤ai,k<230n\le1e5,0\l ...

  9. 698. Partition to K Equal Sum Subsets

    文章目录 1 理解题目 2 分析 2.1进一步优化 2.2 根据花花酱解答 1 理解题目 Given an array of integers nums and a positive integer ...

最新文章

  1. 深度学习的分布式训练--数据并行和模型并行
  2. 谷歌排名第一的编程语言,死磕它这两点,小白也能学的会!不信你看!
  3. 每列大于0的个数_二进制中1的个数(剑指offer第十四天)
  4. 你还不知道mysql中空值和null值的区别吗?
  5. 后端技术:Nginx从安装到高可用,看完本篇就够了!
  6. Visual Studio 2012 简体中文 旗舰正式版 ISO 下载
  7. 5G还没来,我的4G网速就变慢了!运营商到底有没有说实话?
  8. 一篇文章带你快速入门JavaScript(自学者福利)
  9. 从有理数到实数(序)
  10. starccm中文用户指南_【干货】Salesforce系统管理员认证考试指南
  11. jmeter中变量的作用范围_Jmeter参数化方式总结
  12. chrome 打印布局_在打印预览模式下使用Chrome的Element Inspector?
  13. 多元函数(multivariate function)分析(方向导数和梯度)
  14. k8s架构以及相关概念普及
  15. 微擎系统 微信支付 get_brand_wcpay_request:fail
  16. 时序分析 29 - 时序预测 - 格兰杰因果关系(下) python实践2
  17. 一位百度AI工程师的求职经历(offer/面经/干货/感悟)
  18. 如何设计一个吸引访问者的网站主页?
  19. 蓝侠==la*uan,破解中国共享软件联盟著名灌水专家“蓝侠””
  20. 前后端交互node服务器

热门文章

  1. JZOJ 2256. 【BZOJ 2256】【ZJOI 2008】树的统计
  2. python 任务计划_使用Python添加计划任务
  3. python生成静态html_Python写静态HTML
  4. matlab优化应用
  5. 2021.12.20用ULN2003驱动四线步进电机
  6. JZOJ__Day 10:【普及模拟】【USACO】山峰暸望
  7. 索尼a5100_【大象原创】索尼微单最全功能就在这里啦
  8. php结尾的链接_优化 PHP 代码建议(结尾有彩蛋)
  9. 用python搭建个人博客过程_技术分享|利用Python Django一步步搭建个人博客(四)...
  10. 2020-12-03 The Geometry of Rotations and Rigid-Body Motions (刚体运动和旋转的几何表示,罗德里格参数)