Bessie is running a race of length KK (1≤K≤1091≤K≤109) meters. She starts running at a speed of 0 meters per second. In a given second, she can either increase her speed by 1 meter per second, keep it unchanged, or decrease it by 1 meter per second. For example, in the first second, she can increase her speed to 1 meter per second and run 1 meter, or keep it at 0 meters per second and run 0 meters. Bessie's speed can never drop below zero.
Bessie will always run toward the finish line, and she wants to finish after an integer amount of seconds (ending either at or past the goal line at this integer point in time). Furthermore, she doesn’t want to be running too quickly at the finish line: at the instant in time when Bessie finishes running KK meters, she wants the speed she has just been traveling to be no more than XX (1≤X≤1051≤X≤105) meters per second. Bessie wants to know how quickly she can finish the race for NN (1≤N≤10001≤N≤1000) different values of XX.
Test cases 2-4 satisfy N=X=1.N=X=1.
Test cases 5-10 satisfy no additional constraints.
INPUT FORMAT (file race.in):
The first line will contain two integers KK and NN.The next NN lines each contain a single integer XX.
Output NN lines, each containing a single integer for the minimum time Bessie needs to run KK meters so that she finishes with a speed less than or equal to XX.
10 5 1 2 3 4 5
6 5 5 4 4
When X=1X=1, an optimal solution is:
When X=3X=3, an optimal solution is:
Note that the following is illegal when X=3X=3:
This is because at the instant when Bessie has finished running 10 meters, her speed is 4 m/s.
Problem credits: Nick Wu
[/hide]
(Analysis by Nick Wu)
Instead of trying to think about the problem in terms of minimizing the amount of time needed to accomplish a certain distance, we can flip the problem around - if Bessie can run for TT seconds and she wants to be running no more than XX meters per second at the end of the TT seconds, what is the furthest distance she can run?
Intuitively, we want her speed to be as high as possible throughout her run. If there were no speed cap at the end, Bessie would consistently increase her speed every second. Because of the presence of the speed cap though, Bessie may need to switch from speeding up to slowing down in order to meet the requirement of traveling no more than XX meters per second at the end.
As a result, for a given speed VV, Bessie will be traveling at that speed for at most 2 seconds - 1 second when she is speeding up, and one second when she is slowing down. We can therefore simulate Bessie's fastest possible run subject to her starting at 0 meters per second and ending with speed no more than XX meters per second as follows - we will track Bessie's distance traveled while she is speeding up and while she is slowing down. We will increment Bessie's speed starting at 1 meter per second until she has traveled enough distance to finish the race. Increment Bessie's distance covered while speeding up by this speed, and check if Bessie's total distance traveled exceeds KK meters. If the distance has not been exceeded, and Bessie could travel at this speed while slowing down, then increment Bessie's distance covered while slowing down by this speed, and perform the total distance check again.
The moment in time when Bessie's theoretical maximum distance traveled exceeds KK meters is the desired answer. It is worth noting that following this specific strategy of speeding up and slowing down may not actually meet the race conditions properly, but it is always possible to construct a strategy that covers exactly the given distance in the asserted time.
There is one final concern - is simulating this one second at a time fast enough? The worst possible case here is where Bessie needs to run 109109 meters and she must end the race running at 1 meter per second. In this case, it takes 63245 seconds. Performing one thousand of these simulations should therefore run in time comfortably.
#include <stdio.h> int solve(int dist) { int minspeed; scanf("%d", &minspeed); int lhstravel = 0; int rhstravel = 0; int timeused = 0; for(int currspeed = 1;; currspeed++) { lhstravel += currspeed; timeused++; if(lhstravel + rhstravel >= dist) return timeused; if(currspeed >= minspeed) { rhstravel += currspeed; timeused++; if(lhstravel + rhstravel >= dist) return timeused; } } } int main() { freopen("race.in", "r", stdin); freopen("race.out", "w", stdout); int k, n; scanf("%d %d", &k, &n); for(int i = 0; i < n; i++) { printf("%d\n", solve(k)); } }
[/hide]
翰林课程体验,退费流程快速投诉邮箱: yuxi@linstitute.net 沪ICP备2023009024号-1