题目理解
这是一道适合初学者的模拟题。理解题目要求是写出正确代码的第一步。
题目的核心信息是:
- 你记不住国家限速,只知道它是一个 整十的数字。
- 你会依次经过限速标志(数字)和解除限速标志
/。 - 对于每个标志,你都需要根据 目前看到的所有标志,输出当前允许的 最高合法车速。
关键信息
我们可以总结出以下关键点:
- 输入只有两种类型:数字(表示限速)和
/(表示解除上一次限速)。 - 我们需要根据当前状态,动态计算并输出合法的最高速度。
思路
遇到解除限速标志 /
题目说明国家限速是一个 整十的数字,并且 高于所有出现过的限速。
因此,当我们遇到 / 时:
我们需要找到一个 高于历史最高限速的整十数。具体来说,就是找到 历史最高速的下一个整十数。
举例说明: 输入: / 速度变化: → →
可以看到,遇到 / 时,速度恢复到了 ,这是高于历史最高速 的最小整十数。
总结:历史最高速的下一个整十限速
当遇到数字限速标志
当遇到数字时,处理比较简单:
- 更新当前速度状态。
- 同时更新历史最高速(如果当前数字更高的话)。
上代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <string>
#include <bitset>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 998244353;
const int MOD1 = 1e9 + 7;
const double EPS = 1e-8;
void fastOI(){
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
}
ll get(ull num) {
return ((num + 10) / 10) * 10;
}
int main() {
fastOI();
ll n,now_max=1,max=1;
cin>>n;
while (n--)
{
string value;
cin>>value;
if(value=="/"){
ll need=get(max);
cout<<need<<'\n';
}else{
if(1LL*stoi(value)>=max){
max=stoi(value)*1LL;
}
now_max=stoi(value)*1LL;
cout<<now_max<<'\n';
}
}
return 0;
}- 时间复杂度:,我们只需要遍历一次输入数据。
- 空间复杂度:,只使用了常数级别的额外空间。
AI 使用说明
本规范在写作完成后使用 DeepSeek 进行了润色。