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


相关推荐

  • PrintWriter and BufferedWriter区别和使用「建议收藏」

    PrintWriter and BufferedWriter区别和使用「建议收藏」区别:BufferedWriter:将文本写入字符输出流,缓冲各个字符从而提供单个字符,数组和字符串的高效写入。通过write()方法可以将获取到的字符输出,然后通过newLine()进行换行操作。并且BufferedWriter只能对字符流进行操作。PrintWriter:相对于BufferedWriter的好处在于,如果PrintWriter开启了自动刷新,那么当PrintWri…

    2022年6月5日
    36
  • mysql函数

    mysql函数

    2021年10月15日
    43
  • sm总线控制器找不到驱动程序_【KHGEARS钧兴谐波 | 新品】埃斯顿发布总线伺服驱动系统 ProNet Summa…「建议收藏」

    sm总线控制器找不到驱动程序_【KHGEARS钧兴谐波 | 新品】埃斯顿发布总线伺服驱动系统 ProNet Summa…「建议收藏」高工机器人CEO圈群招募中,欢迎感兴趣的朋友们加微信号:13632944360入群;添加微信时请备注单位-姓名-职务,通过审核后我们将邀请进群。2019年3月6日,以“创新设计极致表达”为主题的埃斯顿第三代运动控制解决方案产品发布会在江苏南京埃斯顿自动化总部隆重举行,埃斯顿重磅发布了新一代伺服驱动系统ProNetSumma。ProNetSumma系列驱动器可支持EtherCAT总…

    2022年5月3日
    44
  • tcpdump抓包命令_tcpdump指定ip抓包命令

    tcpdump抓包命令_tcpdump指定ip抓包命令tcpdump是一个功能强大的命令行数据包分析器,它是通过监听服务器的网卡来获取数据包,所有通过网络访问的数据包都能获取到。它也提供了过滤器的功能,可以获取指定的网络、端口或协议的数据包程序员日常排查问题,最常用的是使用过滤器功能获取指定端口的数据包,用来分析服务器是否收到请求、请求数据是否完整。参数介绍tcpdump命令的参数很多,详见如下这里只介绍一些常用的参数​-ccount//count表示数量。抓取数据包的数量达到count后结束命令,如果不使用…

    2022年8月21日
    58
  • sudoers修改_sudoers配置使用

    sudoers修改_sudoers配置使用sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的root命令,如halt,reboot,su等等。这样不仅减少了root用户的登陆和管理时间,同样也提高了安全性。Sudo不是对shell的一个代替,它是面向每个命令的。它的特性主要有这样几点:§sudo能够限制用户只在某台主机上运行某些命令。§sudo提供了丰富的日志,详细地记录了每个…

    2022年6月20日
    28
  • iscroll中文文档_如何正确使用

    iscroll中文文档_如何正确使用CDN:iScroll作用于滚动区域的外层,只有容器元素的第一个子元素能进行滚动,其它子元素完全被忽略;初始化方式:

    2022年8月6日
    6

发表回复

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

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