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,题意很清楚,就是求把一个序列分成若干段,每段的和尽可能相等并且要小。
做法就是记忆搜索每个区间,相等的判小,重置为更小的那个。具体看代码。
【AC代码】
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int m,n,a[12000];
int sum[10005];
int dp[10001][10001];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum[0]=0;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(int i=0;i<n;++i)
{
for(int j=1;j<=n-i;++j)
{
int k=j+i;
dp[j][k]=sum[k]-sum[j-1];
for(int l=j;l<k;++l)
{
if(dp[l+1][k]==sum[l]-sum[j-1]) dp[j][k]=min(dp[j][k],dp[l+1][k]);
if(dp[j][l]==sum[k]-sum[l]) dp[j][k]=min(dp[j][k],dp[j][l]);
if(dp[j][l]==dp[l+1][k]) dp[j][k] = min(dp[j][k],dp[j][l]);
}
}
}
printf("%d %d\n",m,dp[1][n]);
}
return 0;
}