My code solves the problem, but does not fit at the time, it seems to work for n logn as you should. Perhaps something missed in the binary search or in the conditions after leaving it. Now I think that the drawdown is coming at the same elements, i.e. I keep and [5,4] and [5,4,4], although the first is no longer needed and it slows down the search, you can remove it, but it's probably a long time, it is possible to replace None, but have not yet figured out how.

Condition:

Given an integer 1≤n≤10^5, and array A[1...n]A[1...n] contains the non-negative integers not exceeding 10^9. Find greatest non-increasing subsequence in A. In the first line output the length k, the second, the indexes 1≤i1

```
from array import array
def b_search3(xs, q):
l, h = 0, len(xs)-1
while l < h:
mid = (l + h+1)//2
if q > xs[mid]:
h = mid -1
else:
l = mid
return h
def max_sequence(l):
temp = list()
temp.append(array('i', (l[0],)))
for i, v in enumerate(l[1:]):
current = b_search3(tuple(x[-1] for x in temp), v)
if v > temp[current][-1]:
temp[current][-1] = v
else:
new = temp[current] + array('i', (v,))
if len(new) > len(temp):
temp.append(new)
elif v > temp[len(new)-1][-1]:
temp[len(new)-1] = new
answer = []
prev = 0
for el in temp[-1]:
prev += l[prev:].index(el) +1
answer.append(str(prev))
return '\n'.join((str(len(temp[-1])), ' '.join(answer)))
def main():
import sys
reader = (map(int, line.split()) for line in sys.stdin)
next(reader)
l = array('i')
l.fromlist(list(next(reader)))
print(max_sequence(l))
if __name__ == '__main__':
main()
```

Condition:

Given an integer 1≤n≤10^5, and array A[1...n]A[1...n] contains the non-negative integers not exceeding 10^9. Find greatest non-increasing subsequence in A. In the first line output the length k, the second, the indexes 1≤i1

asked July 12th 19 at 17:12

1 answer

answered on

Solution

Your code can't understand Python I do not know

If for each element A[I] stored L[I] is the length of the max. starting with him and a subsequence P[I] is the index of the next item, then

L[I]=max L[J] : J>I, A[J]<=A[I], the complexity is O(N^2)

If for each element A[I] stored L[I] is the length of the max. starting with him and a subsequence P[I] is the index of the next item, then

L[I]=max L[J] : J>I, A[J]<=A[I], the complexity is O(N^2)

Find more questions by tags AlgorithmsPython

The rapid growth in the number of options

Maybe I still don't understand Your algorithm

Python-ovsky adding to the list (array) is not O(1) - is not this the reason for the drawdown? - dayna_Grim commented on July 12th 19 at 17:26

1st iteration: {10}

2nd iteration {10} and {20} - dayna_Grim commented on July 12th 19 at 17:47

So?

In order not to store all the prefixes that are stored for each item a link to prejudise (if not primary) - prejudise uniquely determined, since the element is added to exactly ONE subsequence.

Then the subsequence is uniquely determined by its last element and easily recovered from it + store the length for quick comparison - dayna_Grim commented on July 12th 19 at 17:53

and even according to you - change element will have to move with a shift other is O(N) one Appendix, N^2 for the entire algorithm

I wrote 2 times: every element is added to only ONE populatevfslist therefore the index of the previous element uniquely determined, it is stored in a separate array P[N], then, knowing the last item - write it down, skip to previous index and so on until you write down all podpole from the end - dayna_Grim commented on July 12th 19 at 18:11

https://ideone.com/gW6OcZ - dayna_Grim commented on July 12th 19 at 18:17

> General - is also O(nlogn), but the memory of n^2

This can not be if You in each cell of this memory is ever traded, then the time is O( n^2)

Specifically, the copy subsequence is O(N) - dayna_Grim commented on July 12th 19 at 18:23

I immediately thought of suffix trees.

But Zip does not compress dictionary 6Mb less than 1.5 Mb and you have to squeeze into 64kB! - dayna_Grim commented on July 12th 19 at 18:26

I think the problem is not in compression in dictionary or data structures, and to identify patterns in these words and then for these laws to apreciate the word or not. For example if the string contains no consonants, it is not the word. And so a few checks. In the end, the algorithm should give the probability that the word. - Noemy_Prosac commented on July 12th 19 at 18:29

I do rely on compression - dayna_Grim commented on July 12th 19 at 18:32