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

HDU-3280 Equal Sum Partitions的更多相关文章

  1. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  3. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  4. HDU 1244 Max Sum Plus Plus Plus

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

  5. hdu3280Equal Sum Partitions (区间DP)

    Problem Description An equal sum partition of a sequence of numbers is a grouping of the numbers (in ...

  6. D.6661 - Equal Sum Sets

    Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elem ...

  7. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  8. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  9. 698. Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

随机推荐

  1. 查杀linux线程指令

      工作中重启环境时常常出现内存溢出等等问题,往往需要查杀进程来帮助重启成功,下面就查杀线程的详细指令做下总结:   1.查找需要kill掉的线程: ps -elf|grep [线程关键信息] 比如: ...

  2. 华为3C抢购难度

    上周小米2S降价到1299买了一个,今天突然想体验一下抢购红米和3C的难度.万一抢到了,拿到手机市场贵100块钱卖掉,然后可以请女神吃个饭~~~哈哈哈哈! 结果确实不怎么好抢.刚刚试了一下3C: 验证 ...

  3. 小米2s 降到1299

    关于这个价格,网上叫声一片,尤其是几天前刚买了小米2s的,恨死了雷布斯…… 以下是来自百度贴吧的帖子: [02-27 米粉杂谈]我来说个关于价格的事实吧 http://tieba.baidu.com/ ...

  4. 【Apache运维基础(1)】Apache的安装与使用

    安装 yum -y install httpd httpd-devel # 在Ubuntu里面叫做Apache2,输入localhost能打开就算成功了 额...当然专业的运维还是老老实实的去编译吧; ...

  5. Android 线程通讯类Handler

    handler是线程通讯工具类.用于传递消息.它有两个队列: 1.消息队列 2.线程队列 消息队列使用sendMessage和HandleMessage的组合来发送和处理消息. 线程队列类似一段代码, ...

  6. MongoDB 安装和即基本操作

    http://www.mongodb.org/ Agile and Scalable MongoDB (from "humongous") is an open-source do ...

  7. i386 和amd64 的意思

    首先可以简化一个概念,i386=Intel 80386.其实i386通常被用来作为对Intel(英特尔)32位微处理器的统称. Windows NT类系统的安装盘上,通常i386是其根上的一个文件夹, ...

  8. 将SQLServer2005中的数据同步到Oracle中

    有时由于项目开发的需要,必须将SQLServer2005中的某些表同步到Oracle数据库中,由其他其他系统来读取这些数据.不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. ...

  9. bzoj2251

    以前看到这道题想到的是SA,做起来不是很美观 学了SAM之后,这题简直是随便搞 ..,'] of longint; s,sa,mx,w,fa:..] of longint; i,n,last,t:lo ...

  10. UVa 11054 Wine trading in Gergovia【贪心】

    题意:给出n个等距离的村庄,每个村庄要么买酒,要么卖酒,买酒和卖酒的总量相等, 把k个单位的酒从一个村庄运送到相邻的村庄,需要耗费k个单位劳动力,问怎样运送酒使得耗费的劳动力最少 买     卖    ...