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


相关推荐

  • Ubuntu Mobile Demo at IDF Shanghai (英文)

    Ubuntu Mobile Demo at IDF Shanghai (英文)

    2021年7月27日
    53
  • sessionStorage 使用方法

    sessionStorage 使用方法作为 html5 中 WebStorage 的一种存储方式 localStorage 和 sessionStora 一样都是用来存储客户端临时信息的对象 这两者区别在于前者用于持久化的本地存储 除非主动删除数据 否则数据是永远不会过期的 而 sessionStora 存储的数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁 因此 sessionStora 不是一种持久化的本地存储

    2025年9月6日
    0
  • linux命令之pstack[通俗易懂]

    linux命令之pstack[通俗易懂]很多时候我们想知道在Linux下后台程序到底运行到哪里了,卡住了吗,出错了吗,最简单的我们会使用#psauxf|grep来查看后台程序的状态,可是如果想知道的更多,那就可以用到pstack这个命令了。首先举一个简单的例子(test.c)来引出这个命令 #include#include#includevoid*thread_proc(void*data)

    2022年9月14日
    3
  • Oracle常用操作

    Oracle常用操作

    2021年9月3日
    53
  • 看完很清醒,我相信这是一个同龄人写的。。加油。。被扇醒的感觉

    你学习一般,考上了现在的这个学校,成绩不算好,拿不到校奖国奖,自习不规律上课不常听,考试全靠突击,同学帮一把也能考到七八十分。你家境一般,父母都是普通员工,在这个城市一个月生活费一千二,没事下下馆子,一个月添一件衣服,想买台相机要等几个月,经常要咬咬牙才能买双自己喜欢的鞋。你特长一般,不会吉他不会钢琴不会跳舞不会画画,想学摄影却不会PS,想上台演出却没信心,学校晚会比赛的时候,你经

    2022年3月8日
    33
  • 频分复用带宽计算_信道复用的概念

    频分复用带宽计算_信道复用的概念网工05上半年(25)题:10个9.6KB/s的信道按时分多路复用在一条线路上传输,如果忽略控制开销,在同步TDM情况下,复用线路的带宽应该是(24);在统计TDM情况下,假定每个子信道只有30%的时间忙,复用线路的控制开销为10%,那么复用线路的带宽应该是(25)。供选择的答案(24)A.32Kb/s B.64Kb/s C.72Kb/s D.96Kb/s(2…

    2022年10月11日
    1

发表回复

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

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