88812024-02-02 18:53:59NagyLeoLegmesszebbi rossz sorrendű (35 pont)python3Wrong answer 25/35167ms37744 KiB
def find_misplaced_elements(nums, n):
    max_length = 0
    first_num = 0
    second_num = 0
    minimums = [0] * n
    # minimums[-i] megmondja a leghatso i minimumat
    minimums[-1] = nums[-1]
    for i in range(n - 2, -1, -1):
        minimums[i] = min(minimums[i + 1], nums[i])
    for i in range(n - 1):
        # if minimums[i+1] >= nums[i]:
        #    continue
        if minimums[-1] < nums[i]:
            if n - 1 - i > max_length:
                max_length = n - 1 - i
                first_num = i
                second_num = n - 1
                continue
        lo = max_length + i + 1
        if lo > n - 1:
            break
        hi = n - 1
        if minimums[lo] >= nums[i]:
            continue
        while hi - lo > 1:
            mid = (lo + hi) // 2
            if minimums[mid] < nums[i]:
                lo = mid
            else:
                hi = mid
        max_length = lo - i
        first_num = i
        second_num = lo

    if max_length > 0:
        print(first_num + 1, second_num + 1)
    else:
        print(-1)


#n = int(input())
#nums = tuple(map(int, input().split()))
#n = len(nums)


import bisect
def find_misplaced_elements_2(nums, n):
    max_length = 0
    first_num = 0
    second_num = 0
    minimums = [0]*n
    # minimums[-i] megmondja a leghatso i minimumat
    minimums[-1]=nums[-1]
    for i in range(n-2,-1, -1):
        minimums[i] = min(minimums[i+1],nums[i])
    for i in range(n-1):
        #if minimums[i+1] >= nums[i]:
        #    continue
        if minimums[-1] < nums[i]:
            if n-1-i > max_length:
                max_length = n-1-i
                first_num = i
                second_num = n-1
                continue
        lo=max_length+i+1
        if lo>n-1:
            break
        hi=bisect.bisect(minimums,nums[i])
        #print(minimums)
        #print(nums[i],hi)
        if hi-i-1 > max_length:
            max_length = hi-i-1
            first_num = i
            second_num = hi-1
    if max_length > 0:
        print(first_num+1, second_num+1)
    else:
        print(-1)

n = int(input())
nums = list(map(int, input().split()))
n = len(nums)
#print(n)

find_misplaced_elements_2(nums, n)
SubtaskSumTestVerdictTimeMemory
base25/35
1Accepted0/019ms11548 KiB
2Accepted0/0165ms35064 KiB
3Accepted1/118ms12152 KiB
4Accepted1/118ms12396 KiB
5Accepted1/117ms12128 KiB
6Accepted1/118ms12764 KiB
7Accepted1/118ms12884 KiB
8Accepted1/119ms13192 KiB
9Accepted1/120ms13224 KiB
10Accepted1/123ms13664 KiB
11Accepted1/124ms14144 KiB
12Wrong answer0/275ms21928 KiB
13Wrong answer0/282ms24128 KiB
14Wrong answer0/287ms24776 KiB
15Wrong answer0/261ms20268 KiB
16Wrong answer0/290ms25204 KiB
17Accepted2/2119ms29144 KiB
18Accepted2/2131ms31412 KiB
19Accepted2/2146ms33556 KiB
20Accepted2/2145ms35480 KiB
21Accepted2/2158ms37100 KiB
22Accepted2/2163ms37440 KiB
23Accepted2/2165ms37360 KiB
24Accepted2/2167ms37744 KiB