#include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; struct Summary { int to_left = 0; int to_right = 0; }; int calculate_row(int left_bound, int right_bound, int i, int from_left, int from_right) { if (left_bound < i and i < right_bound) { return min({abs(i - left_bound), abs(i - right_bound), from_left, from_right}); } return 0; } Summary merge(const string &letters, int start, int end, int results[]) { int mid = (start + end) / 2; if ((end - start) > 1) { auto left_summary = merge(letters, start, mid, results); auto right_summary = merge(letters, mid, end, results); int from_left = left_summary.to_right; int from_right = right_summary.to_left; if (from_left > 0 and from_right > 0) { for (int i = start; i < end; ++i) { int add_1 = calculate_row(mid - from_left - 1, mid + from_right - 1, i, from_left, from_right); int add_2 = calculate_row(mid - from_left, mid + from_right, i, from_left, from_right); results[i] += add_1 + add_2; } } } Summary summary; for (int i = start; i < end; ++i) { if (letters[i] == 'L') { summary.to_left += 1; } else { summary.to_right += 1; } } return summary; } void inline one() { int n; cin >> n; string letters; cin >> letters; int results[n] = {}; merge(letters, 0, n, results); REP(i, n) { cout << results[i] << " "; } cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; struct Summary { int to_left = 0; int to_right = 0; }; int calculate_row(int left_bound, int right_bound, int i, int from_left, int from_right) { if (left_bound < i and i < right_bound) { return min({abs(i - left_bound), abs(i - right_bound), from_left, from_right}); } return 0; } Summary merge(const string &letters, int start, int end, int results[]) { int mid = (start + end) / 2; if ((end - start) > 1) { auto left_summary = merge(letters, start, mid, results); auto right_summary = merge(letters, mid, end, results); int from_left = left_summary.to_right; int from_right = right_summary.to_left; if (from_left > 0 and from_right > 0) { for (int i = start; i < end; ++i) { int add_1 = calculate_row(mid - from_left - 1, mid + from_right - 1, i, from_left, from_right); int add_2 = calculate_row(mid - from_left, mid + from_right, i, from_left, from_right); results[i] += add_1 + add_2; } } } Summary summary; for (int i = start; i < end; ++i) { if (letters[i] == 'L') { summary.to_left += 1; } else { summary.to_right += 1; } } return summary; } void inline one() { int n; cin >> n; string letters; cin >> letters; int results[n] = {}; merge(letters, 0, n, results); REP(i, n) { cout << results[i] << " "; } cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |