HDU-3280 equal sum partitions

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3280

Simple enumeration is used.

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 descriptionan Equal sum PartitionOf 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 7May be grouped: (2 5) (1 3 3) (7)To yield an equal sum 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. inputthe 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 in less than 10 values. outputfor 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 input31 62 5 1 3 72 61 2 3 4 5 63 201 1 2 1 1 1 2 1 2 2 11 2 1 1 1 1 1 2 1 1 sample output1 72 213 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;}/*31 62 5 1 3 3 72 61 2 3 4 5 63 201 1 2 1 1 2 1 1 2 11 2 1 1 2 1 1 2 1 1*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.