Atcoder Beginner Contest 453 solution

· · 算法·理论

总结

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

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

总之这次不符合预期。。。加油! ::::success[学到的内容]

  1. 当你不确定答案最优时,应当考虑搜索
  2. 审题
  3. 搜索题数据一般小于 20
  4. 模拟题数据一般在 1000 左右
  5. 有几点五这样数字的,可以都乘上 2 避免小数
    # A Trimo
    ## Problem
    给定一个字符串,输出去掉所有前导 o 后的结果。
    ## Solve
    考察:字符串处理
    难度:入门

::::warning[要注意的地方] 还要输入一个 n,代表字符串的长度。 ::::

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

#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)

B Sensor Data Logging

Problem

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

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

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

Solve

考察:模拟
难度:入门

::::warning[注意的地方]

  1. 共输入 T+1 个数字,而不是 T 个
  2. 从 0 开始

首先从 0 开始输入 A_i。
然后输出 0\ a_0,换行,后面的循环从 1 开始。 用一个变量 last 标记前一个记录的时间。
如果 |last - a_i| \ge x,则 last = a_i
否则 继续。

#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)

C Sneaking Glances(补题)

Problem

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

Solve

考察:搜索
难度:普及-

::::warning[需要注意的地方]

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

我们观察样例可以猜测,当当前位置在 0 以右的时候,应当往负方向走,反之往正方向走,然后注意一下类型应是 double,于是乎: ::::error[5WA 代码]

#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 \le 20)。

首先看 main 里的:

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

然后是变量定义:

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

最后考虑 dfs: ::::info[构建过程]{open} 首先我们可以传参为 (当前位置,经过次数)。

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

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

所以终止条件就是:

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

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

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

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);
}

:::: 于是整合一下:

//待测评(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;
}

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

后记

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