Repository navigation
Expand file tree
/
Copy pathLGIS
More file actions
59 lines (47 loc) · 1.42 KB
/
Copy pathLGIS
File metadata and controls
59 lines (47 loc) · 1.42 KB
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
55
56
57
58
59
from bio import longestIncreasingSequence as lIS
from bio import longestDecreasingSequence as lDS
fi = open('1.txt')
n = int(fi.readline())
pi = map(int,fi.readline().replace('\n', '').split(' '))
def longestIncreasingSequence(data):
n = len(data)
# q[i] contains the max length of sub seq. ending at array[i]
# p[i] contains predecessor of the sub seq. ending at array[i]
q = [0] * n
p = [-1] * n
for i in range(n):
maxLen = 0
for j in range(i):
if data[i] > data[j]:
if q[j] > maxLen:
maxLen = q[j]
p[i] = j
q[i] = maxLen + 1
idx = q.index(max(q))
seq = []
while (idx != -1):
seq = [data[idx]] + seq
idx = p[idx]
return seq
def longestDecreasingSequence(data):
n = len(data)
# q[i] contains the max length of sub seq. ending at array[i]
# p[i] contains predecessor of the sub seq. ending at array[i]
q = [0] * n
p = [-1] * n
for i in range(n):
maxLen = 0
for j in range(i):
if data[i] < data[j]:
if q[j] > maxLen:
maxLen = q[j]
p[i] = j
q[i] = maxLen + 1
idx = q.index(max(q))
seq = []
while (idx != -1):
seq = [data[idx]] + seq
idx = p[idx]
return seq
print(''.join(str(x)+' ' for x in lIS(pi)))
print(''.join(str(x)+' ' for x in lDS(pi)))