5
Mr. Moneybags is a very rich man. He has an assistant Mr. Perfect, who one day, got very mad, and shredded some of Mr. Moneybag’s $100 bills! Mr. Moneybags decided to punish Mr. Perfect.
Mr. Moneybags tells Mr. Perfect to stack his money into different piles.
He starts with N (1 <= N <= 1,000,000, N odd) empty stacks, numbered 1..N. Mr. Moneybags then gives him a sequence of K instructions (1 <= K <= 25,000), each of the form "A B", meaning that Mr. Perfect should add one new bill to the top of each stack in the range A to B, inclusive. For example, if Mr. Perfect is told "11 13", then he should add a bill to each of the stacks 11, 12, and 13.
After Mr. Perfect finishes stacking the money according to his instructions, Mr. Moneybags would like to know the median height of his N stacks -- that is, the height of the middle stack if the stacks were to be arranged in sorted order (N is odd, so this stack is unique). Please write an efficient program to help Mr. Perfect determine the answer to Mr. Moneybag’s question.
INPUT FORMAT:
- Line 1: Two space-separated integers, N K.
- Lines 2..1+K: Each line contains one of Mr. Moneybag’s instructions in the form of two space-separated integers A B (1 <= A <= B <= N).
SAMPLE INPUT:
7 4
5 5
2 4
4 6
3 5
INPUT DETAILS:
There are N=7 stacks, and Mr. Moneybags issues K=4 instructions. The first instruction is to add a bill to stack 5, the second is to add a bill to each stack from 2..4, etc.
OUTPUT FORMAT:
- Line 1: The median height of a stack after Mr. Perfect completes the instructions.
SAMPLE OUTPUT:
1
The winner is the one with the "most efficient" algorithm. So, if someone posts an O(N) solution, then it will beat any O(N^2). If there is a tie, aka, two or more people who post an O(N) solution, for example, then whoever has is the shortest code (code-golfed) wins.
So, your purpose is to write a efficient algorithm, and to prevent possible ties with other people, try to code-golf it as well.
Another thing, the input is to be read from a file and the output must be written to a file.
EDIT Also, the solution must be returned under or equal to 2 seconds. If it is not, you will receive a T. A wrong answer is an X. Which means, if no one can get it under time, then the most efficient algorithm sigh score of T can win.
EDIT #2 Here are some more samples:
EDIT #3 Bonus: (subtract 100 chars from total)
Solve this under 2 seconds :)
If n is guaranteed to be odd, I think you mean n<1,000,000; not n<=1,000,000. – SuperJedi224 – 2015-06-05T16:37:48.743
2If one submission is in O(N) and one is in O(K), which one wins? Of course, that's just a special case - how are more complicated combinations of N and K ordered? – Martin Ender – 2014-07-25T07:21:24.057
Also you might want to remove the the constraints on N and K. They don't really seem relevant if there is no hard time limit and scoring is by big-O notation anyway. And if you keep them, you might end up having to discussing that an answer claims it runs in O(1), because N and K themselves are O(1). ;) – Martin Ender – 2014-07-25T10:08:01.877
2That doesn't answer my question. You could have a submission that loops over N once and K twice (nested loops, of course) and one that loops over K once and N twice. Now the first submission will run in O(N K^2), the second submission will run in O(K N^2). Which one is better? What if one submission has O(K^4 N) and another has O(N^4)? How do you compare these? – Martin Ender – 2014-07-25T11:15:14.527
@MartinBüttner, I would compare these by selecting large numbers of N and K and putting them into the big O notation of the algorithm. So something that is N^2 K is worst as a N and K approach the upper bounds, compared to K^2 N, and so on. – mmk – 2014-07-25T11:45:19.380
@MartinBüttner I guess that one of these numbers shall be limited. If you state the rule "there can't be more than 100 stacks" then N becomes a constant (complexity-wise, i'm sure you understand what I mean). – Fabinout – 2014-07-25T11:46:20.497
@MartinBüttner I have added a time constraint, which will weed out inefficient solutions, as well – mmk – 2014-07-25T11:54:45.417
@Fabinout currently both numbers are limited. (see my second comment). @ mmk: Okay, so here is a suggestion to specify this. Big-O notations are ordered as they would be if both N and K were replaced by a single variable M (this places all 5th order mixed polynomials worse than 4th order mixed polynomials). For ties, we look at the first N/K discrepancy from most to least significant factors, and the algorithm that has a K there is better than the one that has an N. – Martin Ender – 2014-07-25T11:54:45.627
@mmk What purpose does that serve in addition to the complexity? That's a bit like asking a code golf question, and saying "Your submission must not be longer than 1000 characters", which to me sounds like "You can't even participate if you're not good enough at this." – Martin Ender – 2014-07-25T11:56:04.177
@MartinBüttner, I have added that the time constraint is not 100% mandatory. Solutions can get a score of T which means over time. If NO solution can solve it under the time limit, then the most efficient T scored algorithm will win – mmk – 2014-07-25T13:07:26.877