# APCS實作題2024年6月第1題:特技表演
> 日期:2024年7月2日
> 作者:王一哲
> 題目來源:2024年6月實作題第1題
> [ZeroJudge 題目連結](https://zerojudge.tw/ShowProblem?problemid=o076)
## 題目
### 問題描述
有一個城鎮有 $n$ 棟高樓,樓高分別為 $h_1, h_2, \dots, h_n$,市長想要在城鎮中心舉辦高空特技表演,該特技表演會從某棟大樓上朝右側滑翔至地面。為了表演人員的安全,滑翔的路徑樓高必須越來越低,請你找出一個最長的滑翔路徑。
### 輸入說明
第一行有一個正整數 $n ~(5 \leq n \leq100)$。
第二行有 $n$ 個正整數 $h_1, h_2, \dots, h_n ~(1 \leq h_i \leq 1000)$ 代表樓高。
子題分數:
- 60%:$n=5$。
- 40%:無限制。
<br />
### 輸出格式
輸出最長的滑翔路徑長度。
<br />
### 範例輸入1
```
5
6 2 5 3 1
```
### 範例輸出1
```
3
```
### 範例輸入2
```
10
31 41 97 93 23 89 59 26 15 58
```
### 範例輸出2
```
4
```
<br />
## Python 程式碼
費時最久約為 18 ms,使用記憶體最多約為 3.3 MB,通過測試。
```python=
n = int(input()) # 讀取大樓數量 n
hs = list(map(int, input().split())) # 讀取 n 棟大樓高度存入串列 hs
ans = 0 # 答案,預設為 0
now = 0 # 目前的移動距離,預設為 0
hi = 1001 # 目前的樓高,預設為超出題目範圍的值
for h in hs: # 依序讀取大樓高度 h
if h < hi: # 如果 h 小於 hi
now += 1 # 飛行距離加 1
if now > ans: ans = now # 如果 now 大於 ans,更新 ans 為 now
# ans = max(ans, now) # 也可以用 max 更新 ans
else: # 如果 h 大於等於 hi
now = 1 # now 重設為 1
hi = h # 更新 hi
print(ans) # 印出答案
```
<br /><br />
## C++ 程式碼
費時最久約為 2 ms,使用記憶體最多約為 328 kB,通過測試。
```cpp=
#include <iostream>
using namespace std;
int main() {
int n; cin >> n; // 讀取大樓數量 n
int hs[n] = {0}; // 大樓高度 hs
for(int i=0; i<n; i++) { // 讀取 n 棟大樓高度
int h; cin >> h;
hs[i] = h;
}
int ans = 0, now = 0, hi = 1001; // 答案 ans,目前的移動距離 now,目前的樓高 hi
for(int i=0; i<n; i++) { // # 依序讀取大樓高度 h
if (hs[i] < hi) { // 如果 hs[i] 小於 hi
now++; // 飛行距離加 1
if (now > ans) ans = now; // 如果 now 大於 ans,更新 ans 為 now
} else { // 如果 hs[i] 大於等於 hi
now = 1; // now 重設為 1
}
hi = hs[i]; // 更新 hi
}
cout << ans << "\n"; // 印出答案
return 0;
}
```
<br /><br />
---
###### tags:`APCS`、`Python`、`C++`