You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助:基于动态规划求解符合特定规则的最长整数子序列

动态规划求解带约束的最长子序列(针对给定整数序列)

Hey there! I get that figuring out DP for constrained subsequences can be tricky even after learning standard LIS. Let's work through this together with your given sequence.

First, I noticed your constraint was cut off: "若元素索引为奇数,需满足条件:X...". To demonstrate the approach, I'll assume a common variant (feel free to correct me if your actual constraint is different):

Constraint: If an element comes from an odd index (0-based) in the original sequence, it must be strictly greater than the previous element in the subsequence. Elements from even indices can be non-decreasing (≥ previous element).

Given Sequence (0-based indices)

Index0123456789
Value-24115-233-11

Step 1: Define the DP Array

We'll use a 1D array dp where dp[i] represents the length of the longest valid subsequence that ends with the element at original index i. Every element starts as a subsequence of length 1, so initialize dp[i] = 1 for all i.

Step 2: State Transition Logic

For each element at index i, we check all previous elements j (where j < i, since indices must be strictly increasing):

  • If i is odd (original index): We can append sequence[i] to the subsequence ending at j only if sequence[i] > sequence[j].
  • If i is even (original index): We can append it if sequence[i] >= sequence[j].
  • If valid, update dp[i] to be the maximum of its current value and dp[j] + 1.

Step 3: Calculate the DP Array

Let's compute each dp[i] step by step:

  • dp[0] = 1 (element: -2)
  • dp[1]: Check j=0 → 4 > -2 → dp[1] = 1+1=2
  • dp[2]: Check j=0 (1 ≥ -2 → 2) | j=1 (1 ≥4? No) → dp[2]=2
  • dp[3]: Check j=0 (1 > -2 →2) | others fail → dp[3]=2
  • dp[4]: Check j=1 (5≥4 →3) | others give ≤3 → dp[4]=3
  • dp[5]: All previous elements are ≥-2, and i=5 is odd (needs >) → no valid j → dp[5]=1
  • dp[6]: Check j=2 (3≥1 →3) | others give ≤3 → dp[6]=3
  • dp[7]: Check j=2 (3>1 →3) | others give ≤3 → dp[7]=3
  • dp[8]: Check j=0 or j=5 → gives 2 → dp[8]=2
  • dp[9]: Check j=8 (1 > -1 →3) → dp[9]=3

The maximum value in dp is 3, so that's the length of our longest valid subsequence.

Step4: Backtrack to Find the Subsequence

To get one of the longest subsequences, we backtrack from the index where dp[i] is maximum:

  • Example 1: Start at index4 (value5) → backtrack to index1 (4) → index0 (-2) → [-2,4,5]
  • Example2: Start at index6 (3) → backtrack to index2 (1) → index0 (-2) → [-2,1,3]
  • Example3: Start at index9 (1) → backtrack to index8 (-1) → index0 (-2) → [-2,-1,1]

Python Code Implementation

sequence = [-2, 4, 1, 1, 5, -2, 3, 3, -1, 1]
n = len(sequence)
dp = [1] * n

# Fill DP array
for i in range(n):
    for j in range(i):
        # Apply the constraint
        valid = False
        if i % 2 == 1:
            valid = sequence[i] > sequence[j]
        else:
            valid = sequence[i] >= sequence[j]
        if valid and dp[j] + 1 > dp[i]:
            dp[i] = dp[j] + 1

max_length = max(dp)
print(f"最长子序列长度: {max_length}")

# Backtrack to find one valid subsequence
max_idx = dp.index(max_length)
subsequence = [sequence[max_idx]]
current_len = max_length

for i in range(max_idx - 1, -1, -1):
    if dp[i] == current_len - 1:
        # Verify the constraint between i and current max_idx
        valid = False
        if max_idx % 2 == 1:
            valid = sequence[max_idx] > sequence[i]
        else:
            valid = sequence[max_idx] >= sequence[i]
        if valid:
            subsequence.append(sequence[i])
            current_len -= 1
            max_idx = i
            if current_len == 1:
                break

subsequence.reverse()
print(f"其中一个最长子序列: {subsequence}")

If your actual constraint is different (e.g., "odd positions in the subsequence must be strictly increasing"), just let me know and we can adjust the DP approach—for that case, we'd use a 2D DP array tracking whether the subsequence ends at an even or odd length position.

内容的提问来源于stack exchange,提问作者taudau1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:32:08