Motorbike RacingProblem code: M2 |
All submissions for this problem are available.
It's time for the annual exciting Motorbike Race in Byteland.
There are N motorcyclists taking part in the competition. Johnny is watching the race. At the present moment (time 0), Johnny has taken note of the current velocity and position of each motorcyclist.
Johnny wants to know at a given point of time, which motorcyclist is in a specific place in the rank list. Please help him!
If at any given time two motorcyclists are in same position, the motorcyclist with the smaller index will be placed before the one with the larger index.
To make the problem simple, Johnny assumes that each motorcyclist is moving at a constant velocity.
Input
The first line contains a number t (about 10) which is the number of test cases. Then t test cases follow. Each test case has the following form.
The first line of the test case contains a number N (1 <= N <= 20000), the number of motorcyclists.
The i-th line in the next N lines contains two numbers, v and x, which are the velocity and the current position of the i-th motorcyclist (1 <= v, x <= 100,000).
The next line contains a number Q (1 <= Q <= 20000), the number of time queries.
Each line in the next Q lines contains two numbers, t (1 <= t <= 1,000,000,000) and k (1 <= k <= n), representing the query: "at time t, which motorcyclist is positioned k-th in the rank list?"
Output
For each test case, print Q lines, with each line containing the index of the motorcyclist for the corresponding query.
Remember to print a new line after each test case.
Example
Input: 1 4 2 100 3 50 4 60 5 1 4 1 1 50 2 60 4 100 1 Output: 1 4 1 4
| Date: | 2010-01-15 |
| Time limit: | 2.5s |
| Source limit: | 50000 |
| Languages: | C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS |
Comments

Fetching successful submissions

