DP18 分割区间问题 Partition problem @geeksforgeeks

DCN交换机通过boot模式升级 LIC导入成功后,输入enable进入特权模式,把网线接到通讯网口上,配置好对应interface vlan地址,在enable模式用 copy tftp://x.x.x.x/xxxxxx_nos.img nos.img待提示write ok时,reload设备完成升级。交换机启动后,在显示以下信息时输入“Ctrl+B”,一定得是手动按“Ctrl+B”进入的boot模式才能正常升级。升级交换机成功,在BootROM模式下,执行命令run或reboot,回到CLI配置界面。# 交换机通过boot模式升级。 阅读详情

思路:

把能否划分区间的问题转化为能不能找到一个使得和为总和的一半的子区间,然后转为01背包问题,分析每一个元素是否取

Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is same.

Examples

arr[] = {1, 5, 11, 5}
Output: true 
The array can be partitioned as {1, 5, 5} and {11}

arr[] = {1, 5, 3}
Output: false 
The array cannot be partitioned into equal sum sets.

Following are the two main steps to solve this problem:
1) Calculate sum of the array. If sum is odd, there can not be two subsets with equal sum, so return false.
2) If sum of array elements is even, calculate sum/2 and find a subset of array with sum equal to sum/2.

The first step is simple. The second step is crucial, it can be solved either using recursion or Dynamic Programming.

Recursive Solution
Following is the recursive property of the second step mentioned above.

Let isSubsetSum(arr, n, sum/2) be the function that returns true if 
there is a subset of arr[0..n-1] with sum equal to sum/2

The isSubsetSum problem can be divided into two subproblems
 a) isSubsetSum() without considering last element 
    (reducing n to n-1)
 b) isSubsetSum considering the last element 
    (reducing sum/2 by arr[n-1] and n to n-1)
If any of the above the above subproblems return true, then return true. 
isSubsetSum (arr, n, sum/2) = isSubsetSum (arr, n-1, sum/2) ||
                              isSubsetSum (arr, n-1, sum/2 - arr[n-1])

package DP;

public class PartitionProbelm {

	public static void main(String[] args) {
		int[] A = {3, 1, 5, 9, 12};
		int n = A.length;
		System.out.println(findPartitionRec(A, n));
		System.out.println(findPartitionDP(A, n));
	}

	// A utility function that returns true if there is a subset of arr[]
	// with sun equal to given sum
	public static boolean isSubsetSumRec(int[] A, int n, int sum){
		if(sum < 0){
			return false;
		}
		if(sum == 0){
			return true;
		}
		if(n==0 && sum>0){
			return false;
		}
//		if(A[n-1] > sum){		// If last element is greater than sum, then ignore it
//			return isSubsetSumRec(A, n-1, sum);
//		}
		
		/* else, check if sum can be obtained by any of the following
	      (a) excluding the last element 不选A[n-1]
	      (b) including the last element	选A[n-1]
	   */
		return isSubsetSumRec(A, n-1, sum) || isSubsetSumRec(A, n-1, sum-A[n-1]);
	}

	// Returns true if A[] can be partitioned in two subsets of
	// equal sum, otherwise false
	//	Time Complexity: O(2^n) In worst case
	//	, this solution tries two possibilities (whether to include or exclude) for every element.
	public static boolean findPartitionRec(int[] A, int n){
		int sum = 0;		// Calculate sum of the elements in array
		for(int i=0; i<n; i++){
			sum += A[i];
		}
		if(sum%2 != 0){	// If sum is odd, there cannot be two subsets with equal sum
			return false;
		}
		
		// Find if there is subset with sum equal to half of total sum
		return isSubsetSumRec(A, n, sum/2);
	}
	
