Problem:


Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.



Sample input:
3
2 1 2


Sample output:
1

Submission status: Wrong Answer

Solution code:
N=int(input())
number=input().split(' ')
number_list=[]
for i in number:
    number_list.append(int(i))
index=1
flag=0
count=0
for i in number_list:
    if(i==1):
        flag=1
        index+=1
    elif (i==index):
        index+=1
    else:
        count+=1

if(flag==0):
    print(-1)
else:
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

ans = -1
tmp_nums = []
check_num = 2

for i in range(len(A)):
  if A[i] == 1:
    check_num = 2
    tmp_nums = [1]
  elif A[i] == check_num and len(tmp_nums) != 0:
    check_num += 1
    tmp_nums.append(A[i])
    ans = max(ans,len(tmp_nums))

if ans == -1:
  print(-1)
else:
  print(N-ans)

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
a = list(map(int, input().split()))


def li_index(l, x, default='-1'):
    if x in l:
        return l.index(x)
    else:
        return default


if li_index(a, 1) == '-1':
    print(-1)
    sys.exit()


if a == [i + 1 for i in range(N)]:
    print(0)
    sys.exit()

for i in range(N):
    if i == 0:
        continue
    if li_index(a, i + 1) == '-1':
        print(N - i)
        break
    else:
        num = li_index(a, i + 1)
        a = a[num + 1:]

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
if 1 not in a:
  print(-1)
else:
  i = 1
  j = 0
  while j != N:
    if a[j] == i:
      sum = j-i+1
      i += 1
    j += 1
  print(sum)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
count=0

if 1 not in a:
    print(-1)
else:
    for i in range(1,N):
        if a[0]!=i:
            count+=1
        a.pop(0)


print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())+1
a=list(map(int,input().split()))
cnt=0
ans=0
find=a
for i in range(1,N):
    if i not in find:
        break
    elif i in find:
        cnt+=1
        ind=find.index(i)
        ans+=ind
        find=find[ind+1:]
if cnt>0:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
c=0
pos=0
temp=[]
flag=1
if l.count(1)==0:
    print(-1)
else:
        
    for i in range(1,max(l)+1):
        if i==1:
            
            x=l.index(i)
            temp.append(i)
        else:
            try:
                x=l[temp[-1]::].index(i)+temp[-1]
                temp.append(x)
            except:
    
                break
    
    print(n-len(temp))

Submission status: Wrong Answer

Solution code:
N = int(input())
n = input()    
m = n.split()
p = [int(i) for i in m]
j = 1
for i in range(N):
    if p[i] == j:
        j = j+1
print(N - j + 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
c = 0
for nn in range(1, n + 1):
    if nn in a:
        if a.index(nn) == nn -1:
            if a == range(1, nn + 1):
                break
            else:
                continue
        else:
            c += len(a[nn - 1:a.index(nn)])
            del a[nn - 1:a.index(nn)]
    else:
        if c == 0:
            print(-1)
            exit()
        else:
            c +=  len(a[nn:])
            break
print(c)

Submission status: Wrong Answer

Solution code:
a = list(map(int, input().split()))
idx = 1
cnt = 0
for i in a:
  if i == idx:
    idx += 1
    continue
  cnt += 1
if idx == 1:
  cnt = -1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)

if 1 not in a:
  print(-1)
  exit()
  
index1 = int(a.index(1))
count = 0
for i in range(n):
  if a[index1-i] == i+1:
    count+=1
  else:
    print(n-count)
    exit()
if count == 1:
  print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input().split())
B = ''.join(A)
K = len(set(B))
i = 1
a = 0
b = []
while i < min(N+1,K+1):
    j = B.find(str(i))
    if j == -1:
        break
    a += j+1
    b.append(a)
    B = B[j+1:]
    if a == N:
        break
    i += 1
    
if b == []:
    print(-1)
else:
    print(N-len(b))

Submission status: Wrong Answer

Solution code:
import sys

