D. Race

GDUT 2020寒假训练 排位赛三 D

原题链接

题目

原题截图
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<iostream>
#include<cstdio>
#include<cmath>
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值