import sys
from sys import stdin, stdout
N = int(sys.stdin.readline())
lst = [int(x) for x in sys.stdin.readline().split()]
lst.sort()
smallest = [0] * len(lst)
for i in range(len(lst)):
smallest[len(lst)-i-1] = lst[i]
Q = int(sys.stdin.readline())
for i in range(Q):
length = len(smallest)
a, b = map(int, sys.stdin.readline().split())
if a == 1:
for j in range(length):
if smallest[j] < b:
smallest.insert(j, b)
break
elif j == length-1:
smallest.append(b)
else:
if bool(smallest):
sys.stdout.write(str(smallest[-1])+"\n")
smallest.pop()
#π Wall Clock limit/time limit exceeded
84 messages Β· Page 1 of 1 (latest)
@warped herald
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
it is also uncertain if my code works fine for all cases, but it has passed all public test cases that it doesn't time out at for now
i reversed the order so that i could pop instead of having to sort + remove first index everytime which are both O(N) functions
so if it works for the given samples whats causing it to fail?
are there hidden test cases?
wall clock limit/time limit exceeded
yeah but for what input is the time limit exceeded? since youre saying it works for some cases
nobody knows
why would you need to know the input, it's just finding a way to make it faster
theres this one annoying test case for subtask 1 that exceeds the time limit so that means my code is slow af T-T
i just wanted to know if its your loops getting stuck or something like that but i see what you mean now
you could try using a heap instead of a list, its O(logn) instead so that could be it
i have no idea what a heap is and i probably can't learn it in time
hm yeah probably not what they want you to do then
but yeah clearly inserting or appending for every element would be bad performance if you had a really big list
wait a min
nvm
there's no function that reverses a list in O(1)
cuz like i reverse sort them so i can pop, but that means I can't use bisect.bisect
unless there's a way that idk about
idk theres something obvious im missing ive been awake too long, sorry
D:
ok erm im gonna try appending, then sorting, then reversing the order, then popping
sounds like a lot of expensive operations
reversing the list like that isnt helping either
wait that doesn't do anyting lmao
I don't understand why you do this and then use the insert
smallest = [0] * len(lst)
i used insert so that it remains sorted so that i can pop at the end
ok for one instead of sorting the list and then getting the last item as the smallest, you should just go through the list and compare the current number to the smallest one youve seen so far
wouldn't that be slower
no because you are only running through the list once
no but to remove is O(N)
sorting at each insert is probably more expensive than to just change the list when you need to get the smallest value out unless there is a lot of inserts and very few removals
lemme try this rq
wait i tried it alr with the min()
which is the same thing right
min() will only give you the value but not the element index of that value in the list
list.remove(value) removes the earliest occurring value
wwhich can be paired with min()
true, but then it needs to iterate through the lists two times
first through the whole list to find the smallest value, then from the beginning until it finds the first occurrence of that value
A solution that avoids iterating through the list is to insert the smallest value at the beginning of the list if it's smaller than the current first element; otherwise, just append it to the end.
if my_list and number < my_list[0]:
my_list.insert(0, number)
else:
my_list.append(number)
then it needs to alter the list by deleting that element
print(my_list[0])
my_list.pop(0)
?
but you still have to uh
idk it's still O(N) solution to remove it
O(1)
no
[3, 4, 5, 6, 7, 8,..., 100000, 2, 2, 2, 2, 2]
if i remember right, inserting at the front is "expensive" for pythons implementation of lists
yes because you have to move every index back
with the logic I sent, the smallest number will always be index 0
removing the first index also means you have to move every index forward by one
furthermore, you can't guarantee that the smallest index won't be at the back
which i just proved using the list example(?)
[1, 2, 3, 4, 5, 6, 7, 8,..., 100000] so you have this list, then it asks you to insert a 2 but 2 is bigger than 1 so it gets appended [1, 2, 3, 4, 5, 6, 7, 8,..., 100000, 2], then you remove the 1, then you remove the 2 [3, 4, 5, 6, 7, 8,..., 100000, 2] now how do you know the smallest element is at the front
you can use deque
it is an implementation of a doubly linked list, it does not have the problem of moving the remaining indexes
cheap to insert and remove from either end
no
then deque is a good option for you
^
i think i'll just find another question to solve..
i dont have to solve all the questions
they are probably easier
one is findin the smallest span of a subarray that contains all the numbers
isn't this just use a heap?
yeah bbut idk how to use a heap
pretty easily actually, cause there's the heapq stdlib
im havin dinner now so ill see wheen i come finish
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.