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


相关推荐

  • mysql主从复制

    mysql主从复制

    2021年5月29日
    90
  • python 字典最外层使用_python字典底层实现

    python 字典最外层使用_python字典底层实现前言问题1:python中的字典到底是有序还是无序问题2:python中字典的效率如何python字典底层原理在Python3.5以前,字典是不能保证顺序的,键值对A先插入字典,键值对B后插

    2022年7月29日
    5
  • java的常量

    java的常量JAVA变量与常量常量常量值常量常量的声明定义常量常量在c语言编程网中的定义是指在程序的整个运行过程中值保持不变的量。在这里要注意常量和常量值是不同的概念,常量值是常量的具体和直观的表现形式,常量是形式化的表现。这里体现出常量值这一定义,我认为的常量值就是值,具体的某一数值百度出来的常量是定义为两种意思:一是不可变的变量,也是最最最开始接触java知道的,二如上。平时所说的值指的是常量值,常量是不可变的变量(用final修饰的变量)常量值提到常量值不得不说一下计数法,八进制、十进制、十六进制所代

    2022年7月8日
    22
  • 如何删除对象的某个属性(对象属性方法是什么)

    基于React钩子的轻量级状态管理解决方案Ice-store的简单使用优点极简API:只有5个API,简单上手,使用方便,不需要学习Redux里的各种概念。ReactHooks:拥抱Hooks的使用体验,同时也是基于ReactHooks实现。集成异步状态:记录异步action的执行状态,简化view组件中对于loading与error状态的渲染…

    2022年4月13日
    822
  • SQL Server 2008安装图解教程

    SQL Server 2008安装图解教程一、安装SQLServer2008安装SQL2008的过程与SQL2005的程序基本一样,只不过在安装的过程中部分选项有所改变,当然如果只熟悉SQL2000安装的同志来说则是一个革命性的变动。(一)安装前的准备(1)需要.NetFramework3.5,若在Vista或更高的OS上需要3.5SP1的支持(在SQL2008安装的前会自动更新安装)(2)需要Widnows

    2022年6月23日
    25
  • mybatisplus basemapper原理(提供两瓶水)

    MybatisPlus为什么提供BaseMapper和IService两个相似CRUD操作的接口?转载自:https://blog.csdn.net/krismile__qh/article/details/99590872熟悉mybatis-plus的人都知道,mybatis-plus提供两种包含预定义增删改查操作的接口:com.baomidou.mybatisplus.core…

    2022年4月12日
    313

发表回复

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

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