// mro: Mrówki | 2024-03-07 | Solution by dkgl // https://sio2.mimuw.edu.pl/c/pa-2024-1/p/mro/ #include <bits/stdc++.h> using namespace std; const char LEFT = 'L'; const char RIGHT = 'P'; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string directions; directions.reserve(n); cin >> directions; vector<int> prefix_right_count(n+1, 0); vector<int> suffix_left_count(n+1, 0); for (int i = 0; i < n; ++i) { prefix_right_count[i+1] = prefix_right_count[i] + (directions[i] == RIGHT); } for (int i = n-1; i >= 0; --i) { suffix_left_count[i] = suffix_left_count[i+1] + (directions[i] == LEFT); } for (int i = 0; i < n; ++i) { int same_count = prefix_right_count[i]; int different_count = suffix_left_count[i+1]; if (directions[i] == LEFT) swap(same_count, different_count); cout << min(same_count, different_count) * 2 + (different_count > same_count) << ' '; } cout << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // mro: Mrówki | 2024-03-07 | Solution by dkgl // https://sio2.mimuw.edu.pl/c/pa-2024-1/p/mro/ #include <bits/stdc++.h> using namespace std; const char LEFT = 'L'; const char RIGHT = 'P'; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string directions; directions.reserve(n); cin >> directions; vector<int> prefix_right_count(n+1, 0); vector<int> suffix_left_count(n+1, 0); for (int i = 0; i < n; ++i) { prefix_right_count[i+1] = prefix_right_count[i] + (directions[i] == RIGHT); } for (int i = n-1; i >= 0; --i) { suffix_left_count[i] = suffix_left_count[i+1] + (directions[i] == LEFT); } for (int i = 0; i < n; ++i) { int same_count = prefix_right_count[i]; int different_count = suffix_left_count[i+1]; if (directions[i] == LEFT) swap(same_count, different_count); cout << min(same_count, different_count) * 2 + (different_count > same_count) << ' '; } cout << endl; return 0; } |