解説動画
コード
#include
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
int n;
cin >> n;
vector vec_l, vec_r;
rep(i, n) {
int a;
char c;
cin >> a >> c;
if (c == 'L') vec_l.push_back(a);
if (c == 'R') vec_r.push_back(a);
}
int sum = 0;
int size_l = vec_l.size();
int size_r = vec_r.size();
for (int i = 1; i < size_l; i++) {
sum += abs(vec_l[i] - vec_l[i - 1]);
}
for (int i = 1; i < size_r; i++) {
sum += abs(vec_r[i] - vec_r[i - 1]);
}
cout << sum << endl;
return 0;
}
コメント