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

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;
                   }
                 else
                     j++;
                 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
*/

HDU-3280 Equal Sum Partitions

时间: 2024-08-24 17:28:44

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/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

HDU ACM 3280 Equal Sum Partitions

分析:从前往后递推第一个和sum,然后往后依次划分,当第一次能够划分完,就得到最小的结果了. #include<iostream> using namespace std; int a[10005]; int P,M,T,K; void solve() { int sum,cursum,i,j,j2; bool flag; flag=false; for(i=0,j2=0,sum=0;i<M;i++) { for(;j2<=i;j2++) sum+=a[j2]; j=j2; cur

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

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

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

[2016-03-28][HDU][1024][Max Sum Plus Plus]

时间:2016-03-28 17:45:33 星期一 题目编号:[2016-03-28][HDU][1024][Max Sum Plus Plus] 题目大意:从n个数字提取出一定数字组成m个部分,使得这个部分的总和最大 分析: dp[i][j]表示前i段计算第j个数字,dp[i][j] = max(dp[i - 1][j - 1] + a[j],dp[i][k] + a[j]); #include <algorithm> #include <cstring> #include &

HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

本题又是一题单调队列题解. 技巧就是需要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tail) 其中头q(head)代表当前最佳解的起点 这样我们只需要在求某点为结尾的S[i] - S[q(head)就得到当前最佳值. 了解了单调数列,知道其中的记录意义,那么这道题就没有难度了.我也是了解这些信息之后就自己敲出代码的. 不过有些细节没写好也让我WA了几次. 最近少刷水题,而一直都是每天一

hdu 2576 Another Sum Problem

题目大意:求前n项和的前n项和. 数学推导题,f(n)=n*(n+1)*(n+2)/6 推导思路如下: #include"cstdio" #include"cstring" #include"cmath" #include"cstdlib" #include"iostream" #include"algorithm" #include"queue" using nam

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

题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的和为sum[i]. 然后问题变成了求一个max{sum[i]-sum[j]}(i-k<j<i) 意思就是对于每一个sum[i],我们只需要找一个满足条件的最小的sum[j],然后我们就可以用一个单调队列来维护. 1 #include<bits/stdc++.h> 2 #define F

Hdu 1024 Max Sum Plus Plus (dp)

题目链接: Hdu 1024 Max Sum Plus Plus 题目描述: 给出n个数,问m段连续子序列的和相加最大是多少? 解题思路: dp[i][j]表示把前i个元素(包括第i个),分成j段的最大和.状态转移方程就是dp[i][j] = max (dp[i-1][j] + arr[j],  max( dp[k][j-1]) + arr[j]),其中0<k<i.(第i个元素是保存在第j段,还是自己单独成段) 由于1<=n<=1000,000.n*n的数组肯定会爆炸,所以要对方程