HDU-3280 Equal Sum Partitions
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
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.
1 2 1 1 2 1 1 2 1 1
#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的更多相关文章
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1244 Max Sum Plus Plus Plus
虽然这道题看起来和 HDU 1024 Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...
- hdu3280Equal Sum Partitions (区间DP)
Problem Description An equal sum partition of a sequence of numbers is a grouping of the numbers (in ...
- 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 ...
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)
题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...
- 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 ...
- 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 ...
随机推荐
- POJ3080Blue Jeans
http://poj.org/problem?id=3080 题意 : 给你几个DNA序列,让你找他们的共同的最长的子串,若是子串长度小于3,就输出no significant commonaliti ...
- python 判断操作系统类型
#!/bin/python # import platform def TestPlatform(): print ("----------Operation System--------- ...
- servlet会话技术:Cookie
什么是会话会话可以简单理解为:用户开一个浏览器访问某个网站,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话.会话过程中需要解决的一些问题每个用户在使用浏览器与服务器进 ...
- 《数据通信与网络》笔记--TCP中的拥塞控制
1.拥塞窗口 发送方窗口的大小不仅取决于接收方,而.而且还取决于网络拥塞的情况. 发送方有2种信息:接收方通告的窗口大小和拥塞窗口的大小,实际的窗口大小事这两者中的最小者. 实际窗口大小 = min( ...
- IIS 无法打开页面,只能重启的问题
最终解决方案: 要变通解决此问题,启用 EnableAggressiveMemoryUsage 注册表项在注册表中.当启用了 EnableAggressiveMemoryUsage 注册表项 Http ...
- android4.4内核移植
01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...
- HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...
- 转:Android 设置屏幕不待机
本文转载于:http://blog.csdn.net/yudajun/article/details/7748760 Android设置支部待机有两种方法 第一种简单通过设置WindowManager ...
- mysqldump批量导出(多张表)表结构及表数据
Mysql 批量导出表结构(数据) 仅导出结构,不导出数据: 1.导出數據库為dbname的表结构 mysqldump -h主机地址 -u用户名 -p密码 -d dbname >db.s ...
- 8天学通MongoDB——第六天 分片技术
在mongodb里面存在另一种集群,就是分片技术,跟sql server的表分区类似,我们知道当数据量达到T级别的时候,我们的磁盘,内存 就吃不消了,针对这样的场景我们该如何应对. 一:分片 mong ...