117642024-11-09 16:42:16balintSokszorozott maximumokpython3Time limit exceeded 11/1002.101s26924 KiB
import heapq
from functools import reduce


def product_of_k_largest(numbers, k):
    # Use a min-heap to store the k largest elements
    min_heap = []

    for num in numbers:
        # Push the current number onto the heap
        heapq.heappush(min_heap, num)

        # If the heap grows beyond size k, remove the smallest element
        if len(min_heap) > k:
            heapq.heappop(min_heap)

    # Compute the product of the k largest elements
    return reduce(lambda x, y: x * y, min_heap, 1)


def main():
    MOD = 10**9 + 7
    N, Q = map(int, input().split())
    nums = list(map(int, input().split()))
    for _ in range(Q):
        L, R, k = map(int, input().split())
        sub = nums[L : R + 1]
        mul = product_of_k_largest(sub, k)  # prod(sorted(sub, reverse=True)[:k])
        print(mul % MOD)


main()
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted21ms3640 KiB
subtask211/11
2Accepted39ms3640 KiB
3Accepted43ms3660 KiB
4Accepted280ms3800 KiB
5Accepted717ms3896 KiB
6Accepted21ms3644 KiB
7Accepted185ms3724 KiB
subtask30/13
8Accepted35ms3640 KiB
9Accepted41ms3708 KiB
10Accepted287ms3892 KiB
11Time limit exceeded2.076s14556 KiB
12Time limit exceeded2.079s26696 KiB
13Time limit exceeded2.101s26756 KiB
subtask40/19
14Time limit exceeded2.082s22088 KiB
15Time limit exceeded2.081s24516 KiB
16Time limit exceeded2.082s25612 KiB
17Time limit exceeded2.082s26700 KiB
18Time limit exceeded2.078s26924 KiB
subtask50/25
19Time limit exceeded2.082s9252 KiB
20Time limit exceeded2.084s8332 KiB
21Time limit exceeded2.082s8708 KiB
22Time limit exceeded2.082s9728 KiB
23Time limit exceeded2.086s6464 KiB
subtask60/32
24Time limit exceeded2.082s26700 KiB
25Time limit exceeded2.082s26700 KiB
26Time limit exceeded2.081s22096 KiB
27Time limit exceeded2.081s24408 KiB
28Time limit exceeded2.081s25748 KiB
29Time limit exceeded2.081s26704 KiB
30Time limit exceeded2.082s23792 KiB