hdu3280Equal Sum Partitions (区间DP)「建议收藏」

hdu3280Equal Sum Partitions (区间DP)

大家好,又见面了,我是全栈君。

Problem 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
题意:给出一个序列,假设能把该序列分成若干段,使每段和相等,求最小和,若不能分则和为这一整序列。
#include<stdio.h>
int dp[7000][7000];
int min(int a,int b)
{
    return a>b?b:a;
}
int main()
{
    int a[10005],ans[10005],t,c,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&c,&m); ans[0]=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&a[i]);
            ans[i]=ans[i-1]+a[i];
        }
        for(int r=0;r<m;r++)
        for(int i=1;i<=m-r;i++)
        {
            int j=i+r;
            dp[i][j]=ans[j]-ans[i-1];
            for(int k=i;k<j;k++)
            {
                if(ans[k]-ans[i-1]==dp[k+1][j])
                dp[i][j]=min(dp[i][j],dp[k+1][j]);
                if(dp[i][k]==ans[j]-ans[k])
                dp[i][j]=min(dp[i][j],dp[i][k]);
                if(dp[i][k]==dp[k+1][j])
                dp[i][j]=min(dp[i][j],dp[i][k]);
            }
        }
        printf("%d %d\n",c,dp[1][m]);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115941.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 新一代金融神话Filecoin[通俗易懂]

    新一代金融神话Filecoin[通俗易懂]Filecoin是区块链史上耗时最短的ICO融资项目,受到了DCG集团、文克莱沃斯兄弟基金会、联合广场风投、AndersonHollotz基金和红杉资本等投资巨头的青睐。储存即收益,Filecoin与BTC的工作证明是不同的。filecoin只需要提供存储空间和宽带,就可以满足获取身份证明的需求。因此,Filecoin可以从根本上提高人类的效率,是一种真正的共享经济,它可以极大地推动存储资源的使用方式。由于Filecoin是基于强大的IPFS协议,并且由于IPFS的大量应用,Filecoin作为IPFS

    2025年11月3日
    4
  • UVALive 3135–Argus+自己定义优先队列的优先规则「建议收藏」

    UVALive 3135–Argus+自己定义优先队列的优先规则

    2022年2月6日
    55
  • 简单介绍一下spring bean的生命周期_java类的生命周期

    简单介绍一下spring bean的生命周期_java类的生命周期1.springbean生命周期2.Aware接口2.1.作用一个标记,标记在spring容器初始化时需要获取上下文中当前的一些运行环境;2.2.常用接口ApplicationContextAware:获取ApplicationContextspring上下文;ApplicationEventPublisherAware:获取ApplicationEventPublisher事件发布器;BeanClassLoaderAware:获取当前的ClassLoader类加载器;BeanFac

    2025年11月23日
    2
  • iPad2成功越狱

    iPad2成功越狱昨天iPad2的越狱终于出啦,作为小白的我,在网上各种教程的帮助下,成功越狱!其实过程还是很简单的~ 步骤一:备份SHSH(据说如果你的设备无需降级,可以忽略备份SHSH这一步,不会对越狱有影响)参考:http://bbs.weiphone.com/read-htm-tid-20

    2022年5月30日
    45
  • postgresql主从复制配置「建议收藏」

    postgresql主从复制配置「建议收藏」postgresql主从复制是一种高可用解决方案,可以实现读写分离。postgresql主从复制是基于xlog来实现的,主库开启日志功能,从库根据主库xlog来完成数据的同步。主从复需要注意的地方:启动从库之前,不能执行初始化。 启动从库之前,需要通过base_backup从主服务器上同步配置与数据。 启动从库之前,需要对同步之后的配置文件进行修改。 启动从库之前,需要设置一个恢复的…

    2022年8月13日
    8
  • pycharm激活码2021年6月-激活码分享

    (pycharm激活码2021年6月)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWNlbnNlSWQi…

    2022年3月21日
    166

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号