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. poj 1026 Cipher

    置换群就可以搞定!!! 注意下格式就好了…… #include<iostream> #include<stdio.h> #include<algorithm> #i ...

  2. ubuntu 13.10 64bit装BeyondCompare

    1. Beyond Compare官网下载amd-64位的,安装失败,依赖于ia32-libs,但是这个文件已经不在源里了: 2. 官网下载tar.gz源码包,解压安装失败: 3. 直接装32位的,可 ...

  3. Play Framework 2.2.6 安装

    网络上很多安装方法都是互相复制黏贴的, 都没有人考虑到启动application 还有依赖很多jar 包,而其中typesafe 官网提供的只是一个mini的 启动器来安装,很慢,所以以下下载完整包. ...

  4. Mac 如果一定要写个锁屏程序的话就这样

    package test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import ...

  5. mac 设置 ll 等alias 并永久生效

    往上看了在.bash_profile中配置 然后 source  的方法, 试过了, 只是当前的终端有效,当电脑重启或者关闭终端就失效了,只好看看 mac 的 profile 代码 # System- ...

  6. css元素z-index设置为什么不起作用?

    元素位置重叠的背景常识 (x)html文档中的元素默认处于普通流(normal flow)中,也就是说其顺序由元素在文档中的先后位置决定,此时一般不会产生重叠(但指定负边距可能产生重叠). 当我们用c ...

  7. Solr查询详解

    前言:上节是关于Solr的开发准备工作:.NET开发过程中的全文索引使用技巧之Solr(http://www.cnblogs.com/johnwood/p/3447242.html) 这节重点是讲So ...

  8. iOS 中有用的开源库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  9. java:构造函数

    class Dog { Dog(){ } } 构造函数没有返回值定义,构造函数名必须和类名相同,如果类里面没有构造函数,编译器会帮你加一个构造函数. 使用this调用构造函数 class Dog { ...

  10. Java String.compareTo()方法

    描述:java.lang.String.compareTo() 方法比较两个字符串的字典. 比较是基于字符串中的每个字符的Unicode值.此String对象表示的字符序列的 参数字符串表示的字符序列 ...