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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java程序main方法执行流程

    Java程序main方法执行流程Java程序main方法执行流程当我们编写完java源代码程序后,经过javac编译后,执行java命令执行这个程序时,是怎么一步步的调用到我们程序中的main方法的呢?今天通过查看OpenJdk的源码来揭开它的神秘面纱。java命令是在安装jre/jdk时配置到系统环境路径中去的,执行java命令时会找到bin目录下的java可执行程序,并将我们编译后的java程序类名传递进去就可以执行了。…

    2022年5月13日
    49
  • linux下安装tomcat8

    linux下安装tomcat8(一)安装JDK环境64位JDK包:http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz1.用 SecureCRT链接上linux,用命令直接下载 wget http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45

    2022年6月2日
    44
  • kafka基本命令_kafka controller

    kafka基本命令_kafka controllerkafka-console-producer.sh脚本通过调用kafka.tools.ConsoleProducer类加载命令行参数的方式,在控制台生产消息的脚本。本文是基于Kafka_2.12-2.5.0版本编写的,–bootstrap-server参数于此版本开始被使用,而–broker-list也是在此版本开始被置为过时,但其属性值依旧保持不变。在使用较旧版本时,注意…

    2022年10月14日
    2
  • Unity零基础到入门 ☀️| 游戏引擎 Unity 从0到1的 系统学习 路线【全面总结-建议收藏】![通俗易懂]

    Unity零基础到入门 ☀️| 游戏引擎 Unity 从0到1的 系统学习 路线【全面总结-建议收藏】![通俗易懂]Unity基础知识学习,Unity学习路线总结。本篇文章对Unity的学习路线做了一个全面系统的总结,对Unity有兴趣的小伙伴福利到了!敬请品尝~

    2022年6月12日
    31
  • ECCV 2020 的对抗相关论文(对抗生成、对抗攻击)

    ECCV 2020 的对抗相关论文(对抗生成、对抗攻击)

    2020年11月14日
    235
  • Java Web项目 慧心人力资源管理系统[通俗易懂]

    Java Web项目 慧心人力资源管理系统[通俗易懂]美和易思JavaWeb机试试题题目:慧心人力资源管理系统文档下载:https://download.csdn.net/download/weixin_44893902/16336711实现代码下载:目录一、语言和环境二、实现功能三、数据库设计四、具体要求及推荐实现步骤五、评分标准六、实现代码一、语言和环境实现语言:JAVA语言。 环境要求:MyEclipse/Eclipse+Tomcat+MySql。 使用技术:Jsp+Servlet+Jav..

    2022年5月28日
    32

发表回复

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

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