【问题标题】:Count the number of ranges [L; R] which has difference between the maximum and minimum is even计算范围数 [L; R] 最大值和最小值之差是偶数
【发布时间】:2021-11-27 14:25:54
【问题描述】:

给定一个数组 n 个元素 (n

例如,n = 5
a[] = {4, 5, 2, 6, 3}
答案是 11:[1;1], [1;4], [1;5], [2;2], [2;4], [2;5], [3;3], [3; 4]、[3;5]、[4;4]、[5;5] 时间限制为 1 秒

如果 n

有什么有效的方法吗?

【问题讨论】:

  • (我在编辑后恢复了答案。)
  • 显然 LR 这里指的是数组索引(从 1 偏移)所以 [2;4] 是子数组:{ 5, 2, 6}

标签: algorithm stack deque segment-tree rmq


【解决方案1】:

获得O(n log n) 的一种方法是分而治之。除以中间并将左右的结果相加。对于间隔与中间重叠的部分,我们可以使用 max 和 min 前缀,并在O(n) 中计算。记住要开始前缀,包括一起考虑的分隔线的 both 两侧。对于跨越示例的中间部分,我们在 2 处划分并有

           4, 5, 2, 6, 3
           <------||--->
min pfx    2  2  2||2  2
max pfx    6  6  6||6  6

此示例不适用于下一步,因为没有任何变化。 总之,该示例的分治法的中间部分将占[1;4], [1;5], [2;4], [2;5], [3;4], [3;5]

一个更有趣的中间:

           8, 1, 7, 2, 9, 0
           <------||------>
min pfx    1  1  2||2  2  0
max pfx    8  7  7||7  9  9
                 2--7
                 2-----9
              1-----7
              1--------9
           1--------8
           1-----------9
           0--------------9

我们看到,对于每个最小值,我们希望计数延伸到另一侧的较低最小值,与每个最大值配对,首先与它在同一侧配对的那个加上任何较低或相等的最大值对面,然后在对面的其余部分。我们可以通过存储与奇数最大值相关的前缀计数在 O(1) 中得到后者。它之所以有效,是因为保持一个方向,最大前缀是单调的,所以我们只需要计算其中有多少是奇数。

               8, 1, 7, 3, 9, 0
               <------||------>
min pfx        1  1  3||3  3  0
max pfx        8  7  7||7  9  9
max odd counts 2  2  1||1  2  3

(Max odd counts do not overlap the middle)

我们按照最小值递减的顺序执行迭代(或在最大值上镜像迭代)。我们从哪一边开始并不重要,只要一侧占一分钟,并且迭代是顺序的。

               8, 1, 7, 3, 9, 0
               <------||------>
min pfx        1  1  3||3  3  0
max pfx        8  7  7||7  9  9
max odd counts 2  2  1||1  2  3
                     <---
                     <------
[3,7]: extend left to min 1
[3,9]: extend left to min 1
Total: 1 + 1 = 2 overlapping intervals

We could have started on the left and
used the max odd counts on the right:
                     --->-->
[3,7]: extend right to 0, first to
       max 9, then using the max odd
       counts for the remaining window.

接下来的几分钟:

               8, 1, 7, 3, 9, 0
               <------||------>
min pfx        1  1  3||3  3  0
max pfx        8  7  7||7  9  9
max odd counts 2  2  1||1  2  3
                  ------>-->
               --------->-->
               
[1,7]: extend right to 0, first
       to max 9, then use the max
       odd count prefixes.
Total: 1 + 1 = 2 overlapping intervals

[1,8]: extend right to 0, first
       to max 9, then use the max
       odd count prefixes.
Total: 1 + 1 = 2 overlapping intervals

最后一分钟:

               8, 1, 7, 3, 9, 0
               <------||------>
min pfx        1  1  3||3  3  0
max pfx        8  7  7||7  9  9
max odd counts 2  2  1||1  2  3
               <---------------
               
[0,9]: extend left to end
Total: 3 overlapping intervals. They don't count, though, since
the difference is not even.

【讨论】:

  • 这看起来不错,但我仍然不清楚一些事情。 (1) 像“[3,7]:extend left to min 1”这样的句子意思是“最多但不包括”,对吧? (2) 你如何在 O(1) 时间内找到对面的最小值?如果这些 find-lower-min-on-opposite-side 步骤总是交替方向(向左,然后向右,然后向左等),那么它总是可以通过从前一次扫描的开始向后扫描在摊销 O(1) 时间内找到点,但有时不会发生交替——然后呢? (3) 对于偶数,您需要计算最大偶数(只是减法),对吧?
  • @j_random_hacker (1) 是的,“最多但不包括”。 (2) 预先计算。 (3) 是的,我们可以通过从窗口长度中减去奇数的计数来获得最大偶数。
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多