first = int(input())
numbers = list(map(int, sys.stdin.readline().split()))

if numbers == [] or 1 not in numbers:
    print(-1)
    sys.exit(0)

index = 0
value = 1
breaks = 0
while True:
    try:
        n = numbers.index(value)
    except ValueError:
        if len(numbers) == first and first != 1:
            print(-1)
        else:
            print(breaks + len(numbers) - (value - 1))
        sys.exit(0)
    breaks += n - (value - 1)
    numbers = numbers[0:(value - 1)] + numbers[n:]
    value += 1

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [str(x) for x in input().split()]

ans = 0
for i in range(1, N):
    A = ''.join(a)
    c_i = A.find(str(i))
    c_j = A.rfind(str(i+1))
    if c_i == -1:
        ans = -1
        break
    elif c_i < c_j:
        a = a[c_i:]
    elif c_i > c_j:
        ans = N - i
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
s1 = [int(x) for x in input().split()]
Ans = set()

for i in range(1, N+1):
    for k in range(i-1,N):
        if i == s1[k]:
            Ans.add(i)
    if len(Ans) != i:
        break
if len(Ans) == 0:
    print(-1)
else:
    print(N-len(Ans))

Submission status: Wrong Answer

Solution code:
N = int( input() )
A = list( map( int, input().split() ) )
dp = [ 10**4 for x in range(N) ]
#dp[i] i+1まで数える最短のmonotonically increasing列の位置

for i in range(N) :
    if i+1 in A : 
       dp[i]=A.index(i+1)
       A = A[A.index(i+1)+1:]
    else :
       break

print( N - i )

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))

if 1 in a:
    ind=a.index(1)
    stop=1
    for i in range(2,N+1):
        if i in a[ind+1:]:
            ind+=a[ind+1:].index(i)+1
        else:
            stop=i-1
            break
    print(N-stop)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
 
c = 1
b = [1]
while c!=0 and (len(a) != 0) :
    b = []
    c = 0
    for i in enumerate(a):
        if not (i[0]+1 < i[1]):
            b.append(i[1])
        else:
            c = 1
    a = b
 
#print(a)
if len(a)==0:
    print(-1)
else:
    print(N-len(set(a)))

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
count=0

if 1 not in a:
    print(-1)
else:
    for i in range(1,N):
        if a[0]!=i:
            a.pop(0)
            count+=1

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
k=0
if n==1:
    if l[0]==1:
        print(0)
    else:
        print(-1)
else:
    for i in range(1,n):
        if i in l[k:]:
            k=k+l[k:].index(i)
        else:
            break

    if k==0 and i==1:
        print(-1)
    else:
        print(n-l[k])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))

i = 1
ind = 0
while i in a[ind:]:
    ind = a.index(i)
    i += 1
    n -= 1
if n == len(a):
    print(-1)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
N=int(input())
a = list(map(int,input().split()))
c=0
for i in range(0,N):
    if a[i]==1:
      x=i
      c=c+1
      break
if c==0:
  print(-1)
else:
  for i in range(2,N+1):
    y=1
    for j in range(x,N):
      if a[j]==i:
        x=j
        c=c+1
        break
  print(N-c)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))

l = len(A)
f = [0] * l


f[0] = 0
flag=0
if A[0] == 1:
    flag = 1

for i in range(1, l):
    if A[0] == 1 and f[i-1]==0:
        if f[i-1] == 0 and A[i] == i+1:
            f[i] = 0
        else:
            f[i] = f[i-1] + 1
    elif f[i-1] != 0:
        if A[i] == i - f[i-1] + 1:
            f[i] = f[i-1]
        else:
            f[i] = f[i-1] + 1 
    else:
        if A[i] == 1:
            f[i] = 1
        else:
            f[i] = 0

if f[l-1]==0 and flag==0:
    print('-1')
    exit(0)

print(f[l-1])

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
a = list(map(int,input().split()))

if 1 in a == False:
    print(-1)
    sys.exit()

