DP21 LIS的变种问题-建桥问题 Variations of LIS-Building Bridge @geeksforgeeks

亿图机床采集数据 这个数控系统的英文名是HUST,在台湾知名度还可以,但大陆这边我做这么多年项目也只接触过屈指可数的几次,可见市场占有率并不大,联网采集不复杂。 阅读详情

We have discussed Dynamic Programming solution for Longest Increasing Subsequence problem inthis post and a O(nLogn) solution in this post. Following are commonly asked variations of the standard LIS problem.

1. Building Bridges: Consider a 2-D map with a horizontal river passing through its center. There are n cities on the southern bank with x-coordinates a(1) … a(n) and n cities on the northern bank with x-coordinates b(1) … b(n). You want to connect as many north-south pairs of cities as possible with bridges such that no two bridges cross. When connecting cities, you can only connect city i on the northern bank to city i on the southern bank.

8     1     4     3     5     2     6     7  
<---- Cities on the other bank of river---->
--------------------------------------------
  <--------------- River--------------->
--------------------------------------------
1     2     3     4     5     6     7     8
<------- Cities on one bank of river------->

Source: Dynamic Programming Practice Problems. The link also has well explained solution for the problem.



2. Maximum Sum Increasing Subsequence: Given an array of n positive integers. Write a program to find the maximum sum subsequence of the given array such that the intgers in the subsequence are sorted in increasing order. For example, if input is {1, 101, 2, 3, 100, 4, 5}, then output should be {1, 2, 3, 100}. The solution to this problem has been published here.



3. The Longest Chain You are given pairs of numbers. In a pair, the first number is smaller with respect to the second number. Suppose you have two sets (a, b) and (c, d), the second set can follow the first set if b < c. So you can form a long chain in the similar fashion. Find the longest chain which can be formed. The solution to this problem has been published here.



4. Box Stacking You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that any side functions as its base. It is also allowable to use multiple instances of the same type of box.
Source: Dynamic Programming Practice Problems. The link also has well explained solution for the problem.


对于二维的问题,先把一维固定住(排序),在用动态规划处理剩下一维。


package DP;

import java.util.Arrays;
import java.util.Comparator;

public class BuildingBridges {

	public static void main(String[] args) {
		Pair[] A = new Pair[7];
		A[0] = new Pair(22,4);
		A[1] = new Pair(2,6);
		A[2] = new Pair(10,3);
		A[3] = new Pair(15,12);
		A[4] = new Pair(9,8);
		A[5] = new Pair(17,17);
		A[6] = new Pair(4,2);
		
		System.out.println(lis(A));
	}
	
	public static int lis(Pair[] A){
		Arrays.sort(A, new Comparator<Pair>() {
			@Override
			public int compare(Pair o1, Pair o2) {
				return o1.x - o2.x;
			}
		});
		
		int n = A.length;
		int max = 0;
		int[] dp = new int[n];
		Arrays.fill(dp, 1);
		
		for(int i=1; i<n; i++){
			for(int j=0; j<i; j++){
				if(A[i].y > A[j].y){
					dp[i] = Math.max(dp[i], dp[j]+1);
				}
			}
			max = Math.max(max, dp[i]);
		}
		
		return max;
	}
	
	public static class Pair{
		int x, y;
		public Pair(int x_, int y_){
			x = x_;
			y = y_;
		}
	}

}


http://www.geeksforgeeks.org/dynamic-programming-set-14-variations-of-lis/

http://people.csail.mit.edu/bdean/6.046/dp/

Android 13 Settings蓝牙列表卡顿问题排查及优化过程 此时我们的卡顿优化基本完成了,我们应该要考虑内存是否会泄漏,如果大家不知道哪些需要回收可以用leakcanary和android profiler进行内存检测,看代码有没有内存泄漏。 阅读详情

相关推荐

Python爬虫实战(基础篇)—23—爬取微信小程序<随机目的地旅行>生成随机地址(附GUI版)

爬取微信小程序<随机目的地旅行>生成随机地址

weixin_42636075的博客 1228

动态规划 (Dynamic Programming) 之 最长递增子序列(Longest Increase Subsequence)

既然已经说到了最长公共子序列,就把这个递增子序列也说了。同样的,这里subsequence表明了这样的子序列不要求是连续的。比如说有子序列{1, 9, 3, 8, 11, 4, 5, 6, 4, 19, 7, 1, 7 }这样一个字符串的的最长递增子序列就是{1,3,4,5,6,7}或者{1,3,4,5,6,19}。其实这个问题和前面的最长公共子序列问题还是有一定的关联的。假设我们的初始的序列

