So I have been doing some coding questions to practice my coding and I ran into this problem: http://www.usaco.org/index.php?page=viewproblem2&cpid=989
The general gist of the problem is that there is a person named Bessie is racing a length of $K\ge1$ meters. She starts at $0$ meters per second at the $0$ second mark, and then every second after that, she can either increase her speed by $1$ meter per second, stay at the same speed, or decrease her speed by $1$ meters per second. Her speed can not drop below zero(so if Bessie is at $0$ meters per second, she can not decrease her speed.).
Bessie wants to finish the race in an integral amount of seconds, either finishing at the $K$ meter mark or past the $K$ meter mark. But Bessie also doesn't want to finish the race too quick. At the instant when she finishes $K$ meters, she wants to be traveling at a speed of no more than $X\ge1$ meters per second. Bessie wants to know the minimum speed required to finish $K$ meters, given $K$ and $X$.
The logic I use to solve this problem only works for the first 4 test cases, and I'm sure it's not because of an coding error. So my logic is as follows:
Before we do anything, we first have to test whether or not a speed of $X$ meters per second can be reached, as the following solution assumes that $X$ meters per second is reachable.
To do so, we first note that the quickest way to get to $X$ meters per second is to increase the speed by $1$ each second for $X$ seconds. We then note that if after increasing $X-1$ times, if the distance covered is $<K$ meters, then it is guaranteed that $X$ meters per second is reachable. But if after increasing $X-1$ times the distance covered is $\ge K$, then we know that $X$ meters per second is unobtainable.
To calculate the distance covered after $X-1$ increases, we can calculate the following sum:$$1+2+\cdots+(X-2)+(X-1)$$which can be represented as$$\frac{X(X-1)}2$$. We want to test whether or not$$\frac{X(X-1)}2\ge K$$. If this inequality is false, then go to the solution under the gray line. If this inequality is true, then we know $X$ meters per second is unobtainable, and therefore we need to calculate how many increases are required to surpass $K$ meters. We will call this amount $n$. To find the value of $n$ that will cause the distance to go over $K$ meters, we first need to find the formula of the distance covered after $n$ increases. That can be represented with the sum $$1+2+3+\cdots+n=\frac{n(n+1)}2$$. So then we set this sum to be $<K$, then use this inequality to maximize $n$:$$\frac{n(n+1)}2<K\\\frac{n^2+n}2<K\\n^2+n<2K\\(n+1/2)^2-1/4<2K\\n+1/2<\sqrt{2K+1/4}\\n<\frac{\sqrt{8K+1}-1}2$$So the value of $n$ would be:$$n=\left\lceil\frac{\sqrt{8K+1}-1}2\right\rceil$$(without the ceiling function we would be calculating the largest amount of increases that doesn't surpass $K$ meters, instead of actually passing $K$ meters)
First we want to find the maximum speed in which Bessie can go at. Let's say that this maximum speed is $m$ and the target speed(the speed we want to have at $K$ meters) be $X$ meters per second(as stated in the problem). We can find the maximum speed by allowing Bessie to increase its speed every second until it reaches $m$, then immediately start decreasing her speed until she hits $X$ meters per second. We then know that the total distance traveled after this increase and decrease is(which I will denote as $d$): $$d=\underbrace{1+2+3+\cdots+m}_{\text{increasing speed}}+\underbrace{(m-1)+(m-2)+\cdots+(X+1)+X}_{\text{decreasing speed}}$$. We can then find the formula for this sum to be: $$d=m^2-\frac{X(X-1)}2$$. This sum has to be $\le K$(or else we can't decrease enough in time), so we have the following inequality:$$m^2-\frac{X(X-1)}2\le K$$. $K$ and $X$ are already given as inputs in the problem, so we just have to isolate $m$. We get that: $$m\le \sqrt{K+\frac{X(X-1)}2}$$(positive square root). To get the highest $m$, we just need to take the floor of the RHS so:$$m=\left\lfloor\sqrt{K+\frac{X(X-1)}2}\right\rfloor$$. Then if $d$ is $<K$, we need to find out the remaining distance that we need to cover. That is easy to calculate: $K-d$. From this we can calculate how many seconds we need to stay at $m$ meters per second(if we stay at a speed $<m$, we can always stay at a higher speed to potentially reach $K$ meters quicker. Not too sure about this logic though). Each second we stay at $m$ meters per second adds an extra $m$ meters to our distance. So we need to divide $K-d$ by $m$ to see how many times we need to add $m$ to $d$ to reach $K$(I will denote this as $s$). So we get that we need to stay$$s=\left\lceil\frac{K-d}m\right\rceil$$seconds at $m$ meters per second to pass $K$ meters. Then we need to calculate the amount of seconds that passed for traveling $d$ meters using the method stated. To calculate this, we need to count how many terms we added together in the sum. So we need to find the length of this list:$$1,2,3\dots,m,(m-1),(m-2),\cdots,(X-1),X$$This can be calculated with the following formula: $$2m-X$$So finally we calculate $$2m-X+s$$ to obtain the final answer.
The problem is this only works for the first 4 test cases, and this strategy only works for certain values of $K$ and $X$ past test case 4, and is really close to the actual answer for other values(yes, I downloaded the test data), so I'm actually not too sure where I went wrong here. If you guys want the code I can also put it here, but this is more of a math problem, so I decided not to put the code for now.