88812024-02-02 18:53:59NagyLeoLegmesszebbi rossz sorrendű (35 pont)python3Hibás válasz 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)
RészfeladatÖsszpontTesztVerdiktIdőMemória
base25/35
1Elfogadva0/019ms11548 KiB
2Elfogadva0/0165ms35064 KiB
3Elfogadva1/118ms12152 KiB
4Elfogadva1/118ms12396 KiB
5Elfogadva1/117ms12128 KiB
6Elfogadva1/118ms12764 KiB
7Elfogadva1/118ms12884 KiB
8Elfogadva1/119ms13192 KiB
9Elfogadva1/120ms13224 KiB
10Elfogadva1/123ms13664 KiB
11Elfogadva1/124ms14144 KiB
12Hibás válasz0/275ms21928 KiB
13Hibás válasz0/282ms24128 KiB
14Hibás válasz0/287ms24776 KiB
15Hibás válasz0/261ms20268 KiB
16Hibás válasz0/290ms25204 KiB
17Elfogadva2/2119ms29144 KiB
18Elfogadva2/2131ms31412 KiB
19Elfogadva2/2146ms33556 KiB
20Elfogadva2/2145ms35480 KiB
21Elfogadva2/2158ms37100 KiB
22Elfogadva2/2163ms37440 KiB
23Elfogadva2/2165ms37360 KiB
24Elfogadva2/2167ms37744 KiB