跳到正文

算法·理论

Atcoder Beginner Contest 453 solution

Dijkstra_zyl约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年4月11日
收录
2026年4月11日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

总结

这次 AB 题比较容易,C 题一直在拿循环做,结果是 dfs。。。

D 题有些思路但是时间不够。

总之这次不符合预期。。。加油!

学到的内容
  1. 当你不确定答案最优时,应当考虑搜索
  2. 审题
  3. 搜索题数据一般小于 2020
  4. 模拟题数据一般在 10001000 左右
  5. 有几点五这样数字的,可以都乘上 22 避免小数

A Trimo

Problem

给定一个字符串,输出去掉所有前导 o 后的结果。

Solve

考察:字符串处理 难度:入门

要注意的地方

还要输入一个 nn,代表字符串的长度。

我们只需要输入一个字符串 s,然后从头往后遍历找到第一个不是 o 的地方,然后输出后面的部分即可。

C++
#include <iostream>
#include <string>
using namespace std;
int main(){
    int n; cin >> n;
    string s; cin >> s;
    int i = 0;
    while(s[i] == 'o') i++;
    for(int j = i;j < s.size();j++) cout << s[j];
    return 0;
}

复杂度:O(n)O(n)

B Sensor Data Logging

Problem

在某次测量中,根据以下规则记录时间 0,1,…,T0,1,…,T 的传感器读数。

在时间 00 时,保存读数。 在时间 1,2,…,T1,2,…,T 时,如果且仅当当前读数与最近保存的读数之间的绝对差值至少为 XX 时,才保存读数。 时间 i=0,1,…,Ti=0,1,…,T 时的传感器读数为 AiA_i。

按时间升序输出保存读数的时间和保存值。

Solve

考察:模拟 难度:入门

注意的地方
  1. 共输入 T+1T+1 个数字,而不是 TT 个
  2. 从 00 开始

首先从 00 开始输入 AiA_i。 然后输出 0 a00\ a_0,换行,后面的循环从 11 开始。 用一个变量 lastlast 标记前一个记录的时间。 如果 ∣last−ai∣≥x|last - a_i| \ge x,则 last=ailast = a_i 否则 继续。

C++
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
int n,x,last;
int a[N];
int main(){
    cin >> n >> x;
    for(int i = 0;i <= n;i++) cin >> a[i];
    last = a[0];
    cout << "0 " << last << endl;
    for(int i = 1;i <= n;i++){
        if(abs(last-a[i]) >= x){
            cout << i << " " << a[i] << endl;
            last = a[i];
        }
    }return 0;
}

复杂度:O(n)O(n)

C Sneaking Glances(补题)

Problem

高桥在数线上的坐标 0.50.5 处。 他将走 NN 步。 在第 ii 步棋中,他选择了正方向或负方向,并在该方向上移动了 LiL_i 步。 他通过坐标 00 的最大次数是多少? 在此问题的限制条件下,没有一次移动会在坐标 00 处结束。

Solve

考察:搜索 难度:普及-

需要注意的地方
  1. 当你不确定答案最优时,应当使用搜索,我用循环交了 99 次,依然有 10×WA10 \times WA
  2. 注意精度及类型

我们观察样例可以猜测,当当前位置在 00 以右的时候,应当往负方向走,反之往正方向走,然后注意一下类型应是 doubledouble,于是乎:

5WA 代码
C++
#include <iostream>
using namespace std;
int n,ans;
double now = 0.5;
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        int a; cin >> a;
        if(now > 0){
            now = now - a;
            if(now < 0) ans++;//说明经过了原点
        }else if(now < 0){
            now = now + a;
            if(now > 0) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

虽然这个代码错了,但是也为我提供了一个思路,在我补题后我发现了更合理的思路。

在我提交的时候我就感觉:“这一定对吗?”

没法说明一定,所以我们应该搜索每一种情况,然后取最大值,要是用搜索(另外 n≤20n \le 20)。

首先看 mainmain 里的:

C++
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    ...
    cout << ans << endl;
    return 0;
}

然后是变量定义:

C++
const int N = 30;
int n,a[N];//因为保证输入都是整数
int ans;

最后考虑 dfsdfs:

构建过程

首先我们可以传参为 (当前位置,经过次数)(当前位置,经过次数)。

所以在 mainmain 里调用的是 dfs(0.5,1),而传参应是 dfs(double now,int cnt)。

然后思考结束条件,应该是遍历完 NN 步退出,所以还需要一个参数标记当前步数:dfs(double now,int cnt,int id),调用应为 dfs(0.5,1,1),表示当前步数。

所以终止条件就是:

C++
if(id > n){//当前步数大于 n
    ans = max(ans,cnt);//取最大值
    return;//退出
}

然后选择无外乎两种:往正方向走和往负方向走。

然后利用原代码的思路写出 dfsdfs 为:

C++
void dfs(double now,int cnt,int id){
    if(id > n){//当前步数大于 n
        ans = max(ans,cnt);//取最大值
        return;//退出
    }
    //向正方向移动
    double nw = now + a[id];
    if(nw >= 0 && now <= 0) dfs(nw,cnt+1,id+1);
    else dfs(nw,cnt,id+1);
    //向负方向移动
    nw = now - a[id];
    if(nw <= 0 && now >= 0) dfs(nw,cnt+1,id+1);
    else dfs(nw,cnt,id+1);
}

于是整合一下:

C++
//待测评(AT 人机验证丝毫不逊于 CF)
#include <iostream>
using namespace std;
const int N = 30;
int n,a[N],ans;
void dfs(double now,int cnt,int id){
    if(id > n){//当前步数大于 n
        ans = max(ans,cnt);//取最大值
        return;//退出
    }
    //向正方向移动
    double nw = now + a[id];
    if(nw >= 0 && now <= 0) dfs(nw,cnt+1,id+1);
    else dfs(nw,cnt,id+1);
    //向负方向移动
    nw = now - a[id];
    if(nw <= 0 && now >= 0) dfs(nw,cnt+1,id+1);
    else dfs(nw,cnt,id+1);
}
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    dfs(0.5,0,1);
    cout << ans << endl;
    return 0;
}

当然也可以将每个长度都乘 22 来避免小数。而我只想到加 0.5

后记

其实比赛应该在一个不太紧张的环境打,像我每次打 ABC 都只有 20min,而我的目标是 1025pts。

原文