res = 0
j = 0
for i in range(len(a)):
    if i == len(a):
        break
    for _ in range(1, len(a)):
        if a[i] != i + 1:
            res += 1
            a.pop(i)
        else:
            break
print(res)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())

dp = [0]*N

if a[0] == 1:
    dp[0] = 1

for i in range(N-1):
#    print i, a[i+1], dp[i]                                                                            
    if a[i+1] == dp[i] + 1:
        dp[i+1] = dp[i] + 1
    else:
        dp[i+1] = dp[i]

print N - dp[-1]

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = 0
cont = 0
i = 1
if len(a) == 1:
    print(0)
    exit()
if len(set(a)) == 1:
    print(-1)
    exit()
for i in range(n):
    k = len(a)
    for j in range(k):
        if i == a[j]:
            cont += 1
            break
        else:
            del a[j]
            cnt += 1
            break
print(cnt-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))

x = 1
b = 0
for i in a:
    if i != x:
        b += 1
    elif i == x:
        x += 1

if x == n:
    print(-1)
else:
    print(b)

Submission status: Wrong Answer

Solution code:
N=int(input())
number=input().split(' ')
number_list=[]
for i in number:
    number_list.append(int(i))
index=1
flag=0
for i in number_list:
    if(i==1):
        flag=1
        index+=1
    elif i==index:
        index+=1

if(flag==0):
    print(-1)
else:
    print(N-index+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input().split())
B = ''.join(A)
K = len(set(B))
i = 1
a = 0
b = []
while i < min(N+1,K):
    j = B.find(str(i))
    if j == -1:
        break
    a += j+1
    b.append(a)
    B = B[j+1:]
    if a == N:
        break
    i += 1
    
if b == []:
    print(-1)
else:
    print(N-len(b))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
i=0
while i+1 <= len(a):
    if a[i]!=i+1:
        del a[i]
        if len(a)==0:
            print(-1)
            break
    else:
        i+=1
if len(a)==0:
  print(-1)
else:
    print(n-len(a))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
selector=1
break_count=0
while len(a)>0:
    length=len(a)
    try:
        index=a.index(selector)
    except ValueError:
        break_count=-1
        break
    a=a[index:]
    break_count+=length-len(a)
    a.pop(0)
    selector+=1
print(break_count)

Submission status: Wrong Answer

Solution code:
# 1行目の入力を数値として扱う
N = int(input())
# 2行目の入力を数値のリストとして扱う
a = list(map(int, input().split()))

# 答えの準備
res = 0

# 最大値
mx = max(a)

# ループ
for i in range(1, mx):
    if min(a) != i:
        mx = N + 1
        break

# 答えの出力
print(N - mx)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
b=[]
min=0
num=0
out=0
a=(input().split())
for i in range(1,N+1):
  for j in range(i,N+1):
    if i==int(a[j-1]):
      num=j-min-1
      min=j
      out+=num
      b.append(i)
      break
  if j==10:
    break
for i in range(1,max(b)+1):
  if (i in b)==False:
    out=-1
print(out)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))

i_loc = 0
counta = 0
i_memo = 0

for i in range(N):
    for j in range(i_loc, N):
        if i == a[j] and i - i_memo == 1:
            counta += 1
            i_memo = i
            i_loc = j
            break
    if i - i_memo != 0:
        break

if counta != 0:
    print(N-counta)
else:
    if counta == 0 and a[0] != 1:
        print(-1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import math


def inputIntList():
    return [int(s) for s in input().split()]


def inputInt():
    return int(input())


def main():
    N = inputInt()
    a = inputIntList()
    ans = 0
    index = 0
    while index < len(a):
        if not index + 1 in a:
            ans = -1
            break
        if a[index] - 1 != index:
            a.remove(a[index])
            ans += 1
        else:
            index += 1
    return ans


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=1
j=0
b=[]
while True:
  if j>=len(a):
      break
  if a[j]==i:
      a=a[j:]
      b.append(i)
      i+=1
      j=1
  j+=1
for k in range(len(b)):
    if b[k]!=k+1:
        print(-1)
        exit()
if not b:
  print(-1)
  exit()

print(n-len(b))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))


