Equal Sum Partitions(UVALive 4553,模拟)

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

题解:

确定规模,从前往后遍历求解连续元素和相等的最小值,若不存在,规模+1,继续遍历。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
#define fori(n) for(int i=0;i<n;i++)
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;

int a[11111];
int sum[11111];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        scanf("%d%d",&m,&n);
        memset(a,0);
        scanf("%d",&a[1]);
        sum[1]=a[1];
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        int ans;
        for(int i=1;i<=n;i++)
        {
            ans=0;
            for(int j=1;j<=i;j++)
                ans+=a[j];
            int j,ii=i;
            for(j=ii;j<n;j++)
            {
                //cout<<ans<<" ";
                //cout<<sum[j]-sum[ii]<<endl;
                if(ans<sum[j]-sum[ii])
                    break;
                else if(ans>sum[j]-sum[ii])
                    continue;
                else
                {
                    ii=j;
                }
            }
            if(ans!=sum[j]-sum[ii])
                ans=sum[n];
            if(j==n)
                break;
        }
        cout<<m<<" "<<ans<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值