cswork / OA打开练习工作台

UberOA #4

Currency Trading Strategy

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)前缀和区间双指针滑窗

Given rates[i] (the value on day i) and strategy[i] ∈ {-1, 0, 1} (sell / hold / buy), the base profit is sum(strategy[i] * rates[i]). You may modify the strategy in exactly one contiguous window of length k (where k is even): within that window the first half is held (0) and the second half is all sold (-1, contributing +rates[i]). Find the maximum total profit achievable.

Constraints

  • 1 ≤ rates.length = strategy.length = n ≤ 10⁵
  • 1 ≤ rates[i] ≤ 10⁹
  • strategy[i] ∈ {-1, 0, 1}
  • k is a positive even integer with k ≤ n
rates = [2, 4, 1, 5, 10, 6]
strategy = [-1, 1, 0, 1, -1, 0]
k = 4
solution(rates, strategy, k) = 18