	// Returns true if A[] can be partitioned in two subsets of
	// equal sum, otherwise false
//	Time Complexity: O(sum*n)
//	Auxiliary Space: O(sum*n)
	public static boolean findPartitionDP(int[] A, int n){
		int sum = 0;
		for(int i=0; i<n; i++){	// Calculate sum of all elements
			sum += A[i];
		}
		if(sum%2 != 0){	//	If odd, then no chance
			return false;
		}
		
		// part[i][j]: 记录在长为j的数组里能否找到总和为i的子区间
		boolean[][] part = new boolean[sum/2+1][n+1];
		for(int i=0; i<=n; i++){		// initialize top row as true
			part[0][i] = true;		// 如果总和为0,则肯定能找到(即一个都不选)
		}
		for(int i=1; i<=sum/2; i++){	// initialize leftmost column, except part[0][0], as false
			part[i][0] = false;	// 如果总和为i,且数组长为0,则不可能找到
		}
		
		 // Fill the partition table in bottom up manner 
		for(int i=1; i<=sum/2; i++){	// 要找总和为i
			for(int j=1; j<=n; j++){	// 给定数组长度为j
				part[i][j] = part[i][j-1]; // 不选A[j-1]的情况,更新数组长度为j-1
				if(i-A[j-1] >= 0){		// 选择A[j-1]的情况,更新总和为i-A[j-1],也更新数组长度为j-1
					part[i][j] = part[i][j] || part[i-A[j-1]][j-1];
				}
			}
		}
		return part[sum/2][n];
	}
	
}


http://www.geeksforgeeks.org/dynamic-programming-set-18-partition-problem/

CloudSat卫星数据高效下载与自动化处理指南 本文详细介绍了CloudSat卫星数据的高效下载与自动化处理方法,包括交互式图形界面和wget批量下载技巧,以及使用Python脚本实现自动化处理和数据质量控制。内容涵盖从数据准备到实战应用的完整流程,特别适合气象研究和气候建模领域的工作者提升工作效率。 阅读详情

相关推荐

【AAAI 2025】轻量级特征增强新范式!FCM 模块助力小目标检测,即插即用提升模型泛化能力

本文介绍了AAAI 2025提出的轻量级特征增强新范式FCM模块,它通过显式分离并双向交互语义与空间特征,有效解决了小目标检测中特征信息失配的核心难题。该模块设计精巧,计算开销极低,能够即插即用式地嵌入YOLO等主流检测网络,显著提升模型对小目标的泛化检测能力,尤其适用于遥感、自动驾驶等对计算资源敏感的实际场景。

weixin_29056145的博客 287

动态规划之分区问题(Partition problem)

原文地址:Dynamic Programming | Set 18 (Partition problem)分区问题是将已知的集合分成两个子集,这两个子集的元素分别加和是相等的。例子:arr[] = {1, 5, 11, 5} Output: true 这个数组可以被分为:{1, 5, 5}和{11}arr[] = {1, 5, 3} Output: false 这个数组不能被分成两个和相同的子集

K.Sun 1万+

国产数据库迁移必看:达梦到ES的CDC方案优化全记录(含完整代码)

本文详细记录了从达梦数据库到Elasticsearch的CDC方案优化全过程,涵盖触发器方案局限性、Debezium架构优势及完整代码实现。通过Flink集成和性能调优,实现毫秒级延迟同步,显著降低数据库负载,为国产数据库迁移提供可落地的技术方案。

weixin_29079671的博客 499

GeeksForGeeks - Partition problem

Dynamic Programming | Set 18 (Partition problem) Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets ...

架构设计 247

OI 生涯回忆录 《Pilgrimage》

前言 于 NOI2020 后写下本文。 本文简单提及了在写作时尚能回忆起来的,在我的 OI 生涯中对我的影响重大的人、事、比赛等。 写作本文,旨在在回忆允许的范围内,对 OI 生涯进行一定程度上的梳理,总结。 开端 入门 我最早接触计算机程序设计,是在小学。 那时候,我的数学成绩不错,就被老师推荐去学习编程。 就这样,我在局前街小学的何静老师的指导下,走上了编程的道路。 小学时的编程,与其说是 “竞赛” ,不如说是 “兴趣班” 。 当时,包括我在内,很多小孩子天性是浮躁的。支撑我对着无聊的代码学下去的,并不

