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)
上一篇 2022年1月28日 下午12:00
下一篇 2022年1月28日 下午12:00


相关推荐

  • JS它DOM

    JS它DOM

    2022年1月6日
    50
  • “KIMI”关联公司再获资本加持,海南月之暗面增资至5亿美元

    “KIMI”关联公司再获资本加持,海南月之暗面增资至5亿美元

    2026年3月12日
    3
  • win10安装JDK1.8及配置java环境变量详解

    win10安装JDK1.8及配置java环境变量详解首先下载一个jdk,可以通过这个链接下载:https://pan.baidu.com/s/1aP6SdL8UQK_C2GvALLb6Wg接下来就是安装,非常的简单,如下图所示:双击下载的文件,出现该界面,点击下一步。安装路径我们选择默认的,当然,我们也可也修改安装路径,但一定要记得安装路径,这里我们选择默认的。点击下一步。这里我们还是默认的安装路径。点击下一步。到此,安装就完成了…

    2022年7月23日
    9
  • 计算机专业的男生喜欢你,男生暗恋你的20个动作 一秒看出他喜欢你

    计算机专业的男生喜欢你,男生暗恋你的20个动作 一秒看出他喜欢你很多女生想知道是不是有男生在暗恋自己,下面小编为大家介绍一下男生暗恋你的20个动作。1、眼睛总是忍不住盯着那个女生。2、在女生的面前会表现得特别活跃。3、在女生开心的时候会很开心,在女生伤心的时候,心情就会像乌云密布的天空。4、如果那个女生和其他男生表现得很要好,就会忍不住要吃醋。5、如果那个女生心情低落,就会想过去安慰她。6、对于女生所拜托的事情,就算再难做也会答应。7、女生一上Q,就会很兴奋,…

    2022年7月25日
    40
  • JQuery——图片缩放和截图发送

    记录一下图片缩放 和截图发送!图片缩放 https://yihui.name/cn/2007/09/highslide-and-lightbox/ http://www.zhangxinxu.com/jq/balupton_zh/demo/ http://www.dowebok.com/demo/214/ http://www.lanrentuku.com/js/tupian-933.htm

    2022年2月26日
    46
  • 惊艳四射的意思_词语什么四射

    惊艳四射的意思_词语什么四射分享一些CSS3相关的按钮和导航,大部分素材应该都来自一些老外的设计,希望接下来的几篇文章对你会有所帮助,当然你的支持和点评也是我坚持做下去的动力。正文今天的这款CSS3按钮应该说是非常的光彩夺目,因为不仅它的色彩调得非常的和谐,更美妙的是如果你用chrome或者safari浏览器还能看到按钮发光的特效。以下是效果截图在线示例    |    源码下载这里的发光效果主要是如

    2025年6月28日
    2

发表回复

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

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