跳到正文

题解

题解:P14505 [NCPC 2025] km/h

Magnus2012约 3 分钟
原文

这是 2025年11月13日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P14505

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

理解题面

这是道初学者基础题,首先了解题目要干什么,才能写出能过的代码,我们读题(关键区已粗体):

你完全记不住国家限速,只知道它是一个整十的数字。

现在,当你依次经过每一个标志——无论是限速还是解除限速——你需要仅根据目前为止看到的标志,判断自己能够合法行驶的最高车速。

解析信息

从题面可以得出的信息有

  • 我们只需要判断两种状态 第一种 数字 第二种 /也就是解除限速
  • 判断当前的最大合法速度,我需要分类讨论

思路

当时是l时

从题面得知 你完全记不住国家限速,只知道它是一个整十的数字。 从这里我们总结出第一种状态也就是解除限速时,你只需寻找一个输入过最大的数最近的整十数字

我们拿样举例子 输入n=3 l1l_1=90 l2l_2=85 l3l_3=/ 的时最大时速90->85->100 不难发现当lil_i=/的时候你只需要找出ll里面最大的速度找出他最近的整十数字就行了

得出结论找现有最大时速最近的整十数字

当是数字时

你只需要更新最大合法时速的状态就行了


接下来上代码,码风良好。

C++
#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,max=1,now_max=1;
    cin>>n;
    
    while (n--)
    {
        string value;
        cin>>value;
        if(value=="/"){
            ll need=get(now_max);
            cout<<need<<'\n';
        }else{
            if(1LL*stoi(value)>=now_max){
                now_max=stoi(value)*1LL;
            }
            max=stoi(value)*1LL;
            cout<<max<<'\n';
        }
    }
    
    
    return 0;
}
提交你就会发现

你AC了