i didn't get the difference
i didn't get the difference between index number and ranking list.
Initially the motorcyclist is arranged through index wise or ranking wise ??
i don't understand how to
i don't understand how to resolve, when 2 r at same position
If two are the same position,
If two are the same position, the one that comes first in the input is earlier in the rank list.
example shows 4 motorcyclists
example shows 4 motorcyclists but ranks are given as 2 3 4 5 .
why not as 1 2 3 4?
i guess index no is the order
i guess
index no is the order in which motorcyclists are given in the input
and rank is calculated with respect to their position ie higher the position lower(numerically) the rank and when positions are same lower the index lower(numerically) is the rank.
current position means
current position means ranking or coordinate of the biker
@ Dinesh Salve 2,3,4,5 are
@ Dinesh Salve
2,3,4,5 are the speeds not the ranks
@ankit wat is meaning of
@ankit
wat is meaning of current position ith m.cyclist .
2 100
hw is this for 4 mcyclists?
current position can be said
what is current position, can
what is current position, can somoone help me.
for velocity and position,
for velocity and position, are they real numbers or integers?
Submissions aren't appearing
Submissions aren't appearing in recent activity... anyone else submitted yet?
@shashi..same here..
@shashi..same here..
same here!!
same here!!
can somebody explain the
can somebody explain the condition when both the motor cyclist are at same position........... what is meant by the word "Placed"..... does it mean ranked ?????
Is there any limit on max
Is there any limit on max value of v? the conditions says v >=1 but no upper limit?
@pawan When v, x appears in
@pawan When v, x appears in the middle of a bound like that, it means that the bounds apply to both v and x, i.e. 1 <= v <= 100,000 and 1 <= x <= 100,000.
I have some questions
I have some questions regarding the input:
1) "The i-th line in the next N lines contains two numbers, v and x, which are the velocity and the current position of the i-th motorcyclist (1 <= v, x <= 100,000)."
The inputs given in the test case are :
2 100
3 50
4 60
5 1
Is the first number x and the second number v? This would be contrary to the statement mentioned above.
2) If there are only 4 motorcyclists, why is no one placed first and why is at least one placed fifth?
3) Does position mean rank of the motorcyclist or does it refer to distance/displacement form the starting line ?
It says it will be v followed
It says it will be v followed by x. Therefore it will be v followed by x.
The person at position 100 is in first place. Nobody is in 5th place.
Position means position, not rank.
@Abhijit In this case, if you
@Abhijit In this case, if you swap v and x, the output changes. If your output does not match the sample output, you are doing something wrong! Why would you think the first number was x anyway?
Can we get Mustafa and
Can we get Mustafa and Shashi's questions answered (particularly Mustafa's question about whether the inputs are integers or real numbers)? I'm not seeing any rankings, recent activity or successful submissions in the sidebar, but I am seeing updates in the problem list on the main contest page.
can you give some idea of how
can you give some idea of how many calculations the judge performs per second?
encountered a problem for the
encountered a problem for the first time........
i am using gcc of ubuntu..... have written a code for the above problem..... in order to troubleshoot my code i wrote some printf statements to see the control flow..... now i am getting the output but there is this mysterious printf("n") statement which i inserted to have line gap between the output.... now whenever i comment out this statement, the program crashes and it shows segmentation fault..... and if i don't there is unwanted gap between the given input and produced output, which is what i suspect is the reason of getting a WA every time i submit the code......
Can anybody suggest something why commenting a printf("n") is causing program to fail.........
@admins I have a question. If
@admins
I have a question.
If the submission gives error "Time Limit Exceeded".
Then is it confirm that it is giving correct output for all the test cases but only the time limit is exceeding.
@Rahul No.
@Rahul No.
@Stephen Thanks for the
ADMIN, Solutions in C#2 are
@Rahul The FAQ says that the
@Rahul The FAQ says that the answer is "no", as confirmed by Aniruddha above. I believe the way it works is, once the time limit is up, the judge stops running your program.
with 20000 motorcyclists ,it
with 20000 motorcyclists ,it sure will be one hell of a race.
Hi Admin, will the value of
Hi Admin,
will the value of time t in the query be unique?
@legilimen There is nothing
@legilimen There is nothing to suggest that the time values will be unique. I think we should assume that they may or may not be unique.
Hi Admin, I am getting a
Hi Admin, I am getting a "time limit exceeded error" ( using C ).
The FAQ section doesnot mention in detail about it. I feel my code is optimised and works well for large input sizes efficiently.
What is the time limit for C ?
C#2 solutions are now
C#2 solutions are now accepted.
are the time queries in
are the time queries in ascending order or any order?
I have tried several
I have tried several techniques for this problem and all of them give time limit exceeds !!
I even tried an empty loop just read the input and write -1 as output and also it gives time limit exceeds !!!!!
i solved motorbike race
i solved motorbike race problem using the minimum data.but igot run time error. what is the problem???
I am getting a time limit
I am getting a time limit exceeded on my solutions in Ruby.. Shame because I'm pretty sure what I'm doing is correct. Is the time appropriately adjusted for the Ruby language?
@Admin c#2 is working now..
@Admin
c#2 is working now.. however my doubt is still not clear.. Does Visual C# 2008 Express Edition come under C# or C#2 ?? Kindly Reply.. Thanks..
I am getting a run time
I am getting a run time error. I have written my code in Python. And its working fine on my system. Strange. I dont really see an error in my code
How can i get input? i am
How can i get input? i am using php.. and assigning all values using randomn values (as per the given scope)... Is it ok? .. If the N & Q get max value on randomn, it takes more time.
Whats the memory limit on
Whats the memory limit on this one? I think I am running out of memory. My code looks error free but it gives runtime error. I am consuming 4.5M on each run. Please help
@Sandip The FAQ says that 64
@Sandip The FAQ says that 64 MB is guaranteed, so unless you are allocating a huge amount of memory in a single operation, your idea is probably wrong.
@ balavignesh - read the FAQ.
@ balavignesh - read the FAQ.
Admin kindly clarify whether
Admin kindly clarify whether the values of V and X are integers or numbers.. thanks..
Even after so much optimizing
Even after so much optimizing Time Exceeded error.. :(
Any chance of there being given an extension?? :)
Also recent activity thingy isnt working...
What recent activity are you
What recent activity are you talking about? click the all submissions button on the top of the page. The sidebar shows the successful submissions.
oh yes.. sorry.. there are 0
oh yes.. sorry.. there are 0 successful submissions for this question hence the "no recent activity".
Sorry..
I am getting wrong answer as
I am getting wrong answer as a result. I dont know what is wrong in my logic. Following are my inputs and outputs.. Please someone tell me, whether my input are correct.. (also the output..)
input
2
4
2 100
3 50
4 60
5 1
4
1 1
50 2
60 4
100 1
5
6 34
7 5
5 1
3 10
2 23
2
50 1
500 1
output
1
4
1
4
2
2
I solved the problem but
I solved the problem but getting time exceeded. My logic is really simple and it wroks fine. Will the judge increase the time limit for this program ???
I am getting the output on
I am getting the output on my system.But here it shows run-time error(sigsegv).I am using double-dimensional array in my program.Does it any how effect the program.thnx in advance
@balavignesh Anyone
@balavignesh Anyone discussing test cases here risks disqualification, as this is a contest. There have been numerous comments at Soccer League about this.
@admin Ahmed said above that
@admin Ahmed said above that they tried submitting a program that just reads the input and it still gives Time Limit Exceeded! I also notice that there have not been any successful submissions yet. Can you confirm that the time limit is correct?
@
@ admin
since1<=x<=100000
so it means that at 100000 the race ends?
so after that riders will stop instead of going farther
@ adminsince1<=x<=100000so it
@ admin
since1<=x<=100000
so it means that at 100000 the race ends?
so after that riders will stop instead of going farther
Please read the problem
Please read the problem statement. Those are the constraints for the value of 'x'.
@Aashish The important point
@Aashish The important point is the definition of x. This variable is carefully defined in the problem statement.
Does judge's compiler use
Does judge's compiler use optimization flags?
@admin :I am getting the
@admin :I am getting the output on my system.But here it shows run-time error(sigsegv).PLS HELP
The wording is very
The wording is very confusing.
Velocity, "distance traveled" and "position" would be better. Position is too ambiguous.
Is it possible to access the
Is it possible to access the ONLINE_JUDGE property from Python? I tried accessing it as an environment variable using os.getenv(), but no joy.
@ admin please clarify
@ admin please clarify whether the input velocity will be given in sorted manner or not????
and also about the time for which we have to find the rank will be given in sorted manner??????
It clearly doesn't say they
It clearly doesn't say they will be sorted, so you have no reason to believe so.
The time limit on this
The time limit on this problem is pretty hard. I doubt whether anyone can solve 10 test cases of 20000 queries of 20000 racers within 2.5 seconds.
My solution took 25 seconds for 1 test case of 20000 racers with 1000 queries.
And I can't seem to improve it other than optimising the data storage.
":( internal error occurred
I brought the time down to 17
I brought the time down to 17 seconds by using a dynamic new techinue to store and process data. But to no avail.... :(
Even my quantum
Even my quantum computing solution got Time Limit Exceeded... :)
Hey guys.. I have a doubt..
Hey guys.. I have a doubt.. The position 'x' was noted for all bikes at time0 right. (i.e.) Position = velocity*time0.
But if you go by the above formula and calculate time0, it is different for each bike. How?
Is there a flaw in the question?
For example
x is the position at time 0.
x is the position at time 0. So for v=2 and x=100, at time 0 the bike is at 100, at time 1 the bike is at 102, and so on. I don't know where you get 50 from.
Yes, But the time0 should be
Yes, But the time0 should be unique for all bikes..
time is essentialy distance/velocity..
so if u put this formula for each bike in the testcase u should get the same value..
which is not the case right now isn't it.. Or have a missed something here
@Srinivasan, @Stephen It
@Srinivasan, @Stephen It seems like Srinivasan thinks that there is a variable called "time0" and the positions for each bike was noted at time = time0, where position = 0 at time = 0. If that were the case, then Srinivasan's calculations would make sense. In fact, the position for each bike was noted at time = 0.
How come CodeChef's site keep
How come CodeChef's site keep showing this message when I open the contest page :
warning: ksort() expects parameter 1 to be array, null given in /var/www/codechef/sites/all/modules/contest_page/contest_page.module on line 279.
Well... 0.15 sec for 2.9 Mb
Well... 0.15 sec for 2.9 Mb mem. So it's possible.
hi, all the number are
hi, all the number are integer?
is v and x are
is v and x are integers????????????
m newbie hereits my first
m newbie here
its my first time
can u please tell me how to take input
whether as .txt file or somhw else???
I am starting to give up on
I am starting to give up on this. I dont know where I am going wrong but the following solution runs perfectly fine for any imaginable test case possible on mine but gives wrong answer here. Please show me some ight and tell me is it because of the compiler difference because I am working on Turbo c and here it is gcc or something wrong in my code
Here's my soln : http://www.codechef.com/viewsolution/185888
Just tell me this: Are all
Just tell me this: Are all the x and v integers or should I expect floating point? I dont see anything in the question that suggests that they should be integers. I wrote my code for integers and its giving me a runtime error which I think is because a floating point value is being sent as input. Please clarify
I agree this really should
I agree this really should have been clarified a long time ago. I'm guessing they are always integers.
Admin, Why cant problem
Admin, Why cant problem statement be more clear. It always has some issues. We all know that if v and x are real numbers , that would have been mentioned. So I find they are integers as nothing is mentioned.
Why cant this datatype information be specified explicitly. There is fun involved in guessing corner test cases and its a part of the competition , but there is no fun in finding the datatype. Why do you guys still dont explicitly make somethings clear. Everybody who have seen this problem must have got this doubt once. It made me write some sample program to really find the datatype. This is unnecessary.
What does it take to mention
What does it take to mention that it is integer. May be I should not ask this question , as efforts were not taken to assert the problem statement, test cases and even timings for all problems. Why should you worry about explicitly specifying the datatype :)
Can somebody please put me
Can somebody please put me out of my misery and give me some hint as to the approach? Can't think of anything better than O(N) per query (quickselect).
My approach was pretty messy,
My approach was pretty messy, but the basic idea was to partition the competitors into m groups, where you can pick m as you like. Inside each group we keep track of a sorted list of these competitors according to their rank within this group as the race proceeds. This is done by precomputing all events inside a group (i.e. the times when two riders change rank). When we come to a query at time t, we first process all events that have happened since the time of the last query (queries are processed in order of time), so that the sorted list of ranks inside each group is correct.
Given a query, the problem is now to select the k'th overall smallest (or largest) from these sorted lists. This has been solved by Frederickson and Johnson in 1982 in their paper The Complexity of Selection and Ranking in X+Y and Matrices with Sorted Columns in time O(m + plog(k/p)) with p = min(k,m) per query. I didn't implement this approach, since I found it to be a bit too complicated. Instead I answered queries using something very similar to quickselect. Pick a random element from these lists, and compute using a binary search in each list the rank of the element we picked. From this we can decide exactly as in quickselect which part to recurse in. Once we are down to only a few competitors left as candidates for the k'th element, just do a standard quickselect on these.
Great win, congratulations
Great win, congratulations Mark!
These were good problems.
@Mark Greve: That was
@Mark Greve: That was interesting approach.Better than finding product of time velocity of order 1000000000.
I would really like to know what other approaches were used.
Thanks Tomek! I had the
Thanks Tomek! I had the network for n = 20 with 93 gates ready for a few days. I knew this would improve my score by maybe 3-5 points, but I didn't want to submit it until the very last moment. When you hit 149, I knew I could beat it, but when you got 146.9 I wasn't sure at all. So with 20 minutes to go and my first submission using this network not beating your score, I was a bit disappointed. Luckily the optimization I had just tried out shortly before, which didn't give anything turned out to work very well with the new network for n = 20...
//{{{ #include
My solution is actually quite
My solution is actually quite similar to Mark's, but instead of keeping each individual group sorted, I keep the groups themselves in order (that is, every motorcycle in group i will be ahead of every motorcycle in group j if i<j). Then for each query I can find the right group with binary search, then use nth_element to find the right motorcycle within the group. Unfortunately, it's possible for every motorcycle to end up in the same group, but I got lucky and there weren't any test cases like this (I was quite surprised when my submission passed).
Ha, I was thinking you must
Ha, I was thinking you must have found a 92 comparators network. I also had a 93-gate network for n=20, but apparently wasn't optimizing it nearly that well.
I have to admit that my 0.15
I have to admit that my 0.15 solution could have probably been defeated with more difficult test cases - it's O(N^2 + Q log Q) in the worst case. I generate all pairwise position swaps after t=50, by looking at all pairs of bikers with the difference in velocities smaller than 2000. Apparently there weren't that many pairs of bikers with velocities that close.
Thanks guys. I suspected it
Thanks guys. I suspected it would be something along the lines of grouping, but was always put off by easy worst cases.
I would be rather disappointed if the official solution had a worst-case quadratic running time.
Can somebody please post all
Can somebody please post all the test cases used while evaluating the answers?? It will really help me to know what did I miss in this problem with my approach..