0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニング問題集「【連続列】最長減少連続部分列」を解いてみた

Last updated at Posted at 2024-09-25

▼考え方:

考え方は、前問「最長増加連続部分列」とほぼ同じです。

異なる点は、以下の2点です。

①リストa[0]に201を代入したこと(201は身長の最大値より大きい値)。人が1(n=1)のときでも計算できるようにするためです。

②人a_i-1と人a_iに逆背の順が成立するならば、dp[i] = dp[i-1] + 1とした点。(dp[i-1]には、人a_i-1以前において、逆背の順が成立した人数が格納されている。)

▼コード:

########## 処理0(準備) インプット,リスト定義など ###########

n = int(input())

a = [0]*(n+1)
a[0] = 201

########## 処理1 漸化式の定義、計算、出力 ##########

dp = [0]*(n+1)

for i in range(1,n+1):
    a[i] = int(input())

for i in range(1,n+1):

    if a[i-1] >= a[i]:
        dp[i] += dp[i-1] + 1
    else:
        dp[i] = 1

print(max(dp))

▼気づいたこと:

解答コード例をみるとdp[0]=1でした。私はdp[0]=201としたため、考え方が異なるかもしれません。確認したいと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?