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


相关推荐

  • python 用pip安装cv2(超简单的一句话)

    python 用pip安装cv2(超简单的一句话)如果你已经装好了pip,那就直接pipinstallopencv-python就可以了打个小广告~~

    2022年4月20日
    237
  • 2021 Java面试题大全(整理版)1000+面试题附答案详解,最全面详细,看完稳了!

    2021 Java面试题大全(整理版)1000+面试题附答案详解,最全面详细,看完稳了!进大厂是大部分程序员的梦想,而进大厂的门槛也是比较高的,所以这里整理了一份阿里、美团、滴滴、头条等大厂面试大全,其中概括的知识点有:Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、Redis、MySQL、Spring、SpringBoot、SpringCloud、RabbitMQ、Kafka、Linux等技术栈共有1000+道面试题。对于Java后端的朋友来说应该是最全面最完整的面试备战仓库,为了更好地整理每个模块,我也参考了很多网上

    2022年6月21日
    78
  • CS和BS的区别[通俗易懂]

    CS和BS的区别[通俗易懂]1.CS和BS的概念CS,即C/S(Client/Server)结构,是一种客户机和服务器结构。cs也是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销。BS即Browser/Server(浏览器/服务器)结构,就是只安装维护一个服务器,而客户端采用浏览器运行软件。2.CS和BS区别1.开发维护成本cs开发维护成本高于bs。因为因为采用cs结构时,对于不同的客户端要开发不同的程序,而且软件安装调试和升级都需要在所有

    2022年9月10日
    0
  • 关于HeartBleed漏洞的总结「建议收藏」

    关于HeartBleed漏洞的总结「建议收藏」一:前言HeartBleed漏洞又称为心脏出血漏洞,编号(CVE-2014-0160),产生原因:由于未能在memcpy()调用受害用户输入的内容作为长度参数之前正确进行边界检查。攻击者可以追踪OpenSSl所分配的64KB内存,将超出必要范围的字节信息复制到缓存当中,再返回缓存内容,这样一来,受害者的内存内容就会每次泄露64KB.简单来说,这就是OpenSSL缺陷造成的漏洞二:环境…

    2022年7月25日
    12
  • 信道和带宽_信道带宽怎么计算

    信道和带宽_信道带宽怎么计算信道和带宽在用cmw500测试不同band下的throughput时,发现module在某几个band注册不上小区。后来经过同事顺滑的演示,得知是因为不同band支持不同的带宽,而我一直设置cmw500的Cellbandwidth=20MHZ,对于那些最大只支持10MHZ的band自然注册不上。关于不同Band支持的带宽可以参考下表(3GPPTS36.101V17.2.0(2021-06))Table5.6.1-1:E-UTRAchannelbandwidth结尾处分享一

    2022年10月7日
    0
  • Spring Boot 默认数据源 HikariDataSource 与 JdbcTemplate 初遇

    Spring Boot 默认数据源 HikariDataSource 与 JdbcTemplate 初遇目录环境准备新建项目pom.xml默认内容mysql数据库数据库CRUD全局配置文件默认数据源CRUD数据库PhoneController测试结果自动配置原理DataSourceConfiguration1、《SpringBoot数据库访问简介》中已经介绍,SpringBoot可以通过多种方式访问各种数据库,本文将介绍Spr…

    2022年6月23日
    129

发表回复

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

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