UVA - 10131 Is Bigger Smarter?(dp+最大升序子序列)

本文深入探讨了如何通过排序算法找到大象群体中,按照智商从大到小、重量从小到大排列的最长子序列,并详细展示了实现这一目标的具体步骤及代码解析。
题目分析:
找出重量从小到大、智商从大到小排列的最长子序列。
并输出这些序列的编号。


解题思路:
1、对大象按IQ从大到小排序。
2、用最大升序子序列的方法,按照重量从小到大,求出最多的大象,用路径path[i]记下j(i之前那头大象)。

3、输出最长序列的长度,并递归输出路径。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
struct Node {
	int w,s,id;
}e[N];
int n;
int dp[N],ans[N],path[N];

bool cmp(Node a,Node b) {
	return a.s > b.s;
}
void print_path(int cur) {
	if(dp[cur] == 1) {
		printf("%d\n",e[cur].id);
		return;
	}
	print_path(path[cur]);
	printf("%d\n",e[cur].id);
}
int main() {
	n = 0;
	while(scanf("%d%d",&e[n].w,&e[n].s) != EOF) {
		e[n].id = n+1;
		n++;
	}
	sort(e,e+n,cmp);
	for(int i = 0; i < n; i++) {
		dp[i] = 1;
		path[i] = i;
	}

	int maxx = 0 , pos = 0;
	for(int i = 1; i < n; i++) {
		int maxd = 0;
		for(int j = 0; j < i; j++) {
			if(e[i].w > e[j].w && dp[j] > maxd) {
				maxd = dp[j];
				path[i] = j; //i之前的那个大象
			}
		}
		dp[i] = maxd + 1;
		if(maxx < dp[i]) {
			maxx = dp[i];
			pos = i;
		}
	}
	printf("%d\n",maxx);
	print_path(pos);
	return 0;
}


>> L(1) = Link('d', 0, 'a', 0.5, 'alpha', 0, 'standard'); >> L(2) = Link('d', 0, 'a', 0.8, 'alpha', pi/2, 'standard'); >> L(3) = Link('d', 0.2, 'a', 0, 'alpha', -pi/2,'standard'); >> robot = SerialLink(L, 'name', '3DOF-Arm'); >> robot.teach; 索引超过数组元素的数量。索引不能超过 4。 出错 SerialLink/plot>create_robot (第 468 行) d = norm( d(4:6)-d(1:3) ) / 72; ^^^^^^ 出错 SerialLink/plot (第 251 行) handle = create_robot(robot, opt); ^^^^^^^^^^^^^^^^^^^^^^^^ 出错 SerialLink/teach (第 103 行) robot.plot(q, args{:}); ^^^^^^^^^^^^^^^^^^^^^^ >> disp(['机械臂自由度: ', num2str(robot.n)]); 机械臂自由度: 3 >> robot.display robot = 3DOF-Arm:: 3 axis, RRR, stdDH, slowRNE +---+-----------+-----------+-----------+-----------+-----------+ | j | theta | d | a | alpha | offset | +---+-----------+-----------+-----------+-----------+-----------+ | 1| q1| 0| 0.5| 0| 0| | 2| q2| 0| 0.8| 1.5708| 0| | 3| q3| 0.2| 0| -1.5708| 0| +---+-----------+-----------+-----------+-----------+-----------+ >> q_test = [pi/6, -pi/4, pi/3]; >> figure; >> robot.plot(q_test, 'workspace', [-2 2 -2 2 -1 3], 'view', '3d'); 警告: floor tiles too small, making them 2.000000 x bigger - change the size or disable them > 位置:RTBPlot.create_tiled_floor (第 619 行) 位置: RTBPlot.create_floor (第 575 行) 位置: SerialLink/plot (第 250 行) 索引超过数组元素的数量。索引不能超过 4。 出错 SerialLink/plot>create_robot (第 468 行) d = norm( d(4:6)-d(1:3) ) / 72; ^^^^^^ 出错 SerialLink/plot (第 251 行) handle = create_robot(robot, opt); ^^^^^^^^^^^^^^^^^^^^^^^^ >> title('标准DH三自由度机械臂'); >>
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值