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. 通过快捷键及cmd命令注销系统

    公司的外网内网是隔离的 外网的远程电脑屏幕一半卡那了,页面注销键正好在卡死的那一半屏幕上,用以下简单方法注销远程重新连接,问题解决了. 1.通过快捷键win+r打开“运行...” 2.输入CMD 回车 ...

  2. [转]Openstack Havana Dashboard测试和使用

    转贴一篇陈沙克老师的文章:http://www.chenshake.com/openstack-havana-dashboard-to-test-and-use/ Openstack Havana D ...

  3. mvn 安装ojdbc6.jar

    mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion= - Dpackaging=jar -Dfile ...

  4. iOS开发--多线程

    前面在<Bison眼中的iOS开发多线程是这样的(二)>一文中讲完了多线程的NSThread,不难发现这种方式的多线程实现起来非常的复杂,为了简化多线程的开发,iOS提供了GCD来实现多线 ...

  5. Oracle配置详解

    [Oracle连接字符串][Oracle Net Manager 服务命名配置][PL/SQL 登陆数据库] 连接数据库的几个重要参数: 1. 登陆用户名:user: 2. 登录密码:password ...

  6. C++:概述

    1.基本的输入输出,使用cin>>输入输入.使用cout<<输出 #include<iostream> using namespace std; int main( ...

  7. [转]更新Debian软件源

    转自:香神无涯 sudo cp /etc/apt/sources.list /etc/apt/sources.list_bak #备份一下软件源sudo vi /etc/apt/sources.lis ...

  8. ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法

    https://msdn.microsoft.com/zh-cn/library/ee378665(v=vs.110).aspx 此方法有一共有2个,现在只讨论其中一个 public TValue A ...

  9. [leetcode72]Edit Distance(dp)

    题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...

  10. [NYIST16]矩形嵌套(DP,最长上升子序列)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=16 像套娃一样把矩形套起来.先给矩形从小到大排序,然后做最长上升子序列就行 /* ━━━━ ...