给定一个长度为N(不一定不同)的数字序列,例如
{1, 50, 3, 99, 1, 2, 100, 99, 4, 100, 4, 100} (could be very long)
还有一小组M个有序对,比如说
(1, 2)
(2, 1)
(1, 3)
(99, 50)
(99, 100)
我希望能够检测出有序对是否在列表中任何位置出现(它们可以分开,但是顺序很重要)。例如,上面的计数将是:
(1, 2): 2 (each 1 pairs with the later 2)
(2, 1): 0 (no 1's come after the 2)
(1, 3): 1 (only one of the 1's come before the 3)
(99, 50): 0 (no 99's come before the 50)
(99, 100): 5 (3 times for the first 99 and 2 times for the second)
假设有序对中的每个数字都保证出现在列表中,是否存在一种比朴素的O(N * M)时间更快的算法来提取这些计数?作为一个副问题,如果我们限制自己只考虑布尔值而不是计数,是否可能存在快速算法?也就是说:
(1, 2): yes
(2, 1): no
(1, 3): yes
(99, 50): no
(99, 100): yes
非常感谢您的帮助。