• TZOJ 2941 Equal Sum Partitions 模拟


    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.

    输入

    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.

    输出

    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.

    样例输入

     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

    样例输出

    1 7

    2 21

    3 2

    题意:按顺序组合成多组(可以一组)同样的值(最小)

    解析:先存一个前n个和的数组,从第一个开始,依次看后面能否成立,不行就退出进行下一个比较。

    #include<bits/stdc++.h>
    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,sizeof(a));
            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,js=i;
                for(j=js;j<n;j++)
                {
                    if(ans<sum[j]-sum[js])
                        break;
                    else if(ans>sum[j]-sum[js])
                        continue;
                    else
                    {
                        js=j;
                    }
                }
                if(ans!=sum[j]-sum[js])
                    ans=sum[n];
                if(j==n)
                    break;
            }
            cout<<m<<" "<<ans<<endl;
        }
    }

     

  • 相关阅读:
    P3391 文艺平衡树
    隔离村庄(树形dp[01背包])
    cmd指令集
    vs的使用
    博客园第一天
    蓝桥杯 小生物的逃逸 模拟
    蓝桥杯 自行车停放 双向链表
    c++字符数组函数总结
    蓝桥杯 石子游戏 贪心
    蓝桥杯 最大获利 模拟
  • 原文地址:https://www.cnblogs.com/lhlccc/p/11567654.html
Copyright © 2020-2023  润新知