Atcoder Beginner Contest 453 solution
Dijkstra_zyl · · 算法·理论
总结
这次 AB 题比较容易,C 题一直在拿循环做,结果是 dfs。。。
D 题有些思路但是时间不够。
总之这次不符合预期。。。加油! ::::success[学到的内容]
- 当你不确定答案最优时,应当考虑搜索
- 审题
- 搜索题数据一般小于
20 - 模拟题数据一般在
1000 左右 -
有几点五这样数字的,可以都乘上 2 避免小数# A Trimo ## Problem 给定一个字符串,输出去掉所有前导 o后的结果。## Solve 考察:字符串处理 难度:入门
::::warning[要注意的地方]
还要输入一个
我们只需要输入一个字符串 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;
}
复杂度:
B Sensor Data Logging
Problem
在某次测量中,根据以下规则记录时间
在时间
在时间
时间
按时间升序输出保存读数的时间和保存值。
Solve
考察:模拟
难度:入门
::::warning[注意的地方]
- 共输入
T+1 个数字,而不是T 个 -
从 0 开始
首先从
然后输出
如果
否则 继续。
#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;
}
复杂度:
C Sneaking Glances(补题)
Problem
高桥在数线上的坐标
他将走
在第
他通过坐标
在此问题的限制条件下,没有一次移动会在坐标
Solve
考察:搜索
难度:普及-
::::warning[需要注意的地方]
- 当你不确定答案最优时,应当使用搜索,我用循环交了
9 次,依然有10 \times WA -
注意精度及类型
我们观察样例可以猜测,当当前位置在
#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;
}
:::: 虽然这个代码错了,但是也为我提供了一个思路,在我补题后我发现了更合理的思路。
在我提交的时候我就感觉:“这一定对吗?”
没法说明一定,所以我们应该搜索每一种情况,然后取最大值,要是用搜索(另外
首先看
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(0.5,1),而传参应是 dfs(double now,int cnt)。
然后思考结束条件,应该是遍历完 dfs(double now,int cnt,int id),调用应为 dfs(0.5,1,1),表示当前步数。
所以终止条件就是:
if(id > n){//当前步数大于 n
ans = max(ans,cnt);//取最大值
return;//退出
}
然后选择无外乎两种:往正方向走和往负方向走。
然后利用原代码的思路写出
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;
}
当然也可以将每个长度都乘 而我只想到加 0.5
后记
其实比赛应该在一个不太紧张的环境打,像我每次打 ABC 都只有 20min,而我的目标是 1025pts。