Race【模拟】

题目描述:

在这里插入图片描述

思路:

问题的过程有两种情况。第一种,还未加速到限速就已到达终点。第二种,先加速,后减速到限速。所以分情况模拟即可。

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
const ll mod=1e9+10;
 
int main()
{
    int K,N;
    cin>>K>>N;
    while(N--)
    {
        int X;
        cin>>X;
        int add=0,reduce=0;
        int sp=1,ti=0;
        while(1)
        {
            add+=sp;
            ti++;
            if(add+reduce>=K)
            {
                cout<<ti<<endl;
                break;
            }
            if(sp>=X)
            {
                reduce+=sp;
                ti++;
                if(add+reduce>=K)
                {
                    cout<<ti<<endl;
                    break;
                }
            }
            sp++;
        }
    }
    return 0;
}
发布了40 篇原创文章 · 获赞 0 · 访问量 910

猜你喜欢

转载自blog.csdn.net/weixin_45619734/article/details/104782800