斯巴达的勇士已经是雅典的臣民了。。。 1万+

视频教程-SAP S4 HANA FICO COPA 获利能力分析-ERP

SAP S4 HANA FICO COPA 获利能力分析 从业超过8年的SA...

weixin_33456805的博客 1442

十大经典动态规划问题(Dynamic Programming Practice Problems)

我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步! 知乎专栏:https://zhuanlan.zhihu.com/Microstrong Github:https://gith...

Microstrong 2274

Nginx反向代理HTTPS服务报错排查:The plain HTTP request was sent to HTTPS port

在Web服务架构中,反向代理是连接客户端与后端应用的关键组件,它通过转发请求实现负载均衡和安全隔离。其工作原理涉及监听端口、解析协议头部,并根据配置将请求路由至上游服务器。这一机制的技术价值在于提升系统可扩展性、简化部署,并增强安全性。在HTTPS场景下,代理服务器需正确处理SSL/TLS终止与传递,若配置不当,常引发协议不匹配问题。例如,当Nginx将解密后的HTTP请求转发至期待HTTPS的后端,或客户端直接向SSL端口发送明文请求时,便会出现经典报错“The plain HTTP request wa

weixin_33391446的博客 392

告别密码烦恼:用Windows Server NPS给公司Wi-Fi和有线网加一道MAC地址白名单(附H3C交换机配置)

本文详细介绍了如何利用Windows Server NPS(网络策略服务器)与H3C交换机实现MAC地址白名单认证,为企业Wi-Fi和有线网络提供零密码安全方案。通过配置NPS服务器、网络策略及H3C交换机联调,实现无感知认证体验,降低运维负担,同时满足审计追溯需求。适用于中小企业网络管理,提升安全性与便捷性。

weixin_42535703的博客 365

Geeks 面试题: Box Stacking Problem

Dynamic Programming | Set 22 (Box Stacking Problem) You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numb...

编程语言小筑 340

机器学习 线性回归 头歌实训

第1关:简单线性回归与多元线性回归 第2关:线性回归的正规方程解 第3关:衡量线性回归的性能指标 第4关:scikit-learn线性回归实践 - 波斯顿房价预测

weixin_57270536的博客 1万+

DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks

You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as p...

系统 运维 373

LoveSQL

LoveSQL(SQL注入详解) 1、根据题目,显然想到的是sql注入漏洞,直接尝试万能密码 2、此时url构造,查看字段数 /check.php?username=admin' order by 3%23&password=1 # 存在 /check.php?username=admin' order by 4%23&password=1 # 报错 因此,得到总共有三个字段数 注意:url中不能用#,应该用url编码%23 3、用union来查询测试注入点,查看回显点

lunan的博客 1562

uniapp(全端兼容) - 超详细接入百度统计功能,给uniapp全平台各端项目引入添加百度统计的插件统计流量详细的步骤教程(提供最新的百度统计源代码,详细的示例代码复制粘贴即可快速搞定!)

uniapp h5 app 使用加入接入百度统计,uniapp百度统计,uniapp详细引入百度统计的教程,uniapp接入百度统计,uniapp如何实现加入百度的统计代码,uniapp怎么植入百度统计详细教程,uniapp如何对接百度统计,uniapp百度统计不生效怎么办,uniapp使用百度统计不生效常见问题,uniapp引入百度统计后没有生效,uniapp h5网页接入百度统计代码后没有统计无效,uniapp最新接入使用百度统计功能,uniapp增加百度统计代码,uniapp app如何接入百度统计,

✅ 精选推荐文章 1万+

动态规划十大经典案例(Dynamic Programming Practice Problems)

动态规划(Dynamic Programming)是求多阶段决策过程(Multistep Decision Process)最优化的一种数学方法,它将问题的整体按时间或空间的特征分成若干个前后衔接的时空阶段,把多阶段决策问题表示为前后有关的一系列单阶段决策问题,然后逐个求解,从而求出整个问题的最有决策序列。它强调了时间和空间的连续性。 如果没有基础知识,建议参看关文忠教授(退休)的运筹学课程中了...

mr2zhang的博客 7339

Openclaw错误分析与解决方案之:Agent couldn’t generate a response.

Openclaw错误分析与解决方案之:Agent couldn't generate a response. Note: some tool actions may have already been executed — please verify before retrying.

多媒体编程、网络编程、系统编程、网络安全编程、驱动编程 1315

hdu 4584 Building bridges(暴力搜索)

