Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя Titan_2003

Автор Titan_2003, история, 4 часа назад, По-английски

Problem: Minimum Sum of Absolute Differences in Subsequences

Description:

Given an array of length N (where 1≤N≤1e5) and an integer k (where 1≤k≤50), find the minimum possible value of the sum of absolute differences of adjacent elements for all subsequences of length k.

Mathematically, for a subsequence {ai1,ai2,…,aik} where i1<i2<…<iki1​<i2​<…<ik​, we need to minimize the value of:

$$$\sum_{j=1}^k |a_{i} - a_{i-1}|$$$
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
4 часа назад, # |
Rev. 3   Проголосовать: нравится +6 Проголосовать: не нравится

This is a relatively standard dynamic programming problem. Let dp[i][j] represent the minimum total cost of a subsequence of length j ending on the ith element. Our transitions are simply

dp[i][j]= min k<i (dp[k][j-1]+ abs(a[k]-a[i]))

Naively this can be computed in O(N^2*K). To optimize this we can use a segment tree similar to its use in CSES pizzeria queries. This solution will run in O(NKlogN) time.