本文共 994 字,大约阅读时间需要 3 分钟。
为了解决这个问题,我们需要找到一个最长的子序列,使得子序列的最大值不超过最小值乘以给定的参数 ( p )。这个问题可以通过对数组进行排序并使用双指针方法来高效解决。
这种方法的时间复杂度是 ( O(N) ),因为每个元素最多被处理两次,适合处理大数组。
def main(): import sys input = sys.stdin.read().split() N = int(input[0]) p = int(input[1]) arr = list(map(int, input[2:2+N])) arr.sort() max_count = 1 i = j = 0 while i < N: while j < N and arr[j] <= arr[i] * p: max_count = max(max_count, j - i + 1) j += 1 i += 1 print(max_count)if __name__ == "__main__": main()
这种方法确保了我们能够在 ( O(N) ) 时间复杂度内找到最长的完美子序列,适用于大数组。
转载地址:http://nllwz.baihongyu.com/