cz_xuyixuan的博客 1万+

UE5游戏逆向实战:用FModel提取.pak文件中的3D模型(附Dumper-7避坑指南)

本文详细介绍了如何使用FModel工具从UE5游戏的.pak文件中提取3D模型,并提供了Dumper-7的避坑指南。内容涵盖FModel工具链配置、UE5特有的unversioned properties问题解决方案、Dumper-7实战流程以及模型后处理技巧,帮助开发者高效获取游戏资产。

weixin_30872499的博客 422

Partition problem(枚举,搜索+剪枝,好题啊)

把2n个人分成n+n两组,那么一共是种情况,如果每一次都暴力计算竞争力总和,那么每一次的时间复杂度将是O(n^2),总时间复杂度是,对于n≤14这个数据范围来说是过不去的。 考虑如何惰性计算这个竞争力总和,也就是说从状态A转移到状态B的时候根据状态的变化来计算贡献,这是一个常用的思路。 一种很巧妙的思路:算贡献的时候不用加,用减。 假设当前没有分组的变量的竞争值就是,每次将一个元素分到某一组...

永夜莫明的博客 688

CocosCreator | 自定义启动页之H5

01 效果对比 默认启动页: 自定义启动页: 02 启动页存在的意义 我们在 creator 中的开发的游戏都是基于 cocos engine,所以在展示我们的游戏之前,是必须要先加载 cocos engine,然后才是我们自己的业务代码 流程图如下: 而构建完成后未精简的 cocos engine 代码为 1.9M 如果再加上物理相关代码的话,一共是2M 别忘了,我们自己的首场景也要加载过程哦 所以,从游戏启动,到真...

Valiancer 5897

Partition problem(dfs)

F-Partition problem_2019⽜客暑期多校训练营(第⼆场) https://ac.nowcoder.com/acm/contest/882/F 1/1  Partition problem 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目...

Mr_Kingk的博客 288

STK与Matlab连接报错?手把手教你解决mexConnect函数执行问题(附完整避坑指南)

本文针对STK与Matlab连接时常见的“mexConnect函数执行”报错,提供了完整的诊断与解决方案。文章深入剖析了错误根源在于路径冲突或文件损坏,并手把手指导用户通过定位STK连接器文件、正确配置Matlab路径(包括使用addpath命令)以及执行初始化测试三步走策略,彻底解决问题。同时附上版本兼容性检查、安装避坑指南及构建稳健工作流的建议,帮助用户建立可靠的STK-Matlab联合仿真环境。

yolo5detector的博客 472

小黄鸭机器人技术全解-第 07 章 关节自由度与行程参数

—鸭子歪着头转头时,转的平面是斜的,这正是它"灵动"感的来源。仿真里这是自动的(轴定义在父刚体系),复刻时不需要做任何事——

yolo模型训练找我,AI游戏合作找我 67

Partition problem

Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the total competitive value is maximized. 给定2N个人,你需要去把他们分配到红队或...

Untersee-boot XXI 239

备赛电赛学习硬件篇(二):电源板电路设计

1、输入接口要准备各种接口,如2p端子,锂电池接口,typec等,提供多种电源供电方案2、输出接口3、自由拓展。

m0_61022809的博客 3215

Partition Problem

Given a set of positive integers, find if the set can be partitioned into two subsets such that the sum of these two subsets are equal.  DFS approach. Sort the array in an increasing order. Create tw

再见小小ronnie的博客 1118

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Number Partition Problem(数字划分问题|划分问题)

这是神州南部的SZ大学某门水课的大作业。如果你也不幸进了这所大学,也跟我一样报了LH老师的课,我的这份作业也许能帮你节省不少时间。

Ubuntu装机必备 1181

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

牛客多校二(F)

题目:Partition problem 解法一: 我们把所有人两辆之间的竞争力sum求出来,那么我们要求的就是sum - 每个队之间的内部竞争力; 那么为了使结果最大,我们应该使两个队内部之间的竞争力之和最小; 初始时,所有人都没有分队,依次考虑每一个人,要么把他安排在一队,要么放在二队,之和计算内部之间的竞争力并且向下 搜 索 这...

