现在的位置: 首页 > 综合 > 正文

Equal Sum Partitions

2014年04月05日 ⁄ 综合 ⁄ 共 2080字 ⁄ 字号 评论关闭

                                                                                                                                           
点击打开链接

 

                                                                                                            Problem A: Equal Sum Partitions

Equal Sum PartitionsTime Limit: 1 Sec  Memory Limit:
128 MB
Submit: 36  Solved: 27
[Submit][Status][Web Board]

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

 

 

dp题目真费脑子啊。哭

#include<stdio.h>
#include<string.h>
int s[1007],dp[1007][1007];
int main()
{
    int t,i,j,k;
    scanf("%d",&t);
    for(int textcase=1; textcase<=t; textcase++)
    {
        memset(dp,0,sizeof(dp));
        int n,m,sum,flag,flag1=0,max;
        scanf("%d%d",&n,&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d",&s[i]);
        }
        for(j=1; j<=m; j++)
        {
            dp[1][j]=s[j]+dp[1][j-1];
            max=dp[1][j];
            flag=j;
            //printf("\n");
            for(k=flag+1; k<=m; k++)
            {
                dp[flag+1][k]=s[k]+dp[flag+1][k-1];
                //printf("%d\n",dp[flag+1][k]);

                if(k==m)
                {
                    if(dp[flag+1][m]!=max)
                        break;
                    else
                    {
                        flag1=1;
                        break;
                    }
                }
                if(dp[flag+1][k]==max)
                {
                    flag=k;
                }
            }
            if(flag1==1)
                break;
        }
        printf("%d %d\n",textcase,max);
    }

    return 0;
}

 

 

【上篇】
【下篇】

抱歉!评论已关闭.