Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; void fastik() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } void solve() {} bool check(string a) { set<char> st; for (auto i : a) { st.insert(i); } return (int)st.size() == 1; } void tryBlack(string s) {} int main() { fastik(); long long n; cin >> n; string s; cin >> s; string b = s; vector<long long> ans; for (int i = 0; i < (int)(n - 1); ++i) { if (b[i] == 'W') { ans.push_back(i); b[i] = 'B'; if (b[i + 1] == 'B') { b[i + 1] = 'W'; } else { b[i + 1] = 'B'; } } } if (check(b)) { cout << (int)ans.size() << "\n"; for (auto i : ans) { cout << i + 1 << " "; } return 0; } b = s; ans.clear(); for (int i = 0; i < (int)(n - 1); ++i) { if (b[i] == 'B') { ans.push_back(i); b[i] = 'W'; if (b[i + 1] == 'W') { b[i + 1] = 'B'; } else { b[i + 1] = 'W'; } } } if (check(b)) { cout << (int)ans.size() << "\n"; for (auto i : ans) { cout << i + 1 << " "; } return 0; } cout << -1; }
CPP
1271_B. Blocks
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).
2
8
from math import * from collections import * import sys sys.setrecursionlimit(10**9) mod = 10**9 + 7 n = int(input()) a = list(input()) ans = [] for i in range(n-1): if(a[i] == 'B'): continue else: a[i] = 'B' if(a[i+1] == 'B'): a[i+1] = 'W' else: a[i+1] = 'B' ans.append(i+1) if(a[-1] == 'W' and n%2 == 0): print(-1) elif(a[-1] == 'W'): print(len(ans)+n//2) for i in ans: print(i,end = " ") for i in range(1,n,2): print(i,end = " ") else: print(len(ans)) for i in ans: print(i,end = " ")
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const int N = (int)1e3 + 10; int n; string s; vector<int> ans; void change(char &x) { if (x == 'B') x = 'W'; else x = 'B'; } int check(string s) { for (int i = 1; i < (int)s.size() - 1; i++) { if (s[i] != s[i - 1]) { ans.push_back(i + 1); change(s[i]); change(s[i + 1]); } } for (int i = 1; i < (int)s.size(); i++) { if (s[i] != s[0]) { return -1; return 0; } } cerr << s << '\n'; return ans.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> s; if (check(s) != -1) { cout << ans.size() << '\n'; for (int x : ans) { cout << x << ' '; } return 0; } ans.clear(); change(s[0]); change(s[1]); ans.push_back(1); if (check(s) != -1) { cout << ans.size() << '\n'; for (int x : ans) { cout << x << ' '; } return 0; } cout << -1; return 0; }
CPP
1271_B. Blocks
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).
2
8
n = int(input()) s = input() a = list() test = 0 for i in s: if i == 'W': test += 1 a.append(1) else: a.append(0) answer = list() if test == 0 or test == n: print(0) elif ((test % 2 != 0) or ((n-test) % 2 != 0)) and n % 2 == 0: print(-1) else: for i in range(1,n-1): if a[i] != a[i-1]: a[i] = (a[i] +1) %2 a[i+1] = (a[i+1] + 1) % 2 answer.append(i+1) for i in reversed(range(1,n-1)): if a[i] != a[i+1]: a[i] = (a[i] +1) %2 a[i-1] = (a[i-1] + 1) % 2 answer.append(i) print(len(answer)) print(*answer)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int N; string str; int A[205], B[205]; bool inv[205]; void trn(int p) { B[p] = 1 - B[p]; B[p - 1] = 1 - B[p - 1]; } bool solve(int p, bool c) { if (p == N) { return B[p - 1] == c; } if (B[p - 1] != c) { inv[p] = 1; trn(p); } else { inv[p] = 0; } return solve(p + 1, c); } int main() { cin >> N >> str; for (int i = 0; i < N; i++) { A[i] = str[i] == 'B'; } vector<int> ans; for (int i = 0; i < N; i++) B[i] = A[i]; bool res = solve(1, 0); if (res) { for (int i = 1; i < N; i++) if (inv[i]) ans.push_back(i); cout << ans.size() << '\n'; for (int ai : ans) cout << ai << ' '; cout << '\n'; return 0; } for (int i = 0; i < N; i++) B[i] = A[i]; res = solve(1, 1); if (res) { for (int i = 1; i < N; i++) if (inv[i]) ans.push_back(i); cout << ans.size() << '\n'; for (int ai : ans) cout << ai << ' '; cout << '\n'; return 0; } cout << -1 << '\n'; return 0; }
CPP
1271_B. Blocks
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).
2
8
import java.math.BigInteger; import java.util.*; import java.io.*; import java.text.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 32768); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer t; static String sn() { while (t == null || !t.hasMoreTokens()) { try { t = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(); } } return t.nextToken(); } static int ni() { return Integer.parseInt(sn()); } static long nlo() { return Long.parseLong(sn()); } public static void main(String args[]) { int t = 1; while (t-- > 0) { solve(); } out.close(); } static long mod = (long) 998244353; static long pro(long a, long b) { return (a % mod * b % mod) % mod; } static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a % mod - b % mod) + mod) % mod; } static int maxn = 1000005; static int pow(int n) { return (int) (Math.log10(n) / Math.log10(2)); } static long modpow(long x, long y) { long res = 1l; x %= mod; while (y > 0) { if (y % 2 != 0) res = pro(res, x); x = pro(x, x); y /= 2; } return res; } static class pair { String a; int b; pair(String x, int y) { a = x; b = y; } } static long modInverse(long n) { BigInteger n1 = new BigInteger(Long.toString(n)); String s = (n1.modInverse(new BigInteger(Long.toString(mod)))).toString(); return Long.parseLong(s); } static boolean v[] = new boolean[1000005]; static void seive() { v[1] = true; for (int i = 2; i < 1000005; i++) { if (!v[i]) { for (int j = 2 * i; j < 1000005; j += i) v[j] = true; } } } static boolean isprime(long n) { if (!v[(int) n]) return true; return false; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void solve() { int n = ni(); String s = sn(); if (!check(s, n, 'W')) { if (!check(s, n, 'B')) out.println(-1); } } static boolean check(String s, int n, char ch) { char c[] = s.toCharArray(); List<Integer> ans = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { if (c[i] != ch) { ans.add(i + 1); c[i] = ch; if (c[i + 1] == 'W') c[i + 1] = 'B'; else c[i + 1] = 'W'; } } // out.println(Arrays.toString(c)); if (c[n - 1] != ch) return false; out.println(ans.size()); for (int h : ans) out.print(h + " "); return true; } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char s[n], c; cin >> s; vector<int> v; int w[n], b[n]; if (s[0] == 'B') c = 'W'; else c = 'B'; for (int i = 0; i < n - 1; i++) { if (s[i] != c) { s[i] = c; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == c) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } else { if (n % 2 == 1) { for (int i = 1; i < n - 1; i += 2) v.push_back(i); cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } else { cout << "-1"; cout << endl; } } return 0; }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; } inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(long long &x, long long y) { return (y > x) ? x = y, 1 : 0; } const int N = 100000; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int P1 = 31; const int P2 = 57; const int mod = (int)1e9 + 7; const int mod1 = (int)1e9 + 9; const int mod2 = 998244353; const long double eps = 1e-12; const double pi = atan2(0, -1); const int ABC = 26; long long fac[100007]; bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } bool sortbyfirst(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } long long power(long long x, long long y) { long long res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long modInverse(long long n) { return power(n, mod - 2); } long long nCrModPFermat(long long n, long long r) { if (r == 0) return 1; return (fac[n] * modInverse(fac[r]) % mod * modInverse(fac[n - r]) % mod) % mod; } long long binarySearch(vector<long long> arr, long long l, long long r, long long x) { if (r >= l) { long long mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return -1; } long long findPos(vector<long long> arr, long long key) { long long l = 0, h = 1; long long val = arr[0]; while (val < key) { l = h; h = 2 * h; val = arr[h]; } return binarySearch(arr, l, h, key); } int find(long long n) { vector<int> dig; while (n > 0) { dig.push_back(n % 10); n = n / 10; } int s = dig.size(); int i; int max = dig[s - 1]; int flag = 1; for (int i = (s - 1); i > (-1); i--) { if (dig[i] > max) { break; } else if (dig[i] == max) { continue; } else { flag = 0; break; } } if (flag) { return dig[s - 1] + 9 * (s - 1); } else { return dig[s - 1] - 1 + 9 * (s - 1); } } int c[100007]; int main() { int n; cin >> n; int a[n]; int i, count0 = 0, count1 = 0; int anss = 0; for (int i = (0); i < (n); i++) { char k; cin >> k; if (k == 'B') { a[i] = 1; count1++; } else { a[i] = 0; count0++; } } vector<int> ans; if (count0 % 2 == 0) { for (int i = (0); i < (n - 1); i++) { if (a[i] == 0) { ans.push_back(i + 1); anss++; a[i] = 1; a[i + 1] ^= 1; } } } else if (count1 % 2 == 0) { for (int i = (0); i < (n - 1); i++) { if (a[i] == 1) { ans.push_back(i + 1); anss++; a[i] = 0; a[i + 1] ^= 1; } } } else { cout << -1; return 0; } cout << anss << endl; for (int i = (0); i < (ans.size()); i++) { cout << ans[i] << " "; } return 0; }
CPP
1271_B. Blocks
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).
2
8
n=int(input()) s=input() s_=[] for i in s: if i=="W": s_.append(1) else: s_.append(0) if len(set(s_))==1: print(0) exit() A=[] for i in range(n-1): if s_[i]: A.append(i+1) s_[i]^=1 s_[i+1]^=1 if len(set(s_))==1: print(len(A)) for i in A: print(i,end=' ') else: if len(s_)%2!=0: for i in range(0,len(s_)-1,2): A.append(i+1) print(len(A)) for i in A: print(i,end=' ') else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 203; int n; char s[N]; char t[N]; vector<int> ans; void print() { printf("%d\n", (int)ans.size()); for (int i = 0; i < (int)ans.size(); i++) { printf("%d%c", ans[i] + 1, " \n"[i + 1 == (int)ans.size()]); } exit(0); } void solve() { ans.clear(); for (int i = 0; i + 1 < n; i++) { if (s[i] == 0) { ans.push_back(i); s[i] ^= 1; s[i + 1] ^= 1; } } if (s[n - 1] == 1) { print(); } for (int i = n - 1; i - 1 >= 0; i--) { if (s[i] == 0) { ans.push_back(i - 1); s[i] ^= 1; s[i - 1] ^= 1; } } if (s[0] == 1) print(); for (int i = 0; i + 1 < n; i++) { if (s[i] == 0) { ans.push_back(i); s[i] ^= 1; s[i + 1] ^= 1; } } if (s[n - 1] == 1) print(); } int main() { scanf("%d %s", &n, s); strcpy(t, s); for (int i = 0; i < n; i++) s[i] = s[i] == 'B' ? 0 : 1; solve(); for (int i = 0; i < n; i++) s[i] = t[i] == 'B' ? 1 : 0; solve(); printf("-1\n"); }
CPP
1271_B. Blocks
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).
2
8
mod=10**9+7 #import resource #resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY]) #import threading #threading.stack_size(2**27) #import sys #sys.setrecursionlimit(10**6) #fact=[1] #for i in range(1,1000001): # fact.append((fact[-1]*i)%mod) #ifact=[0]*1000001 #ifact[1000000]=pow(fact[1000000],mod-2,mod) #for i in range(1000000,0,-1): # ifact[i-1]=(i*ifact[i])%mod from sys import stdin, stdout import bisect from bisect import bisect_left as bl #c++ lowerbound bl(array,element) from bisect import bisect_right as br #c++ upperbound import itertools import collections import math import heapq #from random import randint as rn #from Queue import Queue as Q def modinv(n,p): return pow(n,p-2,p) def ncr(n,r,p): #for using this uncomment the lines calculating fact and ifact t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p return t def ain(): #takes array as input return list(map(str,sin().split())) def sin(): return input().strip() def GCD(x,y): while(y): x, y = y, x % y return x """*******************************************************""" def main(): n=int(input()) s=list(sin()) b=0 w=0 ans=[] for i in range(n): if(s[i]=="B"): b+=1 else: w+=1 if(b%2==1 and w%2==1): print -1 exit() if(b%2==1): for i in range(n-1): if(s[i]!="B"): ans.append(str(i+1)) s[i]="B" if(s[i+1]=="B"): s[i+1]="W" else: s[i+1]="B" else: for i in range(n-1): if(s[i]!="W"): ans.append(str(i+1)) s[i]="W" if(s[i+1]=="B"): s[i+1]="W" else: s[i+1]="B" print len(ans) print " ".join(ans) ######## Python 2 and 3 footer by Pajenegod and c1729 py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') if __name__ == '__main__': main() #threading.Thread(target=main).start()
PYTHON
1271_B. Blocks
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).
2
8
#ライブラリインポート from collections import defaultdict #入力受け取り def getlist(): return list(map(int, input().split())) #処理内容 def main(): N = int(input()) S = list(input()) B = 0 W = 0 for i in range(N): if S[i] == "B": B += 1 else: W += 1 if B % 2 == 1 and W % 2 == 1: print(-1) return if B % 2 == 0: cnt = 0 ans = [] for i in range(N - 1): if S[i] == "B": cnt += 1 ans.append(i + 1) S[i] = "W" if S[i + 1] == "B": S[i + 1] = "W" else: S[i + 1] = "B" print(cnt) if cnt != 0: print(" ".join(list(map(str, ans)))) else: cnt = 0 ans = [] for i in range(N - 1): if S[i] == "W": cnt += 1 ans.append(i + 1) S[i] = "B" if S[i + 1] == "W": S[i + 1] = "B" else: S[i + 1] = "W" print(cnt) if cnt != 0: print(" ".join(list(map(str, ans)))) if __name__ == '__main__': main()
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) s = input() moves = [] b=w=0 for char in s: if char == 'B': b+=1 else: w+=1 if b&1: s = [ False if s[i]=='W' else True for i in range(n)] else: s = [ True if s[i]=='W' else False for i in range(n)] for i in range(n-1): if s[i] : continue else: s[i] = not s[i] s[i+1] = not s[i+1] moves.append(i+1) else: if s[-1]==s[-2]: print(len(moves)) print(*moves, sep=" ") else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
'''input 3 2 192 ''' # A coding delight from sys import stdin def counter(c): if c == 'B': return 'W' return 'B' # main starts n = int(stdin.readline().strip()) string = list(stdin.readline().strip()) temp = string[:] # changing to B ans = [] for i in range(n - 1): if string[i] == 'B': pass else: string[i] = 'B' string[i + 1] = counter(string[i + 1]) ans.append(i + 1) if string[-1] == 'B': print(len(ans)) print(*ans) exit() ans = [] string = temp[:] for i in range(n - 1): if string[i] == 'W': pass else: string[i] = 'W' string[i + 1] = counter(string[i + 1]) ans.append(i + 1) if string[-1] == 'W': print(len(ans)) print(*ans) exit() print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<bool> cub(n); char c; for (int i = 0; i < n; i++) { cin >> c; if (c == 'W') cub[i] = 1; else cub[i] = 0; } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (cub[i]) { cub[i] = 0; if (cub[i + 1]) cub[i + 1] = 0; else cub[i + 1] = 1; ans.push_back(i + 1); } } if (cub[n - 1] == 0) { cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; } else { for (int i = 0; i < n - 1; i++) { if (cub[i] == 0) { cub[i] = 1; if (cub[i + 1]) cub[i + 1] = 0; else cub[i + 1] = 1; ans.push_back(i + 1); } } if (cub[n - 1] == 1) { cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; } else { cout << -1; } } return 0; }
CPP
1271_B. Blocks
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).
2
8
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class realfast implements Runnable { private static final int INF = (int) 1e9; public void solve() throws IOException { int n = readInt(); String s= readString(); int arr[]= new int[n]; int arr1[]= new int[n]; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='W'){ arr[i]=1; arr1[i]=1; } else{ arr[i]=2; arr1[i]=2; } } int top=-1; int action[]= new int[n+1]; for(int i=0;i<n-1;i++) { if(arr[i]==2) { arr[i]=1; if(arr[i+1]==2) arr[i+1]=1; else arr[i+1]=2; top++; action[top]=i+1; } } if(arr[n-1]==1) { out.println(top+1); for(int i=0;i<=top;i++) { out.print(action[i]+" "); } } else { top=-1; for(int i=0;i<n-1;i++) { if(arr1[i]==1) { arr1[i]=2; if(arr1[i+1]==2) arr1[i+1]=1; else arr1[i+1]=2; top++; action[top]=i+1; } } if(arr1[n-1]==2) { out.println(top+1); for(int i=0;i<=top;i++) { out.print(action[i]+" "); } } else out.println(-1); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int u ; int v ; int val; edge(int u1, int v1 , int val1) { this.u=u1; this.v=v1; this.val=val1; } public int compareTo(edge e) { return this.val-e.val; } }
JAVA
1271_B. Blocks
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).
2
8
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class C { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InputReader s = new InputReader(System.in); PrintWriter p = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); int n = s.nextInt(); String str = s.nextLine(); char[] arr = new char[n]; arr = str.toCharArray(); ArrayList<Integer> list = new ArrayList<>(); int nw = 0, nb = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'W') nw++; else { nb++; } } if (nb % 2 == 1 && nw % 2 == 1) { System.out.println(-1); return; } for (int i = 0; i < str.length(); i++) { if (i == str.length() - 2) { break; } if (arr[i] != arr[i + 1]) { list.add(i + 2); if (arr[i + 1] == 'W') { arr[i + 1] = 'B'; if (arr[i + 2] == 'B') { arr[i + 2] = 'W'; } else { arr[i + 2] = 'B'; } }else if(arr[i + 1] == 'B') { arr[i + 1] = 'W'; if (arr[i + 2] == 'B') { arr[i + 2] = 'W'; } else { arr[i + 2] = 'B'; } } } } for (int i = str.length() - 1; i >= 0; i--) { if (i <= 1) { break; } if (arr[i] != arr[i - 1]) { list.add(i - 1); if (arr[i - 1] == 'W') { arr[i - 1] = 'B'; if (arr[i - 2] == 'B') { arr[i - 2] = 'W'; } else { arr[i - 2] = 'B'; } }else if (arr[i - 1] == 'B') { arr[i - 1] = 'W'; if (arr[i - 2] == 'B') { arr[i - 2] = 'W'; } else { arr[i - 2] = 'B'; } } } } for (int i = 0; i < str.length(); i++) { if (i == str.length() - 2) { break; } if (arr[i] != arr[i + 1]) { list.add(i + 2); if (arr[i + 1] == 'W') { arr[i + 1] = 'B'; if (arr[i + 2] == 'B') { arr[i + 2] = 'W'; } else { arr[i + 2] = 'B'; } }else if(arr[i + 1] == 'B') { arr[i + 1] = 'W'; if (arr[i + 2] == 'B') { arr[i + 2] = 'W'; } else { arr[i + 2] = 'B'; } } } } System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } p.flush(); p.close(); } public static ArrayList Divisors(long n) { ArrayList<Long> div = new ArrayList<>(); for (long i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { div.add(i); if (n / i != i) div.add(n / i); } } return div; } public static int BinarySearch(long[] a, long k) { int n = a.length; int i = 0, j = n - 1; int mid = 0; if (k < a[0]) return 0; else if (k >= a[n - 1]) return n; else { while (j - i > 1) { mid = (i + j) / 2; if (k >= a[mid]) i = mid; else j = mid; } } return i + 1; } public static long GCD(long a, long b) { if (b == 0) return a; else return GCD(b, a % b); } public static long LCM(long a, long b) { return (a * b) / GCD(a, b); } static class pair implements Comparable<pair> { Integer x, y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); return result; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x - x == 0 && p.y - y == 0; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class CodeX { public static void sort(long arr[]) { merge_sort(arr, 0, arr.length - 1); } private static void merge_sort(long A[], long start, long end) { if (start < end) { long mid = (start + end) / 2; merge_sort(A, start, mid); merge_sort(A, mid + 1, end); merge(A, start, mid, end); } } private static void merge(long A[], long start, long mid, long end) { long p = start, q = mid + 1; long Arr[] = new long[(int) (end - start + 1)]; long k = 0; for (int i = (int) start; i <= end; i++) { if (p > mid) Arr[(int) k++] = A[(int) q++]; else if (q > end) Arr[(int) k++] = A[(int) p++]; else if (A[(int) p] < A[(int) q]) Arr[(int) k++] = A[(int) p++]; else Arr[(int) k++] = A[(int) q++]; } for (int i = 0; i < k; i++) { A[(int) start++] = Arr[i]; } } } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long SIZE = 100000; const int INF = 0x3f3f3f3f; const long long ll_INF = 0x3f3f3f3f3f3f3f3f; const long double PI = acos(-1); const long long MAXN = numeric_limits<long long>::max(); const long long MAX = 2000000; void disp(vector<long long> v) { for (auto u : v) cout << u << " "; cout << '\n'; } void solve() { long long n, z = 0, fg = 0, fg1 = 0, sum1 = 0, sum2 = 0; cin >> n; string s; cin >> s; vector<long long> x, y, ans; for (long long i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) z++; else { x.push_back(z + 1); z = 0; } } x.push_back(z + 1); for (long long i = 0; i < ((int)(x).size()); i++) { if (i % 2 == 0) sum1 += x[i]; else sum2 += x[i]; } if (sum1 % 2 == 0) { y = x; for (long long i = 1; i < ((int)(y).size()); i++) y[i] += y[i - 1]; for (long long i = 0; i < ((int)(y).size()) - 1; i += 2) { if (x[i] % 2 != 0) { for (long long j = y[i]; j < y[i + 1]; j++) { ans.push_back(j); } x[i] -= 1; x[i + 2] += 1; } } y.clear(); y.push_back(1); for (long long i = 0; i < ((int)(x).size()); i++) y.push_back(x[i]); for (long long i = 1; i < ((int)(y).size()); i++) y[i] += y[i - 1]; for (long long i = 0; i < ((int)(y).size()) - 1; i += 2) { while (y[i] < y[i + 1]) { ans.push_back(y[i]); y[i] += 2; } } cout << ((int)(ans).size()) << '\n'; if (((int)(ans).size()) != 0) disp(ans); } else if (sum2 % 2 == 0) { y = x; for (long long i = 1; i < ((int)(y).size()); i++) y[i] += y[i - 1]; for (long long i = 1; i < ((int)(y).size()) - 1; i += 2) { if (x[i] % 2 != 0) { for (long long j = y[i]; j < y[i + 1]; j++) { ans.push_back(j); } x[i] -= 1; x[i + 2] += 1; } } y.clear(); y = x; y[0]++; for (long long i = 1; i < ((int)(y).size()); i++) y[i] += y[i - 1]; for (long long i = 0; i < ((int)(y).size()) - 1; i += 2) { while (y[i] < y[i + 1]) { ans.push_back(y[i]); y[i] += 2; } } cout << ((int)(ans).size()) << '\n'; if (((int)(ans).size()) != 0) disp(ans); } else cout << -1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
CPP
1271_B. Blocks
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).
2
8
from sys import stdin input = stdin.readline def mp():return map(int,input().split()) def it():return int(input()) n=it() s=list(input()) s=s[:n] k=s.copy() v=[] for i in range(n-1): # print(s[i]) if s[i]=='B' and s[i+1]=='W': s[i],s[i+1]=s[i+1],s[i] v.append(i+1) elif s[i]=='W': pass elif s[i]=='B' and s[i+1]=="B": s[i]="W" s[i+1]='W' v.append(i+1) # print(s) t=[] for i in range(n-1): # print(s[i]) if k[i]=='W' and k[i+1]=='B': k[i],k[i+1]=k[i+1],k[i] t.append(i+1) elif k[i]=='B': pass elif k[i]=='W' and k[i+1]=="W": k[i]="B" k[i+1]='B' t.append(i+1) if len(set(s))==1 : print(len(v)) print(*v) elif len(set(k))==1: print(len(t)) print(*t) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
a=int(input()) b=input() l=len(b) lw=b.count('W') lb=b.count('B') b=[i for i in b] if lw%2==0: ans=0 arr=[] for i in range(l-1): if b[i]=='W': b[i]='B' if b[i+1]=='W': b[i+1]='B' else: b[i+1]='W' ans+=1 arr.append(i+1) print(ans) print(*arr) elif lb%2==0: ans=0 arr=[] for i in range(l-1): if b[i]=='B': b[i]='W' if b[i+1]=='B': b[i+1]='W' else: b[i+1]='B' ans+=1 arr.append(i+1) print(ans) print(*arr) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.*; import java.util.Map.Entry; import java.lang.*; import java.math.*; import java.text.*; import java.io.*; public final class Solve { static PrintWriter out = new PrintWriter(System.out); static void flush() { out.flush(); } static void run(long s, long e) { NumberFormat formatter = new DecimalFormat("#0.00000"); System.out.print("Execution time is " + formatter.format((e - s) / 1000d) + " seconds"); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } static boolean isPalindrome(String str1, String str2) { String str3 = str1+str2; int i = 0, j = str3.length()-1; while(i < j) { char a = str3.charAt(i), b = str3.charAt(j); if(a != b) return false; i++;j--; } return true; } static boolean isPalindrome(String str) { int i = 0, j = str.length()-1; while(i < j) { char a = str.charAt(i), b = str.charAt(j); if(a != b) return false; i++;j--; } return true; } String next() { while (st == null || !st.hasMoreElements()) { try{st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();} } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next());} static int fact(int n) { if(n == 1) return 1; return n * fact(n-1); } public int[] readIntArray(int n) { int[] arr = new int[n]; for(int i=0; i<n; ++i) arr[i]=nextInt(); return arr; } public int[][] readIntArray(int m, int n){ int[][] arr = new int[m][n]; for(int i = 0;i<m;i++) for(int j = 0;j<n;j++) arr[i][j] = nextInt(); return arr; } public String[] readStringArray(int n) { String[] arr = new String[n]; for(int i=0; i<n; ++i) arr[i]= nextLine(); return arr; } double nextDouble() {return Double.parseDouble(next());} String nextLine() { String str = ""; try{str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str;} } static void solve(int n, char[] ch) { Set<Character> set = new HashSet<>(); for(int i = 0;i<n;i++) { set.add(ch[i]); } if(set.size() == 1) { out.println(0); return; } int fl = 0; List<Integer> list = new ArrayList<>(); char[] arr = new char[n]; for(int i =0;i<n;i++) { arr[i] = ch[i]; } for(int i = 0;i<n-1;i++) { if(ch[i] == 'B' && ch[i+1] == 'W') { ch[i] = 'W'; ch[i+1] = 'B'; list.add(i+1); } else if(ch[i] == 'B' && ch[i+1] == 'B') { ch[i] = 'W'; ch[i+1] = 'W'; list.add(i+1); i++; } } if(ch[n-2] != ch[n-1]) { } else { fl = 1; out.println(list.size()); for(int i = 0;i<list.size();i++) { out.print(list.get(i)+" "); } } if(fl == 0) { list.clear(); for(int i = 0;i<n-1;i++) { if(arr[i] == 'W' && arr[i+1] == 'B'){ arr[i] = 'B'; arr[i+1] = 'W'; list.add(i+1); } else if(arr[i] == 'W' && arr[i+1] == 'W') { arr[i] = 'B'; arr[i+1] = 'B'; list.add(i+1); i++; } } if(arr[n-2] != arr[n-1]) { out.println(-1); return; } else { out.println(list.size()); for(int i = 0;i<list.size();i++) { out.print(list.get(i)+" "); } } } out.println(); } public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); long s1 = System.currentTimeMillis(); int n = sc.nextInt(); char[] ch = sc.next().toCharArray(); solve(n,ch); flush(); long e = System.currentTimeMillis(); // run(s1,e); } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; string s; cin >> s; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { if (s[i] == 'W') cnt1++; else cnt2++; } if (cnt1 % 2 == 1 && cnt2 % 2 == 1) { cout << -1; return; } if (cnt1 & 1) { for (int i = 0; i < s.size(); i++) { if (s[i] == 'W') s[i] = 'B'; else s[i] = 'W'; } } vector<int> v; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W' && s[i + 1] == 'W') { v.push_back(i); s[i] = 'B'; s[i + 1] = 'B'; } } bool flag = 0; for (int i = 0; i < n - 1; i++) { if (flag == 0 && s[i] == 'W') { v.push_back(i); s[i + 1] = 'W'; s[i] = 'B'; flag = 1; } else if (flag == 1) { if (s[i + 1] == 'W') { v.push_back(i); s[i + 1] = 'B'; s[i] = 'B'; flag = 0; } else { v.push_back(i); s[i + 1] = 'W'; s[i] = 'B'; } } } cout << v.size() << '\n'; for (int i = 0; i < v.size(); i++) { cout << v[i] + 1 << " "; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t = 1; while (t--) { solve(); } }
CPP
1271_B. Blocks
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).
2
8
length = int(input()) x = input() tot = 0 in_binary = [] for i in range(length): if x[i] == 'B': tot += 1 to_change = [] x = [x[i] for i in range(length)] if (length-tot) % 2 == 0: for i in range(length-1): if x[i] == 'W': to_change.append(i+1) if x[i+1] == 'W': x[i+1] = 'B' else: x[i+1] = 'W' print(len(to_change)) for i in to_change: print(i, end=" ") # change all to black elif tot % 2 == 0: for i in range(length-1): if x[i] == 'B': to_change.append(i+1) if x[i+1] == 'B': x[i+1] = 'W' else: x[i+1] = 'B' print(len(to_change)) for i in to_change: print(i, end = " "), else: print("-1")
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; vector<int> posz; for (int i = 0; i < s.size(); i++) { if (s[i] == 'W' && i + 1 < s.size()) { s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; posz.push_back(i + 1); } } bool check_sol = true; char pivot = s[0]; for (int i = 1; i < s.size() && check_sol; i++) { if (s[i] != pivot) check_sol = false; } if (!check_sol) { if ((s.size() - 1) % 2 == 0) { for (int i = 0; i < s.size() - 1; i += 2) posz.push_back(i + 1); cout << posz.size() << "\n"; for (int i = 0; i < posz.size(); i++) { cout << posz[i] << " "; } cout << "\n"; } else { cout << "-1\n"; } } else { cout << posz.size() << "\n"; for (int i = 0; i < posz.size(); i++) { cout << posz[i] << " "; } cout << "\n"; } return 0; }
CPP
1271_B. Blocks
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).
2
8
import java.util.ArrayList; import java.util.Scanner; public final class Blocks{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char[] s = sc.next().toCharArray(); ArrayList<Integer> l = new ArrayList<>(); int count = 0; int cw = 0; int cb = 0; for(char c : s){ if(c == 'W') cw++; else cb++; } // boolean change = false; for(int i=1; i<n; i++){ if(l.size() > 3*n || count == n-1){ System.out.println(-1); return; } // System.out.println(i); if(i == 0){ // System.out.println(cw+" "+cb); // s[i+1] != s[i+2] || if(cw != 0 || cb != 0){ // count++; l.add(1); if(s[i] == 'B'){ cw++; cb--; s[i] = 'W'; } else{ cb++; cw--; s[i] = 'B'; } if(s[i+1] == 'B'){ cw++; cb--; s[i+1] = 'W'; } else{ cb++; cw--; s[i+1] = 'B'; } }else{ count++; } }else if(s[i] != s[i-1]){ // System.out.println(s[i]+" "+s[i-1]); if(i+1==n){ i = -1; count = 0; // count = 0; continue; } // count++; l.add(i+1); if(s[i] == 'B'){ cw++; cb--; s[i] = 'W'; } else{ cb++; cw--; s[i] = 'B'; } if(s[i+1] == 'B'){ cw++; cb--; s[i+1] = 'W'; } else{ cb++; cw--; s[i+1] = 'B'; } }else{ count++; } // for(char c: s){ // System.out.print(c); // }System.out.println(); } System.out.println(l.size()); for(int i : l){ System.out.print(i+" "); } sc.close(); } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; string s; cin >> n >> s; int w = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == 'B') b++; else w++; } vector<int> ans; if (b % 2 == 0) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { ans.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else if (w % 2 == 0) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i + 1); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { cout << -1; return 0; } cout << ans.size() << '\n'; for (int it : ans) cout << it << ' '; }
CPP
1271_B. Blocks
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).
2
8
n=int(input()) s=list(reversed(input())) s1=s[::-1] s2=s[::-1] ans=[] for i in range(n-1): if s1[i]=='W': ans.append(i+1) if s1[i+1]=='W': s1[i+1]='B' else: s1[i+1]='W' if s1[n-1]=='B': print(len(ans)) print(*ans,sep=' ') else: ans=[] for i in range(n-1): if s2[i]=='B': ans.append(i+1) if s2[i+1]=='B': s2[i+1]='W' else: s2[i+1]='B' if s2[n-1]=='W': print(len(ans)) print(*ans,sep=' ') else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) if n % 2 == 1: t = 'W' if s.count('W') % 2 == 1 else 'B' o = 'B' if t == 'W' else 'W' # print (s, t * n) c = 0 array = [] for i in range(n - 1): if s[i] == o and s[i + 1] == o: s[i] = t s[i + 1] = t c += 1 array.append(i + 1) elif s[i] == o and s[i + 1] == t: s[i], s[i + 1] = s[i + 1], s[i] c += 1 array.append(i + 1) print (c) print (*array) # print (s) if n % 2 == 0: if s.count('W') % 2 == 1: print (-1) else: r = s.copy() t = 'W' o = 'B' # print (s, t * n) c1 = 0 array = [] for i in range(n - 1): if s[i] == o and s[i + 1] == o: s[i] = t s[i + 1] = t c1 += 1 array.append(i + 1) elif s[i] == o and s[i + 1] == t: s[i], s[i + 1] = s[i + 1], s[i] c1 += 1 array.append(i + 1) # print(c1) # if c1>0:print(*array) # print (s) array1 = array s = r array = [] t = 'B' o = 'W' # print (s, t * n) c2 = 0 for i in range(n - 1): if s[i] == o and s[i + 1] == o: s[i] = t s[i + 1] = t c2 += 1 array.append(i + 1) elif s[i] == o and s[i + 1] == t: s[i], s[i + 1] = s[i + 1], s[i] c2 += 1 array.append(i + 1) # print(c2) # if c2>0:print(*array) if c1 < c2: print (c1) print (*array1) else: print (c2) print (*array) # print (s)
PYTHON3
1271_B. Blocks
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).
2
8
import java.io.*; import java.util.*; import java.lang.*; public class Blocks{ public static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char)c; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } static long func(long a[],long size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i<size;i++){ maxc=Math.max(a[i],maxc+a[i]); max1=Math.max(maxc,max1); } return max1; } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } public static void main(String args[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader sc = new FastReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int n = sc.nextInt(); String s = sc.next(); ArrayList<Integer> wlist = new ArrayList<>(); ArrayList<Integer> blist = new ArrayList<>(); int wcount = 0,bcount = 0; for(int i = 0; i < n; i++) { if(s.charAt(i) == 'W') { if(wcount % 2 == 0) { wlist.add(i+1); } if(bcount % 2 == 1) { blist.add(i+1); } wcount++; } else if(s.charAt(i) == 'B') { if(bcount % 2 == 0) { blist.add(i+1); } if(wcount % 2 == 1) { wlist.add(i+1); } bcount++; } } //w.println(blist); if(bcount % 2 == 1 && wcount % 2 == 1) { w.println("-1"); } if(wcount % 2 == 0) { w.println(wlist.size()); for(int i : wlist) { w.print(i+" "); } } else if(bcount % 2 == 0) { w.println(blist.size()); for(int i : blist) { w.print(i+" "); } } w.close(); } }
JAVA
1271_B. Blocks
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).
2
8
import sys input=sys.stdin.readline n=int(input()) s=input().strip() s=list(s) a=s.count('W') b=s.count("B") for i in range(n): if s[i]=='W': s[i]=0 else: s[i]=1 if a&1 and b&1: print(-1) exit() if a%2==0: #all white ans=[] for i in range(n-1): if s[i]==0: s[i]=1-s[i] s[i+1]=1-s[i+1] ans.append(i+1) # print(s) if s[0]!=s[-1]: for i in range(0,n-1,2): ans.append(i+1) print(len(ans)) print(*ans) elif b%2==0: ans=[] for i in range(n-1): if s[i]==1: s[i]=1-s[i] s[i+1]=1-s[i+1] ans.append(i+1) #print(s) if s[0]!=s[-1]: for i in range(0,n-1,2): ans.append(i+1) print(len(ans)) print(*ans)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, w, b; cin >> s; w = s; b = s; std::vector<int> v1, v2; for (int i = 0; i < n - 1; ++i) { if (w[i] == 'B') { v1.push_back(i + 1); w[i] = 'W'; if (w[i + 1] == 'W') { w[i + 1] = 'B'; } else { w[i + 1] = 'W'; } } } for (int i = 0; i < n - 1; ++i) { if (b[i] == 'W') { v2.push_back(i + 1); b[i] = 'B'; if (b[i + 1] == 'W') { b[i + 1] = 'B'; } else { b[i + 1] = 'W'; } } } int flag; flag = 1; for (int i = 0; i < n; ++i) { if (w[i] != 'W') { flag = -1; break; } } if (flag == 1) { printf("%d\n", v1.size()); for (int i = 0; i < v1.size(); ++i) { printf("%d ", v1[i]); } printf("\n"); return 0; } flag = 1; for (int i = 0; i < n; ++i) { if (b[i] != 'B') { flag = -1; break; } } if (flag == 1) { printf("%d\n", v2.size()); for (int i = 0; i < v2.size(); ++i) { printf("%d ", v2[i]); } printf("\n"); return 0; } printf("-1\n"); return 0; }
CPP
1271_B. Blocks
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).
2
8
n = int(input()) inp = list(input()) res = [] res1 = [] subans = [] ind = 0 changes = True while changes: changes = False ind = 0 while ind < n - 2: if inp[ind] == 'B' and inp[ind + 1] == 'W' and inp[ind + 2] == 'B': inp[ind] = 'W' inp[ind + 1] = 'W' inp[ind + 2] = 'W' subans.append(ind + 1) subans.append(ind + 2) changes = True if inp[ind] == 'W' and inp[ind + 1] == 'B' and inp[ind + 2] == 'W': inp[ind] = 'B' inp[ind + 1] = 'B' inp[ind + 2] = 'B' subans.append(ind + 1) subans.append(ind + 2) changes = True ind += 1 ind = 0 bf = inp[:] while ind < n - 1: if bf[ind] == bf[ind + 1] == 'W': res1.append(ind + 1) bf[ind] = 'B' bf[ind + 1] = 'B' ind += 2 else: if bf[ind] == 'W': bf[ind] = 'B' bf[ind + 1] = 'B' if bf[ind + 1] == 'W' else 'W' res1.append(ind + 1) ind += 1 if 'W' not in bf: res.append(res1) ind = 0 res1 = [] bf = inp[:] while ind < n - 1: if bf[ind] == bf[ind + 1] == 'B': res1.append(ind + 1) bf[ind] = 'W' bf[ind + 1] = 'W' ind += 2 else: if bf[ind] == 'B': bf[ind] = 'W' bf[ind + 1] = 'W' if bf[ind + 1] == 'B' else 'B' res1.append(ind + 1) ind += 1 if 'B' not in bf: res.append(res1) if 'W' not in inp or 'B' not in inp: print(len(subans)) print(*subans) else: if len(res) == 0 and len(subans) == 0: print(-1) else: if len(res) == 0: if 'W' not in inp or 'B' not in inp: print(len(subans)) print(*subans) else: print(-1) elif len(subans) == 0: print(len(res[0])) print(*res[0]) else: print(len(subans) + len(res[0])) print(*(subans + res[0]))
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.ArrayList; import java.util.Scanner; public final class Blocks { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); String str=sc.nextLine(); StringBuilder ans=new StringBuilder(str); int w=0,b=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='W') w++; else b++; } if(w==0 || b==0){ System.out.println("0"); return; } if(w%2!=0 && b%2!=0) System.out.println("-1"); else{ ArrayList<Integer> list=new ArrayList<>(); if(b%2==0){ for(int i=0;i<ans.length()-1;i++){ if(ans.charAt(i)=='B'){ ans.replace(i,i+1,"W"); String op=(ans.charAt(i+1)=='W')?"B":"W"; ans.replace(i+1,i+2,op); list.add(i); } } }else{ for(int i=0;i<ans.length()-1;i++){ if(ans.charAt(i)=='W'){ ans.replace(i,i+1,"B"); String op=(ans.charAt(i+1)=='W')?"B":"W"; ans.replace(i+1,i+2,op); list.add(i); } } } System.out.println(list.size()); for(int ele:list){ System.out.print((ele+1)+" "); } } } }
JAVA
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) sB = 0 sW = 0 for i in range(len(s)): if s[i] == 'W': sW += 1 else: sB += 1 if sW % 2 == 1 and sB % 2 == 1: print(-1) exit() if (sW % 2 == 1 and sB % 2 == 0)or(sW % 2 == 0 and sB % 2 == 0 and sB < sW) : for i in range(len(s)): if s[i] == 'W': s[i] = 'B' else: s[i] = 'W' out = [] for i in range(len(s)-1): if s[i] == 'W' and s[i+1] == 'W': s[i] = 'B' s[i+1] = 'B' out.append(str(i+1)) elif s[i] == 'W' and s[i+1] == 'B': s[i], s[i+1] = s[i+1], s[i] out.append(str(i+1)) print(len(out)) print(' '.join(out))
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; using namespace chrono; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } long long gcd(long long a, long long b) { if (b > a) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } long long expo(long long a, long long b, long long mod) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } void extendgcd(long long a, long long b, long long *v) { if (b == 0) { v[0] = 1; v[1] = 0; v[2] = a; return; } extendgcd(b, a % b, v); long long x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return; } long long mminv(long long a, long long b) { long long arr[3]; extendgcd(a, b, arr); return arr[0]; } long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } bool revsort(long long a, long long b) { return a > b; } long long combination(long long n, long long r, long long m, long long *fact, long long *ifact) { long long val1 = fact[n]; long long val2 = ifact[n - r]; long long val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m; } void google(int t) { cout << "Case #" << t << ": "; } vector<long long> sieve(int n) { int *arr = new int[n + 1](); vector<long long> vect; for (int i = 2; i <= n; i++) if (arr[i] == 0) { vect.push_back(i); for (int j = 2 * i; j <= n; j += i) arr[j] = 1; } return vect; } long long mod_add(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } long long mod_mul(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } long long mod_sub(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } long long mod_div(long long a, long long b, long long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } long long phin(long long n) { long long number = n; if (n % 2 == 0) { number /= 2; while (n % 2 == 0) n /= 2; } for (long long i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) { while (n % i == 0) n /= i; number = (number / i * (i - 1)); } } if (n > 1) number = (number / n * (n - 1)); return number; } bool check(string &s) { int n = s.length(); for (int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { return false; } } return true; } void invert(char &c, char &c2) { if (c == 'W') { c = 'B'; } else if (c == 'B') { c = 'W'; } int ct = 0; if (c2 == 'W') { c2 = 'B'; } else if (c2 == 'B') { c2 = 'W'; } } void out(vector<int> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << ' '; } cout << "\n"; } void solve() { int n; cin >> n; string s; cin >> s; int b = count(s.begin(), s.end(), 'B'); int w = count(s.begin(), s.end(), 'W'); if (b % 2 && w % 2) { cout << -1; return; } if (w == 0 || b == 0) { cout << 0; return; } else { vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B' && w % 2 == 1) { s[i] = 'W'; ans.push_back(i + 1); if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else s[i + 1] = 'B'; } else if (s[i] == 'W' && w % 2 == 0) { s[i] = 'B'; ans.push_back(i + 1); if (s[i + 1] == 'B') { s[i + 1] = 'W'; } else { s[i + 1] = 'B'; } } } cout << ((int)(ans).size()) << "\n"; out(ans); } } int main() { freopen("Error.txt", "w", stderr); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); auto start1 = high_resolution_clock::now(); int t = 1; while (t--) solve(); auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); cerr << "Time: " << duration.count() / 1000 << endl; }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; using namespace std; int s[211]; int m[211]; int bb[211]; int n; int t[211]; int main() { cin >> n; string a; cin >> a; for (int i = 0; i < n; i++) { if (a[i] == 'W') { s[i + 1] = 1; t[i + 1] = 1; } } int sumss = 0; for (int i = 1; i < n - 1; i++) { if (s[i] != s[i + 1]) { s[i + 1] = 1 - s[i + 1]; s[i + 2] = 1 - s[i + 2]; bb[++sumss] = i + 1; } } int ss = 0; t[1] = 1 - t[1]; t[2] = 1 - t[2]; m[++ss] = 1; for (int i = 1; i < n - 1; i++) { if (t[i] != t[i + 1]) { t[i + 1] = 1 - t[i + 1]; t[i + 2] = 1 - t[i + 2]; m[++ss] = i + 1; } } if (s[n] != s[1] && t[n] != t[1]) { cout << "-1" << endl; } else { if (s[n] == s[1]) { cout << sumss << endl; for (int i = 1; i <= sumss; i++) { cout << bb[i] << " "; } } else if (s[n] != s[1] && t[n] == t[1]) { cout << ss << endl; for (int i = 1; i <= ss; i++) { cout << m[i] << " "; } } } }
CPP
1271_B. Blocks
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).
2
8
import java.util.*; public class _1271B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int b = 0, w = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == 'B') b++; else w++; } char []ch=s.toCharArray(); if (b % 2 != 0 && w % 2 != 0) System.out.println("-1"); else if (b == 0 || w == 0) System.out.println("0"); else { int moves = 0; StringBuilder st = new StringBuilder(); if (b % 2 == 0 && b != 0 ) { for (int i = 0; i < n; i++) { if (i + 1 < n && ch[i] == 'B' && ch[i+1] == 'B') { ch[i]='W'; ch[i+1]='W'; st.append((i + 1) + " "); moves++; i++; } else if (i + 1 < n && ch[i] == 'B' && ch[i+1]== 'W') { ch[i]='W'; ch[i+1]='B'; moves++; st.append((i + 1) + " "); } } } else if (w % 2 == 0 && w != 0 ) { for (int i = 0; i < n; i++) { if (i + 1 < n && ch[i]=='W' && ch[i+1]=='W') { ch[i]='B'; ch[i+1]='B'; st.append((i + 1) + " "); moves++; i++; } else if (i + 1 < n && ch[i]== 'W' && ch[i+1] == 'B') { ch[i]='B'; ch[i+1]='W'; moves++; st.append((i + 1) + " "); } } } System.out.println(moves); System.out.println(st); } } }
JAVA
1271_B. Blocks
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).
2
8
n = int(input()) string = input() s = list(string) ans1 = [] ans2 = [] i = 0 count = 0 while i < n-1: if s[i] == 'B' and s[i+1] == 'B': i+=2 elif s[i] == 'B' and s[i+1] == 'W': i+=1 elif s[i] == 'W' and s[i+1] == 'B': s[i],s[i+1] = s[i+1],s[i] count+=1 ans1.append(i+1) i+=1 else: s[i],s[i+1] = 'B','B' count+=1 ans1.append(i+1) i+=2 if s.count('W')==0: print(count) if count!=0: for i in ans1: print(i,end=' ') print() else: i = 0 count = 0 s = list(string) while i < n-1: if s[i] == 'W' and s[i+1] == 'W': i+=2 elif s[i] == 'W' and s[i+1] == 'B': i+=1 elif s[i] == 'B' and s[i+1] == 'W': s[i],s[i+1] = s[i+1],s[i] count+=1 ans2.append(i+1) i+=1 else: s[i],s[i+1] = 'W','W' count+=1 ans2.append(i+1) i+=2 if s.count('B')==0: print(count) if count!=0: for i in ans2: print(i,end=' ') print() else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) t = list(input()) r = [] f = False m = [0 if i == 'W' else 1 for i in t] # print(m) for i in range(1, len(m) - 1): if m[i] != m[i - 1]: r.append(i + 1) m[i] ^= 1 m[i + 1] ^= 1 # print(m) if sum(m) == n or sum(m) == 0: # print('YES') f = True break if m[0] != m[-1]: if len(t) % 2 != 0: for i in range(0, len(m), 2): if i + 1 < len(m): r.append(i + 1) m[i] ^= 1 m[i + 1] ^= 1 # print(m) # print('YES') f = True if f or sum(m) == n or sum(m) == 0: print(len(r)) if len(r): for i in r: print(i, end=' ') print() else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import java.io.*; import java.util.*; public class P608B { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); char[] blocks = br.readLine().toCharArray(); int sum = 0; for(int i=0; i<n; i++) { if(blocks[i] == 'W') sum++; } if(sum % 2 == 1 && n % 2 == 0) { System.out.println(-1); } else { StringBuilder sb = new StringBuilder(); int operations = 0; for(int i=0; i<n-1; i++) { if(blocks[i] == 'B') { operations++; blocks[i] = 'W'; blocks[i+1] = blocks[i+1] == 'W' ? 'B' : 'W'; sb.append((i+1)).append(" "); } } if(blocks[n-1] == 'B') { for(int i=0; i<n-1; i+=2) { operations++; sb.append((i+1)).append(" "); } } System.out.println(operations); System.out.println(sb.toString()); } } }
JAVA
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input().strip()) b = 0 w = 0 for i in range(n): if s[i]=="B": b+=1 else: w+=1 op = [] if b%2==1 and w%2==1: print(-1) elif b==0 or w==0: print(0) else: if b%2==0 and w%2==0: if b<w: for i in range(n-1): if s[i]<s[i+1]: s[i],s[i+1] = s[i+1],s[i] op.append(i+1) elif s[i]==s[i+1]=="B": s[i],s[i+1]="W","W" op.append(i+1) print(len(op)) print(" ".join(map(str,op))) else: for i in range(n-1): if s[i]>s[i+1]: s[i],s[i+1] = s[i+1],s[i] op.append(i+1) elif s[i]==s[i+1]=="W": s[i],s[i+1]="B","B" op.append(i+1) print(len(op)) print(" ".join(map(str,op))) elif b%2==0: for i in range(n-1): if s[i]<s[i+1]: s[i],s[i+1] = s[i+1],s[i] op.append(i+1) elif s[i]==s[i+1]=="B": s[i],s[i+1]="W","W" op.append(i+1) print(len(op)) print(" ".join(map(str,op))) else: for i in range(n-1): if s[i]>s[i+1]: s[i],s[i+1] = s[i+1],s[i] op.append(i+1) elif s[i]==s[i+1]=="W": s[i],s[i+1]="B","B" op.append(i+1) print(len(op)) print(" ".join(map(str,op)))
PYTHON3
1271_B. Blocks
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).
2
8
def paint(blocks,num,colour,idx,changelist): if(idx==num-1): if(blocks[idx]==colour): return True else: return False if(blocks[idx]==colour): return paint(blocks,num,colour,idx+1,changelist) else: blocks[idx] = colour if(blocks[idx+1]=="B"): blocks[idx+1] = "W" else: blocks[idx+1] = "B" changelist.append(idx) return paint(blocks,num,colour,idx+1,changelist) if __name__=="__main__": num = int(input()) blocks = list(str(input())) dblocks = blocks.copy() #print(blocks) #list for recording changes changelist = list() #taking colour as white first col = "W" val = paint(blocks,num,col,0,changelist) if (val == True): print(len(changelist)) for i in changelist: print(i + 1, end=" ") exit(0) changelist = list() #checking with black col = "B" val = paint(dblocks,num,col,0,changelist) if(val==True): print(len(changelist)) for i in changelist: print(i+1,end=" ") exit(0) else: print("-1")
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ Reader scan = new Reader(); scan.init(System.in); OutputStream output = System.out; PrintWriter out = new PrintWriter(output); int N = scan.nextInt(); String A = scan.next(); String[] arr = A.split(""); String[] arr1 = A.split(""); ArrayList<Integer> str = new ArrayList<Integer>(); ArrayList<Integer> str1 = new ArrayList<Integer>(); for (int i=0;i<N-1;i++){ if (arr[i].equals("W")){ str.add(i+1); arr[i] = "B"; arr[i+1] = arr[i+1].equals("W") ? "B":"W"; } if (arr1[i].equals("B")){ str1.add(i+1); arr1[i] = "W"; arr1[i+1] = arr1[i+1].equals("W") ? "B":"W"; } } if (arr[N-1].equals("B") && arr1[N-1].equals("W")){ int len = str.size(); int len1 = str1.size(); if (len<len1){ System.out.println(len); for (int i : str){ System.out.print(i+" "); } System.out.println(); } else{ System.out.println(len1); for (int i : str1){ System.out.print(i+" "); } System.out.println(); } } else if (arr[N-1].equals("B")){ System.out.println(str.size()); for (int i : str){ System.out.print(i+" "); } System.out.println(); } else if (arr1[N-1].equals("W")){ System.out.println(str1.size()); for (int i : str1){ System.out.print(i+" "); } System.out.println(); } else{ System.out.println(-1); } out.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
JAVA
1271_B. Blocks
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).
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static void main(String[] args) { FastReader sc=new FastReader(); int n=sc.nextInt(); ArrayList adj=new ArrayList(); ArrayList adj1=new ArrayList(); String str=sc.next(); int len=str.length(); char c[]=str.toCharArray(); char d[]=str.toCharArray(); for( int i=0;i<len;i++) { if(c[i]=='B') continue; else { if(i+1<len) { c[i]='B'; if(c[i+1]=='W') { c[i+1]='B'; } else c[i+1]='W'; adj.add(i+1); } } } int k=0; for( k=0;k<len;k++) { if(c[k]=='B') continue; else break; } for( int i=0;i<len;i++) { if(d[i]=='W') continue; else { if(i+1<len) { d[i]='W'; if(d[i+1]=='B') { d[i+1]='W'; } else d[i+1]='B'; adj1.add(i+1); } } } int j=0; for( j=0;j<len;j++) { if(d[j]=='W') continue; else break; } if(k==len) { System.out.println(adj.size()); StringBuilder sb=new StringBuilder(); for( int p=0;p<adj.size();p++) { sb.append(adj.get(p)); sb.append(" "); } System.out.println(sb.toString()); } else if(j==len) { System.out.println(adj1.size()); StringBuilder sb=new StringBuilder(); for( int p=0;p<adj1.size();p++) { sb.append(adj1.get(p)); sb.append(" "); } System.out.println(sb.toString()); } else System.out.println(-1); } } class Pair { long val; String str; Pair(long val, String str) { this.val=val; this.str=str; } } class myComparator implements Comparator<Pair> { public int compare( Pair a, Pair b) { if(a.val>b.val) { return 1; } else if(b.val>a.val) { return -1; } else return 0; } }
JAVA
1271_B. Blocks
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).
2
8
import java.util.Collections; import java.util.Comparator; import java.util.Scanner; import java.util.Vector; public class ProblemB { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); char[] c = s.toCharArray(); int whiteCnt = 0; int blackCnt = 0; Vector<Integer> a = new Vector(); for (int i = 0; i < n; i++) { if (c[i] == 'W') whiteCnt++; else blackCnt++; } if (whiteCnt % 2 == 1 && blackCnt % 2 == 1) { System.out.println(-1); return; } else if (whiteCnt % 2 == 0) { for (int i = 0; i < n - 1; i++) { if (c[i] == 'W') { c[i] = 'B'; if(c[i+1] == 'W') c[i+1] = 'B'; else c[i+1] = 'W'; a.add(i+1); } } } else { for (int i = 0; i < n - 1; i++) { if (c[i] == 'B') { c[i] = 'W'; if(c[i+1] == 'W') c[i+1] = 'B'; else c[i+1] = 'W'; a.add(i+1); } } } System.out.println(a.size()); for(int i=0; i<a.size(); i++) { System.out.print(a.get(i) + " "); } } }
JAVA
1271_B. Blocks
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).
2
8
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); FastWriter fw = new FastWriter(); int n = fr.nextInt(); String input = fr.nextString(); map = new HashMap<>(); map.put('W', 'B'); map.put('B', 'W'); boolean ans = tryToMake('B', new StringBuilder(input)) || tryToMake('W', new StringBuilder(input)); if (!ans) { fw.println(-1); } else { fw.println(ansSet.size()); for (int i : ansSet) { fw.printSp(i); } } fw.flush(); } static Map<Character, Character> map; static List<Integer> ansSet; static boolean tryToMake(char ch, StringBuilder input) { ansSet = new ArrayList<>(); int i = 0; while (i + 1 < input.length()) { char c = input.charAt(i); if (c != ch) { input.setCharAt(i, map.get(c)); input.setCharAt(i + 1, map.get(input.charAt(i + 1))); ansSet.add(i + 1); } i++; } return input.charAt(input.length() - 1) == ch; } } class FastReader { public FastReader() { } public FastReader(String fileName) throws FileNotFoundException { this.br = new BufferedReader(new FileReader(fileName)); } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; String nextString() throws IOException { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextString()); } long nextLong() throws IOException { return Long.parseLong(nextString()); } double nextDouble() throws IOException { return Double.parseDouble(nextString()); } } class FastWriter { public FastWriter() { } public FastWriter(String fileName) throws IOException { this.bw = new BufferedWriter(new FileWriter(fileName)); } BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); void print(Object o) throws IOException { bw.write(o.toString()); // bw.flush(); } void println(Object o) throws IOException { print(o.toString() + "\n"); } void printSp(Object o) throws IOException { print(o.toString() + " "); } void flush() throws IOException { bw.flush(); } }
JAVA
1271_B. Blocks
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).
2
8
n=int(input()) s1=list(input()) s2=s1 ans1=ans2=[] for i in range(n-1): if s1[i]=='B': s1[i]='W' if s1[i+1]=='B': s1[i+1]='W' else: s1[i+1]='B' ans1.append(i+1) if s1[n-1]=='W': print(len(ans1)) if len(ans1)>0: print(*ans1) else: for i in range(n-1): if s2[i]=='W': s2[i]='B' if s2[i+1]=='W': s2[i+1]='B' else: s2[i+1]='W' ans2.append(i+1) if s2[n-1]!='W' : print(len(ans2)) if len(ans2)>0: print(*ans2) else: print("-1")
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; bool res, res2 = false; char swapc(char a) { if (a == 'W') return 'B'; else return 'W'; } void solution(string s, char d, vector<int> &a) { int cnt = 0; for (int i = 0; i < s.size() - 1; i++) { if (s[i] != d) { s[i] = swapc(s[i]); s[i + 1] = swapc(s[i + 1]); a.push_back(i + 1); } else cnt++; } if (s[0] != s[s.size() - 1]) a.clear(); else { cnt++; res2 = true; } if (cnt == s.size()) { res = true; a.clear(); } } int main() { int n; cin >> n; string ans; cin >> ans; vector<int> v; solution(ans, 'B', v); if (!res2) solution(ans, 'W', v); if (res) cout << 0 << '\n'; else if (v.empty()) cout << -1 << '\n'; else { cout << v.size() << '\n'; for (auto i : v) cout << i << ' '; cout << '\n'; } return 0; }
CPP
1271_B. Blocks
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).
2
8
n=int(input()) s=input() l=list(s) s=list(s) for i in range(n): if s[i]=='W': s[i]=1 l[i]=1 else: s[i]=0 l[i]=0 f=1 p=[] c=0 for i in range(n-1): if s[i]==1: continue else: p.append(i+1) s[i]=1-s[i] s[i+1]=1-s[i+1] c+=1 #print(s) #print(p) if len(set(s))==1: print(c) if c>0: print(*p) else: p=[] c=0 #print('l',l) for i in range(n-1): if l[i]==0: continue else: p.append(i+1) l[i]=1-l[i] l[i+1]=1-l[i+1] c+=1 if len(set(l))==1: print(c) if c>0: print(*p) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.*; import java.io.*; public class Blocks { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=1; while(t-->0) { int n=Integer.parseInt(br.readLine().trim()); char a[]=br.readLine().trim().toCharArray(); char b[]=Arrays.copyOf(a,n); // for BBBBBB...... ArrayList<Integer> li_a=new ArrayList<>(); for(int i=0;i<n-1;i++) { if(a[i]!='B') { a[i]='B'; li_a.add(i+1); if(a[i+1]=='B') a[i+1]='W'; else a[i+1]='B'; } } int size_1=li_a.size(); if(a[n-1]!='B') size_1=3*n+1; // for WWWWWWW...... ArrayList<Integer> li_b=new ArrayList<>(); for(int i=0;i<n-1;i++) { if(b[i]!='W') { b[i]='W'; li_b.add(i+1); if(b[i+1]=='B') b[i+1]='W'; else b[i+1]='B'; } } int size_2=li_b.size(); if(b[n-1]!='W') size_2=3*n+1; if(size_1<3*n) { pw.println(size_1); for(int x:li_a) pw.print(x+" "); } else if(size_2<3*n) { pw.println(size_2); for(int x:li_b) pw.print(x+" "); } else { pw.println(-1); } } pw.flush(); } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1000; int n; int path[N]; vector<int> v; char s[N]; int tot; int main() { 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'; v.push_back(i); } } if (s[n] == 'W') { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; return 0; } for (int i = 1; i < n; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; v.push_back(i); } } if (s[n] == 'B') { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; return 0; } cout << -1 << endl; return 0; }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, count = 0, flag = 0, i, j, sum = 0; cin >> n; int arr[n], k = 0; char str[n + 1]; cin >> str; str[n] = '\0'; for (i = 0; i < n; i++) { if (str[i] == 'B') { count++; } } if (count % 2 != 0) { if (n % 2 == 0) cout << -1; else { count = n - count; if (count == 0) cout << 0; else { for (i = 0; i < n; i++) { if (str[i] == 'W' && flag == 0) { arr[k++] = i; flag = 1; } else if (str[i] == 'W') { sum = sum + i - arr[k - 1]; arr[k++] = i - 1; flag = 0; } } cout << sum << endl; for (i = 0; i < k; i += 2) { for (j = arr[i]; j <= arr[i + 1]; j++) { cout << j + 1 << " "; } } } } } else { if (count == 0) cout << 0; else { for (i = 0; i < n; i++) { if (str[i] == 'B' && flag == 0) { arr[k++] = i; flag = 1; } else if (str[i] == 'B') { sum += i - arr[k - 1]; arr[k++] = i - 1; flag = 0; } } cout << sum << endl; for (i = 0; i < k; i += 2) { for (j = arr[i]; j <= arr[i + 1]; j++) { cout << j + 1 << " "; } } } } return 0; }
CPP
1271_B. Blocks
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).
2
8
n = int(input()) sq = input().strip() sq = list(sq) b, w = (0, 0) for ch in sq: if ch == 'B': b += 1 else: w += 1 # only single type if b == 0 or w == 0: print(0) exit(0) # any of them is odd elif b & 1 == 1 and w & 1 == 1: print(-1) exit(0) c_to = None other = None if b & 1: c_to = 'B' other = 'W' else: c_to = 'W' other = 'B' ans = [] # print(f'c_to: {c_to}') # go pair wise and convert all the pairwise while True: for i in range(1, n): if sq[i-1] != sq[i] and sq[i-1] == other: sq[i-1] = sq[i] sq[i] = other ans.append(i-1) if sq[i-1] == sq[i] and sq[i-1] != c_to: sq[i-1] = sq[i] = c_to ans.append(i-1) cc = sq.count(other) # print(sq) if cc == 0 or cc == n: print(len(ans)) ans = list(map(lambda x: x + 1, ans)) print(*ans) exit(0)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) def inv(si): if si == "W": return "B" else: return "W" fs = [] i = 0 while i < n-2: b = s[i] s1 = s[i+1] s2 = s[i+2] if b == s1 and s1 == s2: i+=2 elif b == s1: i+=1 elif b != s1: s[i+1] = inv(s[i+1]) s[i+2] = inv(s[i+2]) fs.append(i+1+1) i+=1 if (s[n-3] == s[n-2] and s[n-2] == s[n-1]): print(len(fs)) if (len(fs) > 0): print(*fs) else: if len(s) % 2 == 0: print(-1) else: for i in range(0, n-1, 2): fs.append(i+1) print(len(fs)) if (len(fs) > 0): print(*fs)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) arr = list(input().strip()) def foo(arr, c1, c2): global n c = 0 st = [] for i in range(n-1): if arr[i] == c1: continue else: arr[i] = c1 arr[i+1] = c1 if arr[i+1] == c2 else c2 c += 1 st.append(i+1) if arr[-1] == c2: return(-1, -1) else: return(c, ' '.join(map(str, st))) a, b = foo(list(arr), 'B','W') c, d = foo(list(arr), 'W','B') #print(a) #print(b) #print(c) #print(d) if a!=-1: print(a) print(b) elif c!=-1: print(c) print(d) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader st = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(st.readLine()); String str = st.readLine(); int b = 0; int w = 0; for(int i = 0; i < n; i++) { if(str.charAt(i) == 'B') b++; else w++; } if(b%2 == 1 && w%2 == 1) { System.out.println("-1"); System.exit(0); } boolean black = false; if(w%2 == 0) black = true; ArrayList<Integer> ans = new ArrayList<Integer>(); char next = str.charAt(0); for(int i = 0; i < n-1; i++) { char ch = next; if((ch == 'B' && black) || (ch == 'W' && !black)) { next = str.charAt(i+1); } else { ans.add(i+1); char chh = str.charAt(i+1); if(chh == 'B') next = 'W'; else next = 'B'; } } System.out.println(ans.size()); for(int i:ans) System.out.print(i+" "); System.out.println(""); } }
JAVA
1271_B. Blocks
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).
2
8
amount = int(input()) line = list(input()) if line.count('W') % 2 == 1 and line.count('B') % 2 == 1: print(-1) elif line.count('B') % 2 == 0: ans = [] for i in range(amount - 1): if line[i] == 'B' and line[i + 1] == 'B': ans.append(i + 1) line[i] = 'W' line[i + 1] = 'W' elif line[i] == 'B' and line[i + 1] == 'W': ans.append(i + 1) line[i] = 'W' line[i + 1] = 'B' print(len(ans)) print(*ans) else: ans = [] for i in range(amount - 1): if line[i] == 'W' and line[i + 1] == 'W': ans.append(i + 1) line[i] = 'B' line[i + 1] = 'B' elif line[i] == 'W' and line[i + 1] == 'B': ans.append(i + 1) line[i], line[i + 1] = line[i + 1], line[i] print(len(ans)) print(*ans)
PYTHON3
1271_B. Blocks
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).
2
8
n=int(input()) s=input() d = {'B':[], 'W':[]} for i, e in enumerate(s): d[e].append(i) lb, lw = len(d['B']),len(d['W']) b,c=lb%2!=0,lw%2!=0 if b and c: print(-1) elif (lb and not lw) or (lw and not lb): ## all same print(0) else: #print(d) if not b: B=d['B'] pos = [] i = 0 while i <lb: for y in range(B[i], B[i+1]): pos.append(y+1) i+=2 else: W = d['W'] pos = [] i = 0 while i < lw: for y in range(W[i], W[i+1]): pos.append(y+1) i += 2 print(len(pos)) print(*pos)
PYTHON3
1271_B. Blocks
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).
2
8
def countWhite(s): i=0 for elem in s: if elem == "W": i+=1 return i def countBlack(s): i=0 for elem in s: if elem == "B": i+=1 return i def stringToList(s): list=[] for elem in s: list.append(elem) return list def listToString(l): string="" for elem in l: string += "%s " %(elem) return string def swap(char): if char == "W": char = "B" elif char == "B": char = "W" return char def turnBlack(l): global k list = [] for i in range(0, len(l)): if l[i] == "W": k+=1 list.append(i+1) l[i] = "B" l[i+1] = swap(l[i+1]) return list def turnWhite(l): global k list = [] for i in range(0, len(l)): if l[i] == "B": k+=1 list.append(i+1) l[i] = "W" l[i+1] = swap(l[i+1]) return list n = int(input()) BlackWhite = input() useThis = stringToList(BlackWhite) nB = countBlack(BlackWhite) nW = countWhite(BlackWhite) k = 0 if nB ==0 or nW == 0: print(k) elif nB % 2 !=0 and nW % 2 != 0: print(-1) elif nB % 2 == 0 and nW % 2 == 0: x = turnBlack(useThis) print(k) print(listToString(x)) elif nB % 2 == 0 and nW % 2 !=0: y = turnWhite(useThis) print(k) print(listToString(y)) elif nB % 2 != 0 and nW % 2 == 0: z = turnBlack(useThis) print(k) print(listToString(z))
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const int maxx = 210; char s[maxx]; int ans[maxx]; int main() { int n; scanf("%d", &n); scanf("%s", s + 1); int t = 0; for (int i = 1; i < n; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; ans[++t] = i; } } if (s[n] == 'B') { printf("%d\n", t); for (int i = 1; i <= t; i++) printf("%d ", ans[i]); printf("\n"); } else { if (n % 2) { for (int i = 1; i < n; i += 2) ans[++t] = i; printf("%d\n", t); for (int i = 1; i <= t; i++) printf("%d ", ans[i]); printf("\n"); } else printf("-1\n"); } return 0; }
CPP
1271_B. Blocks
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).
2
8
n=int(input()) s=input() a=list(s) if (a.count("B")%2)*(a.count("W")%2):print(-1);exit() b=[] if a.count("B")%2:m="W" else:m="B" for i in range(n-1): if a[i]==m: b.append(i+1) if a[i]=="W":a[i]="B" else:a[i]="W" if a[i+1]=="W":a[i+1]="B" else:a[i+1]="W" print(len(b)) if len(b):print(*b)
PYTHON3
1271_B. Blocks
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).
2
8
from sys import stdin # noinspection PyShadowingBuiltins input = lambda: stdin.readline().rstrip("\r\n") input_a = lambda fun: map(fun, stdin.readline().split()) def read(): n = int(input()) s = input() return n, s def solve(n, s: str): s = [True if x == 'B' else False for x in s] b, w = s.count(True), s.count(False) if b % 2 != 0 and w % 2 != 0: return None letter = True if b % 2 == 0 else False flips = [] for i in range(n - 1): if s[i] == letter: if s[i] != s[i + 1]: s[i], s[i + 1] = s[i + 1], s[i] else: s[i] = s[i + 1] = not letter flips.append(i + 1) return flips result = solve(*read()) if result is None: print(-1) else: print(len(result)) print(*result)
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.*; import java.io.*; public class Main { public static void main(final String[] args) { final Scanner scanner = new Scanner(System.in); final int n = scanner.nextInt(); final String input = scanner.next(); final int[] array1 = new int[n]; final int[] array2 = new int[n]; for (int i = 0; i < n; i++) { if (input.charAt(i) == 'W') { array1[i] = 1; array2[i] = 1; } } ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { if (array1[i] == 1) { array1[i] ^= 1; array1[i+1] ^= 1; list.add(i+1); } } if (array1[n-1] == 0) { System.out.println(list.size()); for (int i : list) { System.out.print(i + " "); } } else { list = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { if (array2[i] == 0) { array2[i] ^= 1; array2[i+1] ^= 1; list.add(i+1); } } if (array2[n-1] == 1) { System.out.println(list.size()); for (int i : list) { System.out.print(i + " "); } } else { System.out.println(-1); } } } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> bool notsame(char* c, int len) { bool a = false; for (int i = 0; i < len - 1; ++i) { if (c[i] != c[i + 1]) a = true; } return a; } bool notpresent(int* nums, int ele, int idx) { bool a = true; for (int i = 0; i < idx; i++) { if (nums[i] == ele) a = false; } return a; } int main() { int len; scanf("%d", &len); char* c = (char*)malloc(sizeof(char) * (len + 1)); int* nums = (int*)malloc(sizeof(int) * 3 * len); int k = 0; scanf("%s", c); int nob = 0; int now = 0; for (int i = 0; i < len; i++) { if (c[i] == 'B') nob++; if (c[i] == 'W') now++; } int num = 0; if (now % 2 == 1 && nob % 2 == 1) { printf("-1"); return 0; } else { for (int i = 0; i < len - 2; i++) { if (c[i] != c[i + 1]) { if (c[i + 1] == 'B') c[i + 1] = 'W'; else c[i + 1] = 'B'; if (c[i + 2] == 'B') c[i + 2] = 'W'; else c[i + 2] = 'B'; num++; nums[k++] = i + 1; } } for (int i = 0; i < len - 1; i++) { if (!notsame(c, len)) { break; } if (c[i] == c[i + 1]) { num++; if (c[i] == 'B') { c[i] = 'W'; c[i + 1] = 'W'; } else if (c[i] == 'W') { c[i] = 'B'; c[i + 1] = 'B'; } nums[k++] = i; } } printf("%d\n", num); for (int i = 0; i < k; i++) printf("%d ", nums[i] + 1); return 0; } }
CPP
1271_B. Blocks
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).
2
8
import java.util.*; public class codeforce { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); char []a=scan.next().toCharArray(); int i; int w=0; int b=0; for(i=0;i<n;i++) { if(a[i]=='W') w++; else if(a[i]=='B') b++; } ArrayList<Integer>l=new ArrayList<Integer>(); if(w%2!=0&&b%2!=0) { System.out.println("-1"); } else { if(b%2!=0&&w%2==0) { for(i=0;i+1<n;i++) { if(a[i]=='W') { l.add(i+1); a[i]='B'; if(a[i+1]=='B') a[i+1]='W'; else a[i+1]='B'; } } } else if(b%2==0&&w%2!=0) { for(i=0;i+1<n;i++) { if(a[i]=='B') { l.add(i+1); a[i]='W'; if(a[i+1]=='B') a[i+1]='W'; else a[i+1]='B'; } } } else { for(i=0;i<n;i++) { if(a[i]=='B') { l.add(i+1); a[i]='W'; if(a[i+1]=='B') a[i+1]='W'; else a[i+1]='B'; } } } System.out.println(l.size()); for(int data:l) System.out.print(data+" "); } } }
JAVA
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) t = s.copy() ans1 = [] ans2 = [] for i in range(n-1): if s[i] == 'W': if s[i + 1] == 'B': s[i + 1] = 'W' else: s[i + 1] = 'B' ans1.append(i + 1) if t[i] == 'B': if t[i + 1] == 'B': t[i + 1] = 'W' else: t[i + 1] = 'B' ans2.append(i + 1) if s[-1] == 'B': print(len(ans1)) print(' '.join(map(str,ans1))) elif t[-1] == 'W': print(len(ans2)) print(' '.join(map(str,ans2))) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) l = list(input()) f = 0 for i in range(n): if l[i]=='B': l[i] = 0 else: l[i] = 1 l1 = l[:] if f == 0: ans = [] for i in range(n-1): if l[i]==1: l[i] = abs(1-l[i]) l[i+1] = abs(1-l[i+1]) ans.append(i+1) if len(set(l))==1: f = 1 l = l1[:] if f==0: ans = [] for i in range(n-1): if l[i]==0: l[i] = abs(1-l[i]) l[i+1] = abs(1-l[i+1]) ans.append(i+1) if len(set(l))==1: f = 1 if f==1: print(len(ans)) print(*ans) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import math def main(): n = int(input()) s = str(input()) b = 0 w = 0 p = [] for i in range(n): if s[i] == 'B': p.append(1) b += 1 else: p.append(0) w += 1 steps = -1 op = [] if b % 2 == 0: steps = 0 for i in range(n - 1): if p[i] == 1: steps += 1 op.append(i + 1) p[i] = 1 - p[i] p[i + 1] = 1 - p[i + 1] elif w % 2 == 0: steps = 0 for i in range(n - 1): if p[i] == 0: steps += 1 op.append(i + 1) p[i] = 1 - p[i] p[i + 1] = 1 - p[i + 1] print(steps) if steps >= 0: print(' '.join([str(x) for x in op])) if __name__ == '__main__': main()
PYTHON3
1271_B. Blocks
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).
2
8
def func(ss,l,c): if c=="W":c1="B" else:c1="W" for i in range(n-1): if ss[i]==c:ss[i]=c1;ss[i+1]=c if ss[i+1]==c1 else c1;l.append(i+1) if len(set(ss))==1:return l else:return [] n,s=int(input()),list(input()) ans,ans1=func(list(s),[],"B"),func(list(s),[],"W") if len(set(s))==1:print(0) elif not ans and not ans1:print(-1) else:print(len(ans1) if ans1!=[]else len(ans),"\n",*ans1 if ans1!=[]else ans)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) def solve(n,w): b = w.copy() sw, sb = [], [] for i in range(n-1): if w[i]=='B': sw.append(i+1) w[i+1] = 'W' if(w[i+1]=='B') else 'B' if b[i]=='W': sb.append(i+1) b[i+1] = 'W' if(b[i+1]=='B') else 'B' if(len(sw) < 3*n and w[-1]=='W'): return sw if(len(sb) < 3*n and b[-1]=='B'): return sb return None ans = solve(n,s) if(ans is None): print(-1) else: print(len(ans)) print(' '.join([str(i) for i in ans]))
PYTHON3
1271_B. Blocks
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).
2
8
import time def st(it): return [str(ita) for ita in it] LOCAL = False start_time = None if __name__ == '__main__': if LOCAL: start_time = time.time() # n = int(input()) # a = list(map(lambda x: int(x), input().split(' '))) n = int(input()) a = list(map(lambda x: 1 if x == 'B' else 0, input())) sum_a = sum(a) blacks = sum_a whites = n - blacks if blacks % 2 == 1 and whites % 2 == 1: print(-1) else: to_change = 1 if blacks % 2 == 0 else 0 chg = [] for i in range(n - 1): if a[i] == to_change: a[i + 1] = (a[i + 1] + 1) % 2 a[i] = (a[i] + 1) % 2 chg.append(i+1) print(len(chg)) print(' '.join(st(chg))) if LOCAL: print("--- %s seconds ---" % (time.time() - start_time))
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; long long t, n; string s; long long max(long long a, long long b, long long c) { return max(a, max(b, c)); } long long min(long long a, long long b, long long c) { return min(a, min(b, c)); } long long fspow(long long x, long long y) { long long ans; if (y == 0) return 1; ans = fspow(x, y / 2); if (y % 2 == 0) return ans * ans; return x * ans * ans; } long long gcd(long long x, long long y) { if (x < y) swap(x, y); if (y == 0) return x; return gcd(x % y, y); } long long p2(long long x) { long long ans = 0; while (x >= 1) { ans++; x /= 2; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); t = 1; while (t--) { cin >> n; cin >> s; n = s.size(); long long nb = 0, nw = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'B') nb++; else nw++; } if (nb == 0 || nw == 0) { cout << 0 << endl; continue; } if (nw & 1 && nb & 1) { cout << -1 << endl; continue; } vector<long long> ps; char c; if (nb % 2 == 0) c = 'B'; else c = 'W'; long long k = 0; for (long long i = 0; i < n; i++) { if (s[i] == c) ps.push_back(i + 1); } for (long long i = 1; i < ps.size(); i += 2) k += ps[i] - ps[i - 1]; cout << k << endl; for (long long i = 0; i < ps.size(); i += 2) { for (long long j = ps[i]; j < ps[i + 1]; j++) { cout << j << " "; } } cout << endl; } }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int n; cin >> n; string s; cin >> s; int ara[n]; for (int i = 0; i < n; i++) { if (s[i] == 'W') ara[i] = 1; else ara[i] = 0; } for (int i = 1; (i + 1) < n; i++) { if (ara[i] != ara[i - 1]) { ara[i] ^= 1; ara[i + 1] ^= 1; v.push_back(i + 1); } } if (ara[n - 1] == ara[n - 2]) { cout << v.size() << endl; for (auto x : v) { cout << x << " "; } return 0; } for (int i = 0; i < n; i++) { if (s[i] == 'W') ara[i] = 1; else ara[i] = 0; } v.clear(); v.push_back(1); ara[0] ^= 1; ara[1] ^= 1; for (int i = 1; (i + 1) < n; i++) { if (ara[i] != ara[i - 1]) { ara[i] ^= 1; ara[i + 1] ^= 1; v.push_back(i + 1); } } if (ara[n - 1] == ara[n - 2]) { cout << v.size() << endl; for (auto x : v) { cout << x << " "; } return 0; } cout << -1 << endl; }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const long long MAX_N = 2e5 + 5, inf = 1e18, mod = (int)1e9 + 7; const double PI = 3.1415926536; int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool is_prime(long long n) { for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } inline long long getPow(long long a, long long b) { long long res = 1ll, tp = a; while (b) { if (b & 1ll) { res *= tp; } tp *= tp; tp %= mod; res %= mod; b >>= 1ll; } return res; } long long vec_mult(const pair<long long, long long> &t1, const pair<long long, long long> &t2, const pair<long long, long long> &t3) { const long long &x1 = t1.first, y1 = t1.second; const long long &x2 = t2.first, y2 = t2.second; const long long &x3 = t3.first, y3 = t3.second; return ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); } void ok() { cout << "YES" << endl; } void no() { cout << "NO" << endl; } inline long long nxt() { long long x; cin >> x; return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n = nxt(); string s; cin >> s; if (n == 1) { cout << 0; return 0; } vector<long long> t1; vector<long long> t2; for (int i = 0; i < n; i++) { if (s[i] == 'W') t1.push_back(i); else t2.push_back(i); } if (t1.size() % 2 && t2.size() % 2) { cout << -1; return 0; } long long f = 1; if (t2.size() % 2) f = 0; auto get = [&](char c) { if (c == 'W') return 'B'; else return 'W'; }; vector<long long> ans; if (f == 0) { for (int i = 0; i < t1.size(); i += 2) { long long ind1 = t1[i]; long long ind2 = t1[i + 1]; for (int j = ind1; j < ind2; j++) { ans.push_back(j); s[j] = get(s[j]); s[j + 1] = get(s[j + 1]); } } } else { for (int i = 0; i < t2.size(); i += 2) { long long ind1 = t2[i]; long long ind2 = t2[i + 1]; for (int j = ind1; j < ind2; j++) { ans.push_back(j); s[j] = get(s[j]); s[j + 1] = get(s[j + 1]); } } } cout << ans.size() << endl; for (auto x : ans) cout << x + 1 << ' '; return 0; }
CPP
1271_B. Blocks
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).
2
8
import math elem = int(input()) st = list(input()) st2 = st.copy() ch = 0 ar = [] for x in range(len(st)): if st[x] == 'B': continue if x == len(st)-1: ch = 1 break st[x] = 'W' if st[x+1]=='W': st[x+1]='B' else: st[x+1]='W' ar.append(x+1) if ch == 0: print(len(ar)) if len(ar)!=0: print(*ar) else: ch = 0 ar = [] for x in range(len(st)): if st2[x] == 'W': continue if x == len(st2)-1: ch = 1 break st2[x] = 'B' if st2[x+1]=='W': st2[x+1]='B' else: st2[x+1]='W' ar.append(x+1) if ch == 0: print(len(ar)) if len(ar)!=0: print(*ar) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); String s = sc.next(); char a[] = s.toCharArray(); int m[] = new int[n]; for (int i = 0; i < a.length; i++) { if (a[i] == 'W') { m[i] = 0; } else { m[i] = 1; } } Vector<Integer> ans = new Vector<Integer>(); for (int j = 1; j < n - 1; j++) { if (m[j] != m[j - 1]) { ans.add(j); m[j] = 1 - m[j]; m[j + 1] = 1 - m[j + 1]; } } if (m[n - 2] != m[n - 1]) { if (n % 2 == 0) { System.out.println(-1); break; } for (int i = 0; i < n - 1; i += 2) { ans.add(i); } } System.out.println(ans.size()); for (Integer c : ans) { System.out.print((c + 1) + " "); } System.out.println(); } } }
JAVA
1271_B. Blocks
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).
2
8
# -*- coding: utf-8 -*- import sys from collections import Counter def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 N = INT() S = list(input()) X = Y = '' C = Counter(S) if C['B'] % 2 == 0: X = 'B' Y = 'W' elif C['W'] % 2 == 0: X = 'W' Y = 'B' else: print(-1) exit() ans = [] for i in range(N-1): if S[i] == S[i+1] == X: S[i] = S[i+1] = Y ans.append(i+1) elif S[i] == X and S[i+1] == Y: S[i], S[i+1] = S[i+1], S[i] ans.append(i+1) print(len(ans)) if ans: print(*ans)
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) A = [1 if x=='W' else 0 for x in list(input())] R = [] if n==2: if A[1]==A[0]: print(0) else: print(-1) else: # print(A) B= [not x for x in A] if A[0] == 0: A[0] = 1 A[1] = not A[1] R.append(1) for i in range(1,n): if i < n-1 and A[i] == 0: A[i] = 1 A[i+1] = not A[i+1] R.append(i) R.append(i+1) R.append(i) # print(A) # print(A) if A[-1]==1 or (A[-1]==0 and (len(A)-1)%2==0): print(round(len(R) + (((len(A)-1)/2) if A[-1]==0 and (len(A)-1)%2==0 else 0))) for i in R: print(i, end=' ') if A[-1]==0 and (len(A)-1)%2==0: for i in range(0,len(A)-1,2): print(i+1,end=' ') print() else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const long long N = (long long)2e5 + 5; vector<long long> ans; string s; void f(long long x, long long y) { if (s[x] == 'B') { s[x] = 'W'; } else { s[x] = 'B'; } if (s[y] == 'B') { s[y] = 'W'; } else { s[y] = 'B'; } ans.push_back(x); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long l; cin >> l; cin >> s; for (long long i = 0; i < l - 1; i++) { if (s[i] == s[i + 1]) { continue; } if (i + 1 < l && i + 2 < l) { f(i + 1, i + 2); } } for (long long i = l - 1; i > 0; i--) { if (s[i] == s[i - 1]) { continue; } if (i - 1 >= 0 && i - 2 >= 0) { f(i - 2, i - 1); } } long long b = 0, w = 0; for (long long i = 0; i < l; i++) { b += (s[i] == 'B'); w += (s[i] == 'W'); } if (b == 0 || w == 0) { cout << ans.size() << endl; for (auto x : ans) { cout << x + 1 << " "; } cout << endl; return 0; } cout << -1 << endl; return 0; }
CPP
1271_B. Blocks
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).
2
8
n=int(input()) s=input() a=[] b=[] for i in range(n): a.append(s[i]) b.append(s[i]) c=[] for i in range(n-1): if(a[i]=="W"): continue else: a[i]="W" c.append(i+1) if(a[i+1]=="W"): a[i+1]="B" else: a[i+1]="W" if(a.count("W")==n): print(len(c)) print(*c) else: d=[] for i in range(n-1): if(b[i]=="B"): continue else: b[i]="B" d.append(i+1) if(b[i+1]=="W"): b[i+1]="B" else: b[i+1]="W" if(b.count("B")==n): print(len(d)) print(*d) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
input() ans=[] s=input() a=[] for x in s: if x=='B': a.append(0) else: a.append(1) import copy tmp=copy.deepcopy(a) for i in range(1,len(s)): if a[i-1]==0: ans.append(i) a[i-1]=1 a[i]^=1 if a[i]==1: print(len(ans)) print(' '.join([str(x) for x in ans])) else: a=tmp ans=[] for i in range(1,len(s)): if a[i-1]==1: ans.append(i) a[i-1]=0 a[i]^=1 if a[i]==0: print(len(ans)) print(' '.join([str(x) for x in ans])) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
a=int(input()) b=input() if(b.count("B")%2==1 and b.count("W")%2==1): print(-1) else: b=list(b) if(b.count("B")%2==1): for i in range(len(b)): if(b[i]=="B"): b[i]="W" else: b[i]="B" b=list(b) # print(b) ans=[] for i in range(len(b)-1): # print(b) if(b[i]=="B" and b[i+1]=="B"): ans.append(str(i+1)) b[i]="W" b[i+1]="W" for i in range(len(b)-1): # print(b) if(b[i]=="B" and b[i+1]=="B"): ans.append(str(i+1)) b[i]="W" b[i+1]="W" elif(b[i]=="B" and b[i+1]=="W"): ans.append(str(i+1)) b[i]="W" b[i+1]="B" print(len(ans)) print(" ".join(ans))
PYTHON3
1271_B. Blocks
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).
2
8
def find(): if (l[n-1]=='B'): for i in range(0,n-2,2): m.append(i+1) elif (l[0]=='B'): for i in range(n-2,0,-2): m.append(i+1) print(len(m)) for j in range(len(m)): print(m[j],end=" ") n=int(input()) s=str(input()) l=list(s) if ('B' not in s): print("0") else: m=[] for i in range(0,len(s)-1): if l[i]!='W': l[i]='W' m.append(i+1) if l[i+1]=='W': l[i+1]='B' else: l[i+1]='W' if 'B' in l: if (l.count('B')==1) and (l.count('W')%2==0) and (l[0]=='B' or l[len(l)-1]=='B'): find() else: print("-1") else: print(len(m)) for j in range(len(m)): print(m[j],end=" ")
PYTHON3
1271_B. Blocks
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).
2
8
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk=new StringTokenizer(br.readLine()); int n=Integer.parseInt(tk.nextToken()); String s=br.readLine(); int a[]=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++) { if(s.charAt(i)=='W') a[i]=0; else a[i]=1; if(s.charAt(i)=='W') b[i]=0; else b[i]=1; } String s1="";int c1=0,c2=0; for(int i=0;i<n-1;i++) { if(a[i]==0) continue; else { s1=s1+(i+1)+" ";c1++; a[i]=a[i]^1; a[i+1]=a[i+1]^1; } } if(a[n-2]==a[n-1]) { System.out.println(c1); System.out.println(s1);return; } s1=""; for(int i=0;i<n-1;i++) { if(b[i]==1) continue; else { s1=s1+(i+1)+" ";c2++; b[i]=b[i]^1; b[i+1]=b[i+1]^1; } } if(b[n-2]==b[n-1]) { System.out.println(c2); System.out.println(s1); } else System.out.println("-1"); } }
JAVA
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[n]; for (int i = 0; i < n; i++) { if (s[i] == 'B') a[i] = 1; else a[i] = 0; } vector<int> v; for (int i = 1; i + 1 < n; i++) { if (a[i] != a[i - 1]) { a[i] = !a[i]; a[i + 1] = !a[i + 1]; v.push_back(i); } } if (a[n - 1] == a[0]) { cout << v.size() << endl; for (auto i = v.begin(); i != v.end(); i++) cout << *i + 1 << " "; cout << endl; } else { if (n % 2 == 0) cout << "-1" << endl; else { for (int i = 0; i < n - 2; i += 2) v.push_back(i); cout << v.size() << endl; for (auto i = v.begin(); i != v.end(); i++) cout << *i + 1 << " "; cout << endl; } } }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> long long int gcdfun(long long int x, long long int y) { if (y == 0) return x; else return gcdfun(y, x % y); } using namespace std; int power(int x, int y) { if (y == 0) return 1; else if (y % 2 == 0) return ((power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007)) % 1000000007; else return ((x % 1000000007) * (power(x, y / 2) % 1000000007) * (power(x, y / 2) % 1000000007)) % 1000000007; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string a; cin >> a; int b = 0, w = 0; for (int i = 0; i < a.size(); i++) { if (a[i] == 'B') b++; else w++; } if (b == 0 || w == 0) cout << 0; else if (b % 2 != 0 && w % 2 != 0) cout << -1; else { vector<int> ans; int flag = 0; if (b % 2 == 0) { while (1) { flag = 0; for (int i = 0; i < a.size() - 1; i++) { if (a[i] == 'B') { if (a[i] == 'W') a[i] = 'B'; else a[i] = 'W'; if (a[i + 1] == 'B') a[i + 1] = 'W'; else a[i + 1] = 'B'; flag = 1; ans.push_back(i + 1); } } if (flag == 0) break; } } else { while (1) { flag = 0; for (int i = 0; i < a.size() - 1; i++) { if (a[i] == 'W') { if (a[i] == 'W') a[i] = 'B'; else a[i] = 'W'; if (a[i + 1] == 'B') a[i + 1] = 'W'; else a[i + 1] = 'B'; flag = 1; ans.push_back(i + 1); } } if (flag == 0) break; } } cout << ans.size() << '\n'; for (auto i : ans) cout << i << " "; } }
CPP
1271_B. Blocks
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).
2
8
n = input() s = list(raw_input()) tmps = list(s) ans = [] for i in range(n-1): if tmps[i] == 'W': ans.append(i) tmps[i] = 'B' if tmps[i+1] == 'W': tmps[i+1] = 'B' else: tmps[i+1] = 'W' if tmps.count('W') == 0: print len(ans) for i in ans: print i + 1, else: tmps = list(s) ans = [] for i in range(n-1): if tmps[i] == 'B': ans.append(i) tmps[i] = 'W' if tmps[i+1] == 'W': tmps[i+1] = 'B' else: tmps[i+1] = 'W' if tmps.count('B') == 0: print len(ans) for i in ans: print i + 1, else: print -1
PYTHON
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; void solve() { long long n, w = 0, b = 0, i; cin >> n; char arr[n]; vector<long long> ans; for (i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == 'W') w++; else b++; } if (w % 2 && b % 2) { cout << "-1" << endl; return; } else if (w % 2) { for (i = 0; i < n - 1; i++) { if (arr[i] == 'B') { ans.push_back(i + 1); arr[i] = 'W'; if (arr[i + 1] == 'W') arr[i + 1] = 'B'; else arr[i + 1] = 'W'; } } } else { for (i = 0; i < n - 1; i++) { if (arr[i] == 'W') { ans.push_back(i + 1); arr[i] = 'B'; if (arr[i + 1] == 'W') arr[i + 1] = 'B'; else arr[i + 1] = 'W'; } } } cout << ans.size() << endl; for (i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long t = 1; while (t--) { solve(); } return 0; }
CPP
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e2 + 20; int n; vector<int> ans; char s[MAXN], s1[MAXN]; char convert(char a) { if (a == 'B') return 'W'; else return 'B'; } int main() { cin >> n; scanf("%s", s); for (int i = 0; i < n; ++i) { s1[i] = s[i]; } for (int i = 0; i < n - 1; ++i) { if (s[i] == 'W') { s[i] = convert(s[i]), s[i + 1] = convert(s[i + 1]); ans.push_back(i + 1); } } if (s[n - 1] == 'B') { cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } puts(""); return 0; } ans.clear(); for (int i = 0; i < n - 1; ++i) { if (s1[i] == 'B') { s1[i] = convert(s1[i]), s1[i + 1] = convert(s1[i + 1]); ans.push_back(i + 1); } } if (s1[n - 1] == 'W') { cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } puts(""); return 0; } else { puts("-1"); } }
CPP
1271_B. Blocks
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).
2
8
import sys from math import ceil input = sys.stdin.readline n = int(input()) s = [True if x == 'W' else False for x in input().strip()] r = s[:] store = [] for i in range(n-1): if s[i]: continue else: store.append(i+1) s[i] = not s[i] s[i+1] = not s[i+1] if s[-1]: print(len(store)) print(*store) else: store = [] for i in range(n-1): if not r[i]: continue else: store.append(i+1) r[i] = not r[i] r[i+1] = not r[i+1] if not r[-1]: print(len(store)) print(*store) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() { int n; cin >> n; string str; cin >> str; vector<int> pos; for (int k = (1); k <= (2 * n); ++k) { for (int i = 0; i < (n - 1); ++i) { if (k == 1) { if (str[i] == 'W') continue; str[i] = 'W'; pos.push_back(i + 1); if (str[i + 1] == 'B') str[i + 1] = 'W'; else str[i + 1] = 'B'; } else { if (str[i] == 'B') continue; str[i] = 'B'; pos.push_back(i + 1); if (str[i + 1] == 'B') str[i + 1] = 'W'; else str[i + 1] = 'B'; } } if (str[n - 1] == 'B' and k == 1) { continue; } if (str[n - 1] == 'W' and k == 1) { cout << pos.size() << '\n'; for (auto x : pos) cout << x << " "; return; } if (str[n - 1] == 'B' and k == 2) { cout << pos.size() << '\n'; for (auto x : pos) cout << x << " "; return; } } cout << -1 << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
CPP
1271_B. Blocks
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).
2
8
//package codeforces; import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Stack; import java.util.Set; public class q { static Scanner scn = new Scanner(System.in); static int mod=1000000007 ; static int count=0; public static void main(String[] args) { // TODO Auto-generated method stub int n=scn.nextInt(); String str=scn.next(); char ch[]=str.toCharArray(); int w=0,b=0,count=0,no=0; ArrayList<Integer>sb=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(str.charAt(i)=='B') b++; else w++; } if(w%2!=0&&b%2!=0) { System.out.println("-1"); return ; } if(w%2==0 && b%2!=0) { for(int i=0;i<n;i++) { if(ch[i]=='W') { while(i+1<n && ch[i+1]=='B') {count++; sb.add(i+1); i++; } count++; sb.add(i+1); if(i+1<n) ch[i+1]='B'; } } } else if(b%2==0&&w%2!=0) { for(int i=0;i<n;i++) { if(ch[i]=='B') { while(i+1<n && ch[i+1]=='W') {count++; sb.add(i+1); i++; } count++; sb.add(i+1); if(i+1<n) ch[i+1]='W'; } } } else { for(int i=0;i<n;i++) { if(ch[i]=='W') { while(i+1<n && ch[i+1]=='B') {count++; sb.add(i+1); i++; } count++; sb.add(i+1); if(i+1<n) ch[i+1]='B'; } } } System.out.println(count); for(int i=0;i<sb.size();i++) { System.out.print(sb.get(i)+" "); } System.out.println(); } }
JAVA
1271_B. Blocks
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).
2
8
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; /* * */ public class _608_B implements Runnable{ public static void main(String[] args) { new Thread(null, new _608_B(),"Main",1<<27).start(); } @Override public void run() { FastReader fd = new FastReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = fd.nextInt(); char[] data = fd.next().toCharArray(); int wCount = 0,bCount = 0; for(int i = 0; i < n; i++){ if(data[i] == 'W') wCount++; else bCount++; } if(wCount % 2 == 1 && bCount % 2 == 1){System.out.println("-1");return;} int opCount = 0; StringBuilder ans = new StringBuilder(); char process = (wCount % 2 == 0)?'W':'B'; boolean nextInverted = false; for(int i = 0; i < n; i++){ if((data[i] == process && !nextInverted) || (data[i] != process && nextInverted)){ nextInverted = true; opCount++; ans.append(i+1).append(" "); } else{ nextInverted = false; } } out.println(opCount+"\n"+ans); out.close(); } //Helper functions static void getArray(int[] data, boolean isSorted, FastReader fd){ for(int i = 0; i < data.length; i++){ data[i] = fd.nextInt(); } if(isSorted) Arrays.sort(data); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static boolean checkDistinct(int next){// check all digits are distinct. String process = String.valueOf(next); for(int i = 0;i < process.length()-1; i++){ String character = String.valueOf(process.charAt(i)); if(process.substring(i+1).contains(character)){ return false; } } return true; } static int limit = (int) 1e7 + 1; static int[] facts = new int[limit]; static void sieve() { // Store the minimum prime factors of 1 to LIMIT facts[1] = 1; for (int i = 2; i < limit; i++) { if (i % 2 == 0 && i > 2) { facts[i] = 2; } else { facts[i] = i; } } for (int i = 3; i * i < limit; i++) { if (facts[i] == i) { for (int j = i * i; j < limit; j += i) if (facts[j] == j) facts[j] = i; } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1271_B. Blocks
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).
2
8
def getInv(x): if x == 'W': return 'B' return 'W' n = int(input()) sir = list(input()) inv = [] for x in sir: inv.append(getInv(x)) # le fac toate B ans = [] for i in range(0, len(sir) - 1): if sir[i] == 'W': # fac mutare pe i ans.append(i + 1) sir[i] = getInv(sir[i]) sir[i + 1] = getInv(sir[i + 1]) if sir[len(sir) - 1] == 'B': print(len(ans)) for x in ans: print(x, end = " ") else: # le fac toate W ans = [] for i in range(0, len(inv) - 1): if inv[i] == 'W': # fac mutare pe i ans.append(i + 1) inv[i] = getInv(inv[i]) inv[i + 1] = getInv(inv[i + 1]) if inv[len(inv) - 1] == 'B': print(len(ans)) for x in ans: print(x, end = " ") else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
# import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') n = int(input()) s = input() operation = [0]*(3*n) count = 0 arr = [0]*n for i in range(n): if (s[i] == "W"): arr[i] = 1 else: arr[i] = 0 # ODD LENGTH CASE if (n&1==1): x = arr[0] for i in range(1,n-1): if (arr[i] == x): continue operation[count] = i+1 count+=1 arr[i] = arr[i]^1 arr[i+1] = arr[i+1]^1 if (arr[n-1] == arr[0]): print(count) print(*operation[:count]) else: temp = n-3 while (temp >= 0): operation[count] = temp+1 count+=1 temp -= 2 print(count) print(*operation[:count]) # EVEN LENGTH CASE else: x = arr[0] for i in range(1,n-1): if (arr[i]==x): continue operation[count] = i+1 count+=1 arr[i] = arr[i]^1 arr[i+1] = arr[i+1]^1 if (arr[n-1] == arr[0]): print(count) print(*operation[:count]) else: print(-1)
PYTHON3
1271_B. Blocks
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).
2
8
// Main Code at the Bottom import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; public class Main { //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; //Euclidean Algorithm static long gcd(long A,long B){ return (B==0)?A:gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //Modular Inverse static long inverse(long x) { return fastExpo(x,MOD-2); } //Prime Number Algorithm static boolean isPrime(long 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+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static void reverse(char arr[],int l,int r){ while(l<r) { char tmp=arr[l]; arr[l++]=arr[r]; arr[r--]=tmp; } } //Print array static void print1d(int arr[]) { out.println(Arrays.toString(arr)); } static void print2d(int arr[][]) { for(int a[]: arr) out.println(Arrays.toString(a)); } // Pair static class pair{ int x,y; pair(int a,int b){ this.x=a; this.y=b; } public boolean equals(Object obj) { if(obj == null || obj.getClass()!= this.getClass()) return false; pair p = (pair) obj; return (this.x==p.x && this.y==p.y); } public int hashCode() { return Objects.hash(x,y); } } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Main function(The main code starts from here public static void main (String[] args) throws java.lang.Exception { int test=1; //test=sc.nextInt(); while(test-->0){ int n=sc.nextInt(); char s[]=sc.nextLine().toCharArray(); int a[]=new int[n]; int x=0,y=0; for(int i=0;i<n;i++ ) { if(s[i]=='W') { a[i]=0; x++; } else { a[i]=1; y++; } } if(x%2==1 && y%2==1) { out.println(-1); continue; } ArrayList<Integer> ans=new ArrayList<>(); for(int i=1;i<n-1;i++) { if(a[i]!=a[i-1]) { ans.add(i+1); a[i]^=1; a[i+1]^=1; } } for(int i=n-2;i>0;i--) { if(a[i]!=a[i+1]) { ans.add(i); a[i]^=1; a[i-1]^=1; } } out.println(ans.size()); for(int i: ans) out.print(i+" "); } out.flush(); out.close(); } }
JAVA
1271_B. Blocks
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).
2
8
n=int(input()) s=list(input()) nw=0 nb=0 for i in s: if i=='W': nw+=1 else: nb+=1 num=0 ans=[] if nw%2!=0 and nb%2!=0: print(-1) else: if nw%2==0 and nb%2==0: if nw>nb: en='W' else: en='B' elif nw%2==0: en='B' else: en='W' for i in range(n-1): if s[i]!=en: num+=1 ans.append(i+1) if s[i+1]=='W': s[i+1]='B' else: s[i+1]='W' if s[i]=='W': s[i]='B' else: s[i]='W' print(num) if num!=0: print(" ".join(str(k) for k in ans))
PYTHON3
1271_B. Blocks
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).
2
8
n = int(input()) s = list(input()) b = 0 w = 0 T = [] for i in s: if i == 'B': b += 1 else: w += 1 if b % 2 == 1 and w % 2 == 1: print(-1) elif b == 0 or w == 0: print(0) else: for i in range(len(s) - 1): if s[i] == 'B' and s[i + 1] == 'W': s[i] = 'W' s[i + 1] = 'B' T.append(i + 1) elif s[i] == 'B' and s[i + 1] == 'B': s[i] = 'W' s[i + 1] = 'W' T.append(i + 1) if b % 2 == 0: i = len(s) - 1 while s[i] != 'W': s[i] = 'W' s[i - 1] = 'W' T.append(i) i -= 2 else: i = 0 while s[i] != 'B': s[i] = 'B' s[i + 1] = 'B' T.append(i + 1) i += 2 print(len(T)) print(*T)
PYTHON3
1271_B. Blocks
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).
2
8
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() n = int(ri()) s = ri() cb, cw = 0,0 for i in s: if i== 'B': cb+=1 else: cw+=1 if cb%2 == 1 and cw%2 == 1: print(-1) else: ans = [] if cb%2 == 1: prev = -1 for i in range(n): if s[i] == 'W': if prev == -1: prev = i else: for j in range(i-1,prev-1,-1): ans.append(j+1) prev = -1 else: prev = -1 for i in range(n): if s[i] == 'B': if prev == -1: prev = i else: for j in range(i-1,prev-1,-1): ans.append(j+1) prev = -1 print(len(ans)) print(*ans)
PYTHON3
1271_B. Blocks
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).
2
8
# Author : raj1307 - Raj Singh # Date : 17.12.19 from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(100000000) threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace #from math import ceil,floor,log,sqrt,factorial #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import *,threading #from itertools import permutations abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): #for _ in range(ii()): n=ii() s=list(si()) ans=[] w=s.count('W') b=s.count('B') if w%2 and b%2: # odd-even observation print(-1) exit() if w%2: for i in range(n-1): if s[i]=='B': ans.append(i+1) if s[i+1]=='W': s[i+1]='B' else: s[i+1]='W' else: for i in range(n-1): if s[i]=='W': ans.append(i+1) if s[i+1]=='W': s[i+1]='B' else: s[i+1]='W' print(len(ans)) print(*ans) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #dmain() # Comment Read()
PYTHON