88822024-02-02 19:03:34NagyLeoLegmesszebbi rossz sorrendű (35 pont)python3Hibás válasz 25/35165ms38008 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(hi, i)
        #print(minimums)
        #print(nums[i],hi)
        #if hi >= n:
        #    continue
        #if nums[hi-1] >= nums[i]:
        #    continue
        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/018ms11900 KiB
2Elfogadva0/0164ms34868 KiB
3Elfogadva1/118ms12368 KiB
4Elfogadva1/117ms12168 KiB
5Elfogadva1/118ms12304 KiB
6Elfogadva1/118ms12672 KiB
7Elfogadva1/118ms13304 KiB
8Elfogadva1/119ms13188 KiB
9Elfogadva1/120ms13484 KiB
10Elfogadva1/121ms14108 KiB
11Elfogadva1/124ms14704 KiB
12Hibás válasz0/275ms22052 KiB
13Hibás válasz0/282ms24084 KiB
14Hibás válasz0/286ms25124 KiB
15Hibás válasz0/261ms20700 KiB
16Hibás válasz0/290ms25792 KiB
17Elfogadva2/2115ms29376 KiB
18Elfogadva2/2135ms31900 KiB
19Elfogadva2/2145ms33852 KiB
20Elfogadva2/2146ms35544 KiB
21Elfogadva2/2165ms37316 KiB
22Elfogadva2/2164ms37504 KiB
23Elfogadva2/2165ms37996 KiB
24Elfogadva2/2165ms38008 KiB