def li_index(l, x, default='-1'):
    if x in l:
        return l.index(x)
    else:
        return default


if li_index(a, 1) == '-1':
    print(-1)


if a == [i + 1 for i in range(N)]:
    print(0)

# 1以上に関して
for i in range(N):
    if i == 0:
        continue
    if li_index(a, i + 1) == '-1':
        print(N - i)
        break
    else:
        num = li_index(a, i + 1)
        a = a[num + 1:]

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int, input().split(" ")))

c = [0] * n
count = 0
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            del s[j:c[i]]
            #print(s)
            count += c[i]
            j += 1
        elif i not in s:
            break

    print(count-j)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
b=[]
min=0
num=0
out=0
a=(input().split())
for i in range(1,N+1):
  for j in range(i,N+1):
    if i==int(a[j-1]):
      num=j-min-1
      min=j
      out+=num
      print(num,j,out,i)
      b.append(i)
      break
  if j==10:
    break
for i in range(1,max(b)+1):
  if (i in b)==False:
    out=-1
print(out)

Submission status: Wrong Answer

Solution code:
N = int(input())
s1 = [int(x) for x in input().split()]
Ans = set()

for i in range(1, N+1):
    for k in range(i-1,N):
        if i == s1[k]:
            Ans.add(i)
    if len(Ans) != i:
        break
if len(Ans) == 0:
    print(-1)
else:
    print(N-len(Ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))

c = 0
for i in range(max(A)):
    if i+1 in A:
        c += 1
        A = A[A.index(i+1):]
        if max(A) == i+1:
            print(N-c)
            exit()
    else:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
i=0
while i+1 <= len(a):
    if a[i]!=i+1:
        del a[i]
        if len(a)==0:
            print(-1)
    else:
        i+=1
if len(a)==0:
    print(-1)
else:
    print(n-len(a))

Submission status: Wrong Answer

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()
  
def main():
  N = int(input())
  a = list(map(int, input().split()))
  index = 0
  break_ = 0
  #ans = []
  while True:
    if index == len(a) or break_ == N - 1:
      break
    elif index + 1 != a[index]: #一致しなかったら壊す
      a.pop(index)
      break_ += 1
    else: #一致したら次
      #ans.append(index + 1)
      index += 1
  #print(index)
  #print(break_)
  #print([i + 1 for i in range(N - break_)])
  #print(ans)
  #print(a)
  if index + break_ != N:
  #if ans != a:
    print(-1)
  else:
    print(break_)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))

#print(L)
number = 1
ans = -1
for x in L:
    if(x == number):
        number += 1
        ans = x

if(ans == -1):
    print(-1)
elif(ans ==1):
    print(0)
else:
    print(N - number +1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a = list(map(int,input().split()))

j=1
ans=0
tmp=0
for i in range(0,N):
  if a[i]==j:
    ans += i-tmp
    tmp = i+1
    j += 1
print(i,j,ans,tmp)

if ans==0 and tmp==N:
  print(ans)
elif ans==0 and j==1:
  print(-1)
elif N>tmp:
  print(ans+(N-tmp))
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
N = 200000
A = list(range(200000,0,-1))

if not(1 in A):
  print('-1')
else:
  index = 0
  count = 1
  while index<N:
    if A[index] == count:
      count+=1
    index+=1
  print(N-(count-1))

Submission status: Wrong Answer

Solution code:
n=int(input())
a= list((map(int, input().split()))) 
b=[0]
c=[]
if 1 not in a:
  print(-1)
else:  
  for i in range(n):
    if a[0]-b[-1]==1:
      b.append(a.pop(0))
    else:
      c.append(a.pop(0))
#print(b)
print(n-len(b)+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))

# 無理な場合は-1

