题解:P14505 [NCPC 2025] km/h
Magnus2012 · · 题解
题目理解
这是一道适合初学者的模拟题,理解题目要求是写出正确代码的第一步。
题目的核心信息是:
- 你记不住国家限速,只知道它是一个整十的数字。
- 你会依次经过限速标志(数字)和解除限速标志。
- 对于每个标志,你都需要根据目前看到的所有标志,输出当前允许的最高合法车速。
关键信息
我们可以总结出以下关键点:
- 输入只有两种类型:数字(表示限速)和解除限速标志。
- 我们需要根据当前状态,动态计算并输出合法的最高速度。
思路
遇到解除限速标志时:
题目说明国家限速是一个整十的数字,并且高于所有出现过的限速。
因此,当我们遇到解除限速标志时:
我们需要找到一个高于历史最高限速的整十数。具体来说,就是找到历史最高速的下一个整十数。
举例说明:
输入:
速度变化:
可以看到,遇到解除限速标志时,速度恢复到了
总结:历史最高速的下一个整十限速。
当遇到数字限速标志:
当遇到数字时,处理比较简单:
- 更新当前速度状态。
- 同时更新历史最高速(如果当前数字更高的话)。
上代码
#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;
}
- 时间复杂度:
O(n) ,我们只需要遍历一次输入数据。对于这个题来说是相当有优势的了。