当前位置:网站首页>动态规划--怪盗基德的滑翔翼
动态规划--怪盗基德的滑翔翼
2022-06-30 05:42:00 【芜湖男童】
题目描述
怪盗基德是一个充满传奇色彩的怪盗,专门以珠宝为目标的超级盗窃犯。
而他最为突出的地方,就是他每次都能逃脱中村警部的重重围堵,而这也很大程度上是多亏了他随身携带的便于操作的滑翔翼。
有一天,怪盗基德像往常一样偷走了一颗珍贵的钻石,不料却被柯南小朋友识破了伪装,而他的滑翔翼的动力装置也被柯南踢出的足球破坏了。
不得已,怪盗基德只能操作受损的滑翔翼逃脱。
假设城市中一共有N幢建筑排成一条线,每幢建筑的高度各不相同。
初始时,怪盗基德可以在任何一幢建筑的顶端。
他可以选择一个方向逃跑,但是不能中途改变方向(因为中森警部会在后面追击)。
因为滑翔翼动力装置受损,他只能往下滑行(即:只能从较高的建筑滑翔到较低的建筑)。
他希望尽可能多地经过不同建筑的顶部,这样可以减缓下降时的冲击力,减少受伤的可能性。
请问,他最多可以经过多少幢不同建筑的顶部(包含初始时的建筑)?
输入格式
输入数据第一行是一个整数K,代表有K组测试数据。
每组测试数据包含两行:第一行是一个整数N,代表有N幢建筑。第二行包含N个不同的整数,每一个对应一幢建筑的高度h,按照建筑的排列顺序给出。
输出格式
对于每一组测试数据,输出一行,包含一个整数,代表怪盗基德最多可以经过的建筑数量。
数据范围
1 ≤ K ≤ 100 1≤K≤100 1≤K≤100
1 ≤ N ≤ 100 1≤N≤100 1≤N≤100
0 < h < 10000 0<h<10000 0<h<10000
样例
输入样例
3
8
300 207 155 299 298 170 158 65
8
65 158 170 298 299 155 207 300
10
2 1 3 4 5 6 7 8 9 10
输出样例
6
6
9
思路–动态规划
题意:本题原型为最长上升子序列。题意为:求一个高度递减的最长子序列,可以理解为在数组中求一个最长下降子序列。
可以选择任意的位置作为起始位置,但是只能往一个方向逃跑,而且不能中途改变方向。
求:最多可以经过多少栋楼的顶部。
分析:

对于第①种情况来说:
对于第③种情况来说和第一种类似。就是需要从后往前循环,第②种情况也可以从①、③推出!
所以:可以从前往后求解一次最长下降子序列的长度、在从后往前求解一次最长下降子序列的长度。两者取一个max,就是最终以该点作为起点的最大长度。
C++代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110;
int a[N], f[N];
int n;
int main()
{
int t;
cin >> t;
while (t --)
{
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
int res = 0;
for (int i = 1; i <= n; i ++)
{
f[i] = 1;
for (int j = 1; j < i; j ++)
if (a[j] < a[i]) f[i] = max(f[i], f[j] + 1);
}
for (int i = 1; i <= n; i ++) res = max(res, f[i]);
for (int i = n; i >= 1; i --)
{
f[i] = 1;
for (int j = n; j > i; j --)
if (a[j] < a[i]) f[i] = max(f[i], f[j] + 1);
}
for (int i = 1; i <= n; i ++) res = max(res, f[i]);
cout << res << endl;
}
return 0;
}
边栏推荐
- 旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
- [Motrix] download Baidu cloud files using Motrix
- How to judge the quality of network transformer? What symptom is network filter transformer broken?
- Database SQL language 05 SQL exercise
- Uboot reads the DDR memory size by sending 'R' characters through the terminal
- 《谁动了我的奶酪》读后感
- 旋转框目标检测mmrotate v0.3.1入门
- 3D rotation album
- Xi'an Jiaotong automation control theory test simulation question [standard answer]
- Promise知识点拾遗
猜你喜欢

The fourth day of learning C language for Asian people

如何制作CSR(Certificate Signing Request)文件?

How does WPS cancel automatic numbering? Four options

Who is promoting the new inflection point of audio and video industry in 2022?

Using lazy < t > in C # to realize singleton mode in WPF

VFPBS在IIS下调用EXCEL遇到的Access is denied

Sword finger offer 18 Delete the node of the linked list

图扑软件基于钻孔数据的三维地质模型可视化

Database SQL language 04 subquery and grouping function

Unityshader learning notes - Basic Attributes
随机推荐
Codeforces Round #390 (Div. 2) D. Fedor and coupons
PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
UML tools
Installation and getting started with pytoch
Access is denied encountered when vfpbs calls excel under IIS
Xctf--Web--Challenge--area Wp
Solidity - 安全 - 重入攻击(Reentrancy)
Database SQL language 04 subquery and grouping function
旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
English语法_形容词/副词3级-最高级
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Virtual and pure virtual destructions
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
Expansion method of unity scanning circle
We strongly recommend more than a dozen necessary plug-ins for idea development
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
1380. lucky numbers in matrices
Assembly learning tutorial: accessing memory (3)
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
Do you know how to show the health code in only 2 steps