🔵
【ABC421】AtCoder Beginner Contest 421【C++】
コンテスト名
AtCoder Beginner Contest 421
コンテストURL
開催日
2025/08/30 21:00–22:40
A: Misdelivery
解法
-
vector<string>
に各部屋に住んでいる人の名前を記録する
ABC421A.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int n;
cin >> n;
vector<string> S(n);
for(int i=0; i<n; i++){
cin >> S[i];
}
int x;
string y;
cin >> x >> y;
if(S[x-1]==y){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
}
B: Fibonacci Reversed
解法
-
to_string()
とstoll()
を活用する
ABC421B.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
long long int f(long long int x){
string s = to_string(x);
reverse(s.begin(), s.end());
return stoll(s);
}
int main(){
vector<long long int> V(10);
cin >> V[0] >> V[1];
for(int i=0; i<10-2; i++){
V[i+2] = f(V[i+1]+V[i]);
}
cout << V[9] << endl;
return 0;
}
C: Alternated
解法
- 最終的に
ABAB...
かBABA...
のいずれかになることに着目し、それぞれの状態になるまでの操作回数を求める
ABC421C.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int n;
cin >> n;
string s;
cin >> s;
vector<int> V;
for(int i=0; i<2*n; i++){
if(s[i]=='A'){
V.push_back(i);
}
}
long long int cnt1 = 0, cnt2 = 0;
for(int i=0; i<n; i++){
cnt1 += abs(i*2 - V[i]);
}
for(int i=0; i<n; i++){
cnt2 += abs(i*2+1 - V[i]);
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
Discussion