Stock Trader S Alternating Streak You are a day t...

Erstellt am: 2. August 2026

Beantwortet mit GPT-5.6 Thinking von Chat01

Frage

Stock Trader S Alternating Streak

You are a day trader watching N price ticks p[1], p[2], ..., p[N]. You want to find the length of the longest "wiggle" subsequence that satisfies specific conditions.

A subsequence is formed by choosing a subset of the price ticks while preserving their relative order. Let the chosen subsequence be s[1], s[2], ..., s[k]. The subsequence is considered valid if it satisfies the following rules:

  1. Noise Threshold: Each step in the subsequence must clear a noise threshold M. This means for any consecutive elements s[i] and s[i + 1] in the subsequence, the absolute difference must satisfy |s[i + 1] - s[i]| >= M

ohddang

  1. Alternating Directions: Consecutive steps should strictly alternate in direction (up then down, or down then up). An up-step occurs when s[i + 1] > s[i], and a down-step occurs when s[i + 1] < s[i]

  2. The Mulligan: You are allowed at most one "do-over" (mulligan) where a single step in the subsequence breaks the alternation rule (i.e., two consecutive up-steps or two consecutive down-steps). This step must still satisfy the noise
    threshold M. Using the mulligan is optional.

Find the maximum length of a valid subsequence.

mohdda

mohddan

Input Format

heikhh

The first line contains a integer, N, denoting the number of price ticks.

The second line contains a integer, M, denoting the noise threshold.

Each line i of the N subsequent lines (where 0 ≤ i < N) contains a integer, P[i].

mohdda

Constraints

moho

1 <= N <= 10^5

1 <= M <= 10^9

1 <= P[i] <= 10^9

Examples

EXAMPLE 1

Input:
4 10 10 20 10 20

Output:

4

Explanation:

The entire sequence [10, 20, 10, 20] is valid because every step has an absolute difference of exactly 10 and strictly alternates in direction (up, down, ups. This achieves the maximum length of 4 without needing to use the mulligan.

EXAMPLE 2

Input:

4

10

10

20

30

40

Output:

3

Explanation:

Choosing [10, 20, 30] yields steps of +10 and +10, which both meet the noise threshold of 10. Using the single allowed mulligan to tolerate these consecutive up-steps results in a maximum length of 3.

EXAMPLE 3

Input:

4

10

10

15

12

18

Output:

1

Explanation:

Since no two price ticks have an absolute difference of 10 or more, no valid transitions can be made. The longest valid subsequence is restricted to any single element, resulting in a length of 1.
Language Python 3

1 import sys

2

input sys.stdin.readline

3

4

def solve(N: int, M: int, P: list) -> int:

5

Write your solution here I

6789

return 0

10

if name

=="main":

11

try:

12

13

14

15

16

N= int(input())

Mint(input())

Plist(map(int, input().split()))

result solve (N, M, P)

print(result)

17

18

except (EOFError, ValueError):

pass

Sample Tests

Submission

Custom Input.

Case 1

INPUT

4

10

10

20

10

20

EXPECTED OUTPUT

4

e

< Previous

Give program in python 3

Python

python
from itertools import product, combinations from bisect import bisect_left,bisect_right def alg(P,M): vals=sorted(set(P)); k=len(vals) fu0=[0]*(k+1); fd0=[0]*(k+1); fd1=[0]*(k+1) ru0=[0]*(k+1); ru1=[0]*(k+1); rd0=[0]*(k+1) def q(t,i): r=0 while i: if t[i]>r:r=t[i] i-=i&-i return r def upd(t,i,v): while i<=k: if v>t[i]:t[i]=v i+=i&-i ans=1 for x in P: left=bisect_right(vals,x-M) up0=1+q(fd0,left) up1=max(up0,1+q(fd1,left),1+q(fu0,left)) rs=bisect_left(vals,x+M) rlen=k-rs

Gedanke

Validated six-state Fenwick dynamic programming with exhaustive random tests

Worked for 2m 8s

Antwort

Use dynamic programming with Fenwick trees for range-maximum queries.

For every subsequence, track:

  • Its last movement: up or down
  • Whether the mulligan is still unused (0) or may have been used (1)

A transition to the current price x:

  • Is an up-step when the previous price is at most x - M
  • Is a down-step when the previous price is at least x + M
  • May continue the same direction only by moving from a no-mulligan state to a mulligan state
python
import sys from bisect import bisect_left, bisect_right def solve(N: int, M: int, P: list[int]) -> int: if N == 0: return 0 values = sorted(set(P)) size = len(values) value_index = {value: i + 1 for i, value in enumerate(values)} # Fenwick trees for prefix maximum queries. # # Forward trees query previous values <= a limit. forward_up0 = [0] * (size + 1) forward_down0 = [0] * (size + 1) forward_down1 = [0] * (size + 1) # Reverse trees query previous values >= a limit. reverse_up0 = [0] * (size + 1) reverse_up1 = [0] * (size + 1) reverse_down0 = [0] * (size + 1) def query(tree: list[int], index: int) -> int: result = 0 while index > 0: if tree[index] > result: result = tree[index] index -= index & -index return result def update(tree: list[int], index: int, value: int) -> None: while index <= size: if value > tree[index]: tree[index] = value index += index & -index answer = 1 for price in P: # Previous price <= price - M creates an up-step. left_count = bisect_right(values, price - M) best_down0 = query(forward_down0, left_count) up0 = best_down0 + 1 # Either: # 1. Alternate from down1 to up1, or # 2. Use the mulligan by going from up0 to up1. up1 = max( up0, query(forward_down1, left_count) + 1, query(forward_up0, left_count) + 1 ) # Previous price >= price + M creates a down-step. right_start = bisect_left(values, price + M) # In reversed coordinate order, this suffix becomes a prefix. reverse_count = size - right_start best_up0 = query(reverse_up0, reverse_count) down0 = best_up0 + 1 # Either: # 1. Alternate from up1 to down1, or # 2. Use the mulligan by going from down0 to down1. down1 = max( down0, query(reverse_up1, reverse_count) + 1, query(reverse_down0, reverse_count) + 1 ) index = value_index[price] reverse_index = size - index + 1 # Update only after calculating every state, so the current # element cannot transition to itself. update(forward_up0, index, up0) update(forward_down0, index, down0) update(forward_down1, index, down1) update(reverse_up0, reverse_index, up0) update(reverse_up1, reverse_index, up1) update(reverse_down0, reverse_index, down0) answer = max(answer, up1, down1) return answer def main() -> None: data = list(map(int, sys.stdin.buffer.read().split())) if len(data) < 2: return N = data[0] M = data[1] P = data[2:2 + N] print(solve(N, M, P)) if __name__ == "__main__": main()

Complexity

  • Time: O(NlogN)O(N \log N)
  • Space: O(N)O(N)

Diesen Q&A teilen