Equal Sum Partitions

本文探讨了一道经典的算法问题——等分序列求最小和。该问题要求将一个正整数序列分组,使得每组的和相等,并找出这样的分组中最小的可能和。文章提供了完整的输入输出示例及C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                                                                            点击打开链接

 

                                                                                                            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;
}


 

 

Write a C program to partitions a hypergraph G = (V, E) into 2 partitions. The Assignment Write a computer program that takes a netlist represented by a weighted hypergraph and partitions it into two partitions. Each node is associated with an area value and each edge has an edge cost. Your program should minimize the total cost of the cut set, while satisfying the area constraint that the total area of partition 1 should satisfy the balance criteria as described in the class. That is, if the area sum of all the nodes is A, then the area of partition 1 should be greater than or equal to ra-tio_factor *A – amax and less than or equal to ratio_factor *A + amax, where amax is the maximum value among all cell areas. The program should prompt the user for the value of ratio_factor. Assumptions and Requirements of the Implementation 1. Your program should not have any limitation on the maximum number of nodes and the edges of the hypergraph. Each hyperedge could connect any subset of nodes in the hypergraph. 2. Each node area is a non-negative integer, and each edge cost is a non-negative floating- point value. 3. All the ids are 0-based. Namely, the id of the first element is 0, instead of 1. 4. The output of each partition should include the list of node ids, sorted in the ascending order. 5. The partition with the smaller minimum node id is listed first in the output. 6. Use balance criteria as the tiebreaker when there are multiple cell moves giving the max-imum gain, as described in the class. 7. Use the input and output formats given in the Sample Test Cases section. Sample Test Cases Test1: Please enter the number of nodes: 4 Please enter each of the 4 nodes with its id and the node area: 0 1 1 1 2 1 3 1 Please enter the number of edges: 3 Please enter each of the 3 edges with the number of connected nodes and their node ids, followed by the edge cost: 2 0 1 1 2 1 2 3 2 2 3 1 Please enter the percentage of the ratio factor: 50 The node ids of the partition 0 are 0 The node ids of the partition 1 are 1, 2, 3 The total cut cost is 1 Test2: Please enter the number of nodes: 4 Please enter each of the 4 nodes with its id and the node area: 0 1 1 4 2 2 3 1 Please enter the number of edges: 3 Please enter each of the 3 edges with the number of connected nodes and their node ids, followed by the edge cost: 3 0 1 2 5 3 0 2 3 3 3 0 1 3 4 Please enter the percentage of ratio factor: 50 The node ids of the partition 0 are 3 The node ids of the partition 1 are 0, 1, 2 The total cut cost is 7
最新发布
07-08
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值