# 1からN-1が出てくるまでの個数を足していく？
idx = 0
cnt = 0
num = 1
brick = 0
flg = False
while True:
    if num == A[idx]:
        flg = True
        num += 1
        brick += cnt
        cnt = 0
    else:
        cnt += 1
    idx += 1
    if idx > N - 1:
        break

if flg == False:
    brick = -1
print(brick)

Submission status: Wrong Answer

Solution code:
N=int(input())
number=input().split(' ')
number_list=[]
index=1
flag=0
for i in number_list:
    if(i==1):
        flag=1
    elif i==index:
        index+=1

if(flag==0):
    print(-1)
else:
    print(index)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = tuple(map(int, input().split()))
Y = X[::-1]
for k in range(N):
    try:
        i = Y.index(k+1)
        Y = Y[:i]
    except:
        i = -1
        leng = k
        break
    leng = k + 1
if leng == 0:
    ans = -1
else:
    ans = N - leng
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a_unique = list(set(sorted(a)))
#print(a_unique)
K = a[-1]

if K > N:
  print(-1)
  exit()

for i in range(1, K):
  if a_unique[i-1] != i:
    print(-1)
    exit()

print(N-K)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))

count = 0
for i in range(n):
  if(i < len(a)):
    while(len(a) > i and a[i] != i+1):
      a.pop(i)
      count += 1

if count == 0 or len(a) == 0:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int,input().split()))

min_indexlist = [a_list.index(min(a_list))]

m = min(a_list)

for i in range(len(a_list)):
    if( a_list[i] == m+1):
        m = a_list[i]
        min_indexlist.append(i)


#print(min_indexlist)

#砕いたレンガの後のリスト
crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])

#print(crash_list)

#砕いたレンガの数
crash = n - len(min_indexlist)


frag=True

for i in range(len(crash_list)):
    if(crash_list[i] != i+1):
        frag = False


if(frag == True):
    if(crash <= n-1):
        print(crash)
elif(frag == False):
    print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=1
j=0
b=[]
while True:
  if j>=len(a):
      break
  if a[j]==i:
      a=a[j:]
      b.append(i)
      i+=1
      j=1
  if j==len(a):
    break
  j+=1
for k in range(len(b)):
    if b[k]!=k+1:
        print(-1)
        exit()
if not b:
  print(-1)
  exit()

print(n-len(b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int, input().split(" ")))

c = [0] * n
count = 0

if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            del s[c[i-1]+1:c[i]]
    
            count += c[i]
        elif i not in s:
            break

    print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
b = 1
anc = 0
roop = 0
if 1 in a:
    for i,v in enumerate(a):
        if v == b:
            anc += roop
            temp = i
            b+=1
            #print('roop',roop)
            roop = 0
            #print('b',b)
            #print('temp',temp)
            #print('anc',anc)
        else:
            roop +=1
    print(anc)
else:

    print(-1)

Submission status: Wrong Answer

Solution code:
import math


def inputIntList():
    return [int(s) for s in input().split()]


def inputInt():
    return int(input())


def main():
    N = inputInt()
    a = inputIntList()
    ans = 0
    index = 0
    while index < len(a):
        if not index in a:
            break
        if a[index] - 1 != index:
            a.remove(a[index])
            ans += 1
        else:
            index += 1
    return ans if ans != N else -1


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

save = 1
ans = -1
tmp = []
for i in range(1,200000):
    tmp.append(i)

for i in a:
    if save == i:
        if i == 1:
            ans += 1
        save += 1
    else:
        ans += 1
if a == tmp[:len(a)]:
    print(0)
else:
    print(ans if len(a)-1 != ans else '-1')

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))

i_loc = 0
counta = 0
i_memo = 0

for i in range(N):
    for j in range(i_loc, N):
        if i == a[j] and i - i_memo == 1:
            counta += 1
            i_memo = i
            i_loc = j
            break
    if i == a[j] and i - i_memo == 1:
        break
if counta != 0:
    print(N-counta)