题目链接:hdu 4584 Building bridges 题目大意:给出一张图,图由‘H',’O'和‘C'组成,找到哈夫曼距离最小的’H'和‘C',即所有的’H'和‘C'中哈夫曼距离最小的那对,如果相同按照’H'的行坐标、列坐标、‘C’的行坐标、列坐标输出。 解题思路:暴力搜索,现将所有的H和C分别处理出来,然后暴力枚举。 #include #include

不慌不忙、不急不躁 1134

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

ssm框架搭建

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

Problem 1036 - 神奇的盒子

/* Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 249 Accepted: 107 Special Judge: No Description WM平时喜欢在夜晚游历于花丛、土堆等新校区隐蔽的角落,因为他觉得那样可以见到某些平时见不到的东西,找到某些比较稀奇的物品。随着其长期的寻觅,他终于找...

weixin_34248258的博客 146

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

ceoi2017 Building Bridges(build)

ceoi2017 Building Bridges(build) Building Bridges(build) 题目描述   A wide river has nn pillars of possibly different heights standing out of the water. They ...

liankewei的博客 320

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

hdu 4584 building-bridge 暴力的题,怎么暴力怎么干。。2013 ACM-ICPC杭州赛区全国邀请赛——题目重现

题目是有一个由H C O组成的n*m的矩形图,要你求图中距离最近的H和C的坐标,其中对于(x1,y1),(x2,y2)距离是  |x1-x2|+|y1-y2| 如果有多解就选H的x最小的若还是有多解的话,就选x最小的同是y也最小,对于C也是这样处理多解的情况 很暴力的方法,就行因为是水题就是来让你爆的 /********* PRO: hdu 4584 TIT: Building bridge

柯安的专栏 1617

[动态规划]UVA103 - Stacking Boxes

题意: 在数学或电脑科学里,有些概念在一维或二维时还蛮简单的,但到N维就会显得非常复杂。试想一个 n维的「盒子」:在二维空间里,盒子( 2 , 3 )可代表一个长为2个单位,宽为3个单位的盒子;在三维空间里,盒子( 4 , 8 , 9 )则是一个4*8*9(长、宽、高)的盒子。至于在六维空间里,也许我们不清楚( 4 , 5 , 6 , 7 , 8 , 9 )长得怎样,不过我们还是可以分析这些盒子的特性。 在此问题里,我们要算出一组n维盒子里,它们的「最长套入串列」: b 1 , b 2 , ......,b

九问的烦恼 1812

Box Stacking

Box stacking:  You are given a set of boxes {b1, ..., bn}. Each box bj has an associated width wj ,height hj and depth dj . Give an algorithm for creating the highest possible stack of boxes with the

bachelorchen的专栏 660

Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one ortwo dimensions butbecome more complex when extended to arbitrary dimensions. Considersolving differential equations in

追智的专栏 662

Codefroces 250D. Building Bridge (数学思维)

点击打开链接 题意就是,一个坐标轴上,一个西边村庄在(0,0),然后有一条河在x=a和x=b夹着的地方(a 解法:真的是想了两三个小时没有想出来怎么做好。。。然后找了个题解。大致就是,两点之间直线最短。选取b河岸上一点,他所能到达a的方式最优的只有一条路,选取a河岸上的点肯定是最靠近它和东边村庄的连线的那个点,两点之间直线最短嘛。所以我们将a河岸上的点排一下序。枚举b河岸上的点,然后不断

llzhh的博客 360

判断是否字符串重组 Scramble String @LeetCode

思路: 1 递归: 简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。 那么要么s11和s21是scramble的并且s12和s22是scramble的; 要么s11和s22是scramble的并且s12和s21是scramble的。 如果要能通过OJ,还必须把字符串排序后进行剪枝 http://

1万+

深入理解递归

大家都知道,递归的本质和栈数据的存取很相似了,都是先进去,但是往往最后处理!再者对于递归函数的局部变量的存储是按照栈的方式去存的,对于每一层的递归函数在栈中都保存了本层函数的局部变量,一边该层递归函数结束时能够保存原来该层的数据!如图:               如上图递归式依次往下进行的,并且在该层递归函数还没结束即将进入下一层递归调用时,将会把该层函数中的局部变量保存起来,以供下次使用!

7758

Edit Distance 编辑距离算法 @LeetCode

又是一道典型DP! 我们将人生划为诡异的阶段 我们把这个世界表为丰富的状态 package DP; import java.util.Arrays; /** * Edit Distance * * Given two words word1 and word2, find the minimum number of steps required to conve

5661

DP30 求骰子能凑成给定和的组合数 Dice Throw @geeksforgeeks

Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown. The Naive approach is to find all

4570
上一篇: Debian普通用户添加sudo权限
下一篇: DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值