Robot Energy Source Order
InterviewTime limit0.2sMemory limit128 MB
Reorder n energy sources, each with acceleration a_i and duration s_i, to maximize total distance, and print the gain over the given order.
- Level
Medium5 of 10
- Topics
- Sorting, Greedy, Math, Prefix sum
- Solved
- No attempts yet
Problem
You design a robot that explores places people cannot reach, and you want it to travel as far as possible. You have energy sources. Source accelerates the robot at and keeps doing so for seconds. The robot starts at rest, so its initial velocity is 0.
Once you pick a source you use it for all of its seconds, then switch straight to a source you have not used yet. The switch takes no time, and each source can be used only once. You choose the order of the sources so that the distance traveled is as large as possible.
Given the acceleration and the duration of every source, write a program that computes the distance traveled under the best order minus the distance traveled under the order given in the input.
Physics background: if the velocity is right before you start a source whose acceleration is , then after seconds the robot has traveled more meters and its velocity is .
Input
The first line contains the number of energy sources (). Each of the next lines contains the acceleration and the duration of one source, separated by a space. Both values are positive integers no greater than .
Output
Print the distance traveled under the best order minus the distance traveled under the input order, with exactly one digit after the decimal point. The difference is always an integer, so that digit is always 0.