PL博客 193

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

lucene splitRange 切割区间的原理

关于lucene的splitIntRange(final IntRangeBuilder builder,final int precisionStep,final int minBound, final int maxBound)和splitLongRange(finalLongRangeBuilder builder,final int precisionStep, final long

johnnie_deng的专栏 1159

微积分再观赏

大学上来直接描述这个微积分,让人不容易直观理解 微积分也是经历了漫长的时间,直观的从几何图形的切割得到的定义更好理解 理解1:曲线蜕化为直线 将曲线无限切割,在一个很小的区间内,区间长度近似为0 此时在该极小的区间内该曲线可以看成是直线 可以想象下你当前看到的地球地面是不是感觉是一个平面? 理解2:区间分割 将当前区间插入 n个点进行区间分割 , 得到一个区间集合 这个思想也是我们研究连续函数的一个重要手段。学会区间无限小切割。 例如下面的区间 [a,b] 在ab之间插入若.

lambda-fk 的专栏 723

chatgpt赋能python:Python初学者必须掌握的技巧:区间分割

在Python中,区间指的是某个范围内的数字或元素集合。区间分割则是将该区间平均分割为多个子区间的过程。例如,假设我们有一个范围在1到100之间的数字区间,我们可以将其分为10个子区间,每个区间包含10个数字。利用该技术,我们可以进行数据处理,制定算法,或其他各种操作。区间分割可以用于处理连续的数值数据,如时间序列、音频数据、视频数据等。区间分割在Python编程中是一个非常实用和广泛应用的技术。它可以用来处理连续的数值数据、节省内存、优化算法和存储。

test100t的博客 821

【搜索】Partition problem

题目链接:传送门 题面: 【题意】   给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]。 【题解】   爆搜,C(28,14)*28,其实可以稍加优化,因为分到两个队伍,所以第一个人肯定会分到一个队伍中,搜索可以有,C(27.13)*28,其实可以稍加剪枝,其实这个剪枝有点模糊,就是统计第i个人能产生的...

weixin_30609287的博客 169

牛客多校__Partition problem

链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 Given 2N people, you need to assign each of them into either red...

cgtllp1的博客 258

牛客网 Partition problem 【DFS + 预处理】

链接:https://ac.nowcoder.com/acm/contest/882/F Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the total competit...

独钓门前月 400

【C++】密度不均的区间划分问题

题目:密度不均的区间划分问题 现有区间[0,1] ,区间内存在(K-1) 个分界点s11,s21,…,sK-11 。这(K-1) 个分界点将整个区间分割成K 个等长的子区间,即对于任意的1≤k≤K ,满足sk1-sk-11=1K ,其中s01=0,sK1=1 。有一组样本不均匀地分布在区间[0,1] 内。给定每个子区间内样本密度,请重新确定(K-1) 个分界点,使得由这(K-1) 个分界点确定的K 个新的子区间内样本数量相同。例如,如图1所示,区间[0,1]内存在3个等距的分界点0.25、0.5和0.75

Mitchell_Donovan的博客 1179

【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the...

xuanweiace的博客 512

F-Partition problem

#include <bits/stdc++.h> using namespace std; #define maxn 40 typedef unsigned long long ll; ll n,res=0,v[maxn][maxn]; //sta是状压了一个2*n位的二进制位,代表当前A集合选取的状态,若当前位为1则代表选1 //cnt代表A选取了的状态 //pre代表当前已经选了前...

slass-mot-odet 470

HDU6300 Triangle Partition(2018HDU多校联赛第一场,贪心)

Problem Description Chiaki has 3n points p1,p2,…,p3n. It is guaranteed that no three points are collinear. Chiaki would like to construct n disjoint triangles where each vertex comes from th...

riba2534的博客 806
上一篇: DP17 最少回文切割次数 Palindrome Partitioning @geeksforgeeks
下一篇: DP19 自动换行问题 Word Wrap Problem @geeksforgeeks
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值