排位赛3-Race
题目
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.
题意
bessie在进行一场k米长的赛跑,她一开始速度为1m/s,它可以自由提速或降速1m/s,在冲线时她希望用了整数秒并且冲线的速度不超过xm/s,问最短用时,有多组数据。
解法
其实这道题不难,模拟即可,用时最短当然时一直提速当提速超过x时,就开始对称(画个v-s图就知道了)从终点开始同步速度,直到总路程超过k就完成了。
代码:
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
int L,n,x,nl,temp,v,ans;
int main()
{
scanf("%d%d",&L,&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&x);
nl=0;
temp=0;
v=1;
ans=0;
while (1)
{
nl+=v;
ans++;
if (nl+temp>=L)
{
break;
}
if (v>=x)
{
temp+=v;
ans++;
if (nl+temp>=L)
{
break;
}
}
v++;
}
printf("%d\n",ans);
}
return 0;
}