123142024-12-11 20:51:36WellLeopardUsuallyFasor (40)python3Runtime error 36/40120ms27812 KiB
def main():
    
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    
    '''n = 10
    k = 3
    h = [6, 2, 1, 8, 4, 8, 7, 12, 9, 3]'''


    db = [0]*max(h)
    
    maxi = 0
    for i in range(k-1, -1, -1):
        db[h[i]-1] += 1
        if h[i] > maxi:
            maxi = h[i]-1
            

    for i in range(0, k):
        szam_uj = h[i+k]
        
        db[szam_uj-1] += 1
        if szam_uj > maxi:
            maxi = szam_uj-1
        if maxi+1 <= h[i]:
            return i+1

    for i in range (k, n-k):
        
        szam_kivett = h[i-k]
        szam_uj = h[i+k]
        db[szam_kivett-1] -= 1
        db[szam_uj-1] += 1
        if szam_uj > maxi+1:
            maxi = szam_uj-1
        if szam_kivett >= maxi+1 and db[szam_kivett-1] == 0:
            for j in range(szam_kivett-1, 0, -1):
                if db[j] != 0:
                    maxi = j
        if maxi+1 <= h[i]:
            return i+1

    for i in range(n-k, n):
        szam_kivett = h[i-k]
        db[szam_kivett-1] -= 1
        if szam_kivett >= maxi+1 and db[szam_kivett-1] == 0:
            for j in range(szam_kivett-1, 0, -1):
                if db[j] != 0:
                    maxi = j

        if maxi+1 <= h[i]:
            return i+1
    return -1



print(main())
SubtaskSumTestVerdictTimeMemory
base36/40
1Accepted0/017ms3208 KiB
2Accepted0/019ms4312 KiB
3Accepted2/216ms3116 KiB
4Accepted2/216ms3048 KiB
5Accepted2/216ms3140 KiB
6Accepted2/216ms3128 KiB
7Accepted2/216ms3308 KiB
8Accepted2/225ms11220 KiB
9Accepted2/219ms4268 KiB
10Accepted2/228ms12164 KiB
11Accepted2/219ms5044 KiB
12Accepted2/217ms3968 KiB
13Accepted2/281ms17224 KiB
14Runtime error0/250ms14664 KiB
15Accepted2/2103ms26204 KiB
16Accepted2/293ms26268 KiB
17Accepted2/2114ms27812 KiB
18Accepted2/2104ms27676 KiB
19Accepted2/2120ms25024 KiB
20Runtime error0/256ms11324 KiB
21Accepted2/298ms26148 KiB
22Accepted2/2101ms25052 KiB