当前位置:网站首页>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;
}
边栏推荐
- 动态规划--怪盗基德的滑翔翼
- Delete the repeating elements in the sorting list (simple questions)
- Sword finger offer 29 Print matrix clockwise
- Force deduction exercise -- deleting repeated items in ordered sequence 1.0
- D. Big Brush
- [secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
- [road of system analyst] collection of wrong topics in Project Management Chapter
- English语法_形容词/副词3级-最高级
- 旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
- 86. separate linked list
猜你喜欢

Assembly learning tutorial: accessing memory (3)

How to automatically renew a token after it expires?
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

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

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

Digital signature——

Bev instance prediction based on monocular camera (iccv 2021)

9. naive Bayes

OSPF - authentication and load balancing summary (including configuration commands)
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter
随机推荐
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
The fourth day of learning C language for Asian people
El table lazy load refresh
Solidy - fallback function - 2 trigger execution modes
Sword finger offer 18 Delete the node of the linked list
Why can transformer break into the CV world and kill CNN?
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
OpenCL线程代数库ViennaCL的使用
What indicators should safety service engineers pay attention to in emergency response?
如何制作CSR(Certificate Signing Request)文件?
Sound net, debout dans le "sol" de l'IOT
Xi'an Jiaotong automation control theory test simulation question [standard answer]
Summary of common loss functions in pytorch
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
Transfer the token on the matic-erc20 network to the matic polygon
Shenzhou ares tx6 boot logo modification tutorial
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
Learning automation ppt
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand