-
Notifications
You must be signed in to change notification settings - Fork 0
/
MisterCounter.py
54 lines (44 loc) · 999 Bytes
/
MisterCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
lines = [x.strip() for x in sys.stdin]
original = [int(x) for x in lines[1].split(" ")]
q = int(lines[2])
def find_greater(l):
arr_changed = False
for i in range(len(l)-2,-1,-1):
if min(l[i+1:]) > l[i]:
for j in range(i,len(l)):
if l[j]==min(l[i+1:]):
break
temp = l[i]
l[i] = l[j]
l[j] = temp
arr_changed = True
break
if not arr_changed:
i = -1
return l,i
def find_lesser(l,i):
for j in range(i+1,len(l)-1):
if min(l[j+1:]) < l[j]:
for k in range(j+1,len(l)):
if k==min(l[j+1:]):
break
temp = l[j]
l[j] = l[k]
l[k] = temp
return l
def cost(l1,l2):
count = 0
for i in range(len(l1)):
if l1[i] != l2[i]:
count+=1
return count
for line in lines[3:]:
vals = line.split(" ")
lower = int(vals[0])-1
higher = int(vals[1])
a = original[lower:higher]
new_a,index = find_greater(a[:])
if index != -1:
new_a = find_lesser(new_a,index)
print(cost(a,new_a))