123292024-12-12 16:52:09WellLeopardUsuallyFasor (40)python3Runtime error 36/40122ms27812 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/016ms3000 KiB
2Accepted0/019ms4264 KiB
3Accepted2/216ms3128 KiB
4Accepted2/216ms3108 KiB
5Accepted2/217ms3320 KiB
6Accepted2/217ms3116 KiB
7Accepted2/217ms3124 KiB
8Accepted2/228ms11216 KiB
9Accepted2/219ms4304 KiB
10Accepted2/228ms12024 KiB
11Accepted2/219ms5136 KiB
12Accepted2/218ms3896 KiB
13Accepted2/286ms17152 KiB
14Runtime error0/252ms14792 KiB
15Accepted2/297ms26124 KiB
16Accepted2/298ms26096 KiB
17Accepted2/2105ms27612 KiB
18Accepted2/2103ms27812 KiB
19Accepted2/2122ms25136 KiB
20Runtime error0/259ms11340 KiB
21Accepted2/298ms26148 KiB
22Accepted2/2109ms25040 KiB