else:
    if counta == 0 and a[0] != 1:
        print(-1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
li = list(map(int,input().split()))
count = 0
ind = 0
try:
  while True:
   if(li[ind] == ind+1):
   	if(ind==len(li)-1):
   		break
   	ind+=1
   else:
   	count+=1
   	li.pop(ind)
except:
	print(-1)
else:
	print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int,input().split()))
num = len(l)
l_first = min(l)
l_min = min(l)
x = l.index(l_min)

for _ in range(N):
    l = l[x:]
    if l_min+1 in l:
        l_min +=1
        x = l.index(l_min)
    else:
        break

if l_first == l_min:
    print(-1)
else:
    print(num-(l_min-l_first+1))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
c = 0
i = 0
sa = sorted(a)

while i < len(a):
    if a == sa[0:len(a)]:
        print(c)
        exit()
    if i+1 in a:
        ii = a.index(i+1)
        a = a[0:i] + a[ii:]
        c = c + ii-i
        i = i+1
    else:
        break
if i == 0:
    c = -1
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=1
j=0
b=[]
while True:
  if j>=len(a):
      break
  if a[j]==i:
      a=a[j:]
      b.append(i)
      print(b)
      i+=1
      j=1
  if j==len(a):
    break
  j+=1
for k in range(len(b)):
    if b[k]!=k+1:
        print(-1)
        exit()
if not b:
  print(-1)
  exit()

print(n-len(b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n = int(input())
renga_list = list(map(int, input().split()))

tmp = 0
for i in renga_list:
  tmp = i

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))

i_loc = 0
counta = 0
i_memo = 0

for i in range(N):
    for j in range(i_loc, N):
        if i == a[j] and i - i_memo == 1:
            counta += 1
            i_memo = i
            i_loc = j
            break
if counta != 0:
    print(N-counta)
else:
    if counta == 0 and a[0] != 1:
        print(-1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:

n = int(input())
a =[int(i) for i in input().split()]
j = 1
ans = 0
k =0
if 1 not in a:
	ans = -1
else:
		for i in range(k,n):
			if k>=n:
				break
			elif a[i] == j:
				ans+=i-k
				k = i+1
				j +=1

			else:
				if i ==n:
					break


print(ans)
"""
k  =1
j =0
for  i in range(k,10):
		if i !=2:
			print(i)
		else:
			k = 3
			break
		j +=1
"""

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))

x = 1
aa = 0
for i in a:
    if i != x:
        aa += 1
    elif i == x:
        x += 1

print(aa)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

N = int(input())
al = [int(e) for e in input().split(" ")[:N]]

pos = 0
for i in range(1, N+1):
    pos = al.index(i, pos) if i in al[pos:] else -1
    if pos == -1:
        print(-1)
        exit(0)
    elif pos == N-1:
        break

print(N-i)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
total_breaks = 0
position = 0
for i in range(1,n):
    try:
        breaks = a[position::].index(i)
        total_breaks += breaks
        position = position + breaks + 1
    except ValueError:
        if position == 0:
            total_breaks = -1
            break
        else:
            print(position)
            total_breaks = total_breaks + n - position
            break
print(total_breaks)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
search_value = 1
for num in a:
    if num == search_value:
        search_value+=1
print(n-search_value+1)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))

purged_data = [] #purge : [動詞]〜を粛清する
tmp = a[0] - 1
s=1
for i, e in enumerate(a):
    if e==s:
        purged_data.append(e)
        tmp = e
        s+=1
for i in range(len(purged_data)):
    if purged_data[i]!=i+1:
        print(-1)
        sys.exit()
if len(purged_data)==0:
    print(-1)
    sys.exit()
print(len(a)-len(purged_data),purged_data)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = input().split()
l_len = len(l)
flag = 0
for i in range(n):
    if str(i+1) in l:
        index = l.index(str(i+1))
        # print(index)
    else:
        break
    flag += 1
    del l[i:index]

if flag != len(l):
    print("-1")
