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]);
    }
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Agora iOS SDK-开始聊天

    Agora iOS SDK-开始聊天

  • 2020年Vue面试题汇总[通俗易懂]

    2020年Vue面试题汇总[通俗易懂]2020年Vue面试题Interview●●●●作者:@烦恼会解决烦恼vue核心知识——理论篇1、对于Vue是一套渐进式框架的理解渐进式代表的含义是:主张最少。Vue可能有些方面是不如React,不如Angular,但它是渐进的,没有强主张,你可以在原有大系统的上面,把一两个组件改用它实现,当jQuery用;也可以整个用它全家桶开发,当A…

  • python无人机编程_3d硬金是什么意思

    python无人机编程_3d硬金是什么意思往期本文是双足机器人系列的第三篇,在前面的文章中我们介绍了2D线性倒立摆的基本理论,详见:【双足机器人(1)】线性倒立摆及其运动控制(附代码)在这篇文章中我们要详细介绍3D线性倒立摆的基本…

  • 在 Ubuntu系统下安装 OpenCV 全过程

    在 Ubuntu系统下安装 OpenCV 全过程经过不断的试错、安装、删除重装……写下这篇博客,希望其他人安装时能够轻松一点,少几次还原重装……版本:ubuntu18.10OpenCV-4.1.0首先,OpenCV有在ubuntu上安装的官方文档:https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html大家可以参照官方文档进行操作…

  • Statement 和 PreparedStatement之间的关系和区别

    Statement 和 PreparedStatement之间的关系和区别关系:PreparedStatement继承自Statement,都是接口 区别:PreparedStatement可以使用占位符,是预编译的,批处理比Statement效率高表示预编译的SQL语句的对象。接口:publicinterfacePreparedStatementextendsStatement之间的继承关系SQL语句被预编译并存储在PreparedStatement对象中。然后可以使用此对象多次高效地执行该语句。注:用于设置IN参数值的设置方法(setShort

  • 证书签名

    证书签名一、数字签名(digitalsignature)对指定信息使用哈希算法,得到一个固定长度的信息摘要,然后再使用私钥(注意必须是私钥)对该摘要加密,就得到了数字签名。所谓的代码签名就是这个意思。二、数字证书(digitalcertificate)证书生成开发者在申请iOS开发证书时,需要通过keychain生成一个CSR文件(CertificateSigningReque

发表回复

您的电子邮箱地址不会被公开。

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