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


相关推荐

  • 执行SQL语句的优先级顺序

    执行SQL语句的优先级顺序FROM 执行顺序为从后往前 从右到左 数据量较大的表尽量放在后面 WHERE 执行顺序为自下而上 从右到左 将能过滤掉最大数量记录的条件写在 WHERE 字句的最右 GROUPBY 执行顺序从右往左分组 最好在 GROUPBY 前使用 WHERE 将不需要的记录在 GROUPBY 之前过滤掉 HAVING 消耗资源 尽量避免使用 HAVING 会在检索出所有记录之后才对结果进行过滤 需要排序等操作 ORDERBY 执行顺序从左到右 消耗资源 SELECT 少用星号 尽量使用字段名称 oracle 在解析的过

    2026年3月17日
    2
  • html静态网页制作教程_简单html静态网页代码 首页

    html静态网页制作教程_简单html静态网页代码 首页编辑一个文本文件,代码如下;<!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>htmltest1</title></head><body><h1>DDDD</h1><p>PPPPPPPPPPP…</p><tableborder=”1″>

    2025年8月27日
    10
  • 单点登录sso的实现原理(单点登录原理)

    什么是单点登录一个账户在多个系统上实现单一用户的登录为什么用单点登录单点登录可以做到在不记录用户密码的情况下,实现不同系统之间的资源共享,自动登录不安全,单点登录,一处登录,处处都可用,不用做多余的登录操作引用一个很经典的案例比如现在有OA系统、门户系统、人力资源管理系统、档案管理系统、生产管理系统、xx系统等,这么多个系统在一个公司里面,如果一个用户需要使用这么多个系统,那每天都要登录…

    2022年4月14日
    46
  • 如何通俗的介绍什么是Ai agent?

    如何通俗的介绍什么是Ai agent?

    2026年3月14日
    3
  • python全局变量在整个程序内都有效_Python 全局变量使用

    python全局变量在整个程序内都有效_Python 全局变量使用在平时的开发中我们有时候会用到全局变量 但是很多开发语言不允许使用或者建议少使用全局变量 Python 也是如此 但是有时候为了编写程序的方便和灵活 必须使用全局变量 这篇文章记录是我在使用 Python 全局变量的一些体会 写的不是很好 欢迎大家指正 一 使用全局变量首先展示一段不能修改全局变量的代码 gl string helloPython 定义全局变量 gl stringprint i

    2025年8月6日
    3
  • 判断同构数 c语言程序(java人脸识别算法)

    给定的两个邻接矩阵,判断其三个必要非充分条件:①结点数目相同②变数相同③度数相同的结点数相同以①②③为前提进行矩阵变换,看给定的两个矩阵中,其中的一个矩阵是否能变换为另一个矩阵;实现代码和说明:#include<iostream>#include<stdlib.h>#defineMAX100usingnamespacestd;structAdjacencyMatrix{//邻接矩阵intpoints;/

    2022年4月12日
    129

发表回复

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

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