题目链接

在一条数轴上住着 $N$ 条狗和一个动物研究者 Bundle。Bundle 的坐标是 0,狗的坐标都是正整数,可能有多条狗住在同一个位置。每条狗都有一个颜色。Bundle 需要观测 $K$ 条狗。要观测一条狗 Bundle 必须走到狗的住处,并且穿着和狗同色的衣服。Bundle 只能在家换衣服。试问 Bundle 至少要走多长的路程?注意:最后 Bundle 不必回到住处。

Constraints

  • 不超过 100 组测试数据
  • $ 1 \le N \le 1000 $
  • $ 1 \le K \le N $
  • Time limit: 30 s

分析

若 Bundle 最后必须回到住处,反而更好处理,并且复杂度可以做到 $O(N \log C)$,$C$ 表示狗的颜色总数。
思路:从左往右,把相邻的同色狗之间的距离放到一个小顶堆中。

在最后不必回到住处的条件下,貌似就只能 DP 了。

Let dp[i][j][k] denotes the minimum amount of walk needed to observe j dogs such that Bundle has either observed or has decided she'll not observe dogs of colors 1 to i and k is a boolean denoting if we have chosen the last color in the first i colors.

这篇题解提供了另一种思路。

相关文章:

  • 2022-01-30
  • 2021-08-14
  • 2021-06-02
  • 2021-08-01
  • 2022-01-20
  • 2021-12-30
  • 2022-03-01
  • 2021-11-14
猜你喜欢
  • 2022-01-08
  • 2021-05-30
  • 2021-07-28
  • 2021-07-27
  • 2021-06-12
  • 2021-12-05
  • 2022-01-10
相关资源
相似解决方案