D. Race

GDUT 2020寒假训练 排位赛三 D

原题链接

  • D. Race

题目

D. Race_第1张图片
outputstandard output
Bessie is running a race of length K (1≤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 K meters, she wants the speed she has just been traveling to be no more than X (1≤X≤105) meters per second. Bessie wants to know how quickly she can finish the race for N (1≤N≤1000) different values of X.

Input
The first line will contain two integers K and N.

The next N lines each contain a single integer X.

Output
Output N lines, each containing a single integer for the minimum time Bessie needs to run K meters so that she finishes with a speed less than or equal to X.

样例

input

10 5
1
2
3
4
5

output
6
5
5
4
4

题目大意

有一场k米赛跑,Bessie希望在跑过终点线的时候的速度为x,而每一秒才能增长或减少一个单位的速度,求跑完k米需要的最快时间。

思路

找规律
按照样例模拟一下画出v-t图像,可以看出,vt图像基本处于一个对称的状态,也就是说假设最大的速度为vmax,在跑步的过程中,速度从1增长到vmax再降到目标速度x,将该过程分为上升和下降两个阶段,我们只需要模拟跑步的前半部分的速度增加的过程即可。
速度从1开始增加,每一秒就提高一个单位的速度,并将该速度所跑过的路程加入到左半段的路程中;当速度增大到x后,若速度继续增加就应要考虑后半段减速的事情了,也就是在我们模拟到vmax后k米赛跑必然还要有从vmax减速到x的过程,那么从这时开始就应该将当前的速度所跑过的路程加入到后半段中。
那么达到vmax的条件就是当前的左半段路程与右半段路程和大于等于k,也就是说明已经达到了终点,跳出循环即可。

代码

#include
#include
#include
using namespace std;
int main()
{
	int k,n;
	cin>>k>>n;
	while(n--)
	{
		int x;
		cin>>x;
		int l,r,t=0;
		l=r=0;
		for(int i=1;i;i++)
		{
			
			if(l+r>=k)
			{
				break;
			}
			l+=i;
			t++;
			if(l+r>=k)
			{
				break;
			}
			if(i>=x)
			{
				r+=i;
				t++;
			}
		}
		cout<<t<<endl;
	}
	return 0;
}

你可能感兴趣的:(排位赛)