output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; bool check(long long mid, int cnt, long long total) { int i, c = 0, c1 = 0; long long sum = 0; vector<long long> tmp; for (i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (mid <= v[i].first) { c++; sum += v[i].first; } else { c1++; tmp.push_back(v[i].first); } } int rem = max(0, cnt - c); if (c1 < rem) return 0; sort(tmp.begin(), tmp.end()); for (i = 0; i < c1; i++) { if (i < c1 - rem) sum += min(tmp[i], mid); else sum += mid; } if (total < sum) return 0; return 1; } void solve() { int i, t; long long total; cin >> t >> total; v.clear(); for (i = 0; i < t; i++) { int l, r; cin >> l >> r; v.push_back({l, r}); } int cnt = (t + 1) / 2; long long hi, lo, mid; lo = 1, hi = total; while (hi - lo >= 4) { mid = (hi + lo) / 2; if (check(mid, cnt, total)) lo = mid; else hi = mid; } for (i = hi; i >= lo; i--) { if (check(i, cnt, total)) { cout << i << "\n"; break; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while (test--) solve(); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; bool check(long long mid, int cnt, long long total) { int i, c = 0, c1 = 0; long long sum = 0; vector<long long> tmp; for (i = 0; i < v.size(); i++) { if (v[i].second < mid) sum += v[i].first; else if (mid <= v[i].first) { c++; sum += v[i].first; } else { c1++; tmp.push_back(v[i].first); } } int rem = max(0, cnt - c); if (c1 < rem) return 0; sort(tmp.begin(), tmp.end()); for (i = 0; i < c1; i++) { if (i < c1 - rem) sum += min(tmp[i], mid); else sum += mid; } if (total < sum) return 0; return 1; } void solve() { int i, t; long long total; cin >> t >> total; v.clear(); for (i = 0; i < t; i++) { int l, r; cin >> l >> r; v.push_back({l, r}); } int cnt = (t + 1) / 2; long long hi, lo, mid; lo = 1, hi = total; while (hi - lo >= 4) { mid = (hi + lo) / 2; if (check(mid, cnt, total)) lo = mid; else hi = mid; } for (i = hi; i >= lo; i--) { if (check(i, cnt, total)) { cout << i << "\n"; break; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while (test--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int amount; long long total; pair<int, int> salary[1000005]; bool is_good(int median_sal) { int need = (amount + 1) / 2; long long total_need = 0; for (int i = amount; i >= 1; i--) if (salary[i].first >= median_sal) { need--; total_need += salary[i].first; } else break; for (int i = amount; i >= 1; i--) if (salary[i].first < median_sal) { if (need > 0 && salary[i].second >= median_sal) need--, total_need += median_sal; else total_need += salary[i].first; } return (total_need <= total && need <= 0); } void solve() { scanf("%d %lld", &amount, &total); for (int i = 1; i <= amount; i++) scanf("%d %d", &salary[i].first, &salary[i].second); sort(salary + 1, salary + amount + 1); int head = (int)1e9 + 5, tail = 0; while (head - tail > 1) { int mid = (head + tail) >> 1; if (is_good(mid)) tail = mid; else head = mid; } printf("%d\n", tail); } int main() { int tester; scanf("%d", &tester); while (tester--) solve(); }
### Prompt Construct a cpp code solution to the problem outlined: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int amount; long long total; pair<int, int> salary[1000005]; bool is_good(int median_sal) { int need = (amount + 1) / 2; long long total_need = 0; for (int i = amount; i >= 1; i--) if (salary[i].first >= median_sal) { need--; total_need += salary[i].first; } else break; for (int i = amount; i >= 1; i--) if (salary[i].first < median_sal) { if (need > 0 && salary[i].second >= median_sal) need--, total_need += median_sal; else total_need += salary[i].first; } return (total_need <= total && need <= 0); } void solve() { scanf("%d %lld", &amount, &total); for (int i = 1; i <= amount; i++) scanf("%d %d", &salary[i].first, &salary[i].second); sort(salary + 1, salary + amount + 1); int head = (int)1e9 + 5, tail = 0; while (head - tail > 1) { int mid = (head + tail) >> 1; if (is_good(mid)) tail = mid; else head = mid; } printf("%d\n", tail); } int main() { int tester; scanf("%d", &tester); while (tester--) solve(); } ```
#include <bits/stdc++.h> using namespace std; long long l[500005], r[500005], n, s; bool check(long long x) { long long k = n + 1 >> 1; vector<long long> vec; for (long long i = 1, v; i <= n; i++) { v = max(l[i], x); if (r[i] < v) continue; vec.push_back(v - l[i]); } if (vec.size() < k) return 0; long long sum = 0; sort(vec.begin(), vec.end()); for (long long i = 0; i < k; i++) sum += vec[i]; return sum <= s; } signed main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld%lld", &n, &s); for (long long i = 1; i <= n; i++) { scanf("%lld%lld", &l[i], &r[i]); s -= l[i]; } long long ll = 0, rr = 1e9, mid, ans = 0; while (ll <= rr) { long long mid = ll + rr >> 1; if (check(mid)) ans = mid, ll = mid + 1; else rr = mid - 1; } printf("%lld\n", ans); } }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long l[500005], r[500005], n, s; bool check(long long x) { long long k = n + 1 >> 1; vector<long long> vec; for (long long i = 1, v; i <= n; i++) { v = max(l[i], x); if (r[i] < v) continue; vec.push_back(v - l[i]); } if (vec.size() < k) return 0; long long sum = 0; sort(vec.begin(), vec.end()); for (long long i = 0; i < k; i++) sum += vec[i]; return sum <= s; } signed main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld%lld", &n, &s); for (long long i = 1; i <= n; i++) { scanf("%lld%lld", &l[i], &r[i]); s -= l[i]; } long long ll = 0, rr = 1e9, mid, ans = 0; while (ll <= rr) { long long mid = ll + rr >> 1; if (check(mid)) ans = mid, ll = mid + 1; else rr = mid - 1; } printf("%lld\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; struct Node { int L, R; bool operator<(const Node &other) const { return L < other.L; } }; int N, H; long long S; vector<Node> v; bool possible(int median) { long long sum = 0; for (Node &s : v) sum += s.L; int count = 0; for (int i = N - 1; i >= 0 && count < H; i--) if (v[i].R >= median) { sum += max(median - v[i].L, 0); count++; } return count == H && sum <= S; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> S; H = (N + 1) / 2; v.resize(N); for (Node &s : v) cin >> s.L >> s.R; sort(v.begin(), v.end()); int l = 0, r = INF; while (l <= r) { int mid = (l + r) / 2; if (possible(mid)) l = mid + 1; else r = mid - 1; } cout << r << '\n'; } return 0; }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; struct Node { int L, R; bool operator<(const Node &other) const { return L < other.L; } }; int N, H; long long S; vector<Node> v; bool possible(int median) { long long sum = 0; for (Node &s : v) sum += s.L; int count = 0; for (int i = N - 1; i >= 0 && count < H; i--) if (v[i].R >= median) { sum += max(median - v[i].L, 0); count++; } return count == H && sum <= S; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { cin >> N >> S; H = (N + 1) / 2; v.resize(N); for (Node &s : v) cin >> s.L >> s.R; sort(v.begin(), v.end()); int l = 0, r = INF; while (l <= r) { int mid = (l + r) / 2; if (possible(mid)) l = mid + 1; else r = mid - 1; } cout << r << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> t; while (t--) { long long s; cin >> n >> s; vector<int> l(n), r(n); for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](const int &x, const int &y) { return l[x] > l[y]; }); auto check = [&](long long lim) -> bool { long long sum = 0; int cnt = 0; for (auto it : ord) { sum += l[it]; long long cur = max(0LL, lim - l[it]); if (r[it] >= lim && cnt < (n + 1) / 2) { sum += cur; cnt++; } if (sum > s) return 0; } return ((cnt == (n + 1) / 2) && sum <= s); }; long long res = 0; for (long long step = 1LL << 50; step; step >>= 1) { if (check(res + step)) { res += step; } } cout << res << "\n"; } return 0; }
### Prompt In cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, i; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> t; while (t--) { long long s; cin >> n >> s; vector<int> l(n), r(n); for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](const int &x, const int &y) { return l[x] > l[y]; }); auto check = [&](long long lim) -> bool { long long sum = 0; int cnt = 0; for (auto it : ord) { sum += l[it]; long long cur = max(0LL, lim - l[it]); if (r[it] >= lim && cnt < (n + 1) / 2) { sum += cur; cnt++; } if (sum > s) return 0; } return ((cnt == (n + 1) / 2) && sum <= s); }; long long res = 0; for (long long step = 1LL << 50; step; step >>= 1) { if (check(res + step)) { res += step; } } cout << res << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mx = 200005; bool comparator(pair<long long, long long> A, pair<long long, long long> B) { return A.first < B.first; } int main() { ios::sync_with_stdio(0); long long q; cin >> q; while (q--) { long long n, s; long long l; long long r; long long ans = 0; vector<pair<long long, long long>> intv, Min, Max; cin >> n >> s; for (int i = 1; i <= n; ++i) { int l, r; cin >> l >> r; intv.push_back(make_pair(l, r)); Min.push_back(make_pair(l, i - 1)); Max.push_back(make_pair(r, i - 1)); } sort(Min.begin(), Min.end(), comparator); sort(Max.begin(), Max.end(), comparator); l = Min[n / 2].first; r = Max[n / 2].first; while (l <= r) { long long m = (l + r) / 2; long long cost = m; long long cntLeft = 0; long long cntRight = 0; vector<pair<int, int>> both; for (int i = 0; i < n; ++i) { if (intv[i].second < m) cost += intv[i].first, ++cntLeft; else if (intv[i].first > m) cost += intv[i].first, ++cntRight; else if (intv[i].first <= m && intv[i].second >= m) both.push_back(intv[i]); } sort(both.begin(), both.end(), comparator); for (int i = 0; i < both.size() && cntLeft < n / 2; ++i) cost += both[i].first, ++cntLeft; for (int i = both.size() - 1; i >= 0 && cntRight < n / 2; --i) cost += m, ++cntRight; if (cost <= s) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << "\n"; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mx = 200005; bool comparator(pair<long long, long long> A, pair<long long, long long> B) { return A.first < B.first; } int main() { ios::sync_with_stdio(0); long long q; cin >> q; while (q--) { long long n, s; long long l; long long r; long long ans = 0; vector<pair<long long, long long>> intv, Min, Max; cin >> n >> s; for (int i = 1; i <= n; ++i) { int l, r; cin >> l >> r; intv.push_back(make_pair(l, r)); Min.push_back(make_pair(l, i - 1)); Max.push_back(make_pair(r, i - 1)); } sort(Min.begin(), Min.end(), comparator); sort(Max.begin(), Max.end(), comparator); l = Min[n / 2].first; r = Max[n / 2].first; while (l <= r) { long long m = (l + r) / 2; long long cost = m; long long cntLeft = 0; long long cntRight = 0; vector<pair<int, int>> both; for (int i = 0; i < n; ++i) { if (intv[i].second < m) cost += intv[i].first, ++cntLeft; else if (intv[i].first > m) cost += intv[i].first, ++cntRight; else if (intv[i].first <= m && intv[i].second >= m) both.push_back(intv[i]); } sort(both.begin(), both.end(), comparator); for (int i = 0; i < both.size() && cntLeft < n / 2; ++i) cost += both[i].first, ++cntLeft; for (int i = both.size() - 1; i >= 0 && cntRight < n / 2; --i) cost += m, ++cntRight; if (cost <= s) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const long long N = 2e5 + 10; long long T, n, s, mx, ts; struct node { long long l, r; } t[N]; long long cmp(node a, node b) { return a.l < b.l; } inline long long check(long long mid) { long long cnt = 0; s = ts; for (long long i = n; i >= 1; i--) { if (t[i].l >= mid) { cnt++; continue; } if (t[i].r >= mid) { if (s >= (mid - t[i].l)) cnt++, s -= (mid - t[i].l); else return cnt > (n / 2); } } return cnt > (n / 2); } long long bsearch() { long long l = 1, r = mx; while (l < r) { long long mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; } return l; } signed main() { cin >> T; while (T--) { scanf("%lld %lld", &n, &s); mx = s; for (long long i = 1; i <= n; i++) scanf("%lld %lld", &t[i].l, &t[i].r), s -= t[i].l; sort(t + 1, t + 1 + n, cmp); ts = s; printf("%lld\n", bsearch()); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const long long N = 2e5 + 10; long long T, n, s, mx, ts; struct node { long long l, r; } t[N]; long long cmp(node a, node b) { return a.l < b.l; } inline long long check(long long mid) { long long cnt = 0; s = ts; for (long long i = n; i >= 1; i--) { if (t[i].l >= mid) { cnt++; continue; } if (t[i].r >= mid) { if (s >= (mid - t[i].l)) cnt++, s -= (mid - t[i].l); else return cnt > (n / 2); } } return cnt > (n / 2); } long long bsearch() { long long l = 1, r = mx; while (l < r) { long long mid = l + r + 1 >> 1; if (check(mid)) l = mid; else r = mid - 1; } return l; } signed main() { cin >> T; while (T--) { scanf("%lld %lld", &n, &s); mx = s; for (long long i = 1; i <= n; i++) scanf("%lld %lld", &t[i].l, &t[i].r), s -= t[i].l; sort(t + 1, t + 1 + n, cmp); ts = s; printf("%lld\n", bsearch()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t; int n; long long s; struct salary { long long l, r; }; salary a[200001]; bool cmp(salary a, salary b) { return a.l > b.l; } void nhap() { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); } sort(a + 1, a + n + 1, cmp); } bool check(long long k) { int dem = n / 2 + 1; long long sum = 0; for (int i = 1; i <= n; i++) { if (a[i].l <= k && a[i].r >= k && dem > 0) { dem--; sum += k; } else { sum += a[i].l; dem -= (a[i].l > k); } } if (dem > 0) { return false; } return sum <= s; } void binary() { long long l = a[n / 2 + 1].l; long long r = s; long long kq = 0; while (l <= r) { long long mid = (l + r) / 2; if (check(mid)) { kq = mid; l = mid + 1; } else r = mid - 1; } cout << kq; } int main() { int t; cin >> t; while (t--) { nhap(); binary(); cout << "\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t; int n; long long s; struct salary { long long l, r; }; salary a[200001]; bool cmp(salary a, salary b) { return a.l > b.l; } void nhap() { scanf("%d%lld", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); } sort(a + 1, a + n + 1, cmp); } bool check(long long k) { int dem = n / 2 + 1; long long sum = 0; for (int i = 1; i <= n; i++) { if (a[i].l <= k && a[i].r >= k && dem > 0) { dem--; sum += k; } else { sum += a[i].l; dem -= (a[i].l > k); } } if (dem > 0) { return false; } return sum <= s; } void binary() { long long l = a[n / 2 + 1].l; long long r = s; long long kq = 0; while (l <= r) { long long mid = (l + r) / 2; if (check(mid)) { kq = mid; l = mid + 1; } else r = mid - 1; } cout << kq; } int main() { int t; cin >> t; while (t--) { nhap(); binary(); cout << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long read() { char x = getchar(); long long ans = 0, flag = 1; while (!isdigit(x)) if (x == '-') flag = -1, x = getchar(); else x = getchar(); while (isdigit(x)) ans = ans * 10 + x - '0', x = getchar(); return ans * flag; } long long n, s; struct node { long long l, r; bool operator<(node x) const { return l < x.l; } } a[200005]; signed main() { long long T; cin >> T; while (T--) { cin >> n >> s; for (long long i = 1; i <= n; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + n + 1); for (long long i = 1; i <= n; i++) s -= a[i].l; long long l = a[(n + 1) / 2].l, r = 1e9, mid, ans = a[(n + 1) / 2].l; while (l <= r) { long long tmp = s, cnt = 0; bool flag = 0; mid = (l + r) >> 1; for (long long i = n; i >= 1; i--) { if (a[i].l <= mid && a[i].r >= mid && tmp >= mid - a[i].l) { tmp -= mid - a[i].l; cnt++; continue; } if (a[i].l >= mid) cnt++; } if (cnt >= (n + 1) / 2) l = mid + 1, ans = mid; else r = mid - 1; } cout << ans << endl; } return 0; }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long read() { char x = getchar(); long long ans = 0, flag = 1; while (!isdigit(x)) if (x == '-') flag = -1, x = getchar(); else x = getchar(); while (isdigit(x)) ans = ans * 10 + x - '0', x = getchar(); return ans * flag; } long long n, s; struct node { long long l, r; bool operator<(node x) const { return l < x.l; } } a[200005]; signed main() { long long T; cin >> T; while (T--) { cin >> n >> s; for (long long i = 1; i <= n; i++) cin >> a[i].l >> a[i].r; sort(a + 1, a + n + 1); for (long long i = 1; i <= n; i++) s -= a[i].l; long long l = a[(n + 1) / 2].l, r = 1e9, mid, ans = a[(n + 1) / 2].l; while (l <= r) { long long tmp = s, cnt = 0; bool flag = 0; mid = (l + r) >> 1; for (long long i = n; i >= 1; i--) { if (a[i].l <= mid && a[i].r >= mid && tmp >= mid - a[i].l) { tmp -= mid - a[i].l; cnt++; continue; } if (a[i].l >= mid) cnt++; } if (cnt >= (n + 1) / 2) l = mid + 1, ans = mid; else r = mid - 1; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > d; long long s; int f(int cost) { int cnt = 0; long long sum = 0; vector<int> vec; for (int i = 0; i < (int)d.size(); i++) if (cost <= d[i].first) { cnt++; sum += d[i].first; } else if (d[i].second < cost) sum += d[i].first; else if (d[i].first < cost && cost <= d[i].second) vec.push_back(d[i].first); int rem = max(0, ((int)d.size() + 1) / 2 - cnt); if (rem > (int)vec.size()) return false; for (int i = 0; i < (int)vec.size(); i++) if (i < (int)vec.size() - rem) sum += vec[i]; else sum += cost; return sum <= s; } void solve() { int n; cin >> n >> s; d.clear(); d.resize(n); for (int i = 0; i < n; i++) cin >> d[i].first >> d[i].second; sort(d.begin(), d.end()); int l = 1, r = int(1e9) + 100; while (r - l > 1) { int mid = l + (r - l) / 2; if (f(mid)) l = mid; else r = mid; } cout << l << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<int, int> > d; long long s; int f(int cost) { int cnt = 0; long long sum = 0; vector<int> vec; for (int i = 0; i < (int)d.size(); i++) if (cost <= d[i].first) { cnt++; sum += d[i].first; } else if (d[i].second < cost) sum += d[i].first; else if (d[i].first < cost && cost <= d[i].second) vec.push_back(d[i].first); int rem = max(0, ((int)d.size() + 1) / 2 - cnt); if (rem > (int)vec.size()) return false; for (int i = 0; i < (int)vec.size(); i++) if (i < (int)vec.size() - rem) sum += vec[i]; else sum += cost; return sum <= s; } void solve() { int n; cin >> n >> s; d.clear(); d.resize(n); for (int i = 0; i < n; i++) cin >> d[i].first >> d[i].second; sort(d.begin(), d.end()); int l = 1, r = int(1e9) + 100; while (r - l > 1) { int mid = l + (r - l) / 2; if (f(mid)) l = mid; else r = mid; } cout << l << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<pair<long long int, long long int> > v; vector<long long int> v1; long long n, s, lef; bool check(long long x) { long long t = lower_bound((v1).begin(), (v1).end(), x) - v1.begin(); if (t <= n / 2) return 1; long long needed = t; long long left = lef; for (long long i = t - 1; i >= 0; i--) { if (needed == n / 2) { break; } if (v[i].second >= x) { if ((x - v[i].first) <= left) { left -= (x - v[i].first); needed--; } } } if (needed == n / 2) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; cin >> TESTS; while (TESTS--) { cin >> n >> s; long long l = 1000000007, r = -1; lef = s; v.clear(); v1.clear(); for (long long int i = 0; i < n; i++) { long long x, y; cin >> x >> y; lef -= x; l = min(l, x); r = max(r, y); v.push_back({x, y}); v1.push_back(x); } sort((v).begin(), (v).end()); sort((v1).begin(), (v1).end()); long long ans = v[n / 2].first; long long m; while (l <= r) { m = (l + r) / 2; if (check(m)) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << '\n'; } return 0; }
### Prompt Create a solution in cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<long long int, long long int> > v; vector<long long int> v1; long long n, s, lef; bool check(long long x) { long long t = lower_bound((v1).begin(), (v1).end(), x) - v1.begin(); if (t <= n / 2) return 1; long long needed = t; long long left = lef; for (long long i = t - 1; i >= 0; i--) { if (needed == n / 2) { break; } if (v[i].second >= x) { if ((x - v[i].first) <= left) { left -= (x - v[i].first); needed--; } } } if (needed == n / 2) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; cin >> TESTS; while (TESTS--) { cin >> n >> s; long long l = 1000000007, r = -1; lef = s; v.clear(); v1.clear(); for (long long int i = 0; i < n; i++) { long long x, y; cin >> x >> y; lef -= x; l = min(l, x); r = max(r, y); v.push_back({x, y}); v1.push_back(x); } sort((v).begin(), (v).end()); sort((v1).begin(), (v1).end()); long long ans = v[n / 2].first; long long m; while (l <= r) { m = (l + r) / 2; if (check(m)) { ans = max(ans, m); l = m + 1; } else { r = m - 1; } } cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; void solve() { int n; LL s; scanf("%d%lld", &n, &s); vector<int> lb(n), ub(n); for (int i = 0; i < n; ++i) scanf("%d%d", &lb[i], &ub[i]); auto check = [&](int mid) -> int { multiset<LL> se; LL tot = 0; int need = (n - 1) / 2; for (int i = 0; i < n; ++i) { if (ub[i] < mid) --need, tot += lb[i]; else if (lb[i] > mid) tot += lb[i]; else se.insert(lb[i]); } if (need < 0) return 2; while (!se.empty() && need) { tot += *se.begin(); se.erase(se.begin()); --need; } if (need) return -1; tot += 1LL * mid * se.size(); if (tot <= s) return 0; else return 1; }; LL l = 1, r = 1e9; while (l + 1 < r) { LL m = (l + r) >> 1; int ret = check(m); if (ret < 0) l = m + 1; else if (ret > 0) r = m - 1; else l = m; } if (check(r) == 0) printf("%lld\n", r); else printf("%lld\n", l); } int main() { int t; scanf("%d", &t); while (t--) solve(); }
### Prompt Develop a solution in cpp to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; void solve() { int n; LL s; scanf("%d%lld", &n, &s); vector<int> lb(n), ub(n); for (int i = 0; i < n; ++i) scanf("%d%d", &lb[i], &ub[i]); auto check = [&](int mid) -> int { multiset<LL> se; LL tot = 0; int need = (n - 1) / 2; for (int i = 0; i < n; ++i) { if (ub[i] < mid) --need, tot += lb[i]; else if (lb[i] > mid) tot += lb[i]; else se.insert(lb[i]); } if (need < 0) return 2; while (!se.empty() && need) { tot += *se.begin(); se.erase(se.begin()); --need; } if (need) return -1; tot += 1LL * mid * se.size(); if (tot <= s) return 0; else return 1; }; LL l = 1, r = 1e9; while (l + 1 < r) { LL m = (l + r) >> 1; int ret = check(m); if (ret < 0) l = m + 1; else if (ret > 0) r = m - 1; else l = m; } if (check(r) == 0) printf("%lld\n", r); else printf("%lld\n", l); } int main() { int t; scanf("%d", &t); while (t--) solve(); } ```
#include <bits/stdc++.h> long long int paixu(long long int shuzu[][2], long long int daxiao) { long long int i, j, k, l; l = daxiao; if (l >= 1000) { l = 1000; } for (l = l; l >= 1; l /= 2) { for (i = l; i <= daxiao; i++) { for (j = i; ((shuzu[j][1] < shuzu[j - l][1]) & (j > l)); j -= l) { k = shuzu[j][1]; shuzu[j][1] = shuzu[j - l][1]; shuzu[j - l][1] = k; k = shuzu[j][0]; shuzu[j][0] = shuzu[j - l][0]; shuzu[j - l][0] = k; } } } return 0; } long long int paixu2(long long int shuzu[][2], long long int daxiao) { long long int i, j, k, l; l = daxiao; if (l >= 1000) { l = 1000; } for (l = l; l >= 1; l /= 2) { for (i = l; i <= daxiao; i++) { for (j = i; ((shuzu[j][0] < shuzu[j - l][0]) & (j > l)); j -= l) { k = shuzu[j][1]; shuzu[j][1] = shuzu[j - l][1]; shuzu[j - l][1] = k; k = shuzu[j][0]; shuzu[j][0] = shuzu[j - l][0]; shuzu[j - l][0] = k; } } } for (i = 2; i <= daxiao; i++) { if ((shuzu[i - 1][0] == shuzu[i][0]) & (shuzu[i - 1][1] > shuzu[i][1])) { k = shuzu[i][0]; shuzu[i][0] = shuzu[i - 1][0]; shuzu[i - 1][0] = k; } } return 0; } long long int min(long long int x, long long int y) { if (x >= y) { return y; } else { return x; } } long long int max(long long int x, long long int y) { if (x >= y) { return x; } else { return y; } } long long int sb[200001][2], jb[200001][2], m, n, i, j, k, maxhp, hp, p, tmp, zuixiao, mid, mid1, mid2; int main() { scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld%lld", &m, &hp); maxhp = 0; p = m; tmp = 0; for (j = 1; j <= m; j++) { scanf("%lld%lld", &sb[j][0], &sb[j][1]); jb[j][0] = sb[j][0]; jb[j][1] = sb[j][1]; } if (m == 1) { printf("%lld\n", min(hp, sb[1][1])); continue; } paixu(sb, m); mid = mid1 = sb[m / 2 + 1][1]; paixu2(jb, m); zuixiao = jb[m / 2 + 1][0]; mid2 = zuixiao; while (1) { aaa: if (mid == zuixiao) { goto bbb; } k = 0; maxhp = 0; for (j = m; j >= 1; j--) { if (maxhp > hp) { if ((mid1 == mid2 + 1) & (mid == mid1)) { mid = mid2; goto bbb; } else { mid1 = mid; mid = (mid1 + mid2) / 2; goto aaa; } } if (k == m / 2 + 1) { maxhp += jb[j][0]; goto ccc; } if (jb[j][0] >= mid) { maxhp += jb[j][0]; k++; goto ccc; } if (k == m / 2 + 1) { maxhp += jb[j][0]; goto ccc; } if (jb[j][1] >= mid) { maxhp += mid; k++; goto ccc; } else { maxhp += jb[j][0]; } ccc: if ((maxhp > hp) | ((j == 1) & (k < m / 2 + 1))) { if ((mid1 == mid2 + 1) & (mid == mid1)) { mid = mid2; goto bbb; } else { mid1 = mid; mid = (mid1 + mid2) / 2; goto aaa; } } } if ((mid1 == mid2 + 1) & (mid == mid1) | (mid1 == mid2)) { mid = mid1; goto bbb; } mid2 = mid; if (mid1 == mid2 + 1) { mid++; } else { mid = (mid1 + mid2) / 2; } } bbb: printf("%lld\n", mid); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> long long int paixu(long long int shuzu[][2], long long int daxiao) { long long int i, j, k, l; l = daxiao; if (l >= 1000) { l = 1000; } for (l = l; l >= 1; l /= 2) { for (i = l; i <= daxiao; i++) { for (j = i; ((shuzu[j][1] < shuzu[j - l][1]) & (j > l)); j -= l) { k = shuzu[j][1]; shuzu[j][1] = shuzu[j - l][1]; shuzu[j - l][1] = k; k = shuzu[j][0]; shuzu[j][0] = shuzu[j - l][0]; shuzu[j - l][0] = k; } } } return 0; } long long int paixu2(long long int shuzu[][2], long long int daxiao) { long long int i, j, k, l; l = daxiao; if (l >= 1000) { l = 1000; } for (l = l; l >= 1; l /= 2) { for (i = l; i <= daxiao; i++) { for (j = i; ((shuzu[j][0] < shuzu[j - l][0]) & (j > l)); j -= l) { k = shuzu[j][1]; shuzu[j][1] = shuzu[j - l][1]; shuzu[j - l][1] = k; k = shuzu[j][0]; shuzu[j][0] = shuzu[j - l][0]; shuzu[j - l][0] = k; } } } for (i = 2; i <= daxiao; i++) { if ((shuzu[i - 1][0] == shuzu[i][0]) & (shuzu[i - 1][1] > shuzu[i][1])) { k = shuzu[i][0]; shuzu[i][0] = shuzu[i - 1][0]; shuzu[i - 1][0] = k; } } return 0; } long long int min(long long int x, long long int y) { if (x >= y) { return y; } else { return x; } } long long int max(long long int x, long long int y) { if (x >= y) { return x; } else { return y; } } long long int sb[200001][2], jb[200001][2], m, n, i, j, k, maxhp, hp, p, tmp, zuixiao, mid, mid1, mid2; int main() { scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld%lld", &m, &hp); maxhp = 0; p = m; tmp = 0; for (j = 1; j <= m; j++) { scanf("%lld%lld", &sb[j][0], &sb[j][1]); jb[j][0] = sb[j][0]; jb[j][1] = sb[j][1]; } if (m == 1) { printf("%lld\n", min(hp, sb[1][1])); continue; } paixu(sb, m); mid = mid1 = sb[m / 2 + 1][1]; paixu2(jb, m); zuixiao = jb[m / 2 + 1][0]; mid2 = zuixiao; while (1) { aaa: if (mid == zuixiao) { goto bbb; } k = 0; maxhp = 0; for (j = m; j >= 1; j--) { if (maxhp > hp) { if ((mid1 == mid2 + 1) & (mid == mid1)) { mid = mid2; goto bbb; } else { mid1 = mid; mid = (mid1 + mid2) / 2; goto aaa; } } if (k == m / 2 + 1) { maxhp += jb[j][0]; goto ccc; } if (jb[j][0] >= mid) { maxhp += jb[j][0]; k++; goto ccc; } if (k == m / 2 + 1) { maxhp += jb[j][0]; goto ccc; } if (jb[j][1] >= mid) { maxhp += mid; k++; goto ccc; } else { maxhp += jb[j][0]; } ccc: if ((maxhp > hp) | ((j == 1) & (k < m / 2 + 1))) { if ((mid1 == mid2 + 1) & (mid == mid1)) { mid = mid2; goto bbb; } else { mid1 = mid; mid = (mid1 + mid2) / 2; goto aaa; } } } if ((mid1 == mid2 + 1) & (mid == mid1) | (mid1 == mid2)) { mid = mid1; goto bbb; } mid2 = mid; if (mid1 == mid2 + 1) { mid++; } else { mid = (mid1 + mid2) / 2; } } bbb: printf("%lld\n", mid); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long mo(const long long input, const long long ceil) { return input >= ceil ? input % ceil : input; } long long n, s, ans, l, r, i, mid, cnt, sum; pair<long long, long long> a[400000]; void solve(long long tt) { cin >> n >> s; for (i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } sort(a + 1, a + n + 1); ans = 1, l = 1, r = 1000000000000000; while (l <= r) { mid = (l + r) / 2; cnt = (n + 1) / 2; sum = 0; for (i = n; i >= 1; i--) { if (a[i].second >= mid && cnt) { sum = sum + max(a[i].first, mid); cnt--; } else sum = sum + a[i].first; } if (sum <= s && cnt == 0) { l = mid + 1; ans = mid; } else r = mid - 1; } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long tt = 1; cin >> tt; for (long long i = 0; i < tt; i++) solve(tt); cerr << "\n\n\nTime : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\n"; ; return 0; }
### Prompt Please create a solution in Cpp to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mo(const long long input, const long long ceil) { return input >= ceil ? input % ceil : input; } long long n, s, ans, l, r, i, mid, cnt, sum; pair<long long, long long> a[400000]; void solve(long long tt) { cin >> n >> s; for (i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } sort(a + 1, a + n + 1); ans = 1, l = 1, r = 1000000000000000; while (l <= r) { mid = (l + r) / 2; cnt = (n + 1) / 2; sum = 0; for (i = n; i >= 1; i--) { if (a[i].second >= mid && cnt) { sum = sum + max(a[i].first, mid); cnt--; } else sum = sum + a[i].first; } if (sum <= s && cnt == 0) { l = mid + 1; ans = mid; } else r = mid - 1; } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long tt = 1; cin >> tt; for (long long i = 0; i < tt; i++) solve(tt); cerr << "\n\n\nTime : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\n"; ; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; long long s; long long l[200010], r[200010], b[200010]; long long res; bool ok(long long x) { long long tmp = 0; int dem; dem = 0; vector<int> mi; mi.clear(); for (int i = 1; i <= n; i++) { if (r[i] < x) { tmp += l[i]; continue; } if (l[i] > x) { tmp += l[i]; dem++; continue; } tmp += x; dem++; mi.push_back(l[i]); } if (dem < n / 2 + 1) return false; sort(mi.begin(), mi.end()); int now = 0; while (dem > n / 2 + 1) { if (now >= mi.size()) return false; tmp += (mi[now] - x); now++; dem--; } return (tmp <= s); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int ntest; cin >> ntest; for (int test = 1; test <= ntest; test++) { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> l[i] >> r[i]; for (int i = 1; i <= n; i++) b[i] = l[i]; sort(b + 1, b + n + 1); long long lo, hi, mid; lo = b[n / 2 + 1]; hi = 1000000100; while (lo <= hi) { mid = (lo + hi) >> 1; if (ok(mid)) { res = mid; lo = mid + 1; } else hi = mid - 1; } cout << res << endl; } }
### Prompt Create a solution in Cpp for the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; long long s; long long l[200010], r[200010], b[200010]; long long res; bool ok(long long x) { long long tmp = 0; int dem; dem = 0; vector<int> mi; mi.clear(); for (int i = 1; i <= n; i++) { if (r[i] < x) { tmp += l[i]; continue; } if (l[i] > x) { tmp += l[i]; dem++; continue; } tmp += x; dem++; mi.push_back(l[i]); } if (dem < n / 2 + 1) return false; sort(mi.begin(), mi.end()); int now = 0; while (dem > n / 2 + 1) { if (now >= mi.size()) return false; tmp += (mi[now] - x); now++; dem--; } return (tmp <= s); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int ntest; cin >> ntest; for (int test = 1; test <= ntest; test++) { cin >> n >> s; for (int i = 1; i <= n; i++) cin >> l[i] >> r[i]; for (int i = 1; i <= n; i++) b[i] = l[i]; sort(b + 1, b + n + 1); long long lo, hi, mid; lo = b[n / 2 + 1]; hi = 1000000100; while (lo <= hi) { mid = (lo + hi) >> 1; if (ok(mid)) { res = mid; lo = mid + 1; } else hi = mid - 1; } cout << res << endl; } } ```
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int man = 2e5 + 10; template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) { if (!b) { g = a, x = 1, y = 0; } else { exgcd(b, a % b, g, y, x); y -= x * (a / b); } } const long long mod = 1e9 + 7; struct node { int l, r; bool operator<(const node &a) const { return l == a.l ? r > a.r : l > a.l; } } a[man]; int n; long long s; bool check(long long mid) { int cnt = n / 2 + 1; long long sum = 0; for (int i = 1; i <= n; i++) { int l = a[i].l, r = a[i].r; if (l <= mid && r >= mid && cnt) { sum += mid; cnt--; } else { sum += l; if (l > mid) cnt--; } } return (sum <= s && !cnt); } void slove() { long long l = a[n / 2 + 1].l, r = s; long long ans = a[n / 2 + 1].l + 1; while (l <= r) { long long mid = l + r >> 1; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } int main() { int t; cin >> t; while (t--) { cin >> n; cin >> s; for (int i = 1; i <= n; i++) { cin >> a[i].l >> a[i].r; } sort(a + 1, a + 1 + n); slove(); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int man = 2e5 + 10; template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T exgcd(T a, T b, T &g, T &x, T &y) { if (!b) { g = a, x = 1, y = 0; } else { exgcd(b, a % b, g, y, x); y -= x * (a / b); } } const long long mod = 1e9 + 7; struct node { int l, r; bool operator<(const node &a) const { return l == a.l ? r > a.r : l > a.l; } } a[man]; int n; long long s; bool check(long long mid) { int cnt = n / 2 + 1; long long sum = 0; for (int i = 1; i <= n; i++) { int l = a[i].l, r = a[i].r; if (l <= mid && r >= mid && cnt) { sum += mid; cnt--; } else { sum += l; if (l > mid) cnt--; } } return (sum <= s && !cnt); } void slove() { long long l = a[n / 2 + 1].l, r = s; long long ans = a[n / 2 + 1].l + 1; while (l <= r) { long long mid = l + r >> 1; if (check(mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } int main() { int t; cin >> t; while (t--) { cin >> n; cin >> s; for (int i = 1; i <= n; i++) { cin >> a[i].l >> a[i].r; } sort(a + 1, a + 1 + n); slove(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool check(pair<long long int, long long int> arr[], long long int mid, long long int n, long long int s) { vector<pair<long long int, long long int>> both; long long int l = 0, r = 0; for (long long int i = 0; i < n; i++) { if (arr[i].second < mid) { s -= arr[i].first; l++; } else if (arr[i].first > mid) { s -= arr[i].first; r++; } else { both.push_back(arr[i]); } } if (l > (n / 2) || r > (n / 2)) return false; for (long long int i = 0; i < both.size(); i++) { if (l < (n / 2)) { s -= both[i].first; l++; } else { s -= mid; r++; } } if (s < 0) return false; return true; } void solve() { long long int n, s, l, r = 0, a, b; cin >> n >> s; pair<long long int, long long int> arr[n]; for (long long int i = 0; i < n; i++) { cin >> a >> b; arr[i] = {a, b}; r = max(r, b); } sort(arr, arr + n); l = arr[n / 2].first; r = arr[n].second; while (l < r) { long long int mid = (l + r + 1) / 2; if (check(arr, mid, n, s)) { l = mid; } else { r = mid - 1; } } cout << l << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { solve(); } }
### Prompt Your challenge is to write a CPP solution to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(pair<long long int, long long int> arr[], long long int mid, long long int n, long long int s) { vector<pair<long long int, long long int>> both; long long int l = 0, r = 0; for (long long int i = 0; i < n; i++) { if (arr[i].second < mid) { s -= arr[i].first; l++; } else if (arr[i].first > mid) { s -= arr[i].first; r++; } else { both.push_back(arr[i]); } } if (l > (n / 2) || r > (n / 2)) return false; for (long long int i = 0; i < both.size(); i++) { if (l < (n / 2)) { s -= both[i].first; l++; } else { s -= mid; r++; } } if (s < 0) return false; return true; } void solve() { long long int n, s, l, r = 0, a, b; cin >> n >> s; pair<long long int, long long int> arr[n]; for (long long int i = 0; i < n; i++) { cin >> a >> b; arr[i] = {a, b}; r = max(r, b); } sort(arr, arr + n); l = arr[n / 2].first; r = arr[n].second; while (l < r) { long long int mid = (l + r + 1) / 2; if (check(arr, mid, n, s)) { l = mid; } else { r = mid - 1; } } cout << l << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const long long M = 3 * (1e5) + 7, mod = 1e9 + 7; long long n, s; vector<pair<long long, long long>> v; bool ok(long long mid) { vector<pair<long long, long long>> d; for (auto u : v) { if (u.second >= mid) { d.push_back({u.first, u.second}); } } sort(d.begin(), d.end()); if (d.size() < v.size() / 2) return 0; long long cnt = v.size() / 2, sum = mid; while (cnt--) { sum += max(d.back().first, mid); d.pop_back(); } if (d.size() == 0 || d.back().first > mid) return 0; else d.pop_back(); for (auto u : v) { if (u.first < mid && !(u.second >= mid)) { d.push_back({u.first, u.second}); } } if (d.size() != v.size() / 2) return 0; for (auto u : d) sum += u.first; if (sum > s) return 0; else return 1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long tt; cin >> tt; while (tt--) { cin >> n >> s; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); } sort(v.begin(), v.end()); long long lo = v[(long long)v.size() / 2].first, hi = 1e10, ans = 0; while (lo <= hi) { long long mid = (lo + hi) / 2; if (ok(mid)) { lo = mid + 1; ans = mid; } else hi = mid - 1; } cout << ans << "\n"; v.clear(); } return 0; }
### Prompt Please create a solution in CPP to the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long M = 3 * (1e5) + 7, mod = 1e9 + 7; long long n, s; vector<pair<long long, long long>> v; bool ok(long long mid) { vector<pair<long long, long long>> d; for (auto u : v) { if (u.second >= mid) { d.push_back({u.first, u.second}); } } sort(d.begin(), d.end()); if (d.size() < v.size() / 2) return 0; long long cnt = v.size() / 2, sum = mid; while (cnt--) { sum += max(d.back().first, mid); d.pop_back(); } if (d.size() == 0 || d.back().first > mid) return 0; else d.pop_back(); for (auto u : v) { if (u.first < mid && !(u.second >= mid)) { d.push_back({u.first, u.second}); } } if (d.size() != v.size() / 2) return 0; for (auto u : d) sum += u.first; if (sum > s) return 0; else return 1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long tt; cin >> tt; while (tt--) { cin >> n >> s; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; v.push_back({l, r}); } sort(v.begin(), v.end()); long long lo = v[(long long)v.size() / 2].first, hi = 1e10, ans = 0; while (lo <= hi) { long long mid = (lo + hi) / 2; if (ok(mid)) { lo = mid + 1; ans = mid; } else hi = mid - 1; } cout << ans << "\n"; v.clear(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int o = 1 << 17; const int maxn = 200005; const int mod = 1000000007; const int inf = 0x3f3f3f3f; const long long huge = 100000000000000000LL; long long s; int n, t; int l[maxn], r[maxn]; int tmp[maxn]; long long price(int med) { long long ret = 0; int cnt = 0; for (int i = 0; i < n; i++) { if (r[i] < med) continue; tmp[cnt++] = max(0, med - l[i]); } sort(tmp, tmp + cnt); if (cnt < n / 2 + 1) return huge; for (int i = 0; i <= n / 2; i++) ret += tmp[i]; return ret; } void solve() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; s -= l[i]; } int lo = 0; int hi = 1e9; while (lo < hi) { int mid = (lo + hi + 1) / 2; if (price(mid) <= s) lo = mid; else hi = mid - 1; } cout << lo << endl; } int main() { ios::sync_with_stdio(false); cin >> t; for (int i = 0; i < t; i++) solve(); return 0; }
### Prompt In Cpp, your task is to solve the following problem: You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: * the median of the sequence [5, 1, 10, 17, 6] is 6, * the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n ≀ s. Note that you don't have to spend all your s dollars on salaries. You have to answer t test cases. Input The first line contains one integer t (1 ≀ t ≀ 2 β‹… 10^5) β€” the number of test cases. The first line of each query contains two integers n and s (1 ≀ n < 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^{14}) β€” the number of employees and the amount of money you have. The value n is not divisible by 2. The following n lines of each query contain the information about employees. The i-th line contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ 10^9). It is guaranteed that the sum of all n over all queries does not exceed 2 β‹… 10^5. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. βˆ‘_{i=1}^{n} l_i ≀ s. Output For each test case print one integer β€” the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 Note In the first test case, you can distribute salaries as follows: sal_1 = 12, sal_2 = 2, sal_3 = 11 (sal_i is the salary of the i-th employee). Then the median salary is 11. In the second test case, you have to pay 1337 dollars to the only employee. In the third test case, you can distribute salaries as follows: sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7. Then the median salary is 6. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int o = 1 << 17; const int maxn = 200005; const int mod = 1000000007; const int inf = 0x3f3f3f3f; const long long huge = 100000000000000000LL; long long s; int n, t; int l[maxn], r[maxn]; int tmp[maxn]; long long price(int med) { long long ret = 0; int cnt = 0; for (int i = 0; i < n; i++) { if (r[i] < med) continue; tmp[cnt++] = max(0, med - l[i]); } sort(tmp, tmp + cnt); if (cnt < n / 2 + 1) return huge; for (int i = 0; i <= n / 2; i++) ret += tmp[i]; return ret; } void solve() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; s -= l[i]; } int lo = 0; int hi = 1e9; while (lo < hi) { int mid = (lo + hi + 1) / 2; if (price(mid) <= s) lo = mid; else hi = mid - 1; } cout << lo << endl; } int main() { ios::sync_with_stdio(false); cin >> t; for (int i = 0; i < t; i++) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool besarDulu(const int &a, const int &b) { return a > b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string s, sOri; cin >> sOri; s = sOri; int B_count = 0, W_count = 0; bool W_ada = false, B_ada = false; for (int i = 0; i < s.length(); i++) { if (s[i] == 'W') { W_ada = true; W_count++; } else { B_ada = true; B_count++; } } if ((B_ada && !W_ada) || (!B_ada && W_ada)) { cout << "0" << '\n'; return 0; } if (W_count % 2 == 1 && B_count % 2 == 1) { cout << "-1" << '\n'; return 0; } int W_cara = 0, B_cara = 0; vector<int> caraPola_B, caraPola_W; int minim; if (B_count % 2 == 0) { for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'B') { B_cara++; caraPola_B.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } } else { B_cara = INT_MAX; } if (W_count % 2 == 0) { s = sOri; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'W') { W_cara++; caraPola_W.push_back(i + 1); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { W_cara = INT_MAX; } minim = min(W_cara, B_cara); if (minim <= n) { cout << minim << '\n'; for (int i = 0; i < minim - 1; i++) { if (minim == B_cara) cout << caraPola_B[i] << " "; else cout << caraPola_W[i] << " "; } if (minim == B_cara) cout << caraPola_B[minim - 1] << '\n'; else cout << caraPola_W[minim - 1] << '\n'; return 0; } else { cout << "-1" << '\n'; return 0; } }
### Prompt Please provide a CPP coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool besarDulu(const int &a, const int &b) { return a > b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string s, sOri; cin >> sOri; s = sOri; int B_count = 0, W_count = 0; bool W_ada = false, B_ada = false; for (int i = 0; i < s.length(); i++) { if (s[i] == 'W') { W_ada = true; W_count++; } else { B_ada = true; B_count++; } } if ((B_ada && !W_ada) || (!B_ada && W_ada)) { cout << "0" << '\n'; return 0; } if (W_count % 2 == 1 && B_count % 2 == 1) { cout << "-1" << '\n'; return 0; } int W_cara = 0, B_cara = 0; vector<int> caraPola_B, caraPola_W; int minim; if (B_count % 2 == 0) { for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'B') { B_cara++; caraPola_B.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } } else { B_cara = INT_MAX; } if (W_count % 2 == 0) { s = sOri; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'W') { W_cara++; caraPola_W.push_back(i + 1); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { W_cara = INT_MAX; } minim = min(W_cara, B_cara); if (minim <= n) { cout << minim << '\n'; for (int i = 0; i < minim - 1; i++) { if (minim == B_cara) cout << caraPola_B[i] << " "; else cout << caraPola_W[i] << " "; } if (minim == B_cara) cout << caraPola_B[minim - 1] << '\n'; else cout << caraPola_W[minim - 1] << '\n'; return 0; } else { cout << "-1" << '\n'; return 0; } } ```
#include <bits/stdc++.h> using namespace std; char s[200005]; vector<int> ans; int main() { int T = 1; while (T--) { int n; cin >> n; scanf("%s", s + 1); int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) s[i] == 'B' ? cnt1++ : cnt2++; if (cnt1 % 2 && cnt2 % 2) { puts("-1"); continue; } if (cnt1 % 2) { for (int i = 1; i < n; i++) { if (s[i] == 'W') { ans.push_back(i); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { for (int i = 1; i < n; i++) { if (s[i] == 'B') { ans.push_back(i); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } printf("%d ", ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[200005]; vector<int> ans; int main() { int T = 1; while (T--) { int n; cin >> n; scanf("%s", s + 1); int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= n; i++) s[i] == 'B' ? cnt1++ : cnt2++; if (cnt1 % 2 && cnt2 % 2) { puts("-1"); continue; } if (cnt1 % 2) { for (int i = 1; i < n; i++) { if (s[i] == 'W') { ans.push_back(i); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { for (int i = 1; i < n; i++) { if (s[i] == 'B') { ans.push_back(i); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } printf("%d ", ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int c1 = 0, c2 = 0; vector<bool> a(n); for (int i = 0; i < n; i++) { char tmp; cin >> tmp; if (tmp == 'B') { c1++; a[i] = 0; } else { c2++; a[i] = 1; } } if (c1 % 2 == 1 && c2 % 2 == 1) { cout << -1 << '\n'; return; } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (a[i] == 1) { ans.push_back(i + 1); a[i] = !a[i]; a[i + 1] = !a[i + 1]; } } if (n % 2 == 1 && a[n - 1] == 1) { for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { ans.push_back(i + 1); } } } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << '\n'; ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int c1 = 0, c2 = 0; vector<bool> a(n); for (int i = 0; i < n; i++) { char tmp; cin >> tmp; if (tmp == 'B') { c1++; a[i] = 0; } else { c2++; a[i] = 1; } } if (c1 % 2 == 1 && c2 % 2 == 1) { cout << -1 << '\n'; return; } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (a[i] == 1) { ans.push_back(i + 1); a[i] = !a[i]; a[i + 1] = !a[i + 1]; } } if (n % 2 == 1 && a[n - 1] == 1) { for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { ans.push_back(i + 1); } } } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << '\n'; ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int b = 0, c = 0; int k = 0; int n; void solve(void) { cin >> n; string x; cin >> x; vector<int> indexB; vector<int> indexW; for (int i = 0; i < n; i++) { if (x[i] == 'B') { b++; indexB.push_back(i + 1); } else { c++; indexW.push_back((i + 1)); } } if (b & 1 && c & 1) { cout << -1 << endl; } else if (b == 0 || c == 0) cout << 0 << '\n'; else { if (!(b & 1) && (c & 1 || (b <= c))) { int i = 0; int m = 0; for (int k = 0; k < indexB.size(); k += 2) m = m + (indexB[k + 1] - indexB[k]); if (m > 3 * n) { cout << -1 << endl; return; } cout << m << '\n'; while (i < indexB.size()) { for (int j = indexB[i]; j < indexB[i + 1]; j++) cout << j << ' '; i += 2; } } else { int i = 0; int m = 0; for (int k = 0; k < indexW.size(); k += 2) m = m + (indexW[k + 1] - indexW[k]); if (m > 3 * n) { cout << -1 << endl; return; } cout << m << '\n'; while (i < indexW.size()) { for (int j = indexW[i]; j < indexW[i + 1]; j++) cout << j << ' '; i += 2; } } } } int main(void) { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
### Prompt In cpp, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int b = 0, c = 0; int k = 0; int n; void solve(void) { cin >> n; string x; cin >> x; vector<int> indexB; vector<int> indexW; for (int i = 0; i < n; i++) { if (x[i] == 'B') { b++; indexB.push_back(i + 1); } else { c++; indexW.push_back((i + 1)); } } if (b & 1 && c & 1) { cout << -1 << endl; } else if (b == 0 || c == 0) cout << 0 << '\n'; else { if (!(b & 1) && (c & 1 || (b <= c))) { int i = 0; int m = 0; for (int k = 0; k < indexB.size(); k += 2) m = m + (indexB[k + 1] - indexB[k]); if (m > 3 * n) { cout << -1 << endl; return; } cout << m << '\n'; while (i < indexB.size()) { for (int j = indexB[i]; j < indexB[i + 1]; j++) cout << j << ' '; i += 2; } } else { int i = 0; int m = 0; for (int k = 0; k < indexW.size(); k += 2) m = m + (indexW[k + 1] - indexW[k]); if (m > 3 * n) { cout << -1 << endl; return; } cout << m << '\n'; while (i < indexW.size()) { for (int j = indexW[i]; j < indexW[i + 1]; j++) cout << j << ' '; i += 2; } } } } int main(void) { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int count_b = 0, count_w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'W') count_w++; else count_b++; } if (count_b % 2 != 0 && count_w % 2 != 0) { cout << -1 << endl; return 0; } else if (count_b == 0 || count_w == 0) { cout << 0 << endl; return 0; } char chng; if (count_b % 2 != 0) chng = 'B'; else chng = 'W'; vector<int> v; for (int i = 0; i < n - 1; i++) { if (chng == s[i]) continue; else { s[i] = chng; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int count_b = 0, count_w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'W') count_w++; else count_b++; } if (count_b % 2 != 0 && count_w % 2 != 0) { cout << -1 << endl; return 0; } else if (count_b == 0 || count_w == 0) { cout << 0 << endl; return 0; } char chng; if (count_b % 2 != 0) chng = 'B'; else chng = 'W'; vector<int> v; for (int i = 0; i < n - 1; i++) { if (chng == s[i]) continue; else { s[i] = chng; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> ans; char s[100001]; int main() { int n; scanf("%d %s", &n, s); for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'B') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'W') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } printf("-1\n"); }
### Prompt Your task is to create a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> ans; char s[100001]; int main() { int n; scanf("%d %s", &n, s); for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'B') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'W') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } printf("-1\n"); } ```
#include <bits/stdc++.h> using namespace std; vector<int> v; int numOfBlocks, j; string block; void invert(char s) { for (int i = 0; i < numOfBlocks; i++) { if (block[i] == s) { j = i + 1; while (block[j] != s) { v.push_back(j); block[j - 1] = block[j - 1] == 'B' ? 'W' : 'B'; block[j] = block[j] == 'B' ? 'W' : 'B'; j++; } v.push_back(j); block[j - 1] = block[j - 1] == 'B' ? 'W' : 'B'; block[j] = block[j] == 'B' ? 'W' : 'B'; } } } int main() { int white = 0, black = 0; cin >> numOfBlocks >> block; for (int i = 0; i < numOfBlocks; i++) { if (block[i] == 'W') white++; else black++; } if (white == 0 || black == 0) { cout << 0; return 0; } if (white % 2 == 0 || black % 2 == 0) { if (black % 2 != 0) invert('W'); else invert('B'); cout << v.size() << "\n"; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } } else { cout << -1; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v; int numOfBlocks, j; string block; void invert(char s) { for (int i = 0; i < numOfBlocks; i++) { if (block[i] == s) { j = i + 1; while (block[j] != s) { v.push_back(j); block[j - 1] = block[j - 1] == 'B' ? 'W' : 'B'; block[j] = block[j] == 'B' ? 'W' : 'B'; j++; } v.push_back(j); block[j - 1] = block[j - 1] == 'B' ? 'W' : 'B'; block[j] = block[j] == 'B' ? 'W' : 'B'; } } } int main() { int white = 0, black = 0; cin >> numOfBlocks >> block; for (int i = 0; i < numOfBlocks; i++) { if (block[i] == 'W') white++; else black++; } if (white == 0 || black == 0) { cout << 0; return 0; } if (white % 2 == 0 || black % 2 == 0) { if (black % 2 != 0) invert('W'); else invert('B'); cout << v.size() << "\n"; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } } else { cout << -1; } return 0; } ```
#include <bits/stdc++.h> int va = 10e8 + 7; long long val[200007] = {0}; using namespace std; long long getmul(long long n) { long long int k{0}; while (n > 0) { k += n % 10; n = n / 10; } return k; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t{1}; while (t--) { int n; cin >> n; string s, k; cin >> s; k = s; vector<int> ans; for (int i = 0; i + 1 < n; i++) { if (s[i] == 'B') { ans.push_back(i + 1); s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } if (s[n - 1] == 'W') { cout << ((int)(ans).size()) << " "; for (auto i : ans) cout << i << " "; cout << "\n"; } else { ans.clear(); for (int i = 0; i + 1 < n; i++) { if (k[i] == 'W') { ans.push_back(i + 1); k[i] = 'B'; k[i + 1] = (k[i + 1] == 'W' ? 'B' : 'W'); } } if (k[n - 1] == 'B') { cout << ((int)(ans).size()) << " "; for (auto i : ans) cout << i << " "; cout << "\n"; } else cout << "-1" << "\n"; } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> int va = 10e8 + 7; long long val[200007] = {0}; using namespace std; long long getmul(long long n) { long long int k{0}; while (n > 0) { k += n % 10; n = n / 10; } return k; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t{1}; while (t--) { int n; cin >> n; string s, k; cin >> s; k = s; vector<int> ans; for (int i = 0; i + 1 < n; i++) { if (s[i] == 'B') { ans.push_back(i + 1); s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } if (s[n - 1] == 'W') { cout << ((int)(ans).size()) << " "; for (auto i : ans) cout << i << " "; cout << "\n"; } else { ans.clear(); for (int i = 0; i + 1 < n; i++) { if (k[i] == 'W') { ans.push_back(i + 1); k[i] = 'B'; k[i + 1] = (k[i + 1] == 'W' ? 'B' : 'W'); } } if (k[n - 1] == 'B') { cout << ((int)(ans).size()) << " "; for (auto i : ans) cout << i << " "; cout << "\n"; } else cout << "-1" << "\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b, c, d, e, t, max_cost, ans = 0; string s; string original; cin >> n; if (n == 0) { cout << "0"; return 0; } cin >> s; original = s; int arr[n]; int j = 0, f = 0; for (long long int i = 0; i < n; i++) { arr[i] = -1; if (s[i] != 'B') { f = 1; } } if (f == 0) { cout << "0"; return 0; } for (long long int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; arr[j] = i + 1; j++; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; ans++; } } if (s[n - 1] != 'B') { cout << ans << endl; int k = 0; while (arr[k] != -1) { cout << arr[k] << " "; k++; } return 0; } if (s[n - 1] == 'B') { ans = 0; for (long long int i = 0; i < n; i++) { arr[i] = -1; } j = 0; for (long long int i = 0; i < n - 1; i++) { if (original[i] == 'W') { original[i] = 'B'; arr[j] = i + 1; j++; if (original[i + 1] == 'W') original[i + 1] = 'B'; else original[i + 1] = 'W'; ans++; } } } if (original[n - 1] == 'W') cout << "-1"; else { cout << ans << endl; int k = 0; while (arr[k] != -1) { cout << arr[k] << " "; k++; } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b, c, d, e, t, max_cost, ans = 0; string s; string original; cin >> n; if (n == 0) { cout << "0"; return 0; } cin >> s; original = s; int arr[n]; int j = 0, f = 0; for (long long int i = 0; i < n; i++) { arr[i] = -1; if (s[i] != 'B') { f = 1; } } if (f == 0) { cout << "0"; return 0; } for (long long int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; arr[j] = i + 1; j++; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; ans++; } } if (s[n - 1] != 'B') { cout << ans << endl; int k = 0; while (arr[k] != -1) { cout << arr[k] << " "; k++; } return 0; } if (s[n - 1] == 'B') { ans = 0; for (long long int i = 0; i < n; i++) { arr[i] = -1; } j = 0; for (long long int i = 0; i < n - 1; i++) { if (original[i] == 'W') { original[i] = 'B'; arr[j] = i + 1; j++; if (original[i + 1] == 'W') original[i + 1] = 'B'; else original[i + 1] = 'W'; ans++; } } } if (original[n - 1] == 'W') cout << "-1"; else { cout << ans << endl; int k = 0; while (arr[k] != -1) { cout << arr[k] << " "; k++; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; void speed() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const long long N = 1e5 + 5; const long long NN = 105; const long long MAX = 2e5 + 123; const long long MOD = 1e9 + 7; const long long INF = 1e18; vector<pair<long long, long long> > vb, vw; void hajime() { string s; long long n, cnt = 0; cin >> n >> s; string b = s; string w = s; for (long long i = 0; i < b.size(); i++) { if (b[i] == 'W' && i != b.size() - 1) { b[i] = 'B'; if (b[i + 1] == 'W') b[i + 1] = 'B'; else b[i + 1] = 'W'; cnt++; vb.push_back(make_pair(i + 1, i + 2)); } } if (b[b.size() - 1] == b[b.size() - 2]) { cout << cnt << '\n'; for (long long i = 0; i < vb.size(); i++) cout << vb[i].first << ' '; return; } cnt = 0; for (long long i = 0; i < w.size(); i++) { if (w[i] == 'B' && i != w.size() - 1) { w[i] = 'W'; if (w[i + 1] == 'B') w[i + 1] = 'W'; else w[i + 1] = 'B'; cnt++; vw.push_back(make_pair(i + 1, i + 2)); } } if (w[w.size() - 1] == w[w.size() - 2]) { cout << cnt << '\n'; for (long long i = 0; i < vw.size(); i++) cout << vw[i].first << ' '; return; } else cout << -1; } int main() { long long miyagi = 1; speed(); while (miyagi--) hajime(); }
### Prompt In cpp, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void speed() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const long long N = 1e5 + 5; const long long NN = 105; const long long MAX = 2e5 + 123; const long long MOD = 1e9 + 7; const long long INF = 1e18; vector<pair<long long, long long> > vb, vw; void hajime() { string s; long long n, cnt = 0; cin >> n >> s; string b = s; string w = s; for (long long i = 0; i < b.size(); i++) { if (b[i] == 'W' && i != b.size() - 1) { b[i] = 'B'; if (b[i + 1] == 'W') b[i + 1] = 'B'; else b[i + 1] = 'W'; cnt++; vb.push_back(make_pair(i + 1, i + 2)); } } if (b[b.size() - 1] == b[b.size() - 2]) { cout << cnt << '\n'; for (long long i = 0; i < vb.size(); i++) cout << vb[i].first << ' '; return; } cnt = 0; for (long long i = 0; i < w.size(); i++) { if (w[i] == 'B' && i != w.size() - 1) { w[i] = 'W'; if (w[i + 1] == 'B') w[i + 1] = 'W'; else w[i + 1] = 'B'; cnt++; vw.push_back(make_pair(i + 1, i + 2)); } } if (w[w.size() - 1] == w[w.size() - 2]) { cout << cnt << '\n'; for (long long i = 0; i < vw.size(); i++) cout << vw[i].first << ' '; return; } else cout << -1; } int main() { long long miyagi = 1; speed(); while (miyagi--) hajime(); } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string s; cin >> s; char arr[N + 1]; strcpy(arr, s.c_str()); vector<int> v; unordered_map<char, int> um; for (int i = 0; i < N; i++) { um[arr[i]]++; } if (um['B'] % 2 == 1 && um['W'] % 2 == 1) { cout << "-1\n"; } else if (um['B'] == 0 || um['W'] == 0) cout << "0\n"; else { char ch; if (um['B'] % 2 == 0) ch = 'W'; else ch = 'B'; for (int i = 0; i < N; i++) { if (arr[i] != ch) { if (i + 1 < N && arr[i + 1] != ch) { arr[i] = ch; arr[i + 1] = ch; v.push_back(i + 1); } else if (i + 1 < N && arr[i + 1] == ch) { char t = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = t; v.push_back(i + 1); } } } cout << v.size() << "\n"; for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " "; cout << v[v.size() - 1]; } }
### Prompt Please formulate a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string s; cin >> s; char arr[N + 1]; strcpy(arr, s.c_str()); vector<int> v; unordered_map<char, int> um; for (int i = 0; i < N; i++) { um[arr[i]]++; } if (um['B'] % 2 == 1 && um['W'] % 2 == 1) { cout << "-1\n"; } else if (um['B'] == 0 || um['W'] == 0) cout << "0\n"; else { char ch; if (um['B'] % 2 == 0) ch = 'W'; else ch = 'B'; for (int i = 0; i < N; i++) { if (arr[i] != ch) { if (i + 1 < N && arr[i + 1] != ch) { arr[i] = ch; arr[i + 1] = ch; v.push_back(i + 1); } else if (i + 1 < N && arr[i + 1] == ch) { char t = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = t; v.push_back(i + 1); } } } cout << v.size() << "\n"; for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " "; cout << v[v.size() - 1]; } } ```
#include <bits/stdc++.h> using namespace std; char s[300010]; vector<int> ans; int main() { ios::sync_with_stdio(false); int n; cin >> n; cin >> s + 1; for (int i = 1; i < n; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; ans.push_back(i); } } if (s[n] == 'W') { cout << ans.size() << endl; for (auto i : ans) cout << i << " "; } else { if (n & 1) { cout << ans.size() + (n - 1) / 2 << endl; for (auto i : ans) cout << i << " "; for (int i = 1; i <= n - 1; i += 2) cout << i << " "; } else cout << -1 << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[300010]; vector<int> ans; int main() { ios::sync_with_stdio(false); int n; cin >> n; cin >> s + 1; for (int i = 1; i < n; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; ans.push_back(i); } } if (s[n] == 'W') { cout << ans.size() << endl; for (auto i : ans) cout << i << " "; } else { if (n & 1) { cout << ans.size() + (n - 1) / 2 << endl; for (auto i : ans) cout << i << " "; for (int i = 1; i <= n - 1; i += 2) cout << i << " "; } else cout << -1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; int l[n]; int wh = 0; int bl = 0; for (int i = 0; i < n; i++) { l[i] = s.at(i) == 'W' ? 1 : 0; if (l[i] == 0) { bl++; } else { wh++; } } if (bl % 2 == 1 && wh % 2 == 1) { cout << -1; return 0; } int m; if (wh % 2 == 0) { m = 0; } else { m = 1; } vector<int> ans; int ind = -1; for (int i = 0; i < n; i++) { if (l[i] != m) { ind = i; break; } } while (ind != -1) { ans.push_back(ind + 1); l[ind] = l[ind] == 1 ? 0 : 1; l[ind + 1] = l[ind + 1] == 1 ? 0 : 1; ind = -1; for (int i = 0; i < n; i++) { if (l[i] != m) { ind = i; break; } } } cout << ans.size() << "\n"; for (int x : ans) cout << x << " "; }
### Prompt In CPP, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; int l[n]; int wh = 0; int bl = 0; for (int i = 0; i < n; i++) { l[i] = s.at(i) == 'W' ? 1 : 0; if (l[i] == 0) { bl++; } else { wh++; } } if (bl % 2 == 1 && wh % 2 == 1) { cout << -1; return 0; } int m; if (wh % 2 == 0) { m = 0; } else { m = 1; } vector<int> ans; int ind = -1; for (int i = 0; i < n; i++) { if (l[i] != m) { ind = i; break; } } while (ind != -1) { ans.push_back(ind + 1); l[ind] = l[ind] == 1 ? 0 : 1; l[ind + 1] = l[ind + 1] == 1 ? 0 : 1; ind = -1; for (int i = 0; i < n; i++) { if (l[i] != m) { ind = i; break; } } } cout << ans.size() << "\n"; for (int x : ans) cout << x << " "; } ```
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long power(long long x, long long n) { if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) { long long y = power(x, n / 2) % mod; return (y * y) % mod; } if (n & 1) { long long y = power(x, n - 1); return (x % mod * y % mod) % mod; } return 0; } long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; const long long maxn = 200005; void solve() { long long n, i, j, k, m; cin >> n; string s; cin >> s; vector<long long> ans; i = 0; if (s[0] == 'W') i++; for (i; i < n - 1; i++) { if (s[i] == 'B') { ans.push_back(i); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'W') { cout << (long long)ans.size() << '\n'; for (long long x : ans) cout << x + 1 << " "; exit(0); } i = 0; if (s[0] == 'B') i++; for (i; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << (long long)ans.size() << '\n'; for (long long x : ans) cout << x + 1 << " "; exit(0); } cout << -1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) solve(); return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long power(long long x, long long n) { if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) { long long y = power(x, n / 2) % mod; return (y * y) % mod; } if (n & 1) { long long y = power(x, n - 1); return (x % mod * y % mod) % mod; } return 0; } long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; const long long maxn = 200005; void solve() { long long n, i, j, k, m; cin >> n; string s; cin >> s; vector<long long> ans; i = 0; if (s[0] == 'W') i++; for (i; i < n - 1; i++) { if (s[i] == 'B') { ans.push_back(i); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'W') { cout << (long long)ans.size() << '\n'; for (long long x : ans) cout << x + 1 << " "; exit(0); } i = 0; if (s[0] == 'B') i++; for (i; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << (long long)ans.size() << '\n'; for (long long x : ans) cout << x + 1 << " "; exit(0); } cout << -1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; char ar[n]; for (int i = 0; i < n; ++i) { cin >> ar[i]; } int co = 0; vector<int> po; for (int i = 0; i < n - 1; ++i) { if (ar[i] == 'W') { ar[i] = 'B'; co++; po.push_back(i + 1); if (ar[i + 1] == 'B') { ar[i + 1] = 'W'; } else { ar[i + 1] = 'B'; } } } if (ar[n - 1] == 'B') { cout << co << endl; for (int i = 0; i < po.size(); ++i) { cout << po[i] << " "; } cout << endl; } else { if (n % 2 == 0) { cout << -1 << endl; } else { cout << co + (n - 1) / 2 << endl; for (int i = 0; i < po.size(); ++i) { cout << po[i] << " "; } for (int i = 0; i < (n - 1) / 2; ++i) { cout << 2 * i + 1 << " "; } cout << endl; } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; char ar[n]; for (int i = 0; i < n; ++i) { cin >> ar[i]; } int co = 0; vector<int> po; for (int i = 0; i < n - 1; ++i) { if (ar[i] == 'W') { ar[i] = 'B'; co++; po.push_back(i + 1); if (ar[i + 1] == 'B') { ar[i + 1] = 'W'; } else { ar[i + 1] = 'B'; } } } if (ar[n - 1] == 'B') { cout << co << endl; for (int i = 0; i < po.size(); ++i) { cout << po[i] << " "; } cout << endl; } else { if (n % 2 == 0) { cout << -1 << endl; } else { cout << co + (n - 1) / 2 << endl; for (int i = 0; i < po.size(); ++i) { cout << po[i] << " "; } for (int i = 0; i < (n - 1) / 2; ++i) { cout << 2 * i + 1 << " "; } cout << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } int main() { file(); fast(); int n; cin >> n; string str; cin >> str; int flib = 0; map<char, int> mp; mp['B'] = 0, mp['W'] = 1; vector<int> show; vector<int> st(n + 9); for (int i = 0; i < n - 1; i++) { flib = st[i]; int x = mp[str[i]]; if (flib) x = 1 - x; if (!x) continue; show.push_back(i + 1); st[i + 1] = 1 - st[i + 1]; } int s = mp[str.back()]; if (st[n - 1]) s = 1 - s; if (!s) { cout << (int)(show.size()) << "\n"; for (auto it : show) cout << it << ' '; return cout << "\n" << "\n", 0; } show.clear(); flib = 0; st = vector<int>(n + 9); for (int i = 0; i < n - 1; i++) { flib = st[i]; int x = mp[str[i]]; if (flib) x = 1 - x; if (x == 1) continue; show.push_back(i + 1); st[i + 1] = 1 - st[i + 1]; } s = mp[str.back()]; if (st[n - 1]) s = 1 - s; if (s) { cout << (int)(show.size()) << "\n"; for (auto it : show) cout << it << ' '; return cout << "\n" << "\n", 0; } cout << -1 << "\n"; return 0; }
### Prompt Please formulate a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } int main() { file(); fast(); int n; cin >> n; string str; cin >> str; int flib = 0; map<char, int> mp; mp['B'] = 0, mp['W'] = 1; vector<int> show; vector<int> st(n + 9); for (int i = 0; i < n - 1; i++) { flib = st[i]; int x = mp[str[i]]; if (flib) x = 1 - x; if (!x) continue; show.push_back(i + 1); st[i + 1] = 1 - st[i + 1]; } int s = mp[str.back()]; if (st[n - 1]) s = 1 - s; if (!s) { cout << (int)(show.size()) << "\n"; for (auto it : show) cout << it << ' '; return cout << "\n" << "\n", 0; } show.clear(); flib = 0; st = vector<int>(n + 9); for (int i = 0; i < n - 1; i++) { flib = st[i]; int x = mp[str[i]]; if (flib) x = 1 - x; if (x == 1) continue; show.push_back(i + 1); st[i + 1] = 1 - st[i + 1]; } s = mp[str.back()]; if (st[n - 1]) s = 1 - s; if (s) { cout << (int)(show.size()) << "\n"; for (auto it : show) cout << it << ' '; return cout << "\n" << "\n", 0; } cout << -1 << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long ans, sum, tot, cnt, mx, mn, idx, chk, rem, ret, tmp, mid, lft, rgt, pos; long long tc, n, m, a, b, c, d, g, h, l, r, x, y, i, j, k; cin >> n; string s; cin >> s; long long w; w = b = 0; for (i = 0; i < n; ++i) { if (s[i] == 66) b++; else w++; } if (b & 1 && w & 1) { cout << -1 << endl; return 0; } if (b == 0 || w == 0) { cout << 0 << endl; return 0; } vector<long long> v; char cc; if (b % 2 == 0 && w % 2 == 0) cc = s[0]; else if (b & 1) cc = 'B'; else cc = 'W'; for (i = 0; i < n; ++i) { if (s[i] != cc) { swap(s[i], s[i + 1]); v.push_back(i + 1); } if (s[i] == s[i + 1]) i++; } cout << v.size() << endl; for (auto it : v) cout << it << ' '; return 0; }
### Prompt Create a solution in CPP for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long ans, sum, tot, cnt, mx, mn, idx, chk, rem, ret, tmp, mid, lft, rgt, pos; long long tc, n, m, a, b, c, d, g, h, l, r, x, y, i, j, k; cin >> n; string s; cin >> s; long long w; w = b = 0; for (i = 0; i < n; ++i) { if (s[i] == 66) b++; else w++; } if (b & 1 && w & 1) { cout << -1 << endl; return 0; } if (b == 0 || w == 0) { cout << 0 << endl; return 0; } vector<long long> v; char cc; if (b % 2 == 0 && w % 2 == 0) cc = s[0]; else if (b & 1) cc = 'B'; else cc = 'W'; for (i = 0; i < n; ++i) { if (s[i] != cc) { swap(s[i], s[i + 1]); v.push_back(i + 1); } if (s[i] == s[i + 1]) i++; } cout << v.size() << endl; for (auto it : v) cout << it << ' '; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e9; const ll MOD = 1e9 + 7; int main() { int n; string s; cin >> n >> s; vector<int> b; string t = s; for (int i = 0; i < (int)(n - 1); i++) { if (t[i] == 'W') { t[i] = 'B'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; b.push_back(i); } } bool ok = true; for (int i = 0; i < (int)(n); i++) { if (t[i] != 'B') ok = false; } if (ok) { cout << b.size() << endl; for (int i = 0; i < (int)(b.size()); i++) cout << b[i] + 1 << " "; cout << endl; return 0; } vector<int> w; t = s; for (int i = 0; i < (int)(n - 1); i++) { if (t[i] == 'B') { t[i] = 'W'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; w.push_back(i); } } ok = true; for (int i = 0; i < (int)(n); i++) { if (t[i] != 'W') ok = false; } if (ok) { cout << w.size() << endl; for (int i = 0; i < (int)(w.size()); i++) cout << w[i] + 1 << " "; cout << endl; return 0; } cout << -1 << endl; }
### Prompt Generate a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e9; const ll MOD = 1e9 + 7; int main() { int n; string s; cin >> n >> s; vector<int> b; string t = s; for (int i = 0; i < (int)(n - 1); i++) { if (t[i] == 'W') { t[i] = 'B'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; b.push_back(i); } } bool ok = true; for (int i = 0; i < (int)(n); i++) { if (t[i] != 'B') ok = false; } if (ok) { cout << b.size() << endl; for (int i = 0; i < (int)(b.size()); i++) cout << b[i] + 1 << " "; cout << endl; return 0; } vector<int> w; t = s; for (int i = 0; i < (int)(n - 1); i++) { if (t[i] == 'B') { t[i] = 'W'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; w.push_back(i); } } ok = true; for (int i = 0; i < (int)(n); i++) { if (t[i] != 'W') ok = false; } if (ok) { cout << w.size() << endl; for (int i = 0; i < (int)(w.size()); i++) cout << w[i] + 1 << " "; cout << endl; return 0; } cout << -1 << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int a[1000]; int cnt = 0; for (int i = 1; i <= n - 2; i++) { if (s[i] != s[i - 1]) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; a[cnt++] = i; } } if (s[0] != s[n - 1]) if ((n - 1) % 2 == 0) { for (int i = 0; i <= n - 3; i += 2) a[cnt++] = i; s[n - 1] = s[0]; } if (s[0] != s[n - 1]) puts("-1"); else { cout << cnt << endl; for (int i = 0; i < cnt; i++) cout << a[i] + 1 << ' '; } return 0; }
### Prompt Generate a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int a[1000]; int cnt = 0; for (int i = 1; i <= n - 2; i++) { if (s[i] != s[i - 1]) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; a[cnt++] = i; } } if (s[0] != s[n - 1]) if ((n - 1) % 2 == 0) { for (int i = 0; i <= n - 3; i += 2) a[cnt++] = i; s[n - 1] = s[0]; } if (s[0] != s[n - 1]) puts("-1"); else { cout << cnt << endl; for (int i = 0; i < cnt; i++) cout << a[i] + 1 << ' '; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; string s; cin >> n >> s; int nb = 0, nw = 0; for (auto ai : s) { if (ai == 'B') { nb++; } else { nw++; } } if (nb & 1 && nw & 1) { cout << -1; return 0; } vector<int> ve; if (!(nb & 1) && !(nw & 1)) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { ve.emplace_back(i); i++; } else if (s[i] == 'B') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } } else if (nb & 1) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = s[i + 1] = 'W'; ve.emplace_back(i++); } else if (s[i] == 'B') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } for (int i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'W') { ve.emplace_back(i); i++; } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = s[i + 1] = 'B'; ve.emplace_back(i++); } else if (s[i] == 'W') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { ve.emplace_back(i); i++; } } } cout << ve.size() << '\n'; for (auto ai : ve) { cout << ai + 1 << ' '; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; string s; cin >> n >> s; int nb = 0, nw = 0; for (auto ai : s) { if (ai == 'B') { nb++; } else { nw++; } } if (nb & 1 && nw & 1) { cout << -1; return 0; } vector<int> ve; if (!(nb & 1) && !(nw & 1)) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { ve.emplace_back(i); i++; } else if (s[i] == 'B') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } } else if (nb & 1) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = s[i + 1] = 'W'; ve.emplace_back(i++); } else if (s[i] == 'B') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } for (int i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'W') { ve.emplace_back(i); i++; } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = s[i + 1] = 'B'; ve.emplace_back(i++); } else if (s[i] == 'W') { swap(s[i], s[i + 1]); ve.emplace_back(i); } } for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'B') { ve.emplace_back(i); i++; } } } cout << ve.size() << '\n'; for (auto ai : ve) { cout << ai + 1 << ' '; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> n >> s; int white = count(s.begin(), s.end(), 'W'); int black = count(s.begin(), s.end(), 'B'); if (white % 2 && black % 2) { cout << "-1" << endl; return; } if (white == s.size() || black == s.size()) { cout << "0" << endl; return; } vector<int> v; if (white % 2) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { v.push_back(i + 1); if (s[i + 1] == 'B') { i++; } else { s[i + 1] = 'B'; } } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { v.push_back(i + 1); if (s[i + 1] == 'W') { i++; } else { s[i + 1] = 'W'; } } } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; solve(); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> n >> s; int white = count(s.begin(), s.end(), 'W'); int black = count(s.begin(), s.end(), 'B'); if (white % 2 && black % 2) { cout << "-1" << endl; return; } if (white == s.size() || black == s.size()) { cout << "0" << endl; return; } vector<int> v; if (white % 2) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { v.push_back(i + 1); if (s[i + 1] == 'B') { i++; } else { s[i + 1] = 'B'; } } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { v.push_back(i + 1); if (s[i + 1] == 'W') { i++; } else { s[i + 1] = 'W'; } } } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; string str; cin >> str; int cw = 0, cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'B') { cb++; } else { cw++; } } if (cb % 2 != 0 && cw % 2 != 0) { cout << -1; return 0; } int tr = 0; vector<int> ans; bool f = false; if (cb % 2 == 0) { f = true; } while (tr < 3 * n) { for (int i = 0; i < n - 1; i++) { if (cb == 0 || cw == 0) { cout << ans.size() << "\n"; for (int j = 0; j < ans.size(); j++) { cout << ans[j] << " "; } return 0; } if (f) { if (str[i] == 'B') { str[i] = 'W'; cb--; cw++; ans.push_back(i + 1); tr++; if (str[i + 1] == 'B') { str[i + 1] = 'W'; cb--; cw++; } else { str[i + 1] = 'B'; cw--; cb++; } } } else { if (str[i] == 'W') { str[i] = 'B'; cb++; cw--; ans.push_back(i + 1); tr++; if (str[i + 1] == 'B') { str[i + 1] = 'W'; cb--; cw++; } else { str[i + 1] = 'B'; cw--; cb++; } } } } } cout << -1; }
### Prompt Please create a solution in cpp to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; string str; cin >> str; int cw = 0, cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'B') { cb++; } else { cw++; } } if (cb % 2 != 0 && cw % 2 != 0) { cout << -1; return 0; } int tr = 0; vector<int> ans; bool f = false; if (cb % 2 == 0) { f = true; } while (tr < 3 * n) { for (int i = 0; i < n - 1; i++) { if (cb == 0 || cw == 0) { cout << ans.size() << "\n"; for (int j = 0; j < ans.size(); j++) { cout << ans[j] << " "; } return 0; } if (f) { if (str[i] == 'B') { str[i] = 'W'; cb--; cw++; ans.push_back(i + 1); tr++; if (str[i + 1] == 'B') { str[i + 1] = 'W'; cb--; cw++; } else { str[i + 1] = 'B'; cw--; cb++; } } } else { if (str[i] == 'W') { str[i] = 'B'; cb++; cw--; ans.push_back(i + 1); tr++; if (str[i + 1] == 'B') { str[i + 1] = 'W'; cb--; cw++; } else { str[i + 1] = 'B'; cw--; cb++; } } } } } cout << -1; } ```
#include <bits/stdc++.h> using namespace std; void fragmentarA() { long long h1, h2, h3, h4, h5, h6, h7, h8, h9; h1 = h2 + h3 + h4; h2 = h1 + h3 + h5 + 9; h3 = h4 + h1 + h6; h3 = h5 + h6; h5 = h6 + h2; h1 = h2 + h5 + h6; h6 = h1 + h2; h5 = h2 + h3; h9 = h2 + h6; h5 = h6 + h7; h2 = h6 + h7 + h5; h1 = h2 + h3 + h6; return; } void fragmentarB() { long long u1, u2, u3, u4, u5, u6, u7, u8, u9; u1 = u1 + u2 + u3; u3 + u2 + u4 + u2; u7 = u1 + u5; u8 = u1 + u2; u9 = u2 + u5 + u9; u2 = u1 + u2 + u3; u5 = u1 + u4; u6 = u1 + u2 + u3 + u4; u7 = u1 + u7 + u8 + u6; u3 = u1 + u2 + u3 + u4 + u5 + u7; u1 = u6 + u4 + u6 + u7 + u8; return; } void fragmentarC() { long long y1, y2, y3, y4, y5, y6, y7, y8, y9; y3 = y1 = y6 + y7 + y8 + y9 + y3; y1 = y2 + y4 + y7 + y8; y5 = y2 + y4 + y6 + y8; y6 = y2 + y4 + y5 + y7 + y8; y8 = y1 + y2 + y4 + y6 + y8; y4 = y1 + y2 + y4 + y5; y2 = y7 + y8 + y9 + y5; y6 = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8; y2 = y1 + y2 + y3 + y4 + y6 + y7; y2 = y3 + y4 + y6 + y7; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; string s; cin >> s; long long sum1 = 0, sum2 = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') sum1++; if (s[i] == 'B') sum2++; } long long min1 = min(sum1, sum2); string t = s; vector<long long> v1, v2; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; v1.push_back(i + 1); } } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); for (long long i = 0; i < n - 1; i++) { if (t[i] == 'B') { t[i] = 'W'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; v2.push_back(i + 1); } } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); if (t[n - 1] == 'B' && s[n - 1] == 'W') cout << -1 << "\n"; else if (s[n - 1] == 'B') { cout << v1.size() << "\n"; for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " "; } else { cout << v2.size() << "\n"; for (long long i = 0; i < v2.size(); i++) cout << v2[i] << " "; } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void fragmentarA() { long long h1, h2, h3, h4, h5, h6, h7, h8, h9; h1 = h2 + h3 + h4; h2 = h1 + h3 + h5 + 9; h3 = h4 + h1 + h6; h3 = h5 + h6; h5 = h6 + h2; h1 = h2 + h5 + h6; h6 = h1 + h2; h5 = h2 + h3; h9 = h2 + h6; h5 = h6 + h7; h2 = h6 + h7 + h5; h1 = h2 + h3 + h6; return; } void fragmentarB() { long long u1, u2, u3, u4, u5, u6, u7, u8, u9; u1 = u1 + u2 + u3; u3 + u2 + u4 + u2; u7 = u1 + u5; u8 = u1 + u2; u9 = u2 + u5 + u9; u2 = u1 + u2 + u3; u5 = u1 + u4; u6 = u1 + u2 + u3 + u4; u7 = u1 + u7 + u8 + u6; u3 = u1 + u2 + u3 + u4 + u5 + u7; u1 = u6 + u4 + u6 + u7 + u8; return; } void fragmentarC() { long long y1, y2, y3, y4, y5, y6, y7, y8, y9; y3 = y1 = y6 + y7 + y8 + y9 + y3; y1 = y2 + y4 + y7 + y8; y5 = y2 + y4 + y6 + y8; y6 = y2 + y4 + y5 + y7 + y8; y8 = y1 + y2 + y4 + y6 + y8; y4 = y1 + y2 + y4 + y5; y2 = y7 + y8 + y9 + y5; y6 = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8; y2 = y1 + y2 + y3 + y4 + y6 + y7; y2 = y3 + y4 + y6 + y7; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; string s; cin >> s; long long sum1 = 0, sum2 = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') sum1++; if (s[i] == 'B') sum2++; } long long min1 = min(sum1, sum2); string t = s; vector<long long> v1, v2; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; v1.push_back(i + 1); } } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); for (long long i = 0; i < n - 1; i++) { if (t[i] == 'B') { t[i] = 'W'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; v2.push_back(i + 1); } } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); if (t[n - 1] == 'B' && s[n - 1] == 'W') cout << -1 << "\n"; else if (s[n - 1] == 'B') { cout << v1.size() << "\n"; for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " "; } else { cout << v2.size() << "\n"; for (long long i = 0; i < v2.size(); i++) cout << v2[i] << " "; } fragmentarC(); fragmentarC(); fragmentarB(); fragmentarA(); fragmentarB(); fragmentarA(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; string s; cin >> s; string p = s; long long ans1 = 0, ans2 = 0; vector<long long> v1, v2; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; v1.push_back(i); } } for (int i = 0; i < n; i++) { if (s[i] == 'B') continue; else { ans1 = -1; break; } } if (ans1 != -1) { cout << v1.size() << endl; for (auto x : v1) cout << x + 1 << " "; cout << endl; return 0; } s = p; if (ans1 == -1) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; v2.push_back(i); } } for (int i = 0; i < n; i++) { if (s[i] == 'W') continue; else { ans2 = -1; break; } } } if (ans2 != -1) { cout << v2.size() << endl; for (auto x : v2) cout << x + 1 << " "; cout << endl; } else cout << "-1" << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; string s; cin >> s; string p = s; long long ans1 = 0, ans2 = 0; vector<long long> v1, v2; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; v1.push_back(i); } } for (int i = 0; i < n; i++) { if (s[i] == 'B') continue; else { ans1 = -1; break; } } if (ans1 != -1) { cout << v1.size() << endl; for (auto x : v1) cout << x + 1 << " "; cout << endl; return 0; } s = p; if (ans1 == -1) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; v2.push_back(i); } } for (int i = 0; i < n; i++) { if (s[i] == 'W') continue; else { ans2 = -1; break; } } } if (ans2 != -1) { cout << v2.size() << endl; for (auto x : v2) cout << x + 1 << " "; cout << endl; } else cout << "-1" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string z; cin >> n >> z; int c1 = 0, c2 = 0; for (int i = 0; i < n; i++) { if (z[i] == 'W') c1++; else c2++; } if (c1 % 2 != 0 && c2 % 2 != 0) { cout << -1; return 0; } if (c1 % 2 == 0) { vector<int> ans; for (int i = 0; i < n - 1; i++) { if (z[i] == 'W') { if (z[i + 1] == 'W') { ans.push_back(i + 1); i++; } else { ans.push_back(i + 1); z[i + 1] = 'W'; } } } int counter = ans.size(); cout << counter << "\n"; for (int i = 0; i < counter; i++) { cout << ans[i] << " "; } return 0; } if (c2 % 2 == 0) { vector<int> ans; for (int i = 0; i < n - 1; i++) { if (z[i] == 'B') { if (z[i + 1] == 'B') { ans.push_back(i + 1); i++; } else { ans.push_back(i + 1); z[i + 1] = 'B'; } } } int counter = ans.size(); cout << counter << "\n"; for (int i = 0; i < counter; i++) { cout << ans[i] << " "; } return 0; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string z; cin >> n >> z; int c1 = 0, c2 = 0; for (int i = 0; i < n; i++) { if (z[i] == 'W') c1++; else c2++; } if (c1 % 2 != 0 && c2 % 2 != 0) { cout << -1; return 0; } if (c1 % 2 == 0) { vector<int> ans; for (int i = 0; i < n - 1; i++) { if (z[i] == 'W') { if (z[i + 1] == 'W') { ans.push_back(i + 1); i++; } else { ans.push_back(i + 1); z[i + 1] = 'W'; } } } int counter = ans.size(); cout << counter << "\n"; for (int i = 0; i < counter; i++) { cout << ans[i] << " "; } return 0; } if (c2 % 2 == 0) { vector<int> ans; for (int i = 0; i < n - 1; i++) { if (z[i] == 'B') { if (z[i + 1] == 'B') { ans.push_back(i + 1); i++; } else { ans.push_back(i + 1); z[i + 1] = 'B'; } } } int counter = ans.size(); cout << counter << "\n"; for (int i = 0; i < counter; i++) { cout << ans[i] << " "; } return 0; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 10010; int pos[N]; int main() { int n; cin >> n; string s; cin >> s; s = " " + s; string b = s; int cnt = 0; int i; int s1 = 0, s2 = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'W') s1++; } if (s1 == n || s1 == 0) { cout << 0 << endl; return 0; } for (i = 2; i <= s.size(); i++) { if (s[i] == 'W' && s[i - 1] == 'W') { pos[cnt++] = i - 1; s[i] = s[i - 1] = 'B'; } } int sum = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'W') sum++; } if (sum % 2) { s = b; int cnt = 0; int i; for (i = 2; i <= s.size(); i++) { if (s[i] == 'B' && s[i - 1] == 'B') { pos[cnt++] = i - 1; s[i] = s[i - 1] = 'W'; } } int sum = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'B') sum++; } if (sum % 2) { cout << -1 << endl; } else { for (i = 1; i < s.size(); i++) { if (s[i] == 'B') { pos[cnt++] = i; s[i] = 'W'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; } } if (cnt > 3 * n) { cout << -1 << endl; return 0; } cout << cnt << endl; for (i = 0; i < cnt - 1; i++) { cout << pos[i] << " "; } cout << pos[i] << endl; } return 0; } else { for (i = 1; i < s.size(); i++) { if (s[i] == 'W') { pos[cnt++] = i; s[i] = 'B'; s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B'; } } if (cnt > 3 * n) { cout << -1 << endl; return 0; } cout << cnt << endl; for (i = 0; i < cnt - 1; i++) { cout << pos[i] << " "; } cout << pos[i] << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 10010; int pos[N]; int main() { int n; cin >> n; string s; cin >> s; s = " " + s; string b = s; int cnt = 0; int i; int s1 = 0, s2 = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'W') s1++; } if (s1 == n || s1 == 0) { cout << 0 << endl; return 0; } for (i = 2; i <= s.size(); i++) { if (s[i] == 'W' && s[i - 1] == 'W') { pos[cnt++] = i - 1; s[i] = s[i - 1] = 'B'; } } int sum = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'W') sum++; } if (sum % 2) { s = b; int cnt = 0; int i; for (i = 2; i <= s.size(); i++) { if (s[i] == 'B' && s[i - 1] == 'B') { pos[cnt++] = i - 1; s[i] = s[i - 1] = 'W'; } } int sum = 0; for (i = 1; i <= s.size(); i++) { if (s[i] == 'B') sum++; } if (sum % 2) { cout << -1 << endl; } else { for (i = 1; i < s.size(); i++) { if (s[i] == 'B') { pos[cnt++] = i; s[i] = 'W'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; } } if (cnt > 3 * n) { cout << -1 << endl; return 0; } cout << cnt << endl; for (i = 0; i < cnt - 1; i++) { cout << pos[i] << " "; } cout << pos[i] << endl; } return 0; } else { for (i = 1; i < s.size(); i++) { if (s[i] == 'W') { pos[cnt++] = i; s[i] = 'B'; s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B'; } } if (cnt > 3 * n) { cout << -1 << endl; return 0; } cout << cnt << endl; for (i = 0; i < cnt - 1; i++) { cout << pos[i] << " "; } cout << pos[i] << endl; } } ```
#include <bits/stdc++.h> using namespace std; vector<char> z; long long x, y, dl, mi, ma, b, s, m, k, w, a[1000], ko; char p; int main() { cin >> dl; for (int i = 0; i < dl; i++) { cin >> p; z.push_back(p); if (p == 'W') w++; else b++; } if (w % 2 != 0 && b % 2 != 0) { cout << -1; return 0; } else if (w % 2 == 0) { for (int i = 0; i < dl; i++) { if (z[i] == 'W' && z[i + 1] == 'W') { a[ko] = i; z[i] = 'B'; z[i + 1] = 'B'; ko++; } else if (z[i] == 'W' && z[i + 1] == 'B') { z[i] = 'B'; z[i + 1] = 'W'; a[ko] = i; ko++; } } cout << ko << endl; for (int i = 0; i < ko; i++) cout << a[i] + 1 << " "; } else if (b % 2 == 0) { for (int i = 0; i < dl; i++) { if (z[i] == 'B' && z[i + 1] == 'B') { z[i] = 'W'; z[i + 1] = 'W'; a[ko] = i; ko++; } else if (z[i] == 'B' && z[i + 1] == 'W') { z[i] = 'W'; z[i + 1] = 'B'; a[ko] = i; ko++; } } cout << ko << endl; for (int i = 0; i < ko; i++) cout << a[i] + 1 << " "; } return 0; }
### Prompt Create a solution in Cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<char> z; long long x, y, dl, mi, ma, b, s, m, k, w, a[1000], ko; char p; int main() { cin >> dl; for (int i = 0; i < dl; i++) { cin >> p; z.push_back(p); if (p == 'W') w++; else b++; } if (w % 2 != 0 && b % 2 != 0) { cout << -1; return 0; } else if (w % 2 == 0) { for (int i = 0; i < dl; i++) { if (z[i] == 'W' && z[i + 1] == 'W') { a[ko] = i; z[i] = 'B'; z[i + 1] = 'B'; ko++; } else if (z[i] == 'W' && z[i + 1] == 'B') { z[i] = 'B'; z[i + 1] = 'W'; a[ko] = i; ko++; } } cout << ko << endl; for (int i = 0; i < ko; i++) cout << a[i] + 1 << " "; } else if (b % 2 == 0) { for (int i = 0; i < dl; i++) { if (z[i] == 'B' && z[i + 1] == 'B') { z[i] = 'W'; z[i + 1] = 'W'; a[ko] = i; ko++; } else if (z[i] == 'B' && z[i + 1] == 'W') { z[i] = 'W'; z[i + 1] = 'B'; a[ko] = i; ko++; } } cout << ko << endl; for (int i = 0; i < ko; i++) cout << a[i] + 1 << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int c = 0; unordered_map<string, bool> memo; bool sb(int i, string s, vector<int> moves, vector<int>& sol, int mreset) { string rem = s; if (memo.find(rem) != memo.end()) return memo.at(rem); if (s.find('W') == string::npos || s.find('B') == string::npos) { sol = moves; memo[rem] = true; return true; } if (i >= s.size() - 1) { if (mreset == 0) { memo[rem] = false; return false; } mreset = 0; i = 0; } if (moves.size() >= s.size() * 3) { moves.clear(); memo[rem] = false; return false; } if (s[i] == s[i + 1]) { s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; moves.push_back(i); mreset++; if (sb(i + 2, s, moves, sol, mreset)) { return true; } } if (sb(i + 1, s, moves, sol, mreset)) { return true; } if (true) { s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; moves.push_back(i); mreset++; if (sb(i + 1, s, moves, sol, mreset)) { return true; } } memo[rem] = false; return false; } int main() { int n; string s; cin >> n >> s; vector<int> sol; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B' && s[i + 1] == 'W') { s[i] = 'W'; s[i + 1] = 'B'; sol.push_back(i); } else if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; sol.push_back(i); } } const int nW = count(begin(s), end(s), 'W'); const int nB = count(begin(s), end(s), 'B'); if (nW % 2 != 0 && nB % 2 != 0) { cout << -1 << endl; } else { if (nW % 2 != 0) { for (int i = 0; i < s.size() - 1; ++i) { if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; sol.push_back(i); } } } else { for (int i = 0; i < s.size() - 1; ++i) { if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = 'B'; s[i + 1] = 'B'; sol.push_back(i); } } } cout << sol.size() << endl; for (int i = 0; i < sol.size(); ++i) { cout << sol[i] + 1; if (i == sol.size() - 1) cout << endl; else cout << " "; } } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int c = 0; unordered_map<string, bool> memo; bool sb(int i, string s, vector<int> moves, vector<int>& sol, int mreset) { string rem = s; if (memo.find(rem) != memo.end()) return memo.at(rem); if (s.find('W') == string::npos || s.find('B') == string::npos) { sol = moves; memo[rem] = true; return true; } if (i >= s.size() - 1) { if (mreset == 0) { memo[rem] = false; return false; } mreset = 0; i = 0; } if (moves.size() >= s.size() * 3) { moves.clear(); memo[rem] = false; return false; } if (s[i] == s[i + 1]) { s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; moves.push_back(i); mreset++; if (sb(i + 2, s, moves, sol, mreset)) { return true; } } if (sb(i + 1, s, moves, sol, mreset)) { return true; } if (true) { s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; moves.push_back(i); mreset++; if (sb(i + 1, s, moves, sol, mreset)) { return true; } } memo[rem] = false; return false; } int main() { int n; string s; cin >> n >> s; vector<int> sol; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B' && s[i + 1] == 'W') { s[i] = 'W'; s[i + 1] = 'B'; sol.push_back(i); } else if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; sol.push_back(i); } } const int nW = count(begin(s), end(s), 'W'); const int nB = count(begin(s), end(s), 'B'); if (nW % 2 != 0 && nB % 2 != 0) { cout << -1 << endl; } else { if (nW % 2 != 0) { for (int i = 0; i < s.size() - 1; ++i) { if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; sol.push_back(i); } } } else { for (int i = 0; i < s.size() - 1; ++i) { if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = 'B'; s[i + 1] = 'B'; sol.push_back(i); } } } cout << sol.size() << endl; for (int i = 0; i < sol.size(); ++i) { cout << sol[i] + 1; if (i == sol.size() - 1) cout << endl; else cout << " "; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string s, s1; cin >> s; s1 = s; int i, j, k, l; vector<int> v; for (i = 0; i < n; i++) { if (i + 1 < n && s[i] == 'B') { v.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'W') { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } v.clear(); for (i = 0; i < n; i++) { if (i + 1 < n && s1[i] == 'W') { v.push_back(i + 1); s1[i] = 'B'; if (s1[i + 1] == 'B') s1[i + 1] = 'W'; else s1[i + 1] = 'B'; } } if (s1[n - 1] == 'B') { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; } else { cout << "-1\n"; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string s, s1; cin >> s; s1 = s; int i, j, k, l; vector<int> v; for (i = 0; i < n; i++) { if (i + 1 < n && s[i] == 'B') { v.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'W') { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } v.clear(); for (i = 0; i < n; i++) { if (i + 1 < n && s1[i] == 'W') { v.push_back(i + 1); s1[i] = 'B'; if (s1[i + 1] == 'B') s1[i + 1] = 'W'; else s1[i + 1] = 'B'; } } if (s1[n - 1] == 'B') { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; } else { cout << "-1\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arrp[n]; int arrn[n]; char t; int b = 0, w = 0; for (int i = 0; i < n; i++) { cin >> t; if (t == 'B') { b++; arrp[i] = -1; arrn[i] = -1; } else { w++; arrp[i] = 1; arrn[i] = 1; } } if (n % 2 == 0 && b % 2 != 0) { cout << -1; return 0; } if (b == 0 || w == 0) { cout << 0; return 0; } vector<int> ind; ind.reserve(n); int changes = 0; bool noChange = true; bool solved = true; for (int i = 0; i < n; i++) { if (arrp[i] == -1) { if (i == n - 1) { solved = false; } else { arrp[i] *= -1; changes++; arrp[i + 1] *= -1; ind.push_back(i + 1); } } } if (!solved) { solved = true; ind.clear(); changes = 0; for (int i = 0; i < n; i++) { if (arrn[i] == 1) { if (i == n - 1) { solved = false; } else { arrn[i] *= -1; arrn[i + 1] *= -1; ind.push_back(i + 1); changes++; } } } } if (solved) { cout << changes << endl; for (auto i : ind) { cout << i << " "; } } else { cout << -1; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arrp[n]; int arrn[n]; char t; int b = 0, w = 0; for (int i = 0; i < n; i++) { cin >> t; if (t == 'B') { b++; arrp[i] = -1; arrn[i] = -1; } else { w++; arrp[i] = 1; arrn[i] = 1; } } if (n % 2 == 0 && b % 2 != 0) { cout << -1; return 0; } if (b == 0 || w == 0) { cout << 0; return 0; } vector<int> ind; ind.reserve(n); int changes = 0; bool noChange = true; bool solved = true; for (int i = 0; i < n; i++) { if (arrp[i] == -1) { if (i == n - 1) { solved = false; } else { arrp[i] *= -1; changes++; arrp[i + 1] *= -1; ind.push_back(i + 1); } } } if (!solved) { solved = true; ind.clear(); changes = 0; for (int i = 0; i < n; i++) { if (arrn[i] == 1) { if (i == n - 1) { solved = false; } else { arrn[i] *= -1; arrn[i + 1] *= -1; ind.push_back(i + 1); changes++; } } } } if (solved) { cout << changes << endl; for (auto i : ind) { cout << i << " "; } } else { cout << -1; } return 0; } ```
#include <bits/stdc++.h> int main() { int n; std::cin >> n; std::vector<char> str(n); int cnt[2] = {0, 0}; for (int i = 0; i < n; i++) { std::cin >> str[i]; str[i] = str[i] == 'B' ? 0 : 1; cnt[str[i]]++; } if (cnt[0] % 2 && cnt[1] % 2) { printf("-1\n"); return 0; } bool need = (cnt[0] % 2) ? 0 : 1; std::vector<int> ans; for (int i = 0; i < n; i++) { if (str[i] != need) { str[i] = !str[i]; str[i + 1] = !str[i + 1]; ans.push_back(i); } } printf("%d\n", (int)ans.size()); for (auto x : ans) { printf("%d ", x + 1); } puts(""); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> int main() { int n; std::cin >> n; std::vector<char> str(n); int cnt[2] = {0, 0}; for (int i = 0; i < n; i++) { std::cin >> str[i]; str[i] = str[i] == 'B' ? 0 : 1; cnt[str[i]]++; } if (cnt[0] % 2 && cnt[1] % 2) { printf("-1\n"); return 0; } bool need = (cnt[0] % 2) ? 0 : 1; std::vector<int> ans; for (int i = 0; i < n; i++) { if (str[i] != need) { str[i] = !str[i]; str[i + 1] = !str[i + 1]; ans.push_back(i); } } printf("%d\n", (int)ans.size()); for (auto x : ans) { printf("%d ", x + 1); } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; int ans[1000]; int ans2[1000]; int main() { int n; scanf("%d", &n); string a; cin >> a; string b = a; int flag1 = 1; int flag2 = 1; int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < a.length() - 1; i++) { if (a[i] == 'B') { ans[++cnt1] = i + 1; a[i] = 'W'; if (a[i + 1] == 'B') a[i + 1] = 'W'; else a[i + 1] = 'B'; } } for (int i = 0; i < a.length(); i++) { if (a[i] == 'B') { flag1 = 0; break; } } for (int i = 0; i < b.length() - 1; i++) { if (b[i] == 'W') { ans2[++cnt2] = i + 1; b[i] = 'B'; if (b[i + 1] == 'B') b[i + 1] = 'W'; else b[i + 1] = 'B'; } } for (int i = 0; i < b.length(); i++) { if (b[i] == 'W') { flag2 = 0; break; } } if (!flag1 && !flag2) { printf("-1"); return 0; } if (flag1) { printf("%d\n", cnt1); for (int i = 1; i <= cnt1; i++) printf("%d ", ans[i]); return 0; } if (flag2) { printf("%d\n", cnt2); for (int i = 1; i <= cnt2; i++) printf("%d ", ans2[i]); } return 0; }
### Prompt In CPP, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int ans[1000]; int ans2[1000]; int main() { int n; scanf("%d", &n); string a; cin >> a; string b = a; int flag1 = 1; int flag2 = 1; int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < a.length() - 1; i++) { if (a[i] == 'B') { ans[++cnt1] = i + 1; a[i] = 'W'; if (a[i + 1] == 'B') a[i + 1] = 'W'; else a[i + 1] = 'B'; } } for (int i = 0; i < a.length(); i++) { if (a[i] == 'B') { flag1 = 0; break; } } for (int i = 0; i < b.length() - 1; i++) { if (b[i] == 'W') { ans2[++cnt2] = i + 1; b[i] = 'B'; if (b[i + 1] == 'B') b[i + 1] = 'W'; else b[i + 1] = 'B'; } } for (int i = 0; i < b.length(); i++) { if (b[i] == 'W') { flag2 = 0; break; } } if (!flag1 && !flag2) { printf("-1"); return 0; } if (flag1) { printf("%d\n", cnt1); for (int i = 1; i <= cnt1; i++) printf("%d ", ans[i]); return 0; } if (flag2) { printf("%d\n", cnt2); for (int i = 1; i <= cnt2; i++) printf("%d ", ans2[i]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; int cw = 0, cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } if (cb % 2 != 0 && cw % 2 != 0) { cout << "-1" << endl; return 0; } vector<int> ans; while (!(cb == 0 || cw == 0)) { if (cb < cw) { for (int i = 0; i < n - 1; i++) { if (str[i] == 'W') { ans.push_back(i + 1); str[i] = 'B'; if (str[i + 1] == 'B') { str[i + 1] = 'W'; } else { str[i + 1] = 'B'; } } } cw = cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } } else { for (int i = 0; i < n - 1; i++) { if (str[i] == 'B') { ans.push_back(i + 1); str[i] = 'W'; if (str[i + 1] == 'B') { str[i + 1] = 'W'; } else { str[i + 1] = 'B'; } } } cw = cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } } } cout << ans.size() << endl; for (auto c : ans) cout << c << " "; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; int cw = 0, cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } if (cb % 2 != 0 && cw % 2 != 0) { cout << "-1" << endl; return 0; } vector<int> ans; while (!(cb == 0 || cw == 0)) { if (cb < cw) { for (int i = 0; i < n - 1; i++) { if (str[i] == 'W') { ans.push_back(i + 1); str[i] = 'B'; if (str[i + 1] == 'B') { str[i + 1] = 'W'; } else { str[i + 1] = 'B'; } } } cw = cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } } else { for (int i = 0; i < n - 1; i++) { if (str[i] == 'B') { ans.push_back(i + 1); str[i] = 'W'; if (str[i + 1] == 'B') { str[i + 1] = 'W'; } else { str[i + 1] = 'B'; } } } cw = cb = 0; for (int i = 0; i < n; i++) { if (str[i] == 'W') cw++; else cb++; } } } cout << ans.size() << endl; for (auto c : ans) cout << c << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; bool a[n]; char c; for (int i = 0; i < n; ++i) { cin >> c; a[i] = (c == 'W'); } bool b[2][n]; for (int i = 0; i < n; ++i) { b[0][i] = 0; b[1][i] = 1; } vector<int> ans; for (int z = 0; z < 2; ++z) { ans.clear(); for (int i = 0; i < n - 1; ++i) { if (b[z][i] != a[i]) { b[z][i] = !b[z][i]; b[z][i + 1] = !b[z][i + 1]; ans.push_back(i + 1); } } if (b[z][n - 1] != a[n - 1]) { continue; } cout << ans.size() << '\n'; for (int i : ans) { cout << i << ' '; } cout << '\n'; return 0; } cout << -1 << '\n'; return 0; }
### Prompt Create a solution in Cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; bool a[n]; char c; for (int i = 0; i < n; ++i) { cin >> c; a[i] = (c == 'W'); } bool b[2][n]; for (int i = 0; i < n; ++i) { b[0][i] = 0; b[1][i] = 1; } vector<int> ans; for (int z = 0; z < 2; ++z) { ans.clear(); for (int i = 0; i < n - 1; ++i) { if (b[z][i] != a[i]) { b[z][i] = !b[z][i]; b[z][i + 1] = !b[z][i + 1]; ans.push_back(i + 1); } } if (b[z][n - 1] != a[n - 1]) { continue; } cout << ans.size() << '\n'; for (int i : ans) { cout << i << ' '; } cout << '\n'; return 0; } cout << -1 << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T = 1; while (T--) { long long n, b = 0, w = 0; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b % 2 && w % 2) { cout << "-1\n"; continue; } vector<long long> v; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'W') { v.push_back(i + 1); s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; } } if (s[n - 1] == 'W') for (long long i = 0; i < n - 2; i += 2) v.push_back(i + 1); cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; cout << "\n"; } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T = 1; while (T--) { long long n, b = 0, w = 0; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b % 2 && w % 2) { cout << "-1\n"; continue; } vector<long long> v; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'W') { v.push_back(i + 1); s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; } } if (s[n - 1] == 'W') for (long long i = 0; i < n - 2; i += 2) v.push_back(i + 1); cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; cout << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; string str; cin >> str; vector<char> sv, sb; for (long long i = 0; i < n; i++) { sv.push_back(str[i]); sb.push_back(str[i]); } vector<long long> ans; for (long long i = 0; i < n - 1; i++) { if (sv[i] == 'B') { ans.push_back(i + 1); sv[i] = 'W'; if (str[i + 1] == 'B') sv[i + 1] = 'W'; else sv[i + 1] = 'B'; } } if (sv[n - 1] == 'W') { cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return; } ans.clear(); for (long long i = 0; i < n - 1; i++) { if (sb[i] == 'W') { ans.push_back(i + 1); sb[i] = 'B'; if (sb[i + 1] == 'W') sb[i + 1] = 'B'; else sb[i + 1] = 'W'; } } if (sb[n - 1] == 'B') { cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return; } cout << -1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; string str; cin >> str; vector<char> sv, sb; for (long long i = 0; i < n; i++) { sv.push_back(str[i]); sb.push_back(str[i]); } vector<long long> ans; for (long long i = 0; i < n - 1; i++) { if (sv[i] == 'B') { ans.push_back(i + 1); sv[i] = 'W'; if (str[i + 1] == 'B') sv[i + 1] = 'W'; else sv[i + 1] = 'B'; } } if (sv[n - 1] == 'W') { cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return; } ans.clear(); for (long long i = 0; i < n - 1; i++) { if (sb[i] == 'W') { ans.push_back(i + 1); sb[i] = 'B'; if (sb[i + 1] == 'W') sb[i + 1] = 'B'; else sb[i + 1] = 'W'; } } if (sb[n - 1] == 'B') { cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return; } cout << -1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string c; cin >> c; int cntb = 0, cntw = 0; for (int i = 0; i < n; i++) { if (c[i] == 'B') cntb++; else cntw++; } if (cntb % 2 == 1 && cntw % 2 == 1) { cout << -1; return 0; } vector<int> p; char swp = 'W', neg = 'B'; if (cntw % 2) swp = 'B', neg = 'W'; for (int i = 1; i < n; i++) { if (c[i] == c[i - 1] && c[i] == swp) { c[i] = c[i - 1] = neg; p.push_back(i); } } for (int i = 0; i < n - 1; i++) { if (c[i] == swp) { while (c[i + 1] != swp && i < n - 1) { c[i] = neg; c[i + 1] = swp; i++; p.push_back(i); } c[i] = c[i + 1] = neg; p.push_back(i + 1); } } cerr << c << '\n'; cout << p.size() << '\n'; for (auto i : p) cout << i << " "; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string c; cin >> c; int cntb = 0, cntw = 0; for (int i = 0; i < n; i++) { if (c[i] == 'B') cntb++; else cntw++; } if (cntb % 2 == 1 && cntw % 2 == 1) { cout << -1; return 0; } vector<int> p; char swp = 'W', neg = 'B'; if (cntw % 2) swp = 'B', neg = 'W'; for (int i = 1; i < n; i++) { if (c[i] == c[i - 1] && c[i] == swp) { c[i] = c[i - 1] = neg; p.push_back(i); } } for (int i = 0; i < n - 1; i++) { if (c[i] == swp) { while (c[i + 1] != swp && i < n - 1) { c[i] = neg; c[i + 1] = swp; i++; p.push_back(i); } c[i] = c[i + 1] = neg; p.push_back(i + 1); } } cerr << c << '\n'; cout << p.size() << '\n'; for (auto i : p) cout << i << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; unsigned long long gcd(unsigned long long n, unsigned long long m) { if (m == 0) return n; else return gcd(m, n % m); } int longestsub(string x, string y, int n, int m) { int lcs[n + 1][m + 1]; int result = 0; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < m + 1; j++) { if (i == 0 || j == 0) { lcs[i][j] = 0; } else if (x[i - 1] == y[j - 1]) { lcs[i][j] = 1 + lcs[i - 1][j - 1]; result = max(result, lcs[i][j]); } else lcs[i][j] = 0; } } return result; } unsigned long long fast_pow(unsigned long long a, unsigned long long p) { unsigned long long res = 1; while (p) { if (p % 2 == 0) { a = a * 1ll * a % 1000000007; p /= 2; } else { res = res * 1ll * a % 1000000007; p--; } } return res; } unsigned long long fact(unsigned long long n) { unsigned long long res = 1; for (int i = 1; i <= n; i++) { res = res * 1ll * i % 1000000007; } return res; } unsigned long long C(unsigned long long n, unsigned long long k) { return fact(n) * 1ll * fast_pow(fact(k), 1000000007 - 2) % 1000000007 * 1ll * fast_pow(fact(n - k), 1000000007 - 2) % 1000000007; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t = 1; while (t--) { int n; cin >> n; string s, z; cin >> s; z = s; vector<int> v, l; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else if (s[i + 1] == 'B') s[i + 1] = 'W'; v.push_back(i); } if (z[i] == 'W') { z[i] = 'B'; if (z[i + 1] == 'W') z[i + 1] = 'B'; else if (z[i + 1] == 'B') z[i + 1] = 'W'; l.push_back(i); } } if (s[n - 1] == 'B' and z[n - 1] == 'W') cout << -1 << '\n'; else { if (s[n - 1] != 'B') { cout << (int)v.size() << '\n'; for (auto i : v) { cout << i + 1 << " "; } cout << '\n'; } else { cout << (int)l.size() << '\n'; for (auto i : l) { cout << i + 1 << " "; } cout << '\n'; } } } }
### Prompt Create a solution in CPP for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; unsigned long long gcd(unsigned long long n, unsigned long long m) { if (m == 0) return n; else return gcd(m, n % m); } int longestsub(string x, string y, int n, int m) { int lcs[n + 1][m + 1]; int result = 0; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < m + 1; j++) { if (i == 0 || j == 0) { lcs[i][j] = 0; } else if (x[i - 1] == y[j - 1]) { lcs[i][j] = 1 + lcs[i - 1][j - 1]; result = max(result, lcs[i][j]); } else lcs[i][j] = 0; } } return result; } unsigned long long fast_pow(unsigned long long a, unsigned long long p) { unsigned long long res = 1; while (p) { if (p % 2 == 0) { a = a * 1ll * a % 1000000007; p /= 2; } else { res = res * 1ll * a % 1000000007; p--; } } return res; } unsigned long long fact(unsigned long long n) { unsigned long long res = 1; for (int i = 1; i <= n; i++) { res = res * 1ll * i % 1000000007; } return res; } unsigned long long C(unsigned long long n, unsigned long long k) { return fact(n) * 1ll * fast_pow(fact(k), 1000000007 - 2) % 1000000007 * 1ll * fast_pow(fact(n - k), 1000000007 - 2) % 1000000007; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t = 1; while (t--) { int n; cin >> n; string s, z; cin >> s; z = s; vector<int> v, l; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else if (s[i + 1] == 'B') s[i + 1] = 'W'; v.push_back(i); } if (z[i] == 'W') { z[i] = 'B'; if (z[i + 1] == 'W') z[i + 1] = 'B'; else if (z[i + 1] == 'B') z[i + 1] = 'W'; l.push_back(i); } } if (s[n - 1] == 'B' and z[n - 1] == 'W') cout << -1 << '\n'; else { if (s[n - 1] != 'B') { cout << (int)v.size() << '\n'; for (auto i : v) { cout << i + 1 << " "; } cout << '\n'; } else { cout << (int)l.size() << '\n'; for (auto i : l) { cout << i + 1 << " "; } cout << '\n'; } } } } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; const long long nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; void IO() {} template <class T> void unisort(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } void reverse(string& string) { long long n = string.length(); for (long long i = 0; i < n / 2; i++) swap(string[i], string[n - i - 1]); } long long fact(long long nb) { long long res = 1; for (long long i = 2; i <= nb; i++) res *= i; return res; } bool sortbysec(const pair<long long, long long>& a, const pair<long long, long long>& b) { if (a.second == b.second) { return a.first < b.first; } return (a.second < b.second); } vector<string> v; void subsequences(const string& a, const string& suffix) { if (a.length() >= 1) { v.push_back(a); } for (size_t i = 0; i < suffix.length(); ++i) subsequences(a + suffix[i], suffix.substr(i + 1)); } int32_t main() { IO(); ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; long long n, b, w; string s; char c; cin >> n >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (w % 2 && b % 2) { cout << -1; return 0; } vector<long long> v; if (w % 2 == 0) c = 'B'; else c = 'W'; w = 0; for (long long i = 0; i < s.size(); i++) { if (w % 2 == 1) v.push_back(i); if (s[i] != c) w++; } cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) cout << v[i] << ' '; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; const long long nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; void IO() {} template <class T> void unisort(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } void reverse(string& string) { long long n = string.length(); for (long long i = 0; i < n / 2; i++) swap(string[i], string[n - i - 1]); } long long fact(long long nb) { long long res = 1; for (long long i = 2; i <= nb; i++) res *= i; return res; } bool sortbysec(const pair<long long, long long>& a, const pair<long long, long long>& b) { if (a.second == b.second) { return a.first < b.first; } return (a.second < b.second); } vector<string> v; void subsequences(const string& a, const string& suffix) { if (a.length() >= 1) { v.push_back(a); } for (size_t i = 0; i < suffix.length(); ++i) subsequences(a + suffix[i], suffix.substr(i + 1)); } int32_t main() { IO(); ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; long long n, b, w; string s; char c; cin >> n >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (w % 2 && b % 2) { cout << -1; return 0; } vector<long long> v; if (w % 2 == 0) c = 'B'; else c = 'W'; w = 0; for (long long i = 0; i < s.size(); i++) { if (w % 2 == 1) v.push_back(i); if (s[i] != c) w++; } cout << v.size() << "\n"; for (long long i = 0; i < v.size(); i++) cout << v[i] << ' '; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long int; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const ll mod = 1e9 + 7; const ll N = 1e5 + 8; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; string s; cin >> s; for (auto &c : s) { if (c == 'W') c = '1'; else c = '0'; } for (int color = 0; color < 2; color++) { string t = s; vector<int> ans; for (int i = 0; i < n - 1; i++) { if (t[i] != color + '0') { if (t[i] == '0') t[i] = '1'; else t[i] = '0'; if (t[i + 1] == '0') t[i + 1] = '1'; else t[i + 1] = '0'; ans.push_back(i + 1); } } if (t.back() - '0' == color) { cout << ans.size() << endl; for (auto i : ans) { cout << i << ' '; } cout << endl; return 0; } } cout << -1 << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long int; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const ll mod = 1e9 + 7; const ll N = 1e5 + 8; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; string s; cin >> s; for (auto &c : s) { if (c == 'W') c = '1'; else c = '0'; } for (int color = 0; color < 2; color++) { string t = s; vector<int> ans; for (int i = 0; i < n - 1; i++) { if (t[i] != color + '0') { if (t[i] == '0') t[i] = '1'; else t[i] = '0'; if (t[i + 1] == '0') t[i + 1] = '1'; else t[i + 1] = '0'; ans.push_back(i + 1); } } if (t.back() - '0' == color) { cout << ans.size() << endl; for (auto i : ans) { cout << i << ' '; } cout << endl; return 0; } } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j = 0, cnt = 0, w = 0, b = 0, sam = 1; cin >> n; int ans[100000]; string s; cin >> s; for (i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) sam++; } for (i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'W') { swap(s[i], s[i + 1]); ans[j++] = i + 1; } else if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; ans[j++] = i + 1; } } if (s[n - 1] == 'B') { for (i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'B') { swap(s[i], s[i + 1]); ans[j++] = i + 1; } else if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = 'B'; s[i + 1] = 'B'; ans[j++] = i + 1; } } } if (sam == n) cout << 0 << endl; else if (s[0] == s[n - 1]) { cout << j << endl; for (i = 0; i < j; i++) cout << ans[i] << ' '; cout << endl; } else cout << -1 << endl; }
### Prompt Create a solution in CPP for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, i, j = 0, cnt = 0, w = 0, b = 0, sam = 1; cin >> n; int ans[100000]; string s; cin >> s; for (i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) sam++; } for (i = 0; i < n - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'W') { swap(s[i], s[i + 1]); ans[j++] = i + 1; } else if (s[i] == 'B' && s[i + 1] == 'B') { s[i] = 'W'; s[i + 1] = 'W'; ans[j++] = i + 1; } } if (s[n - 1] == 'B') { for (i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'B') { swap(s[i], s[i + 1]); ans[j++] = i + 1; } else if (s[i] == 'W' && s[i + 1] == 'W') { s[i] = 'B'; s[i + 1] = 'B'; ans[j++] = i + 1; } } } if (sam == n) cout << 0 << endl; else if (s[0] == s[n - 1]) { cout << j << endl; for (i = 0; i < j; i++) cout << ans[i] << ' '; cout << endl; } else cout << -1 << endl; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; string s; cin >> s; long long int w = 0, b = 0; for (long long int i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } vector<long long int> v; if (b == 0 or w == 0) cout << 0 << endl; else if (w % 2 == 0) { for (long long int i = 0; i < n; i++) { if (s[i] == 'W') { s[i] = 'B'; v.push_back(i + 1); if (s[i + 1] == 'W') { s[i + 1] = 'B'; } else s[i + 1] = 'W'; } } cout << v.size() << '\n'; for (long long int x : v) cout << x << " "; cout << endl; } else if (b % 2 == 0) { for (long long int i = 0; i < n; i++) { if (s[i] == 'B') { s[i] = 'W'; v.push_back(i + 1); if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; } } cout << v.size() << endl; for (long long int x : v) cout << x << " "; cout << '\n'; } else cout << "-1\n"; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; string s; cin >> s; long long int w = 0, b = 0; for (long long int i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } vector<long long int> v; if (b == 0 or w == 0) cout << 0 << endl; else if (w % 2 == 0) { for (long long int i = 0; i < n; i++) { if (s[i] == 'W') { s[i] = 'B'; v.push_back(i + 1); if (s[i + 1] == 'W') { s[i + 1] = 'B'; } else s[i + 1] = 'W'; } } cout << v.size() << '\n'; for (long long int x : v) cout << x << " "; cout << endl; } else if (b % 2 == 0) { for (long long int i = 0; i < n; i++) { if (s[i] == 'B') { s[i] = 'W'; v.push_back(i + 1); if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; } } cout << v.size() << endl; for (long long int x : v) cout << x << " "; cout << '\n'; } else cout << "-1\n"; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; string st, st1; cin >> st; st1 = st; vector<long long int> ve, ve1; for (int i = 1; i < n; i++) { if (st[i] == 'W') { if (st[i - 1] != st[i]) { ve.push_back(i); swap(st[i], st[i - 1]); } } else { if (st[i] == st[i - 1]) { ve.push_back(i); st[i] = 'W'; st[i - 1] = 'W'; } } } for (int i = 1; i < n; i++) { if (st1[i] == 'B') { if (st1[i - 1] != st1[i]) { ve1.push_back(i); swap(st1[i], st1[i - 1]); } } else { if (st1[i] == st1[i - 1]) { ve1.push_back(i); st1[i] = 'B'; st1[i - 1] = 'B'; } } } long long int ck = 1; for (int i = 1; i < n; i++) { if (st[i] != st[i - 1]) { ck = 2; for (int j = 1; j < n; j++) { if (st1[j] != st1[j - 1]) { cout << -1; return 0; } } } } if (ck == 1) { cout << ve.size() << endl; for (int i = 0; i < ve.size(); i++) cout << ve[i] << " "; } else { cout << ve1.size() << endl; for (int i = 0; i < ve1.size(); i++) cout << ve1[i] << " "; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; string st, st1; cin >> st; st1 = st; vector<long long int> ve, ve1; for (int i = 1; i < n; i++) { if (st[i] == 'W') { if (st[i - 1] != st[i]) { ve.push_back(i); swap(st[i], st[i - 1]); } } else { if (st[i] == st[i - 1]) { ve.push_back(i); st[i] = 'W'; st[i - 1] = 'W'; } } } for (int i = 1; i < n; i++) { if (st1[i] == 'B') { if (st1[i - 1] != st1[i]) { ve1.push_back(i); swap(st1[i], st1[i - 1]); } } else { if (st1[i] == st1[i - 1]) { ve1.push_back(i); st1[i] = 'B'; st1[i - 1] = 'B'; } } } long long int ck = 1; for (int i = 1; i < n; i++) { if (st[i] != st[i - 1]) { ck = 2; for (int j = 1; j < n; j++) { if (st1[j] != st1[j - 1]) { cout << -1; return 0; } } } } if (ck == 1) { cout << ve.size() << endl; for (int i = 0; i < ve.size(); i++) cout << ve[i] << " "; } else { cout << ve1.size() << endl; for (int i = 0; i < ve1.size(); i++) cout << ve1[i] << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N; string s; vector<int> res; bool Solve() { res.clear(); string t = s; for (int i = 0; i < (int)t.size() - 1; i++) { if (t[i] == 'W') { res.push_back(i + 1); t[i] = 'B'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; } } for (int i = 0; i < t.size(); i++) if (t[i] == 'W') return false; return true; } int main() { cin >> N >> s; if (Solve()) { cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) cout << res[i] << " "; return 0; } for (int i = 0; i < s.size(); i++) { if (s[i] == 'W') s[i] = 'B'; else s[i] = 'W'; } if (Solve()) { cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) cout << res[i] << " "; return 0; } cout << -1; return 0; }
### Prompt Please create a solution in CPP to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N; string s; vector<int> res; bool Solve() { res.clear(); string t = s; for (int i = 0; i < (int)t.size() - 1; i++) { if (t[i] == 'W') { res.push_back(i + 1); t[i] = 'B'; if (t[i + 1] == 'W') t[i + 1] = 'B'; else t[i + 1] = 'W'; } } for (int i = 0; i < t.size(); i++) if (t[i] == 'W') return false; return true; } int main() { cin >> N >> s; if (Solve()) { cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) cout << res[i] << " "; return 0; } for (int i = 0; i < s.size(); i++) { if (s[i] == 'W') s[i] = 'B'; else s[i] = 'W'; } if (Solve()) { cout << res.size() << "\n"; for (int i = 0; i < res.size(); i++) cout << res[i] << " "; return 0; } cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> q; string s; cin >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; q.push_back(i); } } if (s[n - 1] == 'B') { cout << q.size() << endl; for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' '; cout << endl; return 0; } for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; q.push_back(i); } } if (s[n - 1] == 'W') { cout << q.size() << endl; for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' '; cout << endl; return 0; } cout << "-1" << endl; }
### Prompt Please provide a cpp coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> q; string s; cin >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; q.push_back(i); } } if (s[n - 1] == 'B') { cout << q.size() << endl; for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' '; cout << endl; return 0; } for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W'; q.push_back(i); } } if (s[n - 1] == 'W') { cout << q.size() << endl; for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' '; cout << endl; return 0; } cout << "-1" << endl; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { long long n; cin >> n; vector<int> v; string a; cin >> a; int w = 0, b = 0; for (int i = 0; i < n; ++i) { if (a[i] == 'W') w++; else b++; } if (b % 2 == 1 && w % 2 == 1) { cout << -1; exit(0); } else if (b == 0 || w == 0) { cout << 0; exit(0); } char c; if (b % 2 == 1) c = 'W'; else c = 'B'; for (int i = 0; i < n - 1; ++i) { if (a[i] == c) if (a[i + 1] == a[i]) i++; else { v.push_back(i + 1); swap(a[i], a[i + 1]); } } for (int i = 0; i < n; ++i) { if (a[i] == c) { v.push_back(i + 1); i += 1; } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); ++i) { cout << v[i] << ' '; } cout << '\n'; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { long long n; cin >> n; vector<int> v; string a; cin >> a; int w = 0, b = 0; for (int i = 0; i < n; ++i) { if (a[i] == 'W') w++; else b++; } if (b % 2 == 1 && w % 2 == 1) { cout << -1; exit(0); } else if (b == 0 || w == 0) { cout << 0; exit(0); } char c; if (b % 2 == 1) c = 'W'; else c = 'B'; for (int i = 0; i < n - 1; ++i) { if (a[i] == c) if (a[i + 1] == a[i]) i++; else { v.push_back(i + 1); swap(a[i], a[i + 1]); } } for (int i = 0; i < n; ++i) { if (a[i] == c) { v.push_back(i + 1); i += 1; } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); ++i) { cout << v[i] << ' '; } cout << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int maxn = 1e5 + 6; const long long int MOD = 1e9 + 7; vector<int> prim(1000005, 1); int fact[maxn]; long long int binomialCoeff(long long int n, long long int k) { long long int res = 1; if (k > n - k) k = n - k; for (long long int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } bool isVowel(char ch) { if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'u' || ch == 'o') { return true; } return false; } long long int power(long long int x, long long int i, long long int mod) { long long int ans = 1; while (i > 0) { if (i & 1) ans = (ans * x) % mod; i >>= 1; x = (x * x) % mod; } return ans; } long long int modInverse(long long int x, long long int mod) { return power(x, mod - 2, mod); } int nCr(int n, int r) { if (n < r) { return 0; } return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) * modInverse(fact[r], MOD)) % MOD; } long long int power(int x, unsigned int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } void erath(int n) { prim[1] = 0; prim[0] = 0; prim[2] = 1; for (int i = 2; i * i <= n; i++) { if (prim[i]) { for (int j = i * i; j <= n; j += i) { prim[j] = 0; } } } } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a / gcd(a, b)) * b; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long int n, m, k; int getmid(int l, int r) { return (l + (r - l) / 2); } struct segtree { int siz; vector<pair<long long int, long long int> > sums; void init(int n) { siz = 1; while (siz < n) siz *= 2; sums.assign(2 * siz, {LONG_MAX, 0}); } void set(int i, int v, int cur, int l, int r) { if (r - l == 1) { sums[cur].first = v; sums[cur].second = 1; return; } int m = getmid(l, r); if (i < m) { set(i, v, 2 * cur + 1, l, m); } else set(i, v, 2 * cur + 2, m, r); sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first); if (sums[2 * cur + 1].first == sums[cur].first && sums[2 * cur + 2].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second; } else if (sums[2 * cur + 1].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second; } else { sums[cur].second = sums[cur * 2 + 2].second; } } void set(int i, int v) { set(i, v, 0, 0, siz); } pair<long long int, long long int> sum(int l, int r, int cur, int lx, int rx) { if (lx >= r || l >= rx) return {LONG_MAX, 0}; if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second}; int mid = getmid(lx, rx); pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid), s2 = sum(l, r, 2 * cur + 2, mid, rx); if (s1.first < s2.first) { return s1; } else if (s1.first > s2.first) return s2; else return {s1.first, s1.second + s2.second}; } pair<long long int, long long int> sum(int l, int r) { return sum(l, r, 0, 0, siz); } }; vector<int> gr[100000]; void ans() { cin >> n; string s; cin >> s; int bl = 0, wh = 0; for (auto it : s) { if (it == 'W') wh++; } bl = n - wh; if (bl == 0 || bl == n) { cout << 0 << endl; } else if (bl % 2 == 1 && wh % 2 == 1) { cout << -1 << endl; } else { int pos; char c; int i = 0, j = n - 1; if (bl % 2 == 0) { c = 'B'; pos = bl; } else { c = 'W'; pos = wh; } vector<long long int> ans; while (j > 0) { if (s[j] == c) { if (s[j - 1] != c) { ans.push_back(j); swap(s[j - 1], s[j]); } else { ans.push_back(j); j--; } } j--; } cout << ans.size() << endl; for (auto it : ans) cout << it << " "; cout << endl; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) { ans(); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int maxn = 1e5 + 6; const long long int MOD = 1e9 + 7; vector<int> prim(1000005, 1); int fact[maxn]; long long int binomialCoeff(long long int n, long long int k) { long long int res = 1; if (k > n - k) k = n - k; for (long long int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } bool isVowel(char ch) { if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'u' || ch == 'o') { return true; } return false; } long long int power(long long int x, long long int i, long long int mod) { long long int ans = 1; while (i > 0) { if (i & 1) ans = (ans * x) % mod; i >>= 1; x = (x * x) % mod; } return ans; } long long int modInverse(long long int x, long long int mod) { return power(x, mod - 2, mod); } int nCr(int n, int r) { if (n < r) { return 0; } return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) * modInverse(fact[r], MOD)) % MOD; } long long int power(int x, unsigned int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } void erath(int n) { prim[1] = 0; prim[0] = 0; prim[2] = 1; for (int i = 2; i * i <= n; i++) { if (prim[i]) { for (int j = i * i; j <= n; j += i) { prim[j] = 0; } } } } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a / gcd(a, b)) * b; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long int n, m, k; int getmid(int l, int r) { return (l + (r - l) / 2); } struct segtree { int siz; vector<pair<long long int, long long int> > sums; void init(int n) { siz = 1; while (siz < n) siz *= 2; sums.assign(2 * siz, {LONG_MAX, 0}); } void set(int i, int v, int cur, int l, int r) { if (r - l == 1) { sums[cur].first = v; sums[cur].second = 1; return; } int m = getmid(l, r); if (i < m) { set(i, v, 2 * cur + 1, l, m); } else set(i, v, 2 * cur + 2, m, r); sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first); if (sums[2 * cur + 1].first == sums[cur].first && sums[2 * cur + 2].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second; } else if (sums[2 * cur + 1].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second; } else { sums[cur].second = sums[cur * 2 + 2].second; } } void set(int i, int v) { set(i, v, 0, 0, siz); } pair<long long int, long long int> sum(int l, int r, int cur, int lx, int rx) { if (lx >= r || l >= rx) return {LONG_MAX, 0}; if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second}; int mid = getmid(lx, rx); pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid), s2 = sum(l, r, 2 * cur + 2, mid, rx); if (s1.first < s2.first) { return s1; } else if (s1.first > s2.first) return s2; else return {s1.first, s1.second + s2.second}; } pair<long long int, long long int> sum(int l, int r) { return sum(l, r, 0, 0, siz); } }; vector<int> gr[100000]; void ans() { cin >> n; string s; cin >> s; int bl = 0, wh = 0; for (auto it : s) { if (it == 'W') wh++; } bl = n - wh; if (bl == 0 || bl == n) { cout << 0 << endl; } else if (bl % 2 == 1 && wh % 2 == 1) { cout << -1 << endl; } else { int pos; char c; int i = 0, j = n - 1; if (bl % 2 == 0) { c = 'B'; pos = bl; } else { c = 'W'; pos = wh; } vector<long long int> ans; while (j > 0) { if (s[j] == c) { if (s[j - 1] != c) { ans.push_back(j); swap(s[j - 1], s[j]); } else { ans.push_back(j); j--; } } j--; } cout << ans.size() << endl; for (auto it : ans) cout << it << " "; cout << endl; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) { ans(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, s1; cin >> s; s1 = s; vector<int> v; int coun = 0; for (int i = 0; i < n; i++) { if (s[i] == 'W') { coun++; continue; } else if (i < n - 1) { v.push_back(i + 1); s[i] = 'W'; coun++; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (coun == n) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } else { v.clear(); int coun = 0; for (int i = 0; i < n; i++) { if (s1[i] == 'B') { coun++; continue; } else if (i < n - 1) { v.push_back(i + 1); s1[i] = 'B'; coun++; if (s1[i + 1] == 'W') s1[i + 1] = 'B'; else s1[i + 1] = 'W'; } } if (coun == n) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } else cout << -1 << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, s1; cin >> s; s1 = s; vector<int> v; int coun = 0; for (int i = 0; i < n; i++) { if (s[i] == 'W') { coun++; continue; } else if (i < n - 1) { v.push_back(i + 1); s[i] = 'W'; coun++; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (coun == n) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } else { v.clear(); int coun = 0; for (int i = 0; i < n; i++) { if (s1[i] == 'B') { coun++; continue; } else if (i < n - 1) { v.push_back(i + 1); s1[i] = 'B'; coun++; if (s1[i + 1] == 'W') s1[i + 1] = 'B'; else s1[i + 1] = 'W'; } } if (coun == n) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } else cout << -1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; ll fast_pow(ll a, ll b, ll m) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b = b / 2; } return res; } template <typename T, typename U> std::pair<T, U> operator-(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first - r.first, l.second - r.second}; } template <typename T, typename U> std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } const ll maxn = 1e5 + 10LL; const ll inf = 1e9 + 10LL; vector<int> prefix_function(string s) { int n = (int)s.length(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } int main() { int n; cin >> n; string s; cin >> s; vector<int> ans; int ctr = 0; bool a = true; while (ctr < n - 1) { if (s[ctr] == 'W' && s[ctr + 1] == 'W') { ans.push_back(ctr + 1); s[ctr] = 'B'; s[ctr + 1] = 'B'; ctr += 2; } else if (s[ctr] == 'W' && s[ctr + 1] == 'B') { s[ctr] = 'B'; s[ctr + 1] = 'W'; ans.push_back(ctr + 1); ctr++; } else { ctr++; } } if (s[n - 1] == 'W') { ctr = n - 3; while (ctr >= 0) { if (s[ctr] == 'B' && s[ctr + 1] == 'B') { s[ctr] = 'B'; s[ctr + 1] = 'B'; ans.push_back(ctr + 1); } ctr -= 2; } } if (n % 2 == 0 && s[n - 1] == 'W') a = false; if (a) { cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; } else { cout << -1 << endl; } }
### Prompt In Cpp, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; ll fast_pow(ll a, ll b, ll m) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b = b / 2; } return res; } template <typename T, typename U> std::pair<T, U> operator-(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first - r.first, l.second - r.second}; } template <typename T, typename U> std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } const ll maxn = 1e5 + 10LL; const ll inf = 1e9 + 10LL; vector<int> prefix_function(string s) { int n = (int)s.length(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } int main() { int n; cin >> n; string s; cin >> s; vector<int> ans; int ctr = 0; bool a = true; while (ctr < n - 1) { if (s[ctr] == 'W' && s[ctr + 1] == 'W') { ans.push_back(ctr + 1); s[ctr] = 'B'; s[ctr + 1] = 'B'; ctr += 2; } else if (s[ctr] == 'W' && s[ctr + 1] == 'B') { s[ctr] = 'B'; s[ctr + 1] = 'W'; ans.push_back(ctr + 1); ctr++; } else { ctr++; } } if (s[n - 1] == 'W') { ctr = n - 3; while (ctr >= 0) { if (s[ctr] == 'B' && s[ctr + 1] == 'B') { s[ctr] = 'B'; s[ctr + 1] = 'B'; ans.push_back(ctr + 1); } ctr -= 2; } } if (n % 2 == 0 && s[n - 1] == 'W') a = false; if (a) { cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; } else { cout << -1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; vector<int> ans; char s[221]; int main() { int n; scanf("%d %s", &n, s); for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'B') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'W') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } printf("-1\n"); }
### Prompt Please formulate a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> ans; char s[221]; int main() { int n; scanf("%d %s", &n, s); for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'B') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W'; ans.push_back(i + 1); } } if (s[n - 1] == 'W') { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " "); return 0; } printf("-1\n"); } ```
#include <bits/stdc++.h> using namespace std; vector<int> v; bool slove(string s, char co, char nc) { v.clear(); for (int i = 0; i < s.length() - 1; ++i) { if (s[i] != co) { s[i] = co; if (s[i + 1] != co) s[i + 1] = co; else s[i + 1] = nc; v.push_back(i + 1); } } for (int i = 0; i < s.length(); ++i) { if (s[i] != co) return false; } cout << v.size() << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } return true; } int main() { int n; cin >> n; getchar(); string s; cin >> s; if (slove(s, 'B', 'W')) return 0; else if (slove(s, 'W', 'B')) return 0; else cout << "-1"; return 0; }
### Prompt Create a solution in Cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v; bool slove(string s, char co, char nc) { v.clear(); for (int i = 0; i < s.length() - 1; ++i) { if (s[i] != co) { s[i] = co; if (s[i + 1] != co) s[i + 1] = co; else s[i + 1] = nc; v.push_back(i + 1); } } for (int i = 0; i < s.length(); ++i) { if (s[i] != co) return false; } cout << v.size() << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } return true; } int main() { int n; cin >> n; getchar(); string s; cin >> s; if (slove(s, 'B', 'W')) return 0; else if (slove(s, 'W', 'B')) return 0; else cout << "-1"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int c = 0; vector<int> v; int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) cout << "0"; else if (w == 0 && b == n) cout << "0"; else { char ch; if (s[0] == 'B') ch = 'W'; else ch = 'B'; for (int i = 0; i < n - 1; i++) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else { char ch; if (s[n - 1] == 'B') ch = 'W'; else ch = 'B'; for (int i = n - 1; i > 0; i--) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i - 1] == 'B') s[i - 1] = 'W'; else s[i - 1] = 'B'; v.push_back(i - 1); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else { char ch; if (s[0] == 'B') ch = 'W'; else ch = 'B'; for (int i = 0; i < n - 1; i++) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else cout << "-1"; } } } cout << endl; }
### Prompt Please formulate a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int c = 0; vector<int> v; int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) cout << "0"; else if (w == 0 && b == n) cout << "0"; else { char ch; if (s[0] == 'B') ch = 'W'; else ch = 'B'; for (int i = 0; i < n - 1; i++) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else { char ch; if (s[n - 1] == 'B') ch = 'W'; else ch = 'B'; for (int i = n - 1; i > 0; i--) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i - 1] == 'B') s[i - 1] = 'W'; else s[i - 1] = 'B'; v.push_back(i - 1); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else { char ch; if (s[0] == 'B') ch = 'W'; else ch = 'B'; for (int i = 0; i < n - 1; i++) { if (s[i] == ch) { if (s[i] == 'B') s[i] = 'W'; else s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i); c++; } } int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } if (b == 0 && w == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else if (w == 0 && b == n) { cout << c << endl; for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " "; } else cout << "-1"; } } } cout << endl; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; const int q = 2e5 + 5; const int INF = 1e9; void done() { int n, k; cin >> n >> k; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } vector<int> pos; int ok = 0; for (int i = 2; i < n; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) pos.push_back(i); } int cnt = 0, d = 1, ans = 1; for (int i = 0, j = 0; i < pos.size(); i++) { int q = pos[i]; while ((pos[j] - q) + 2 <= k && j < pos.size()) { j++; } if (j - i > d) { ans = pos[i] - 1; cnt = j - i + 1; d = j - i; ok = 1; } } cout << cnt << " " << ans << '\n'; } void solve() {} void another() { int n; int b, w, cnt = 0; string first; int a[205]; int t[205]; cin >> n; cin >> first; for (int i = 0; i < first.size(); i++) { if (first[i] == 'B') t[i] = 0, b++; else t[i] = 1, w++; } if (b % 2 & w % 2) { cout << -1; return; } b = b % 2 ? 0 : 1; for (int i = 0; i < n - 1; i++) { if (t[i] ^ b) { t[i] ^= 1; t[i + 1] ^= 1; a[cnt++] = i + 1; } } cout << cnt << endl; for (int i = 0; i < cnt; i++) cout << a[i] << ' '; return; } void test_case() { int t; cin >> t; while (t--) another(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); another(); }
### Prompt Generate a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; const int q = 2e5 + 5; const int INF = 1e9; void done() { int n, k; cin >> n >> k; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } vector<int> pos; int ok = 0; for (int i = 2; i < n; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) pos.push_back(i); } int cnt = 0, d = 1, ans = 1; for (int i = 0, j = 0; i < pos.size(); i++) { int q = pos[i]; while ((pos[j] - q) + 2 <= k && j < pos.size()) { j++; } if (j - i > d) { ans = pos[i] - 1; cnt = j - i + 1; d = j - i; ok = 1; } } cout << cnt << " " << ans << '\n'; } void solve() {} void another() { int n; int b, w, cnt = 0; string first; int a[205]; int t[205]; cin >> n; cin >> first; for (int i = 0; i < first.size(); i++) { if (first[i] == 'B') t[i] = 0, b++; else t[i] = 1, w++; } if (b % 2 & w % 2) { cout << -1; return; } b = b % 2 ? 0 : 1; for (int i = 0; i < n - 1; i++) { if (t[i] ^ b) { t[i] ^= 1; t[i + 1] ^= 1; a[cnt++] = i + 1; } } cout << cnt << endl; for (int i = 0; i < cnt; i++) cout << a[i] << ' '; return; } void test_case() { int t; cin >> t; while (t--) another(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); another(); } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<long long int> ans; long long int n, cw, i; char re; string s; cin >> n; cin >> s; cw = count(s.begin(), s.end(), 'W'); if (cw == 0 || cw == n) cout << "0"; else { re = s[n - 1]; for (i = n - 2; i > 0; i--) { if (s[i] != re) { s[i] = re; s[i - 1] = s[i - 1] == 'W' ? 'B' : 'W'; ans.push_back(i - 1); } } if (s[0] == s[n - 1]) { cout << ans.size() << endl; for (auto x : ans) cout << (x + 1) << " "; } else { if (n % 2 == 1) { for (i = 1; i < n; i += 2) ans.push_back(i); cout << ans.size() << endl; for (auto x : ans) cout << (x + 1) << " "; } else cout << "-1"; } } }
### Prompt Your task is to create a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<long long int> ans; long long int n, cw, i; char re; string s; cin >> n; cin >> s; cw = count(s.begin(), s.end(), 'W'); if (cw == 0 || cw == n) cout << "0"; else { re = s[n - 1]; for (i = n - 2; i > 0; i--) { if (s[i] != re) { s[i] = re; s[i - 1] = s[i - 1] == 'W' ? 'B' : 'W'; ans.push_back(i - 1); } } if (s[0] == s[n - 1]) { cout << ans.size() << endl; for (auto x : ans) cout << (x + 1) << " "; } else { if (n % 2 == 1) { for (i = 1; i < n; i += 2) ans.push_back(i); cout << ans.size() << endl; for (auto x : ans) cout << (x + 1) << " "; } else cout << "-1"; } } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i = 0, b = 0, w = 0; vector<long long int> v; cin >> n; char s[230]; cin >> s; for (i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (b % 2 == 1 && w % 2 == 1) cout << "-1" << endl; else { if (w % 2 == 0) { for (i = 0; i < n - 1;) { if (s[i] == 'W') { if (s[i + 1] == 'W') { v.push_back(i + 1); i += 2; } else { s[i + 1] = 'W'; v.push_back(i + 1); i++; } } else i++; } } else { for (i = 0; i < n - 1;) { if (s[i] == 'B') { if (s[i + 1] == 'B') { v.push_back(i + 1); i += 2; } else { s[i + 1] = 'B'; v.push_back(i + 1); i++; } } else i++; } } long long int x = v.size(); cout << x << endl; for (i = 0; i < x; i++) cout << v[i] << " "; cout << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n, i = 0, b = 0, w = 0; vector<long long int> v; cin >> n; char s[230]; cin >> s; for (i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (b % 2 == 1 && w % 2 == 1) cout << "-1" << endl; else { if (w % 2 == 0) { for (i = 0; i < n - 1;) { if (s[i] == 'W') { if (s[i + 1] == 'W') { v.push_back(i + 1); i += 2; } else { s[i + 1] = 'W'; v.push_back(i + 1); i++; } } else i++; } } else { for (i = 0; i < n - 1;) { if (s[i] == 'B') { if (s[i + 1] == 'B') { v.push_back(i + 1); i += 2; } else { s[i + 1] = 'B'; v.push_back(i + 1); i++; } } else i++; } } long long int x = v.size(); cout << x << endl; for (i = 0; i < x; i++) cout << v[i] << " "; cout << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 2e5 + 7; int a[N]; void solve() { int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } vector<int> v; if (w % 2 && b % 2) { cout << "-1" << '\n'; return; } else if (w % 2) { for (int i = n - 1; i >= 0; i--) { while ((i - 1) >= 0 && s[i] == 'B') { v.push_back(i - 1); s[i] = 'W'; w++; b--; if (s[i - 1] == 'B') s[i - 1] = 'W', w++, b--; else s[i - 1] = 'B', b++, w--; } if (!b) break; } } else { for (int i = n - 1; i >= 0; i--) { while ((i - 1) >= 0 && s[i] == 'W') { v.push_back(i - 1); s[i] = 'B'; w--; b++; if (s[i - 1] == 'B') s[i - 1] = 'W', w++, b--; else s[i - 1] = 'B', b++, w--; } if (!w) break; } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); i++) { cout << v[i] + 1 << " "; } cout << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } }
### Prompt Generate a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 2e5 + 7; int a[N]; void solve() { int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } vector<int> v; if (w % 2 && b % 2) { cout << "-1" << '\n'; return; } else if (w % 2) { for (int i = n - 1; i >= 0; i--) { while ((i - 1) >= 0 && s[i] == 'B') { v.push_back(i - 1); s[i] = 'W'; w++; b--; if (s[i - 1] == 'B') s[i - 1] = 'W', w++, b--; else s[i - 1] = 'B', b++, w--; } if (!b) break; } } else { for (int i = n - 1; i >= 0; i--) { while ((i - 1) >= 0 && s[i] == 'W') { v.push_back(i - 1); s[i] = 'B'; w--; b++; if (s[i - 1] == 'B') s[i - 1] = 'W', w++, b--; else s[i - 1] = 'B', b++, w--; } if (!w) break; } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); i++) { cout << v[i] + 1 << " "; } cout << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; string s; cin >> s; string t = s; long long o = 0; vector<long long> v; for (long long i = 0; i < s.size() - 1; i++) { if (s[i] == 'B') { o++; v.push_back(i); s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } if (s[n - 1] == 'W') { cout << o << endl; for (auto i : v) cout << i + 1 << " "; } else { s = t; vector<long long> v1; long long o1 = 0; for (long long i = 0; i < s.size() - 1; i++) { if (s[i] == 'W') { o1++; v1.push_back(i); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << o1 << endl; for (auto i : v1) cout << i + 1 << " "; } else cout << "-1"; } }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; string s; cin >> s; string t = s; long long o = 0; vector<long long> v; for (long long i = 0; i < s.size() - 1; i++) { if (s[i] == 'B') { o++; v.push_back(i); s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } if (s[n - 1] == 'W') { cout << o << endl; for (auto i : v) cout << i + 1 << " "; } else { s = t; vector<long long> v1; long long o1 = 0; for (long long i = 0; i < s.size() - 1; i++) { if (s[i] == 'W') { o1++; v1.push_back(i); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << o1 << endl; for (auto i : v1) cout << i + 1 << " "; } else cout << "-1"; } } ```
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, c = 0, c1 = 0; cin >> n; string s, s1; cin >> s; s1 = s; vector<long long> v, v1; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; v.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } for (long long i = 0; i < n - 1; i++) { if (s1[i] == 'W') { s1[i] = 'B'; v1.push_back(i + 1); if (s1[i + 1] == 'B') s1[i + 1] = 'W'; else s1[i + 1] = 'B'; } } for (long long i = 0; i < n; i++) { if (s[i] == 'W') c++; } for (long long i = 0; i < n; i++) { if (s1[i] == 'B') c1++; } if (c != n && c1 != n) cout << "-1"; else { if (c == n) { cout << v.size() << endl; for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } if (c1 == n) { cout << v1.size() << endl; for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " "; return 0; } } }
### Prompt Create a solution in cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, c = 0, c1 = 0; cin >> n; string s, s1; cin >> s; s1 = s; vector<long long> v, v1; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; v.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } for (long long i = 0; i < n - 1; i++) { if (s1[i] == 'W') { s1[i] = 'B'; v1.push_back(i + 1); if (s1[i + 1] == 'B') s1[i + 1] = 'W'; else s1[i + 1] = 'B'; } } for (long long i = 0; i < n; i++) { if (s[i] == 'W') c++; } for (long long i = 0; i < n; i++) { if (s1[i] == 'B') c1++; } if (c != n && c1 != n) cout << "-1"; else { if (c == n) { cout << v.size() << endl; for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } if (c1 == n) { cout << v1.size() << endl; for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " "; return 0; } } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int countb = 0; int countw = 0; for (int i = 0; i < s.length(); ++i) { if (s[i] == 'B') countb++; else if (s[i] == 'W') countw++; } if (countb == n or countw == n) { cout << 0 << endl; return 0; } int count = 0; if (countw % 2 != 0 && countb % 2 != 0) { cout << -1 << endl; return 0; } else { if (countb % 2 == 0) { vector<int> v; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { count++; s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; v.push_back(i + 1); } } cout << count << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } } else if (countw % 2 == 0) { vector<int> v; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { count++; s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; v.push_back(i + 1); } } cout << count << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } } } }
### Prompt Create a solution in cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int countb = 0; int countw = 0; for (int i = 0; i < s.length(); ++i) { if (s[i] == 'B') countb++; else if (s[i] == 'W') countw++; } if (countb == n or countw == n) { cout << 0 << endl; return 0; } int count = 0; if (countw % 2 != 0 && countb % 2 != 0) { cout << -1 << endl; return 0; } else { if (countb % 2 == 0) { vector<int> v; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'B') { count++; s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; v.push_back(i + 1); } } cout << count << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } } else if (countw % 2 == 0) { vector<int> v; for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { count++; s[i] = (s[i] == 'B') ? 'W' : 'B'; s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B'; v.push_back(i + 1); } } cout << count << endl; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } } } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; vector<int> nums; int a = 0; for (int i = (0); i < (n); i++) { if (s[i] == 'B') a++; } if (a & 1 and !(n & 1)) { cout << -1 << endl; return 0; } if (a & 1) { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } else { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } cout << nums.size() << endl; for (int i = (0); i < (nums.size()); i++) cout << nums[i] + 1 << " \n"[i == nums.size() - 1]; }
### Prompt Please create a solution in Cpp to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; vector<int> nums; int a = 0; for (int i = (0); i < (n); i++) { if (s[i] == 'B') a++; } if (a & 1 and !(n & 1)) { cout << -1 << endl; return 0; } if (a & 1) { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } else { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } cout << nums.size() << endl; for (int i = (0); i < (nums.size()); i++) cout << nums[i] + 1 << " \n"[i == nums.size() - 1]; } ```
#include <bits/stdc++.h> using namespace std; int n; string s; vector<int> v[2]; bool f(char c, int ind) { int last = -1; for (int i = 1; i <= n; i++) { if (s[i - 1] == c) { if (last == -1) { last = i; v[ind].push_back(i); } else { last = -1; } } else if (last != -1) { v[ind].push_back(i); } } if (last != -1) { return 0; } else { return 1; } } int main() { cin >> n; cin >> s; bool bb = f('B', 0); bool ww = f('W', 1); if (bb && ww) { if (v[0].size() < v[1].size()) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } } else if (bb) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else if (ww) { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } else cout << -1; return 0; }
### Prompt Create a solution in cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; vector<int> v[2]; bool f(char c, int ind) { int last = -1; for (int i = 1; i <= n; i++) { if (s[i - 1] == c) { if (last == -1) { last = i; v[ind].push_back(i); } else { last = -1; } } else if (last != -1) { v[ind].push_back(i); } } if (last != -1) { return 0; } else { return 1; } } int main() { cin >> n; cin >> s; bool bb = f('B', 0); bool ww = f('W', 1); if (bb && ww) { if (v[0].size() < v[1].size()) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } } else if (bb) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else if (ww) { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } else cout << -1; return 0; } ```
#include <bits/stdc++.h> const long long MOD = 1000 * 1000 * 1000 + 7; const long long MOD1 = 998244353; using namespace std; long long power(unsigned long long x, unsigned long long y) { unsigned long long res = 1; while (y > 0) { if (y & 1) res = (unsigned long long)(res * x); y = y >> 1; if (x <= 100000000) x = (unsigned long long)(x * x); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long N; string S; cin >> N; cin >> S; vector<long long> pos; for (int i = 0; i < S.length() - 1; i++) { if (S[i] == 'B') continue; if (S[i] == 'W' && S[i + 1] == 'B') { S[i] = 'B'; S[i + 1] = 'W'; pos.push_back(i + 1); } if (S[i] == 'W' && S[i + 1] == 'W') { S[i] = 'B'; S[i + 1] = 'B'; pos.push_back(i + 1); } } if (S[N - 1] == 'B') { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) cout << pos[i] << ' '; } else { for (int i = N - 1; i >= 1; i--) { if (S[i] == 'W') continue; if (S[i] == 'B' && S[i - 1] == 'W') { S[i] = 'W'; S[i - 1] = 'B'; pos.push_back(i); } if (S[i] == 'B' && S[i - 1] == 'B') { S[i] = 'W'; S[i - 1] = 'W'; pos.push_back(i); } } if (S[0] == 'B') cout << -1; else { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) { cout << pos[i] << ' '; } } } return 0; }
### Prompt Create a solution in CPP for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> const long long MOD = 1000 * 1000 * 1000 + 7; const long long MOD1 = 998244353; using namespace std; long long power(unsigned long long x, unsigned long long y) { unsigned long long res = 1; while (y > 0) { if (y & 1) res = (unsigned long long)(res * x); y = y >> 1; if (x <= 100000000) x = (unsigned long long)(x * x); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long N; string S; cin >> N; cin >> S; vector<long long> pos; for (int i = 0; i < S.length() - 1; i++) { if (S[i] == 'B') continue; if (S[i] == 'W' && S[i + 1] == 'B') { S[i] = 'B'; S[i + 1] = 'W'; pos.push_back(i + 1); } if (S[i] == 'W' && S[i + 1] == 'W') { S[i] = 'B'; S[i + 1] = 'B'; pos.push_back(i + 1); } } if (S[N - 1] == 'B') { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) cout << pos[i] << ' '; } else { for (int i = N - 1; i >= 1; i--) { if (S[i] == 'W') continue; if (S[i] == 'B' && S[i - 1] == 'W') { S[i] = 'W'; S[i - 1] = 'B'; pos.push_back(i); } if (S[i] == 'B' && S[i - 1] == 'B') { S[i] = 'W'; S[i - 1] = 'W'; pos.push_back(i); } } if (S[0] == 'B') cout << -1; else { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) { cout << pos[i] << ' '; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; string s; cin >> n >> s; vector<int64_t> ans, ans2; string s2 = s; bool ok = false, ok2 = false; map<char, char> m = {{'B', 'W'}, {'W', 'B'}}; for (int64_t i = 0; i < n - 1; ++i) { if (s[i] == 'B' or ok) { ok = true; if (s[i + 1] == 'B') ok = false; s[i] = m[s[i]]; s[i + 1] = m[s[i + 1]]; ans.emplace_back(i); } if (s2[i] == 'W' or ok2) { ok2 = true; if (s2[i + 1] == 'W') ok2 = false; s2[i] = m[s2[i]]; s2[i + 1] = m[s2[i + 1]]; ans2.emplace_back(i); } } if (!ok and s[n - 1] != 'B') { cout << ans.size() << '\n'; for (auto i : ans) cout << i + 1 << ' '; } else if (!ok2 and s2[n - 1] != 'W') { cout << ans2.size() << '\n'; for (auto i : ans2) cout << i + 1 << ' '; } else cout << -1; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; string s; cin >> n >> s; vector<int64_t> ans, ans2; string s2 = s; bool ok = false, ok2 = false; map<char, char> m = {{'B', 'W'}, {'W', 'B'}}; for (int64_t i = 0; i < n - 1; ++i) { if (s[i] == 'B' or ok) { ok = true; if (s[i + 1] == 'B') ok = false; s[i] = m[s[i]]; s[i + 1] = m[s[i + 1]]; ans.emplace_back(i); } if (s2[i] == 'W' or ok2) { ok2 = true; if (s2[i + 1] == 'W') ok2 = false; s2[i] = m[s2[i]]; s2[i + 1] = m[s2[i + 1]]; ans2.emplace_back(i); } } if (!ok and s[n - 1] != 'B') { cout << ans.size() << '\n'; for (auto i : ans) cout << i + 1 << ' '; } else if (!ok2 and s2[n - 1] != 'W') { cout << ans2.size() << '\n'; for (auto i : ans2) cout << i + 1 << ' '; } else cout << -1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int cnt[2] = {0}; for (auto &v : s) { if (v == 'B') cnt[0]++; else cnt[1]++; } if (cnt[0] % 2 && cnt[1] % 2) { cout << -1 << '\n'; } else { if (cnt[0] % 2 == 0) { for (auto &v : s) { if (v == 'W') v = 'B'; else v = 'W'; } } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i + 1); if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; s[i] = 'B'; } } cout << ans.size() << '\n'; for (auto &v : ans) cout << v << ' '; cout << '\n'; } }
### Prompt Construct a cpp code solution to the problem outlined: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int cnt[2] = {0}; for (auto &v : s) { if (v == 'B') cnt[0]++; else cnt[1]++; } if (cnt[0] % 2 && cnt[1] % 2) { cout << -1 << '\n'; } else { if (cnt[0] % 2 == 0) { for (auto &v : s) { if (v == 'W') v = 'B'; else v = 'W'; } } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i + 1); if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; s[i] = 'B'; } } cout << ans.size() << '\n'; for (auto &v : ans) cout << v << ' '; cout << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; long long int xpow(long long int a, long long int b) { if (b == 0) return 1; if (b % 2 == 0) { long long int k = xpow(a, b / 2); return k * k; } if (b % 2 != 0) return a * xpow(a, b - 1); } void display(vector<int> v) { for (auto k : v) cout << k << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n; cin >> n; string s; cin >> s; string temp = s; vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; ans.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } vector<int> ans1; for (int i = 0; i < n - 1; i++) { if (temp[i] == 'W') { temp[i] = 'B'; ans1.push_back(i + 1); if (temp[i + 1] == 'W') temp[i + 1] = 'B'; else temp[i + 1] = 'W'; } } set<char> se1, se2; for (auto c : s) se1.insert(c); for (auto c : temp) se2.insert(c); if (se1.size() == 1) { cout << ans.size() << endl; display(ans); } else if (se2.size() == 1) { cout << ans1.size() << endl; display(ans1); } else cout << "-1" << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int xpow(long long int a, long long int b) { if (b == 0) return 1; if (b % 2 == 0) { long long int k = xpow(a, b / 2); return k * k; } if (b % 2 != 0) return a * xpow(a, b - 1); } void display(vector<int> v) { for (auto k : v) cout << k << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n; cin >> n; string s; cin >> s; string temp = s; vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; ans.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } vector<int> ans1; for (int i = 0; i < n - 1; i++) { if (temp[i] == 'W') { temp[i] = 'B'; ans1.push_back(i + 1); if (temp[i + 1] == 'W') temp[i + 1] = 'B'; else temp[i + 1] = 'W'; } } set<char> se1, se2; for (auto c : s) se1.insert(c); for (auto c : temp) se2.insert(c); if (se1.size() == 1) { cout << ans.size() << endl; display(ans); } else if (se2.size() == 1) { cout << ans1.size() << endl; display(ans1); } else cout << "-1" << endl; return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; const int inf = (int)1e9 + 99999, mod = (int)1e9 + 7; const long long linf = (long long)2e18 + 99999; int kob(int x, int y) { return ((long long)x * y) % mod; } int aza(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int kos(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } const int maxn = (int)1e5 + 777, maxn2 = (int)1e6 + 55; static long holdrand = 1L; void srand(unsigned int seed) { holdrand = (long)seed; } int rand() { return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff); } void solve() { int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') b++; else w++; } if (b % 2 == w % 2 && w % 2 == 1) { cout << "-1"; return; } vector<int> res, v; if (b % 2 == 0) { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') { v.push_back(i); } } } else { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'W') { v.push_back(i); } } } for (int i = 0; i < (int)v.size(); i += 2) { for (int j = v[i]; j < v[i + 1] - 1; ++j) res.push_back(j); res.push_back(v[i + 1] - 1); } cout << (int)res.size() << '\n'; for (int i = 0; i < (int)res.size(); i++) cout << res[i] + 1 << ' '; } int main() { int t = 1; while (t--) { solve(); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; const int inf = (int)1e9 + 99999, mod = (int)1e9 + 7; const long long linf = (long long)2e18 + 99999; int kob(int x, int y) { return ((long long)x * y) % mod; } int aza(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int kos(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } const int maxn = (int)1e5 + 777, maxn2 = (int)1e6 + 55; static long holdrand = 1L; void srand(unsigned int seed) { holdrand = (long)seed; } int rand() { return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff); } void solve() { int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') b++; else w++; } if (b % 2 == w % 2 && w % 2 == 1) { cout << "-1"; return; } vector<int> res, v; if (b % 2 == 0) { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') { v.push_back(i); } } } else { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'W') { v.push_back(i); } } } for (int i = 0; i < (int)v.size(); i += 2) { for (int j = v[i]; j < v[i + 1] - 1; ++j) res.push_back(j); res.push_back(v[i + 1] - 1); } cout << (int)res.size() << '\n'; for (int i = 0; i < (int)res.size(); i++) cout << res[i] + 1 << ' '; } int main() { int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; char change(char c) { if (c == 'B') return 'W'; else return 'B'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; vector<long long> op; for (long long i = 1; i < n - 1; i++) { if (s[i] == s[i - 1]) continue; s[i] = change(s[i]); s[i + 1] = change(s[i + 1]); op.push_back(i + 1); } if (s[n - 1] == s[n - 2]) { cout << op.size() << '\n'; for (long long x : op) cout << x << ' '; cout << '\n'; return 0; } for (long long i = n - 2; i > 0; i--) { if (s[i] == s[i + 1]) continue; s[i] = change(s[i]); s[i - 1] = change(s[i - 1]); op.push_back(i); } if (s[0] != s[1]) { cout << "-1\n"; return 0; } cout << op.size() << "\n"; for (long long x : op) cout << x << ' '; cout << '\n'; }
### Prompt Develop a solution in CPP to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std::chrono; char change(char c) { if (c == 'B') return 'W'; else return 'B'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; vector<long long> op; for (long long i = 1; i < n - 1; i++) { if (s[i] == s[i - 1]) continue; s[i] = change(s[i]); s[i + 1] = change(s[i + 1]); op.push_back(i + 1); } if (s[n - 1] == s[n - 2]) { cout << op.size() << '\n'; for (long long x : op) cout << x << ' '; cout << '\n'; return 0; } for (long long i = n - 2; i > 0; i--) { if (s[i] == s[i + 1]) continue; s[i] = change(s[i]); s[i - 1] = change(s[i - 1]); op.push_back(i); } if (s[0] != s[1]) { cout << "-1\n"; return 0; } cout << op.size() << "\n"; for (long long x : op) cout << x << ' '; cout << '\n'; } ```
#include <bits/stdc++.h> using namespace std; void solve() { long long n; string s, t; cin >> n >> t; s = t; long long f = 1; std::vector<long long> v; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') continue; else { v.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else { v.clear(); for (long long i = 0; i < n - 1; i++) { if (t[i] == 'W') continue; else { v.push_back(i + 1); if (t[i + 1] == 'B') t[i + 1] = 'W'; else t[i + 1] = 'B'; } } if (t[n - 1] == 'W') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else cout << -1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T = 1; while (T--) solve(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long n; string s, t; cin >> n >> t; s = t; long long f = 1; std::vector<long long> v; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') continue; else { v.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else { v.clear(); for (long long i = 0; i < n - 1; i++) { if (t[i] == 'W') continue; else { v.push_back(i + 1); if (t[i + 1] == 'B') t[i + 1] = 'W'; else t[i + 1] = 'B'; } } if (t[n - 1] == 'W') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else cout << -1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T = 1; while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m, flag = 0; string s, ss; vector<long long> res; long long a[1000009], b[1000009]; void check(long long num) { res.clear(); for (i = 2; i <= n; i++) { if (a[i - 1] != num) { a[i - 1] ^= 1; a[i] ^= 1; res.push_back(i - 1); } } if (a[n] == num) { cout << res.size() << '\n'; for (auto to : res) cout << to << ' '; cout << '\n'; exit(0); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; for (i = 1; i <= n; i++) { if (s[i - 1] == 'W') b[i] = 1, k++; } if (k == 0 || k == n) return cout << 0 << '\n', 0; for (i = 1; i <= n; i++) a[i] = b[i]; check(0); for (i = 1; i <= n; i++) a[i] = b[i]; check(1); cout << -1 << '\n'; exit(0); }
### Prompt Create a solution in cpp for the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m, flag = 0; string s, ss; vector<long long> res; long long a[1000009], b[1000009]; void check(long long num) { res.clear(); for (i = 2; i <= n; i++) { if (a[i - 1] != num) { a[i - 1] ^= 1; a[i] ^= 1; res.push_back(i - 1); } } if (a[n] == num) { cout << res.size() << '\n'; for (auto to : res) cout << to << ' '; cout << '\n'; exit(0); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; for (i = 1; i <= n; i++) { if (s[i - 1] == 'W') b[i] = 1, k++; } if (k == 0 || k == n) return cout << 0 << '\n', 0; for (i = 1; i <= n; i++) a[i] = b[i]; check(0); for (i = 1; i <= n; i++) a[i] = b[i]; check(1); cout << -1 << '\n'; exit(0); } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; string str2; str2 = str; int ans = 0; vector<int> a; int ans2 = 0; vector<int> a2; for (int i = 0; i < n - 1; i++) { if (str[i] == 'B') { ans++; a.push_back(i + 1); str[i] = 'W'; if (str[i + 1] == 'B') str[i + 1] = 'W'; else str[i + 1] = 'B'; } } for (int i = 0; i < n - 1; i++) { if (str2[i] == 'W') { ans2++; a2.push_back(i + 1); str2[i] = 'B'; if (str2[i + 1] == 'W') str2[i + 1] = 'B'; else str2[i + 1] = 'W'; } } for (int i = 0; i < n; i++) { if (str[i] == 'B') { for (int j = 0; j < n; j++) { if (str2[i] == 'W') { cout << "-1"; return 0; } } cout << ans2 << endl; for (int i = 0; i < a2.size(); i++) { cout << a2[i] << " "; } return 0; } } cout << ans << endl; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; string str2; str2 = str; int ans = 0; vector<int> a; int ans2 = 0; vector<int> a2; for (int i = 0; i < n - 1; i++) { if (str[i] == 'B') { ans++; a.push_back(i + 1); str[i] = 'W'; if (str[i + 1] == 'B') str[i + 1] = 'W'; else str[i + 1] = 'B'; } } for (int i = 0; i < n - 1; i++) { if (str2[i] == 'W') { ans2++; a2.push_back(i + 1); str2[i] = 'B'; if (str2[i + 1] == 'W') str2[i + 1] = 'B'; else str2[i + 1] = 'W'; } } for (int i = 0; i < n; i++) { if (str[i] == 'B') { for (int j = 0; j < n; j++) { if (str2[i] == 'W') { cout << "-1"; return 0; } } cout << ans2 << endl; for (int i = 0; i < a2.size(); i++) { cout << a2[i] << " "; } return 0; } } cout << ans << endl; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0; vector<int> a; for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'W') { s.at(i) = 'W'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } int flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != 'W') { flag = 0; break; } } if (flag == 0) { for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'B') { s.at(i) = 'B'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } } flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != s.at(0)) { flag = 0; break; } } if (flag) { cout << c << '\n'; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } } else { cout << -1; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0; vector<int> a; for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'W') { s.at(i) = 'W'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } int flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != 'W') { flag = 0; break; } } if (flag == 0) { for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'B') { s.at(i) = 'B'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } } flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != s.at(0)) { flag = 0; break; } } if (flag) { cout << c << '\n'; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } } else { cout << -1; } return 0; } ```
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; long long flag = 0; string str; cin >> str; string s = str; vector<long long> vec; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; vec.push_back(i + 1); } } if (s[n - 1] != 'W') { for (long long i = n - 1; i > 0; i--) { if (s[i] == 'W') { s[i] = 'B'; if (s[i - 1] == 'W') s[i - 1] = 'B'; else s[i - 1] = 'W'; vec.push_back(i); } } if (s[0] == 'W') flag = 1; } if (flag == 1) cout << "-1"; else { cout << vec.size() << endl; for (long long i = 0; i < vec.size(); i++) cout << vec[i] << " "; } }
### Prompt Develop a solution in cpp to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; long long flag = 0; string str; cin >> str; string s = str; vector<long long> vec; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; vec.push_back(i + 1); } } if (s[n - 1] != 'W') { for (long long i = n - 1; i > 0; i--) { if (s[i] == 'W') { s[i] = 'B'; if (s[i - 1] == 'W') s[i - 1] = 'B'; else s[i - 1] = 'W'; vec.push_back(i); } } if (s[0] == 'W') flag = 1; } if (flag == 1) cout << "-1"; else { cout << vec.size() << endl; for (long long i = 0; i < vec.size(); i++) cout << vec[i] << " "; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string second; cin >> n >> second; string t = second; bool ok = 1; vector<int> vec; for (int i = 0; i < n; i++) { if (second[i] == 'W') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'W'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'W'; } second[to] = 'W'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; cout << endl; return 0; } ok = 1; vec.clear(); second = t; for (int i = 0; i < n; i++) { if (second[i] == 'B') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'B'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'B'; } second[to] = 'B'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; cout << endl; return 0; } cout << -1 << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string second; cin >> n >> second; string t = second; bool ok = 1; vector<int> vec; for (int i = 0; i < n; i++) { if (second[i] == 'W') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'W'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'W'; } second[to] = 'W'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; cout << endl; return 0; } ok = 1; vec.clear(); second = t; for (int i = 0; i < n; i++) { if (second[i] == 'B') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'B'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'B'; } second[to] = 'B'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; cout << endl; return 0; } cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, w = 0, b = 0; cin >> n; string s; cin >> s; queue<long long> q; for (long long i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (w % 2 == 1 && b % 2 == 1) { cout << -1; return 0; } else if (w % 2 == 1 && b % 2 == 0) { for (long long i = 0; i < n; i++) { if (s[i] == 'B') { q.push(i); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { for (long long i = 0; i < n; i++) { if (s[i] == 'W') { q.push(i); s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } } cout << q.size() << endl; while (!q.empty()) { cout << q.front() + 1 << " "; q.pop(); } }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, w = 0, b = 0; cin >> n; string s; cin >> s; queue<long long> q; for (long long i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (w % 2 == 1 && b % 2 == 1) { cout << -1; return 0; } else if (w % 2 == 1 && b % 2 == 0) { for (long long i = 0; i < n; i++) { if (s[i] == 'B') { q.push(i); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { for (long long i = 0; i < n; i++) { if (s[i] == 'W') { q.push(i); s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } } cout << q.size() << endl; while (!q.empty()) { cout << q.front() + 1 << " "; q.pop(); } } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)3e5 + 5; const int mod = (int)1e9 + 7; int n, b, w; char a[300]; vector<int> v; inline void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 'B') b++; else w++; } if (b == 0 || w == 0) { cout << 0; return; } if (b % 2 == 1 && w % 2 == 1) { cout << -1; return; } if (w % 2 == 0) { for (int i = 1; i < n; i++) { if (a[i] == 'W') { if (a[i + 1] == 'W') { v.push_back(i); a[i] = 'B'; a[i + 1] = 'B'; } else { v.push_back(i); a[i] = 'B'; a[i + 1] = 'W'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; return; } for (int i = 1; i < n; i++) { if (a[i] == 'B') { if (a[i + 1] == 'B') { v.push_back(i); a[i] = 'W'; a[i + 1] = 'W'; } else { v.push_back(i); a[i] = 'W'; a[i + 1] = 'B'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tt = 1; while (tt--) { solve(); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)3e5 + 5; const int mod = (int)1e9 + 7; int n, b, w; char a[300]; vector<int> v; inline void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 'B') b++; else w++; } if (b == 0 || w == 0) { cout << 0; return; } if (b % 2 == 1 && w % 2 == 1) { cout << -1; return; } if (w % 2 == 0) { for (int i = 1; i < n; i++) { if (a[i] == 'W') { if (a[i + 1] == 'W') { v.push_back(i); a[i] = 'B'; a[i + 1] = 'B'; } else { v.push_back(i); a[i] = 'B'; a[i + 1] = 'W'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; return; } for (int i = 1; i < n; i++) { if (a[i] == 'B') { if (a[i + 1] == 'B') { v.push_back(i); a[i] = 'W'; a[i + 1] = 'W'; } else { v.push_back(i); a[i] = 'W'; a[i + 1] = 'B'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tt = 1; while (tt--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long double pi = 3.14159265358979323846; vector<long long> al[500005]; long long vis[500005], color[500005], I[101][101]; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long bpow(long long n, long long po) { long long res = 1; while (po > 0) { if (po % 2) { res *= n; po--; } else { n *= n; po /= 2; } } return res; } long long binpow(long long n, long long po, long long mod) { long long res = 1; while (po > 0) { if (po % 2) { res = (res * n) % mod; po--; } else { n = (n * n) % mod; po /= 2; } } return res; } long long swap(long long *a, long long *b) { long long temp = *a; *a = *b; *b = temp; } long long fact(long long n) { if ((n == 0) || (n == 1)) return 1; else return n * fact(n - 1); } long long C(long long n, long long r) { if (r > n - r) r = n - r; long long ans = 1; long long i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; ans = ans % 1000000007; } return ans; } void mmul(long long A[][101], long long B[][101], long long dim) { long long res[dim][dim]; for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { res[i][j] = 0; for (long long k = 0; k < dim; k++) res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % 1000000007; } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = res[i][j]; } void mpow(long long A[][101], long long dim, long long po) { for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { if (i == j) I[i][j] = 1; else I[i][j] = 0; } while (po > 0) { if (po % 2 == 1) { mmul(I, A, dim); po--; } else { mmul(A, A, dim); po /= 2; } } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = I[i][j]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long testcases = 1; while (testcases--) { long long n; cin >> n; string s; cin >> s; long long white = 0, black = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') white++; else black++; } vector<long long> ans; if (black == 0 || white == 0) cout << 0; else if (n % 2 == 0 && white % 2 == 1) cout << -1; else if (black % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'B' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'W' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'B' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } else if (white % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'B' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'W' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } } }
### Prompt Your task is to create a Cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long double pi = 3.14159265358979323846; vector<long long> al[500005]; long long vis[500005], color[500005], I[101][101]; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long bpow(long long n, long long po) { long long res = 1; while (po > 0) { if (po % 2) { res *= n; po--; } else { n *= n; po /= 2; } } return res; } long long binpow(long long n, long long po, long long mod) { long long res = 1; while (po > 0) { if (po % 2) { res = (res * n) % mod; po--; } else { n = (n * n) % mod; po /= 2; } } return res; } long long swap(long long *a, long long *b) { long long temp = *a; *a = *b; *b = temp; } long long fact(long long n) { if ((n == 0) || (n == 1)) return 1; else return n * fact(n - 1); } long long C(long long n, long long r) { if (r > n - r) r = n - r; long long ans = 1; long long i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; ans = ans % 1000000007; } return ans; } void mmul(long long A[][101], long long B[][101], long long dim) { long long res[dim][dim]; for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { res[i][j] = 0; for (long long k = 0; k < dim; k++) res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % 1000000007; } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = res[i][j]; } void mpow(long long A[][101], long long dim, long long po) { for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { if (i == j) I[i][j] = 1; else I[i][j] = 0; } while (po > 0) { if (po % 2 == 1) { mmul(I, A, dim); po--; } else { mmul(A, A, dim); po /= 2; } } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = I[i][j]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long testcases = 1; while (testcases--) { long long n; cin >> n; string s; cin >> s; long long white = 0, black = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') white++; else black++; } vector<long long> ans; if (black == 0 || white == 0) cout << 0; else if (n % 2 == 0 && white % 2 == 1) cout << -1; else if (black % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'B' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'W' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'B' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } else if (white % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'B' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'W' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> v; string s; cin >> n >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') goto out; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') { cout << -1; return 0; } out: cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; }
### Prompt In cpp, your task is to solve the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> v; string s; cin >> n >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') goto out; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') { cout << -1; return 0; } out: cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<long long int> v[3]; long long int n, i, r = 0; cin >> n; string s, ss; cin >> s; char rev = ('B' ^ 'W'); ss = s; for (i = 0; i < n - 1; ++i) { if (s[i] == 'B') { v[1].push_back(i); s[i] = 'W'; s[i + 1] = rev ^ s[i + 1]; } } if (s[n - 1] == 'B') { r = 1; for (i = 0; i < n - 1; ++i) { if (ss[i] == 'W') { v[2].push_back(i); ss[i] = 'B'; ss[i + 1] = rev ^ ss[i + 1]; } } } if (r == 0 && s[n - 1] == 'W') { cout << v[1].size() << "\n"; for (auto it : v[1]) cout << it + 1 << " "; return 0; } if (r == 1 && ss[n - 1] == 'B') { cout << v[2].size() << "\n"; for (auto it : v[2]) cout << it + 1 << " "; return 0; } cout << "-1"; return 0; }
### Prompt Please create a solution in Cpp to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<long long int> v[3]; long long int n, i, r = 0; cin >> n; string s, ss; cin >> s; char rev = ('B' ^ 'W'); ss = s; for (i = 0; i < n - 1; ++i) { if (s[i] == 'B') { v[1].push_back(i); s[i] = 'W'; s[i + 1] = rev ^ s[i + 1]; } } if (s[n - 1] == 'B') { r = 1; for (i = 0; i < n - 1; ++i) { if (ss[i] == 'W') { v[2].push_back(i); ss[i] = 'B'; ss[i + 1] = rev ^ ss[i + 1]; } } } if (r == 0 && s[n - 1] == 'W') { cout << v[1].size() << "\n"; for (auto it : v[1]) cout << it + 1 << " "; return 0; } if (r == 1 && ss[n - 1] == 'B') { cout << v[2].size() << "\n"; for (auto it : v[2]) cout << it + 1 << " "; return 0; } cout << "-1"; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<long long, long long>; template <class T> void cmax(T& x, T y) { (x < y) && (x = y); } template <class T> void cmin(T& x, T y) { (x > y) && (x = y); } string s; long long n, b; vector<long long> ans; signed main() { ios ::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; for (long long i = 1; i <= n; i++) { if (s[i - 1] == 'B') b++; } if (s.size() % 2 == 0 && b % 2) return puts("-1"), 0; for (long long i = 1; i < n - 1; i++) { if (s[i - 1] != s[i]) { ans.push_back(i + 1); s[i] = (s[i] == 'B' ? 'W' : 'B'); s[i + 1] = (s[i + 1] == 'B' ? 'W' : 'B'); } } for (long long i = 0; i < n; i++) { if (s.size() % 2 == 0 && s[i] == 'W') { ans.push_back(i + 1); i++; } else if (s.size() % 2 && b % 2 && s[i] == 'W') ans.push_back(i + 1), ++i; else if (s.size() % 2 && b % 2 == 0 && s[i] == 'B') ans.push_back(i + 1), ++i; } cout << ans.size() << '\n'; for (long long x : ans) cout << x << ' '; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<long long, long long>; template <class T> void cmax(T& x, T y) { (x < y) && (x = y); } template <class T> void cmin(T& x, T y) { (x > y) && (x = y); } string s; long long n, b; vector<long long> ans; signed main() { ios ::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; for (long long i = 1; i <= n; i++) { if (s[i - 1] == 'B') b++; } if (s.size() % 2 == 0 && b % 2) return puts("-1"), 0; for (long long i = 1; i < n - 1; i++) { if (s[i - 1] != s[i]) { ans.push_back(i + 1); s[i] = (s[i] == 'B' ? 'W' : 'B'); s[i + 1] = (s[i + 1] == 'B' ? 'W' : 'B'); } } for (long long i = 0; i < n; i++) { if (s.size() % 2 == 0 && s[i] == 'W') { ans.push_back(i + 1); i++; } else if (s.size() % 2 && b % 2 && s[i] == 'W') ans.push_back(i + 1), ++i; else if (s.size() % 2 && b % 2 == 0 && s[i] == 'B') ans.push_back(i + 1), ++i; } cout << ans.size() << '\n'; for (long long x : ans) cout << x << ' '; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i; cin >> n; string s; cin >> s; long long int c = 0; vector<long long int> ans; string s1 = s; for (i = 0; i < n - 1; i++) { if (s1[i] != 'W') { s1[i] = 'W'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'W') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } c = 0; s1 = s; ans.clear(); for (i = 0; i < n - 1; i++) { if (s1[i] != 'B') { s1[i] = 'B'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'B') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } cout << -1 << endl; }
### Prompt Please formulate a cpp solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n, i; cin >> n; string s; cin >> s; long long int c = 0; vector<long long int> ans; string s1 = s; for (i = 0; i < n - 1; i++) { if (s1[i] != 'W') { s1[i] = 'W'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'W') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } c = 0; s1 = s; ans.clear(); for (i = 0; i < n - 1; i++) { if (s1[i] != 'B') { s1[i] = 'B'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'B') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } cout << -1 << endl; } ```
#include <bits/stdc++.h> using namespace std; inline bool valid(int x, int n) { return 0 <= x && x < n; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; template <typename T> inline T pop(queue<T>& q) { T front = q.front(); q.pop(); return front; } template <typename T> inline T gcd(T a, T b) { for (; b; a %= b, swap(a, b)) ; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; vector<int> rst; int w = 0, b = 0; for (char c : s) c == 'W' ? w++ : b++; if (w % 2 && b % 2) { cout << -1; return 0; } if (!w || !b) { cout << 0; return 0; } if (w % 2) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { rst.push_back(i + 1); s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { rst.push_back(i + 1); s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } cout << rst.size() << "\n"; for (int i : rst) cout << i << " "; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline bool valid(int x, int n) { return 0 <= x && x < n; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; template <typename T> inline T pop(queue<T>& q) { T front = q.front(); q.pop(); return front; } template <typename T> inline T gcd(T a, T b) { for (; b; a %= b, swap(a, b)) ; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; vector<int> rst; int w = 0, b = 0; for (char c : s) c == 'W' ? w++ : b++; if (w % 2 && b % 2) { cout << -1; return 0; } if (!w || !b) { cout << 0; return 0; } if (w % 2) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { rst.push_back(i + 1); s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { rst.push_back(i + 1); s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } cout << rst.size() << "\n"; for (int i : rst) cout << i << " "; } ```