How to improve this code?
TASK: Find median of given numbers. Print the integer part of it.
Sample Input:
1
3
4
60
70
50
2
Sample Output:
1
2
3
3
4
27
4
Numbers in [0, 2^31].
There will be less than 50001 numbers.
Currently it takes around 29 seconds to run with a 50k line input I generated myself. How should I shrink it down to 0.5 second? Or should I switch language? I just learnt C++ in 2 days.
UPDATE: The problem is originally from UVa Judge and I am running it on an Hong Kong Online Judge ('Private'?) but the number of input and time allowed is changed.
One way to speed it up is to use array.reserve(50000) or whatever the new max input is
so there will no more memory allocations.
Also consider using push_back instead of insert.
Also if you want speed stop using the std::string/stoi() for the input and use the proper type of variable instead. And note that you're using type long not type int so you should be using stol() not stoi().
Also do you really need to use the type long? Using an int seems a better choice for the input you've shown.
Doesn't median require at least three elements in your array?
Lastly you shouldn't be calling median() inside the loop it should be called after the loop, especially since you're sorting the array inside that function.
Median of medians finds an approximate median in linear time only, which is limited but an additional overhead for quickselect. When this approximate median is used as an improved pivot, the worst-case complexity of quickselect reduces significantly from quadratic to linear ... https://en.wikipedia.org/wiki/Median_of_medians
The problem is that you're sorting the vector after reading each number. Since the average size of the vector is N/2 and you're sorting it N times, the complexity is something like N/2 * N *log2(N) = 25000 * 50000 * 15.61 ~ 19.5*109. If we optimistically say that a 4GHz processor can execute 2 instructions per clock cycle then even if the inner part of the sort function was just 1 instruction, it would still take 2.4 seconds to run the program
So you need a different algorithm, not just better code for the algorithm you have.
Since you print the median of each vector as you add it, the trick to this problem is to figure out how to compute the median of the numbers A1...Angiven that you've already computed the median of the numbers A1...An-1.
My advice is to store the numbers in a set rather than a vector. Since a set's iterator remains valid when you insert a new item you can keep an iterator that points to the median value (or the smaller of the two). After inserting a new value, adjust the iterator depending on whether the new value is bigger or smaller than the old median and maybe on whether the new count is odd or even. You will only have to increment or decrement the iterator once (make it point to the next larger or next smaller number). Then compute the new median from the iterator. Repeat the process.