当前位置:网站首页>Dynamic programming -- gliding wing of the strange thief Kidd
Dynamic programming -- gliding wing of the strange thief Kidd
2022-06-30 05:46:00 【Wuhu boy】
Title Description
Kidd is a legendary robber , A super thief who specializes in jewelry .
And what stands out most about him is , That is, he can escape the heavy siege of Zhongcun police department every time , And this is largely due to his easy to operate glider .
one day , Strange thief Kidd stole a precious diamond as usual , Unexpectedly, Conan children saw through the disguise , The power unit of his glider wing was also damaged by Conan's football .
I have to , The rogue Kidd can only operate the damaged glider wing to escape .
Suppose there are... In the city N The buildings line up , The height of each building is different .
At the beginning , Kidd can be on the top of any building .
He can choose one direction to escape , But you can't change direction halfway ( Because the Zhongsen police department will pursue behind ).
Because the glider power unit is damaged , He can only slide down ( namely : Can only glide from higher buildings to lower buildings ).
He wants to pass the tops of different buildings as much as possible , This will slow down the impact of descent , Reduce the possibility of injury .
Excuse me, , The maximum number of different buildings he can pass through ( Including the initial building )?
Input format
The first line of input data is an integer K, On behalf of K Group test data .
Each set of test data contains two lines : The first line is an integer N, On behalf of N Building . The second line contains N Different integers , Each corresponds to the height of a building h, According to the order of buildings .
Output format
For each set of test data , Output one line , Contains an integer , Represents the maximum number of buildings Kidd can pass through .
Data range
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
Examples
sample input
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
sample output
6
6
9
Ideas – Dynamic programming
The question : The prototype of this problem is the longest ascending subsequence . It means : Find the longest subsequence with decreasing height , It can be understood as finding the longest descending subsequence in the array .
You can choose any position as the starting position , But you can only run in one direction , And you can't change direction halfway .
seek : The maximum number of buildings you can pass through .
analysis :

For the first ① In one case :
For the first ③ In this case, it is similar to the first one . It just needs to cycle from back to front , The first ② In this case, the ①、③ Introduction !
therefore : The length of the longest descent subsequence can be solved from front to back 、 The length of the longest descent subsequence is calculated from back to front . Take one of the two max, Is the maximum length of the starting point at this point .
C++ Code
#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;
}
边栏推荐
- 8 ways to earn passive income
- 10-【istio】istio Sidecar
- Video summary of my station B
- 抓取手机端变体组合思路设想
- Golden code of programmer interview
- Sound network, standing in the "soil" of the Internet of things
- How to create a CSR (certificate signing request) file?
- [Blue Bridge Road -- bug free code] DS1302 time module code analysis
- 云服务器部署 Web 项目
- [typescript] defines the return value type of promise
猜你喜欢

剑指 Offer 18. 删除链表的节点

Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"

Rotating box target detection mmrotate v0.3.1 getting started

Finally someone can make the server so straightforward

Here comes the nearest chance to Ali

How to create a CSR (certificate signing request) file?

Projet Web de déploiement du serveur Cloud

Xctf--Web--Challenge--area Wp

What indicators should safety service engineers pay attention to in emergency response?

About modifying dual system default startup item settings
随机推荐
1380. lucky numbers in matrices
How to use js to control the scroll bar of moving div
旋转框目标检测mmrotate v0.3.1 学习配置
Sword finger offer 22 The penultimate node in the linked list
MySQL advanced (Advanced SQL statement)
云服务器部署 Web 项目
Here comes the nearest chance to Ali
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
Unity shader flat shadow
Attempt to redefine 'timeout' at line 2 solution
Codeforces Round #390 (Div. 2) D. Fedor and coupons
Finally someone can make the server so straightforward
You don't know how to deduce the location where HashSet stores elements?
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
How to judge the quality of network transformer? What symptom is network filter transformer broken?
Summary of common loss functions in pytorch
剑指 Offer 18. 删除链表的节点
C. Divan and bitwise operations
After getting these performance test decomposition operations, your test path will be more smooth
强烈推荐十几款IDEA开发必备的插件