else:
    print(l_len - len(l))

Submission status: Wrong Answer

Solution code:
n = int(input())
block = [int(x) for x in input().split()]
s = 1
c = 0
for i in range(n):
  if block[i] == s:
    s += 1
  else:
    c += 1
if s == 0:
  print(-1)
else:
  print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
cnt=0

while True:
    f = False
    for i, a in enumerate(A):
        if A[i] > i+1:
            A[i]=0
            cnt+=1
            f = True
    while 0 in A:
        A.remove(0)
    if f == False:
        break
    
for i in range(len(A)):
    if A[i] != i+1:
        A.pop(i)
        cnt+=1
        break
#print(A)
    

if len(A)==0:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
cnt = 0
d = 0
for b in range(n):
  d = my_index(a, b+1, -1)
  if d == -1 and cnt == 0:
    cnt = -1
    break
  if d != -1:
    cnt += a.index(b+1)
    a = a[(a.index(b+1)+1)::]
  if len(a) < 1:
    break;
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = -1
cnt = 0
i = 1

while(i in a):
    if a.pop(0) == i:
        i += 1
        continue
    else:
        cnt += 1

if i not in a and cnt != 0:
    cnt += len(a)

if len(a) == 0 or cnt > 0:
    ans = cnt

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
min=0
num=0
out=0
a=(input().split())
for i in range(1,N+1):
  for j in range(i,N+1):
    if i==int(a[j-1]):
      num=j-min-1
      min=j
      out+=num
      print(num,j,out,i)
      break
  if j==10:
    break

print(out)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))

#print(L)
number = 1
ans = -1
for x in L:
    if(x == number):
        number += 1
        ans = x

if(ans == -1):
    print(-1)
elif(ans ==1):
    print(1)
else:
    print(N - number +1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))

x = 1
aa = 0
for i in a:
    if i != x:
        print(i,x)
        aa += 1
    else:
        x += 1
print(aa)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input().split())
B = ''.join(A)
i = 1
a = 0
b = []
while i < N+1:
    j = B.find(str(i))
    if j == -1:
        break
    a += j+1
    b.append(a)
    B = B[j+1:]
    if a == N:
        break
    i += 1
    
if b == []:
    print(-1)
else:
    print(N-len(b))

Submission status: Wrong Answer

Solution code:
N=int(input())+1
a=list(map(int,input().split()))
cnt=0
ans=0
find=a
for i in range(1,N):
    if i in find:
        cnt+=1
        ans+=find.index(i)
        find=find[i+1:]
    elif i not in find:
        break
if cnt>0:
    print(ans-cnt+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split(" ")
a = [int(i) for i in a]

c = 0

while 1:
  
  tmp = a
  a = [a[i] for i in range(len(a)) if a[i] <= i+1]
  
  if len(tmp) == len(a):
    break  
  elif c > 1000000:
    print("finite loop")
    break
  else:
    c = c + 1

#print(a)
if len(a) == 0:
  print(-1)
else:
  b = [a[0]]
  for i in range(len(a) - 1):
    if a[i+1] == a[i]:
      pass
    elif a[i+1] > a[i] + 1:
      b.append(-1)
    else:
      b.append(a[i+1])

  #print(b)

  if min(b) == -1:
    print(-1)
  else:
    print(n - len(b))

Submission status: Wrong Answer

Solution code:
n = int(input())
L = [x for x in map(int, input().split())]

out = []
for i in range(1, n+1):
    for j in range(len(L)):
        if L[j] == i:
            out.append(i)
            break
    L = L[j:len(L)]


is_false = 0
for i in range(len(out)):
    if out[i] != i + 1:
        is_false = 1

if is_false == 1:
    print("-1")
else:
    print(n - len(out))

Submission status: Wrong Answer

Solution code:
def main(n: int, a: list):
    ans = 0

    i = 1

    for _a in a:
        if _a != i:
            ans += 1
        else:
            i += 1

    print(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    main(n, a)