HDU 3280 Equal Sum partitions (binary search)

Source: Internet
Author: User

Equal Sum PartitionsTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 551 Accepted Submission (s): 409


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 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 are an equal sum partition with the sum equal to the sum of All the numbers in the sequence.

For this problem, you'll write a program that takes as input a sequence of positive integers and returns the smallest SU m 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 all 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, and separated by a single space. The last line of the dataset may contain less than 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 a equal sum partition of the sequence.
Sample Input
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

Sample Output
1 72 213 2

Source2009 Greater New York Regional
Recommend



/* Test instructions: N number, divided into several sets, requires the number of each set and the same, to find the set of the minimum idea: Enumerate the current set to determine whether to meet the conditions */#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std;typedef __int64 ll; #define N 10005# Define INF 0x3f3f3f3fint Sum[n];int n;bool FDD (ll temp) {     int hh=0;     int pos=0;     while (pos!=n)     {         hh+=temp;         Pos=upper_bound (SUM+1,SUM+N+1,HH)-(sum+1);         if (sum[pos]!=hh)         {            return false;         }     }     return true;} int main () {    int i,j,t,ca;    sum[0]=0;    scanf ("%d", &t);    while (t--)    {        scanf ("%d%d", &ca,&n);        int x;        for (i=1;i<=n;i++)        {            scanf ("%d", &x);            sum[i]=sum[i-1]+x;        }        for (i=1;i<=n;i++)            if (FDD (sum[i))) break;        printf ("%d%d\n", Ca,sum[i]);    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3280 Equal Sum partitions (binary search)

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.