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.

输入
The first line will contain two integers K and N.
The next N lines each contain a single integer X.

输出
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.

样例输入
10 5
1
2
3
4
5

样例输出
6
5
5
4
4

思路
对于一次比赛,在限定最终速度为x的情况下,要跑出的最长距离需要在比赛中途达到最快速度并马上放缓速度,而此时跑完固定距离所需要的时间也是最短的,根据这种思路直接模拟即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2005;
const int M=10005;
const int INF=0x3f3f3f;
const ull sed=31;
const ll mod=1e9+7;
const double eps=1e-9;
typedef pair<int,int>P;
typedef pair<double,double>Pd;
 
int k,n;
 
int main()
{
    scanf("%d%d",&k,&n);
    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        int ld=0,rd=0,t=0;
        for(int v=1;v;v++)
        {
            ld+=v;
            t++;
            if(ld+rd>=k)
            {
                printf("%d\n",t);
                break;
            }
            if(v>=x)
            {
                rd+=v;
                t++;
                if(ld+rd>=k)
                {
                    printf("%d\n",t);
                    break;
                }
            }
        }
    }
    return 0;
}
发布了235 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43935894/article/details/104171998