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>
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double EPS = 1e-8;
const int inf = 1e9;
const long double PI = acos(-1);
int mod = (int)998244353;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const long long kekmod = 1791791791;
const long long bestmod = 1148822869;
const long long secmod = (int)1e9 + 113;
vector<long long> mods = {kekmod, bestmod, mod, MOD9, 1000000007};
vector<long long> hashpows = {29, 31, 37, 43, 47, 53, 179, 229};
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count() +
228 + 'i' + 'q' + 1337 + 1488);
signed main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
srand(time(NULL));
long long n;
cin >> n;
string s;
cin >> s;
long long c1 = count((s).begin(), (s).end(), 'W');
if (c1 == 0 || c1 == s.size()) {
cout << 0;
return 0;
}
string ts = s;
vector<long long> ans;
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
c1 = count((s).begin(), (s).end(), 'W');
if (c1 == 0 || c1 == s.size()) {
cout << ans.size() << endl;
for (long long i = 0; i < ans.size(); ++i) cout << ans[i] << ' ';
;
} else {
s = ts;
ans.clear();
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
for (long long i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = (s[i + 1] ^ 'W' ^ 'B');
ans.push_back(i + 1);
}
}
c1 = count((s).begin(), (s).end(), 'B');
if (c1 == 0 || c1 == s.size()) {
cout << ans.size() << endl;
for (long long i = 0; i < ans.size(); ++i) cout << ans[i] << ' ';
;
} else {
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 | for g in range(0,1):
n=int(input())
s=input()
b=0
w=0
indexb=[]
indexw=[]
for i in range(0,len(s)):
if(s[i]=="B"):
b=b+1
indexb.append(i+1)
else:
w=w+1
indexw.append(i+1)
ops=0
position=[]
if(b==0):
print(0)
elif(w==0):
print(0)
elif(w%2!=0 and b%2!=0):
print(-1)
elif(w%2==0 and b%2!=0):
for i in range(0,len(indexw),2):
ops+=indexw[i+1]-indexw[i]
k=0
for j in range(0,indexw[i+1]-indexw[i]):
position.append(indexw[i]+k)
k=k+1
print(ops)
for i in range(0,len(position)):
print(position[i],end=" ")
print("")
elif(b%2==0 and w%2!=0):
for i in range(0,len(indexb),2):
ops+=indexb[i+1]-indexb[i]
k=0
for j in range(0,indexb[i+1]-indexb[i]):
position.append(indexb[i]+k)
k=k+1
print(ops)
for i in range(0,len(position)):
print(position[i],end=" ")
print("")
elif(b%2==0 and w%2==0):
if(b>w):
for i in range(0,len(indexw),2):
ops+=indexw[i+1]-indexw[i]
k=0
for j in range(0,indexw[i+1]-indexw[i]):
position.append(indexw[i]+k)
k=k+1
print(ops)
for i in range(0,len(position)):
print(position[i],end=" ")
print("")
else:
for i in range(0,len(indexb),2):
ops+=indexb[i+1]-indexb[i]
k=0
for j in range(0,indexb[i+1]-indexb[i]):
position.append(indexb[i]+k)
k=k+1
print(ops)
for i in range(0,len(position)):
print(position[i],end=" ")
print("")
| 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 can(S, c):
ans = ''
nc = False
for i in range(len(S)):
if (S[i] != c and nc==False) or (S[i]==c and nc==True):
ans += str(i+1) + ' '
nc = True
else:
nc = False
if nc == True:
return False
return ans
n = int(input())
s = input()
ans = can(s, 'B')
if ans != False:
print(str(ans.count(' ')) + '\n' + ans)
exit()
ans = can(s, 'W')
if ans != False:
print(str(ans.count(' ')) + '\n' + 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 | t = int(input())
s = input()
white = []
white_count = 0
temp = list(s)
for j in range(len(s)-1):
if(temp[j]=='B'):
white_count+=1
temp[j+1]='W' if temp[j+1]=='B' else 'B'
white.append(j+1)
if(temp[-1]=='W'):
print(white_count)
if(len(white)!=0):
print(*white)
else:
black = []
black_count = 0
temp = list(s)
for j in range(len(s)-1):
if(temp[j]=='W'):
black_count+=1
temp[j+1]='B' if temp[j+1]=='W' else 'W'
black.append(j+1)
if(temp[-1]=='B'):
print(black_count)
if(len(black)!=0):
print(*black)
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;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
inline long long gcd(long long a, long long b) {
a = ((a) < 0 ? -(a) : (a));
b = ((b) < 0 ? -(b) : (b));
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
long long ext_gcd(long long A, long long B, long long* X, long long* Y) {
long long x2, y2, x1, y1, x, y, r2, r1, q, r;
x2 = 1;
y2 = 0;
x1 = 0;
y1 = 1;
for (r2 = A, r1 = B; r1 != 0;
r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y) {
q = r2 / r1;
r = r2 % r1;
x = x2 - (q * x1);
y = y2 - (q * y1);
}
*X = x2;
*Y = y2;
return r2;
}
inline long long modInv(long long a, long long m) {
long long x, y;
ext_gcd(a, m, &x, &y);
x %= m;
if (x < 0) x += m;
return x;
}
inline long long bigmod(long long a, long long p, long long m) {
long long res = 1 % m, x = a % m;
while (p) {
if (p & 1) res = (res * x) % m;
x = (x * x) % m;
p >>= 1;
}
return res;
}
const long long inf = 2147383647;
const long long mod = 1000000007;
const double pi = 2 * acos(0.0);
const double eps = 1e-11;
vector<int> V1, V2;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string str;
cin >> str;
if (str.size() == 1) {
cout << "0\n";
return 0;
}
string S1 = str, S2 = str;
for (int i = 0; i < S1.size() - 1; i++) {
if (S1[i] == 'B' && S1[i + 1] == 'B') {
S1[i] = 'W';
S1[i + 1] = 'W';
V1.push_back(i + 1);
} else if (S1[i] == 'B' && S1[i + 1] == 'W') {
S1[i] = 'W';
S1[i + 1] = 'B';
V1.push_back(i + 1);
}
}
if (S1[S1.size() - 1] == 'W') {
cout << V1.size() << "\n";
for (int i = 0; i < V1.size(); i++) {
if (i != 0) cout << " ";
cout << V1[i];
}
if (V1.size() > 0) cout << "\n";
return 0;
}
for (int i = 0; i < S2.size() - 1; i++) {
if (S2[i] == 'W' && S2[i + 1] == 'W') {
S2[i] = 'B';
S2[i + 1] = 'B';
V2.push_back(i + 1);
} else if (S2[i] == 'W' && S2[i + 1] == 'B') {
S2[i] = 'B';
S2[i + 1] = 'W';
V2.push_back(i + 1);
}
}
if (S2[S2.size() - 1] == 'B') {
cout << V2.size() << "\n";
for (int i = 0; i < V2.size(); i++) {
if (i != 0) cout << " ";
cout << V2[i];
}
if (V2.size() > 0) 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 | n = int(input().strip())
blocks = list(input().strip())
c = blocks[0]
cnt = 0
for b in blocks:
if b == c:
cnt += 1
if cnt%2 and (n-cnt)%2:
print(-1)
else:
moves = []
if (n-cnt)%2:
c = 'W' if c == 'B' else 'B'
for i in range(n-1):
if blocks[i] != c:
blocks[i] = c
blocks[i+1] = 'W' if blocks[i+1] == 'B' else 'B'
moves.append(str(i+1))
print(len(moves))
print(' '.join(moves))
| 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, i;
cin >> n;
vector<char> v1;
for (i = 0; i < n; i++) {
char in;
cin >> in;
v1.push_back(in);
}
vector<int> v2;
for (i = 0; i < n - 1; i++) {
if (v1[i] != 'W') {
v2.push_back(i + 1);
v1[i] = 'W';
if (v1[i + 1] == 'W') {
v1[i + 1] = 'B';
} else {
v1[i + 1] = 'W';
}
}
}
if (v1[n - 2] == v1[n - 1]) {
cout << v2.size() << endl;
for (i = 0; i < v2.size(); i++) {
cout << v2[i] << " ";
}
} else {
for (i = 0; i < n - 1; i++) {
if (v1[i] != 'B') {
v2.push_back(i + 1);
v1[i] = 'B';
if (v1[i + 1] == 'W') {
v1[i + 1] = 'B';
} else {
v1[i + 1] = 'W';
}
}
}
if (v1[n - 2] == v1[n - 1]) {
cout << v2.size() << endl;
for (i = 0; i < v2.size(); i++) {
cout << v2[i] << " ";
}
} else {
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 | def num():
return int(input())
def line():
return map(int, input().split())
n = num()
s = [c=='B' for c in input()]
ans=[]
for k in range(3):
q=s[0]
for i in range(1,n-1):
if s[i]!=q:
ans.append(i+1 if k%2==0 else n-i-1)
s[i]=1-s[i]
s[i+1]=1-s[i+1]
if s[-1]==q:
break
s=s[::-1]
if s[0]==s[-1]:
print(len(ans))
print(' '.join(map(str, 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 sys
def I():
return sys.stdin.readline().rstrip()
def main():
n = int(I())
s = I()
ind = []
l = [ i == 'W' for i in s ]
for i in range(n - 1):
if l[ i ]:
ind.append( i + 1 )
l[ i ] = not l[ i ]
l[ i + 1 ] = not l[ i + 1 ]
if not l[ -1 ]:
print( len( ind ) )
print( *ind )
return
ind = []
l = [ i == 'B' for i in s ]
for i in range(n - 1):
if l[ i ]:
ind.append( i + 1 )
l[ i ] = not l[ i ]
l[ i + 1 ] = not l[ i + 1 ]
if not l[ -1 ]:
print( len( ind ) )
print( *ind )
return
print( -1 )
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 | import sys
import math
import bisect
def solve1(A, ans):
n = len(A)
ans.clear()
for i in range(n - 1):
if A[i] != 'B':
A[i] = 'B'
if A[i+1] == 'W':
A[i+1] = 'B'
else:
A[i+1] = 'W'
ans.append(i)
#print('A: ' + str(A))
#print('ans: ' + str(ans))
return A[n-1] == 'B'
def solve2(A, ans):
n = len(A)
ans.clear()
for i in range(n - 1):
if A[i] != 'W':
A[i] = 'W'
if A[i+1] == 'W':
A[i+1] = 'B'
else:
A[i+1] = 'W'
ans.append(i)
#print('A: ' + str(A))
#print('ans: ' + str(ans))
return A[n-1] == 'W'
def main():
n = int(input())
A = list(input())
B = A.copy()
ans = []
if (solve1(B, ans)):
print(len(ans))
print(' '.join(list(str(a+1) for a in ans)))
else:
B = A.copy()
if solve2(B, ans):
print(len(ans))
print(' '.join(list(str(a+1) for a in ans)))
else:
print(-1)
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 |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class CF {
private static Scanner cin = new Scanner(System.in);
static final int N = 100111;
static int tc;
int n, q, c;
int a[];
boolean use[];
int b[];
String str;
public void init() {
}
int getMin(int x, int y) {
return x < y ? x : y;
}
public void input() {
n = cin.nextInt();
str = cin.next();
a = new int[n];
}
public boolean check(char c, boolean show) {
char ch[] = str.toCharArray();
int k = 0;
for (int i = n - 2; i >= 0; i--) {
if (ch[i+1] != c) {
if (ch[i] == 'W') {
ch[i] = 'B';
} else {
ch[i] = 'W';
}
a[k++] = i+1;
}
}
if (ch[0] == c) {
System.out.println(k);
for (int i = 0; i < k; i++) {
System.out.printf("%d ", a[i]);
}
System.out.println();
return true;
}
return false;
}
public void solve() {
if (!check('B', true) && !check('W', true)) {
System.out.println(-1);
}
}
public static void main(String[] args) {
CF cf = new CF();
while (cin.hasNext()) {
cf.input();
cf.solve();
// System.out.println(Arrays.toString(cf.b));
}
}
}
| 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 swap(l,n,i):
if(l[i]=='B'):
l[i]='W'
else:
l[i]='B'
return l
def check(l,n,col):
if(col=='B'):
for i in l:
if(i=='W'):
return False
return True
else:
for i in l:
if(i=='B'):
return False
return True
def solve(s,n):
cb=0
cw=0
ans=list()
for i in range(n):
if(s[i]=='W'):
cw+=1
else:
cb+=1
if(n%2==0):
if(cb%2==1):
print(-1)
return
else:
col='x'
if(cb>cw):
col='B'
else:
col='W'
while(not check(s,n,col)):
for i in range(n):
if(s[i]!=col):
s=swap(l,n,i)
s=swap(l,n,i+1)
ans.append(i+1)
else:
col='x'
if(cb%2==1):
col='B'
else:
col='W'
ans=list()
while(not check(s,n,col)):
for i in range(n):
if(s[i]!=col):
s=swap(l,n,i)
s=swap(l,n,i+1)
ans.append(i+1)
print(len(ans))
for i in range(len(ans)):
print(ans[i],end=" ")
print()
n=int(input())
s=input()
l=list()
for i in range(n):
l.append(s[i])
solve(l,n) | 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()
i=0
a=[i for i in s]
flag=1
arr1=[]
while i<n:
if a[i]=='W':
i+=1
continue
else:
if i==n-1:
flag=0
break
a[i]='W'
if a[i+1]=='B':
a[i+1]='W'
else:
a[i+1]='B'
arr1.append(i+1)
i+=1
i=0
b=[i for i in s]
f=1
arr2=[]
while i<n:
if b[i]=='B':
i+=1
continue
else:
if i==n-1:
f=0
break
b[i]='B'
if b[i+1]=='B':
b[i+1]='W'
else:
b[i+1]='B'
arr2.append(i+1)
i+=1
if flag==1:
print(len(arr1))
if len(arr1)>0:
print(*arr1)
elif f==1:
print(len(arr2))
if len(arr2)>0:
print(*arr2)
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 | inp = lambda cast=int: [cast(x) for x in input().split()]
printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs)
n, = inp()
A, = inp(str)
b = A.count('B')
w = n - b
A = list(A)
B = A[::]
if b%2 and w%2:
print(-1)
else:
res, flag = 0, False
R = []
for i in range(n-1):
if A[i] == 'B':
A[i], A[i+1] = 'W', 'W' if A[i+1] == 'B' else 'B'
res += 1
R.append(str(i+1))
if A[n-1] == 'B':
res, R = 0, []
A = B[::]
for i in range(n-1):
if A[i] == 'W':
A[i], A[i+1] = 'B', 'B' if A[i+1] == 'W' else 'W'
res += 1
R.append(str(i+1))
print(res)
print(' '.join(R))
| 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()
a = []
for i in s:
if i == "B":
a.append(0)
else:
a.append(1)
ans = []
st = a[0]
for i in range(1,len(s)-1):
if a[i]!=st:
a[i]=(a[i]+1)%2
a[i+1]=(a[i+1]+1)%2
ans.append(i+1)
st=a[i]
if a[n-1]!=a[n-2] and n%2==1:
for i in range(0,n-2,2):
a[i]=(a[i]+1)%2
a[i+1]=(a[i+1]+1)%2
ans.append(i+1)
if a.count(1)==n or a.count(0)==n:
print(len(ans))
for i in ans:
print(i,end=" ")
print()
else:
print(-1)
# print(a)
| 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() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += (s[i] == 'B');
}
vector<int> A;
if (sum % 2 == 0) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
A.push_back(i + 1);
s[i] = 'W';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else {
s[i + 1] = 'B';
}
}
}
cout << A.size() << "\n";
for (auto u : A) {
cout << u << " ";
}
cout << "\n";
} else {
if (n % 2 == 1) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
A.push_back(i + 1);
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else {
s[i + 1] = 'B';
}
}
}
cout << A.size() << "\n";
for (auto u : A) {
cout << u << " ";
}
cout << "\n";
} else {
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 | n = int(input())
seq = input()
S = [(0 if c == 'W' else 1) for c in seq]
operations = []
if seq.count('W') % 2 == 0:
# transform all W -> B
for start in range(n - 1):
if S[start] == 0:
S[start] = 1 - S[start]
S[start + 1] = 1 - S[start + 1]
operations.append(start + 1)
print(str(len(operations)) + '\n' + ' '.join(map(str, operations)))
elif seq.count('B') % 2 == 0:
# transform all B -> W
for start in range(n - 1):
if S[start] == 1:
S[start] = 1 - S[start]
S[start + 1] = 1 - S[start + 1]
operations.append(start + 1)
print(str(len(operations)) + '\n' + ' '.join(map(str, operations)))
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
n = int(input())
ss = raw_input()
s = list(ss)
w, b = 0, 0
for c in s:
if c == 'W':
w += 1
else:
b += 1
if(w&1 and b&1):
print -1
sys.exit(0)
ans = list()
if(w&1):
for i in range(len(s)):
if s[i] == 'B':
ans.append(i+1)
if i+1 < len(s):
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
else:
for i in range(len(s)):
if s[i] == 'W':
ans.append(i+1)
if i+1 < len(s):
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
print len(ans)
for val in ans:
print val, | 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;
const int MAX = 205;
char input[MAX], s[MAX];
vector<int> v;
map<char, char> mp;
int main() {
mp['W'] = 'B';
mp['B'] = 'W';
int n;
scanf("%d%s", &n, input);
strcpy(s, input);
int tot = 0;
for (int i = 0; i < n - 1; i++)
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = mp[s[i + 1]];
tot++;
v.push_back(i + 1);
}
if (s[n - 1] == 'B') {
printf("%d\n", tot);
for (int i = 0; i < v.size(); i++) printf("%d ", v[i]);
printf("\n");
return 0;
}
tot = 0;
v.clear();
strcpy(s, input);
for (int i = 0; i < n - 1; i++)
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = mp[s[i + 1]];
tot++;
v.push_back(i + 1);
}
if (s[n - 1] == 'W') {
printf("%d\n", tot);
for (int i = 0; i < v.size(); i++) printf("%d ", v[i]);
printf("\n");
return 0;
}
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 | n=int(input())
s=input()
b=s.count("B")
w=s.count("W")
f=False
count=0
answer=[]
if b%2!=0 and w%2!=0:
print(-1)
else:
if b%2!=0:
i=0
while i<(n-1):
if s[i] == "W":
f = True
if f:
count+=1
answer.append(i+1)
if s[i+1]=="W":
i+=1
f=False
i+=1
else:
i = 0
while i < (n - 1):
if s[i] == "B":
f = True
if f:
count += 1
answer.append(i + 1)
if s[i + 1] == "B":
i += 1
f = False
i += 1
print(count)
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 | a=int(input())
qqq=input()
b=list(qqq)
ans=[]
p='B'
q='W'
for i in range(len(b)-1):
if b[i] != p:
b[i]=p
ans.append(i+1)
if b[i+1]==p:
b[i+1]=q
else :
b[i+1]=p
if b==list('W'*len(b)) or b == list('B'*len(b)):
ans.reverse()
if len(ans) == 0:
print('0')
else:
print(len(ans))
print(' '.join([str(i) for i in ans]))
exit(0)
else:
b = list(qqq)
ans = []
p = 'W'
q = 'B'
for i in range(len(b) - 1):
if b[i] != p:
b[i]=p
ans.append(i + 1)
if b[i + 1] == p:
b[i + 1] = q
else:
b[i + 1] = p
if b == list('W' * len(b)) or b == list('B' * len(b)):
ans.reverse()
if len(ans) == 0:
print('0')
else:
print(len(ans))
print(' '.join([str(i) for i in ans]))
exit(0)
else:
print('-1')
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 |
def sol():
n = int(input())
s = input()
b = sum(map(lambda x: x == 'B', s))
w = sum(map(lambda x: x == 'W', s))
if w % 2 == 1 and b % 2 == 1:
print(-1)
return
ans = list()
s = list(s)
if w % 2 == 1:
odd = 'W'
even = 'B'
else:
odd = 'B'
even = 'W'
for i in range(n-1):
if s[i] != odd:
ans.append(i + 1)
s[i] = odd
s[i+1] = even if s[i+1] == odd else odd
print(len(ans))
print(' '.join(map(str, ans)))
sol() | 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 f():
n = int(input())
d = {'B':0,'W':1}
line = [d[char] for char in input()]
# print(line)
if not any(line):
print(0)
return
if all(line):
print(0)
return
modify0 = []
modify1 = []
for i in range(n):
if line[i]:
modify1.append(i)
else:
modify0.append(i)
modify = None
if len(modify1)%2==0:
modify = modify1
elif len(modify0)%2==0:
modify = modify0
if modify is None:
print(-1)
return
m = len(modify)
ops = []
for i in range(0,m,2):
x = modify[i]
y = modify[i+1]
for ind in range(x,y):
ops.append((ind,ind+1))
print(len(ops))
print(' '.join(str(op[1]) for op in ops))
f() | 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 | #Bhargey Mehta (Junior)
#DA-IICT, Gandhinagar
import sys, math, queue, collections
MOD = 10**9+7
#sys.stdin = open('input.txt', 'r')
n = int(input())
s = list(input())
t = s[:]
ans = []
for i in range(n-1):
if s[i] == 'W':
s[i] = 'B'
if s[i+1] == 'W':
s[i+1] = 'B'
else:
s[i+1] = 'W'
ans.append(i+1)
if len(set(s)) == 1:
print(len(ans))
print(*ans)
exit()
s = t[:]
ans = []
for i in range(n-1):
if s[i] == 'B':
s[i] = 'W'
if s[i+1] == 'W':
s[i+1] = 'B'
else:
s[i+1] = 'W'
ans.append(i+1)
if len(set(s)) == 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 | n=int(input())
s=input()
t1=s.count('B')
t2=s.count('W')
a=[x for x in s]
if t1%2==1 and t2%2==1:
print("-1")
else:
c=0
a1=[]
b="#"
if t2%2==0:
b="W"
else:
b="B"
for i in range(n-1):
if(a[i]==b):
c+=1
if(a[i+1]=='W'):
a[i+1]='B'
else:
a[i+1]='W'
a1.append(i+1)
print(c)
print(*a1) | 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;
vector<long long int> change(string& s, char a, char b, long long int a_count,
long long int b_count, long long int n) {
vector<long long int> steps;
long long int p1 = 0;
while (p1 < n && a_count) {
while (s[p1] == b) p1++;
long long int p2 = p1 + 1;
while (s[p2] == b) p2++;
for (long long int i = p1; i < p2; i++) steps.push_back(i + 1);
p1 = p2 + 1;
a_count -= 2;
}
return steps;
}
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
long long int b = 0;
long long int w = 0;
for (long long int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if ((b & 1) && (w & 1)) {
cout << -1;
return;
}
if ((b > 0 && w == 0) || (b == 0 && w > 0)) {
cout << 0;
return;
}
vector<long long int> steps;
if (b % 2 == 0 && w % 2 == 0) {
if (b > w) {
steps = change(s, 'W', 'B', w, b, n);
} else {
steps = change(s, 'B', 'W', b, w, n);
}
} else if (b % 2 == 0) {
steps = change(s, 'B', 'W', b, w, n);
} else {
steps = change(s, 'W', 'B', w, b, n);
}
cout << steps.size() << '\n';
for (auto x : steps) {
cout << x << " ";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int T = 1;
while (T--) {
solve();
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;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
int count = 0;
vector<int> a;
if (b == 0 || w == 0) {
cout << 0 << endl;
return 0;
} else if (n % 2 == 0 && b % 2 == 1) {
cout << -1 << endl;
return 0;
} else if (n % 2 == 0) {
if (b >= w) {
int i = 0;
while (i < n - 1) {
if (s[i] == 'W') {
count++;
a.push_back(i + 1);
s[i] = 'B';
s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B';
}
i++;
}
} else {
int i = 0;
while (i < n - 1) {
if (s[i] == 'B') {
count++;
a.push_back(i + 1);
s[i] = 'W';
s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B';
}
i++;
}
}
} else {
if (b % 2 == 1) {
int i = 0;
while (i < n - 1) {
if (s[i] == 'W') {
count++;
a.push_back(i + 1);
s[i] = 'B';
s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B';
}
i++;
}
} else {
int i = 0;
while (i < n - 1) {
if (s[i] == 'B') {
count++;
a.push_back(i + 1);
s[i] = 'W';
s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B';
}
i++;
}
}
}
cout << count << endl;
for (int i = 0; i < (int)a.size(); i++) {
cout << a[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 | #include <bits/stdc++.h>
using namespace std;
int b, i, j, k, m, n, w, x[600];
string s;
void P(int i) {
if (s[i] == 66)
s[i] = 87;
else
s[i] = 66;
}
int main() {
cin >> n >> s;
for (; i < n; i++) s[i] % 2 ? w++ : b++;
if (n % 2 < 1 && w % 2)
m = 1;
else if (n % 2 < 1) {
for (i = 0; i < n - 1; i++) {
if (s[i] % 2) {
x[k++] = i + 1;
P(i);
P(i + 1);
}
}
} else {
if (b % 2) {
for (i = 0; i < n - 1; i++) {
if (s[i] % 2) {
x[k++] = i + 1;
P(i);
P(i + 1);
}
}
} else {
for (i = 0; i < n - 1; i++) {
if (s[i] % 2 < 1) {
x[k++] = i + 1;
P(i);
P(i + 1);
}
}
}
}
if (m)
cout << -1;
else {
for (cout << k << endl, i = 0; i < k;) cout << x[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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string colors;
cin >> colors;
vector<int> col(n);
for (int i = 0; i < n; i++) col[i] = (colors[i] == 'W' ? 0 : 1);
int p = col[0];
int k = 0;
vector<int> op;
for (int i = 1; i < n - 1;) {
if (col[i] == p)
i += 1;
else {
col[i] = p;
col[i + 1] = (col[i + 1] == 1 ? 0 : 1);
op.push_back(i + 1);
i += 1;
k += 1;
}
}
if (col[n - 1] != p) {
if ((n - 1) % 2 == 1)
cout << -1 << endl;
else {
cout << k + (n - 1) / 2 << endl;
for (int el : op) cout << el << " ";
for (int i = 1; i < n - 1; i += 2) cout << i << " ";
cout << endl;
}
} else {
cout << k << endl;
for (int el : op) cout << el << " ";
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 | def inv(c):
if(c=="W"):
return "B"
else:
return "W"
n = int(input())
bs = list(input())
rs = [inv(s) for s in bs]
changes = 0
cindex = []
flag = False
while(len(bs)>2):
el = len(bs)
if(bs[el-1]=="W"):
bs[el-1]="B"
bs[el-2]=inv(bs[el-2])
changes += 1
cindex.append(el-1)
else:
if(bs[el-2]=="B"):
bs.pop()
bs.pop()
else:
bs[el-2]="B"
bs[el-3]=inv(bs[el-3])
changes += 1
cindex.append(el-2)
if(bs==['W', 'W']):
changes += 1
cindex.append(1)
elif(bs==['B', 'W'] or bs==['W', 'B'] or bs==['W']):
flag = True
if(not flag):
print(changes)
if(changes != 0):
print(" ".join(str(i) for i in cindex))
else:
bs = rs
changes = 0
cindex = []
flag = False
while(len(bs)>2):
el = len(bs)
if(bs[el-1]=="W"):
bs[el-1]="B"
bs[el-2]=inv(bs[el-2])
changes += 1
cindex.append(el-1)
else:
if(bs[el-2]=="B"):
bs.pop()
bs.pop()
else:
bs[el-2]="B"
bs[el-3]=inv(bs[el-3])
changes += 1
cindex.append(el-2)
if(bs==['W', 'W']):
changes += 1
cindex.append(1)
elif(bs==['B', 'W'] or bs==['W', 'B'] or bs==['W']):
flag = True
if(flag):
print(-1)
else:
print(changes)
if(changes != 0):
print(" ".join(str(i) for i in cindex))
| 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 | //package CodeForces;
import java.util.ArrayList;
import java.util.Scanner;
public class Blocks {
static boolean solve(char ch[],int index,char x,ArrayList<Integer> arr)
{
if(index==ch.length)
{
StringBuilder ans=new StringBuilder();
System.out.println(arr.size());
for(int i=0;i<arr.size();i++)
{
ans.append((arr.get(i)+1)+" ");
}
System.out.println(ans);
return true;
}
if(ch[index]==x)
{
return solve(ch,index+1,x,arr);
}
else
{
if(index==ch.length-1)
{
return false;
}
ch[index]=x;
if(ch[index+1]=='B')
{
ch[index+1]='W';
}
else
{
ch[index+1]='B';
}
arr.add(index);
boolean temp=solve(ch,index+1,x,arr);
arr.remove(arr.size()-1);
return temp;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String str=s.next();
char ch[]=str.toCharArray();
ArrayList<Integer> arr=new ArrayList<Integer>();
if(!solve(ch,0,'W',arr))
{
ch=str.toCharArray();
if(!solve(ch,0,'B',arr))
{
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 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Unstoppable solver = new Unstoppable();
//int t=in.nextInt();
//while(t-->0)
solver.solve(in, out);
out.close();
}
static class Unstoppable {
public void solve(InputReader in, PrintWriter out) {
int n=in.nextInt();
char ch[]=in.next().toCharArray();
int b=0,w=0;
ArrayList<Integer> array=new ArrayList<>();
for(int i=0;i<n;i++){
if(ch[i]=='W') w++; else b++;
}
char c='z';
if(b%2==0&&b>0) c='B'; if(w%2==0&&w>0) c='W';
if(w==0||b==0) out.println("0");
else if(c=='z') out.println("-1");
else{
for(int i=0;i<n-1;i++){
if(ch[i]==c) { if(ch[i]=='W') ch[i]='B'; else ch[i]='W'; if(ch[i+1]=='W') ch[i+1]='B'; else ch[i+1]='W'; array.add(i+1); }
}
out.println(array.size());
for(int i=0;i<array.size();i++) out.print(array.get(i)+" "); out.println("");
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong(){
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(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 | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using dl = double;
const int N = 2e5 + 10;
ll aarray[200000 + 10];
ll magic[101][101];
vector<ll> primes;
bool prime[1000001];
vector<ll> B, W;
int main() {
ios_base::sync_with_stdio(false);
string str, black, white;
ll i, j, n, m, k, t;
vector<ll> v;
cin >> n;
cin >> str;
black = str;
white = str;
for (i = 0; i < black.size() - 1; i++) {
if (black[i] == 'W') {
B.push_back(i + 1);
black[i] = 'B';
if (black[i + 1] == 'W')
black[i + 1] = 'B';
else
black[i + 1] = 'W';
}
}
for (i = 0; i < white.size() - 1; i++) {
if (white[i] == 'B') {
W.push_back(i + 1);
white[i] = 'W';
if (white[i + 1] == 'W')
white[i + 1] = 'B';
else
white[i + 1] = 'W';
}
}
ll f1 = 0, f2 = 0;
for (i = 0; i < black.size(); i++) {
if (black[i] == 'W') {
f1 = 1;
break;
}
}
if (!f1) {
cout << B.size() << endl;
for (auto c : B) cout << c << " ";
cout << endl;
return 0;
}
for (i = 0; i < white.size(); i++) {
if (white[i] == 'B') {
f2 = 1;
break;
}
}
if (!f2) {
cout << W.size() << endl;
for (auto c : W) cout << c << " ";
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 | b=int(input())
s=input()
cb=s.count('B')
cw=s.count('W')
o=''
co=0
if cb==0 or cw==0:
print(0)
elif cw%2==0:
i=0
while i!=b-1:
if s[i]=='W':
c='B'
if s[i+1]=='W':
ci='B'
else:
ci='W'
s=s[:i]+c+ci+s[i+2:]
co+=1
o+=str(i+1)
o+=' '
i+=1
print(co)
print(o)
elif cb%2==0:
i=0
while i!=b-1:
if s[i]=='B':
c='W'
if s[i+1]=='B':
ci='W'
else:
ci='B'
s=s[:i]+c+ci+s[i+2:]
co+=1
o+=str(i+1)
o+=' '
i+=1
print(co)
print(o)
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 | /*
*
* @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya)
* Dhirubhai Ambani Institute of Information And Communication Technology
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
public class Code56
{
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int test_case = 1;
for(int t=0;t<test_case;t++)
{
int n = in.nextInt();
char[] s = new char[n];
s = in.nextLine().toCharArray();
ArrayList<Integer> black = new ArrayList<>();
ArrayList<Integer> white = new ArrayList<>();
for(int i=0;i<n;i++){
if(s[i]=='W')
white.add(i);
else
black.add(i);
}
int blackCount = black.size();
int whiteCount = white.size();
if(blackCount==0 || whiteCount ==0 )
pw.println(0);
else if(blackCount%2==1 && whiteCount%2==1)
pw.println(-1);
else{
ArrayList<Integer> ans = new ArrayList<>();
if(blackCount%2==0){
for(int i=0;i<blackCount;i+=2){
int current = black.get(i);
int next = black.get(i+1);
for(int j=current;j<next;j++){
ans.add(j+1);
}
}
}else{
for(int i=0;i<whiteCount;i+=2){
int current = white.get(i);
int next = white.get(i+1);
for(int j=current;j<next;j++){
ans.add(j+1);
}
}
}
pw.println(ans.size());
for(int v : ans)
pw.print(v + " ");
}
}
pw.flush();
pw.close();
}
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);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
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 (Math.abs(p.x-x)==0 && Math.abs(p.y-y)==0);
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
} | 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>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long flag = 0, flag2 = 0;
map<long long, long long> mm;
set<long long> s;
vector<long long> v;
set<long long>::iterator itr;
long long n;
cin >> n;
string a;
cin >> a;
long long cnt = 0, cnt2 = 0;
for (long long i = 1; i < n - 1; i++) {
if (a[i] != a[0]) {
a[i] = a[0];
if (a[i + 1] == 'W')
a[i + 1] = 'B';
else
a[i + 1] = 'W';
v.push_back(i + 1);
cnt++;
}
}
for (long long i = 0; i < n; i++) {
if (a[i] == a[0]) cnt2++;
}
if (cnt2 == n) {
cout << cnt << endl;
for (std::vector<long long>::iterator i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
cout << endl;
} else if (n % 2 != 0) {
for (long long i = 0; i < n - 1; i = i + 2) {
cnt++;
v.push_back(i + 1);
}
cout << cnt << endl;
for (std::vector<long long>::iterator i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
cout << endl;
} else {
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 |
import java.io.*;
import java.util.*;
public class test5 {
public static final int mod=1000000007;
public static void main(String[] args) throws IOException{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
//Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=Integer.parseInt(sc.readLine());
char[] arr=sc.readLine().toCharArray();
int[] a=new int[n];
int[] b=new int[n];
int bc=0,wc=0;
for(int i=0;i<n;i++) {
if(arr[i]=='W') {wc++;a[i]=b[i]=0;}
if(arr[i]=='B') {bc++;a[i]=b[i]=1;}
}
if(n%2==0 && (wc*bc)%2==1) {
out.print(-1);
out.flush();
return;
}
if(wc==0 || bc==0) {
out.print(0);
out.flush();
return;
}
List<Integer> lista=new LinkedList<>();
List<Integer> listb=new LinkedList<>();
// W->0 B->1
int target;
if(n%2==1) {
if(wc%2==0) target=0;
else target=1;
for(int i=0;i<n-1;i++) {
if(a[i]==target) {
lista.add(i+1);
a[i]=1-a[i];
a[i+1]=1-a[i+1];
}
}
out.println(lista.size());
for(int ele:lista) {
out.print(ele+" ");
}
out.flush();
return;
} else {
target=0;
for(int i=0;i<n-1;i++) {
if(a[i]==target) {
lista.add(i+1);
a[i]=1-a[i];
a[i+1]=1-a[i+1];
}
}
target=1;
for(int i=0;i<n-1;i++) {
if(b[i]==target) {
listb.add(i+1);
b[i]=1-b[i];
b[i+1]=1-b[i+1];
}
}
if(lista.size()<=listb.size()) {
out.println(lista.size());
for(int ele:lista) {
out.print(ele+" ");
}
} else {
out.println(listb.size());
for(int ele:listb) {
out.print(ele+" ");
}
}
}
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 | #include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 1;
int main() {
int n;
cin >> n;
string s;
cin >> s;
string st = s;
vector<int> output1, output2;
for (int j = 0; j < 3; ++j) {
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'W') {
output1.push_back(i + 1);
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
}
string t = st;
for (int j = 0; j < 3; ++j) {
for (int i = 0; i < n - 1; ++i) {
if (t[i] == 'B') {
output2.push_back(i + 1);
t[i] = 'W';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
}
}
}
if (s[n - 1] != 'W') {
cout << output1.size() << '\n';
for (int i = 0; i < output1.size(); ++i) cout << output1[i] << ' ';
} else if (t[n - 1] != 'B') {
cout << output2.size() << '\n';
for (int i = 0; i < output2.size(); ++i) cout << output2[i] << ' ';
} else
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 | import java.util.*;
public class Block608 {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
StringBuilder sub = new StringBuilder(str);
ArrayList<Character> list = new ArrayList<>();
list.add('B');
list.add('W');
for(char c:list) {
int count = 0;
StringBuilder ans = new StringBuilder();
for(int i=0;i<sub.length()-1;i++) {
if(sub.charAt(i)!=c) {
sub.setCharAt(i, c);
sub.setCharAt(i+1,sub.charAt(i+1)=='W'?'B':'W');
ans.append(i+1+" ");
count++;
}
}
if(sub.charAt(sub.length()-1)==c) {
System.out.println(count+"\n"+ans);
return;
}
sub = new StringBuilder(str);
}
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() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
int n;
cin >> n;
string s;
cin >> s;
string r = s;
vector<int> v1, v2;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W' && i < n - 1) {
if (s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
v1.push_back(i + 1);
i++;
} else {
v1.push_back(i + 1);
s[i] = 'B';
s[i + 1] = 'W';
}
}
}
int f = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W') f = 1;
}
if (f == 0) {
cout << v1.size() << "\n";
for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " ";
cout << "\n";
} else {
s = r;
for (long long i = 0; i < n; i++) {
if (s[i] == 'B' && i < n - 1) {
if (s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
v2.push_back(i + 1);
i++;
} else {
s[i] = 'W';
s[i + 1] = 'B';
v2.push_back(i + 1);
}
}
}
f = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'B') f = 1;
}
if (f == 1)
cout << "-1\n";
else {
cout << v2.size() << "\n";
for (long long i = 0; i < v2.size(); i++) cout << v2[i] << " ";
cout << "\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 | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
long long int n, a = 0, b = 0;
string second;
vector<long long int> v;
int32_t main() {
ios_base::sync_with_stdio();
cin.tie();
cout.tie();
;
cin >> n >> second;
for (long long int i = 0; i < n; i++)
if (second[i] == 'B') {
a++;
} else
b++;
if (a % 2 == 1 && b % 2 == 1) {
return cout << "-1", 0;
}
if (a % 2 == 0) {
for (long long int i = 0; i < second.size() - 1; i++)
if (second[i] == 'B') {
v.push_back(i + 1);
second[i] = 'W';
if (second[i + 1] == 'B')
second[i + 1] = 'W';
else
second[i + 1] = 'B';
}
cout << v.size() << '\n';
for (long long int i = 0; i < v.size(); i++) cout << v[i] << ' ';
return 0;
} else {
for (long long int i = 0; i < second.size() - 1; i++)
if (second[i] == 'W') {
v.push_back(i + 1);
second[i] = 'B';
if (second[i + 1] == 'B')
second[i + 1] = 'W';
else
second[i + 1] = 'B';
}
cout << v.size() << '\n';
for (long long int i = 0; i < v.size(); i++) cout << v[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 | import java.util.*;
public class Blocks{
static Scanner in=new Scanner(System.in);
public static void main(String array[]){
int n=in.nextInt();
char ch[]=in.next().toCharArray();
int white=0,black=0;
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<n;i++){
if(ch[i]=='W')
white++;
else
black++;
}
char c;
if(ch[0]==ch[1])
c=ch[0];
else
c=ch[1];
if((n%2==0&&(black%2==0&&white%2==0))||(n%2!=0&&((white%2==0&&black%2!=0)||(white%2!=0&&black%2==0)))){
while(true){
for(int i=0;i<n-1;i++){
if(ch[i]!=c){
list.add(i+1);
ch[i]=c;
ch[i+1]=(ch[i+1]=='B')?'W':'B';
}
}
if(ch[n-1]==c)
break;
else
c=ch[n-1];
}
int size=list.size();
if(size<=3*n){
System.out.println(list.size());
for(int i:list)
System.out.print(i+" ");
System.out.println();
}
else
System.out.println(-1);
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<int> ans;
int ct = 0;
for (int i = int(0); i <= int(n - 1); i++) {
if (s[i] == 'W') ct++;
}
int ct2 = n - ct;
if (ct % 2 && ct2 % 2) {
cout << -1;
return 0;
}
if (ct % 2 == 0) {
for (int i = int(0); i <= int(n - 2); i++) {
if (s[i] == 'W' && s[i + 1] == 'W') {
ans.push_back(i + 1);
s[i] = 'B';
s[i + 1] = 'B';
}
}
for (int i = int(0); i <= int(n - 1); i++) {
if (s[i] == 'W') {
for (int j = int(i + 1); j <= int(n - 1); j++) {
if (s[j] == 'W') {
ans.push_back(j);
s[j] = 'B';
s[j - 1] = 'B';
break;
}
ans.push_back(j);
swap(s[j - 1], s[j]);
}
}
}
} else {
for (int i = int(0); i <= int(n - 2); i++) {
if (s[i] == 'B' && s[i + 1] == 'B') {
ans.push_back(i + 1);
s[i] = 'W';
s[i + 1] = 'W';
}
}
for (int i = int(0); i <= int(n - 1); i++) {
if (s[i] == 'B') {
for (int j = int(i + 1); j <= int(n - 1); j++) {
if (s[j] == 'B') {
ans.push_back(j);
s[j] = 'W';
s[j - 1] = 'W';
break;
}
ans.push_back(j);
swap(s[j - 1], s[j]);
}
}
}
}
cout << ans.size() << "\n";
for (auto 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 = input()
cnt = list()
x = 0
if s.count('B') % 2 == 1 and s.count('W') % 2 == 1:
print(-1)
else:
if s.count('B') % 2 == 0:
while s.count('B') != 0:
for i in range(s.find('B') + 1, s[s.find('B') + 1::].find('B') + s.find('B') + 2):
cnt.append(str(x + i))
s, x = s[s[s.find('B') + 1::].find('B') + 2 + s.find('B')::], x + s[s.find('B') + 1::].find('B') + s.find('B') + 2
print(len(cnt))
print(' '.join(cnt))
else:
while s.count('W') != 0:
for i in range(s.find('W') + 1, s[s.find('W') + 1::].find('W') + s.find('W') + 2):
cnt.append(str(x + i))
s, x = s[s[s.find('W') + 1::].find('W') + 2 + s.find('W')::], x + s[s.find('W') + 1::].find('W') + s.find('W') + 2
print(len(cnt))
print(' '.join(cnt)) | 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 | # HEY STALKER
from collections import Counter
n = int(input())
l = list(input())
x = Counter(l)
if x['B'] % 2 and x['W'] % 2:
print(-1)
elif x['B'] == n or x['W'] == n:
print(0)
else:
k = []
b = ''
w = ''
if not x['B'] % 2:
b = 'B'
w = 'W'
else:
b = 'W'
w = 'B'
t = 0
while t < n-1:
if l[t] == b:
if l[t+1] == b:
k.append(t+1)
l[t] = w
l[t+1] = w
elif l[t+1] == w:
k.append(t+1)
l[t] = w
l[t+1] = b
t += 1
print(len(k))
print(*k) | 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()
sw=list(s)
sb=list(s)
ans=0
l=[]
for j in range(n-1):
if sw[j]=='B':
sw[j]='W'
sw[j+1]='W' if sw[j+1]=='B' else 'B'
ans+=1
l.append(j+1)
if sw[n-1]=='W':
print(ans)
print(*l)
else:
ans=0
l=[]
for j in range(n-1):
if sb[j]=='W':
sb[j]='B'
sb[j+1]='B' if sb[j+1]=='W' else 'W'
ans+=1
l.append(j+1)
if sb[n-1]=='B':
print(ans)
print(*l)
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.lang.Math;
public class Main{
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static String nexts() throws IOException {
tokenizer = new StringTokenizer(reader.readLine());
String s="";
while (tokenizer.hasMoreTokens()) {
s+=tokenizer.nextElement()+" ";
}
return s;
}
public static int gcd(int x, int y){
if (y == 0) return x; else return gcd(y, x % y);
}
public static boolean isPrime(int nn)
{
if(nn<=1){
return false; }
if(nn<=3){
return true; }
if (nn%2==0||nn%3==0){
return false; }
for (int i=5;i*i<=nn;i=i+6){
if(nn%i==0||nn%(i+2)==0){
return false; }
}
return true;
}
public static void shuffle(int[] A) {
for (int i = 0; i < A.length; i++) {
int j = (int)(i * Math.random());
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
public static void shufflel(Long[] A) {
for (int i = 0; i < A.length; i++) {
int j = (int)(i * Math.random());
long tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
public static String reverse(String input)
{
StringBuilder input2=new StringBuilder();
input2.append(input);
input2 = input2.reverse();
return input2.toString();
}
public static long power(int x, long n)
{
// long mod = 1000000007;
if (n == 0) {
return 1;
}
long pow = power(x, n / 2);
if ((n & 1) == 1) {
return (x * pow * pow);
}
return (pow * pow);
}
public static long power(long x, long y, long p) //x^y%p
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % p;
}
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long ncr(int n, int k)
{
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k) {
k = n - k;
}
// Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
public static int inv(int a, int m) //Returns modulo inverse of a with respect to m using extended euclid algorithm
{
int m0=m,t,q;
int x0=0,x1=1;
if(m==1){
return 0; }
while(a>1)
{
q=a/m;
t=m;
m=a%m;a=t;
t=x0;
x0=x1-q*x0;
x1=t; }
if(x1<0){
x1+=m0; }
return x1;
}
static int modInverse(int n, int p)
{
return (int)power((long)n, (long)(p - 2),(long)p);
}
static int ncrmod(int n, int r, int p)
{
if (r == 0) // Base case
return 1;
int[] fac = new int[n + 1]; // Fill factorial array so that we can find all factorial of r, n and n-r
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; //IMPORTANT
}
static int upper(int a[], int x) //Returns rightest index of x in sorted arr else -1
{ //If not present returns index of just smaller element
int n=a.length;
int l=0;
int r=n-1;
int ans=-1;
while(l<=r){
int m=(r-l)/2+l;
if(a[m]<=x){
ans=m;
l=m+1;
}
else{
r=m-1;
}
}
return ans;
}
static int lower(int a[], int x) //Returns leftest index of x in sorted arr else n
{ //If not present returns index of just greater element
int n=a.length;
int l=0;
int r=n-1;
int ans=n;
while(l<=r){
int m=(r-l)/2+l;
if(a[m]>=x){
ans=m;
r=m-1;
}
else{
l=m+1;
}
}
return ans;
}
public static long stringtoint(String s){ // Convert bit string to number
long a = 1;
long ans = 0;
StringBuilder sb = new StringBuilder();
sb.append(s);
sb.reverse();
s=sb.toString();
for(int i=0;i<s.length();i++){
ans = ans + a*(s.charAt(i)-'0');
a = a*2;
}
return ans;
}
String inttostring(long a,long m){ // a is number and m is length of string required
String res = ""; // Convert a number in bit string representation
for(int i=0;i<(int)m;i++){
if((a&(1<<i))==1){
res+='1';
}else
res+='0';
}
StringBuilder sb = new StringBuilder();
sb.append(res);
sb.reverse();
res=sb.toString();
return res;
}
static class R implements Comparable<R>{
int x, y;
public R(int x, int y) { //a[i]=new R(x,y);
this.x = x;
this.y = y;
}
public int compareTo(R o) {
return x-o.x; //Increasing order(Which is usually required)
}
}
/* FUNCTION TO SORT HASHMAP ACCORDING TO VALUE
//USE IT AS FOLLOW
//Make a new list
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(h.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue()); //This is used to sort string stored as VALUE, use below function here accordingly
}
});
// put data from sorted list to new hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp; //Now temp is our new sorted hashmap
OR
for(int i = 0;i<h.size();i++) //accessing elements from the list made
{
int count = list.get(i).getValue(); //Value
while(count >= 0)
{
int key=list.get(i).getKey(); //key
count -- ;
}
}
////////////////////////////////////////////////////////////////////////////////////////
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2)
{
if(o1.getValue() != o2.getValue())
return o1.getValue() > o2.getValue()?1:-1; //Sorts Value in increasing order
else if(o1.getValue() == o2.getValue()){
return o1.getKey() > o2.getKey() ? 1:-1; //If value equal, then sorts key in increasing order
}
return Integer.MIN_VALUE;
}
*/
//Check if palindrome string - System.out.print(A.equals(new StringBuilder(A).reverse().toString())?"Yes":"No");
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
}
private static void solve() throws IOException {
// int t = nextInt();
// while(t-->0){
//String s= nextToken();
//String[] s = str.split("\\s+");
ArrayList<Integer> ar=new ArrayList<Integer>();
//HashSet<Integer> set=new HashSet<Integer>();
//HashMap<Integer,String> h=new HashMap<Integer,String>();
//R[] a1=new R[n];
//PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder());
//NavigableSet<Integer> pq=new TreeSet<>();
int n = nextInt();
//long n = nextLong();
char[] c=nextToken().toCharArray();
//int[] a=new int[n];
//long[] a=new long[n];
int k=0;
for(int i=0;i<n-1;i++){
if(c[i]!=c[i+1])k=1;
}
if(k==0){
writer.println(0);
return;
}
int u=0;
int co=0;
char[] cc=new char[n];
for(int i=0;i<n;i++){
cc[i]=c[i];
}
for(int i=0;i<n-1;i++){
if(c[i]=='W'){
ar.add(i+1);
co++;
c[i]='B';
c[i+1]=c[i+1]=='W'?'B':'W';
}
}
for(int i=0;i<n;i++)if(c[i]=='W')u=1;
if(u==0){
writer.println(co);
for(int p:ar)writer.print(p+" ");
return;
}
co=0;
u=0;
ArrayList<Integer> a=new ArrayList<Integer>();
for(int i=0;i<n-1;i++){
if(cc[i]=='B'){
a.add(i+1);
co++;
cc[i]='W';
cc[i+1]=cc[i+1]=='W'?'B':'W';
}
}
for(int i=0;i<n;i++)if(cc[i]=='B')u=1;
if(u==0){
writer.println(co);
for(int p:a)writer.print(p+" ");
return;
}
writer.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 | import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
String s = scanner.next();
StringBuffer str = new StringBuffer(s);
Map<Character,Integer>map = new HashMap<>();
map.put('B',0);
map.put('W',0);
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='B')
map.put('B',map.get('B')+1);
else
map.put('W',map.get('W')+1);
}
if(map.get('B')%2==1&&map.get('W')%2==1) {
System.out.println(-1);
return;
}
int cnt =0;
ArrayList<Integer>arr = new ArrayList<>();
ArrayList<Integer>arr2 = new ArrayList<>();
int j = 0;
if(map.get('B')%2==1&&map.get('W')%2==0){
//η§»ε¨w
for(int i=0;i<s.length()-1;i++){
if(str.charAt(i)=='W'&&str.charAt(i+1)=='B'){
arr.add(i+1);
cnt++;
str.replace(i,i+1,"B");
str.replace(i+1,i+2,"W");
}
else if(str.charAt(i)=='W'&&str.charAt(i+1)=='W'){
cnt++;
arr.add(i+1);
str.replace(i,i+1,"B");
str.replace(i+1,i+2,"B");
i++;
}
}
}
else if(map.get('B')%2==0&&map.get('W')%2==1){
//η§»ε¨w
for(int i=0;i<s.length()-1;i++){
if(str.charAt(i)=='B'&&str.charAt(i+1)=='W'){
arr.add(i+1);
cnt++;
str.replace(i,i+1,"W");
str.replace(i+1,i+2,"B");
}
else if(str.charAt(i)=='B'&&str.charAt(i+1)=='B'){
cnt++;
arr.add(i+1);
str.replace(i,i+1,"W");
str.replace(i+1,i+2,"W");
i++;
}
}
}
else{
//η§»ε¨w
StringBuffer str2 = new StringBuffer(s);
for(int i=0;i<s.length()-1;i++){
if(str.charAt(i)=='W'&&str.charAt(i+1)=='B'){
arr.add(i+1);
cnt++;
str.replace(i,i+1,"B");
str.replace(i+1,i+2,"W");
}
else if(str.charAt(i)=='W'&&str.charAt(i+1)=='W'){
cnt++;
arr.add(i+1);
str.replace(i,i+1,"B");
str.replace(i+1,i+2,"B");
i++;
}
}
for(int i=0;i<s.length()-1;i++){
if(str2.charAt(i)=='B'&&str2.charAt(i+1)=='W'){
arr2.add(i+1);
j++;
str2.replace(i,i+1,"W");
str2.replace(i+1,i+2,"B");
}
else if(str2.charAt(i)=='B'&&str2.charAt(i+1)=='B'){
j++;
arr2.add(i+1);
str2.replace(i,i+1,"W");
str2.replace(i+1,i+2,"W");
i++;
}
}
}
System.out.println(j==0?cnt:Math.min(j,cnt));
if(cnt<j||j==0) {
for (int m : arr) {
System.out.print(m + " ");
}
}
else{
for (int m : arr2) {
System.out.print(m + " ");
}
}
}
}
| 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;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
int n, ans[10000], cnt;
char s[10000], t[10000];
int a[1000], b[1000];
void End() {
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) {
printf("%d ", ans[i]);
}
}
int main() {
n = read();
scanf("%s", s + 1);
bool ok = true;
for (int i = 1; i <= n; i++) {
if (s[i] == 'W')
a[i] = 1;
else
a[i] = 0;
}
for (int i = 2; i <= n; i++) {
if (a[i] != a[1]) {
ok = false;
break;
}
}
if (ok) {
printf("0\n");
return 0;
}
for (int i = 1; i <= n; i++) {
b[i] = a[i];
}
for (int i = 1; i < n; i++) {
if (b[i] == 1) {
ans[++cnt] = i;
b[i] ^= 1, b[i + 1] ^= 1;
}
}
if (b[n] == 0) {
End();
return 0;
}
cnt = 0;
for (int i = 1; i <= n; i++) {
b[i] = a[i];
}
for (int i = 1; i < n; i++) {
if (b[i] == 0) {
ans[++cnt] = i;
b[i] ^= 1, b[i + 1] ^= 1;
}
}
if (b[n] == 1) {
End();
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 | import sys
input = sys.stdin.readline
def sol():
n = int(input())
s = list(input())
b = s.count('B')
w = s.count('W')
if w % 2 == 1 and b % 2 == 1:
print(-1)
return
ans = []
s = list(s)
if w % 2 == 1:
odd = 'W'
even = 'B'
else:
odd = 'B'
even = 'W'
for i in range(n-1):
if s[i] != odd:
ans.append(i + 1)
s[i] = odd
s[i+1] = even if s[i+1] == odd else odd
print(len(ans))
print(*ans)
sol() | 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.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Ques2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine()), i;
String z=br.readLine();
StringBuilder s = new StringBuilder(z);
StringBuilder t = new StringBuilder(z);
ArrayList<Integer> l = new ArrayList<>();
for(i=0;i<n-1;i++){
if(s.charAt(i)=='W'){
s.setCharAt(i, 'B');
l.add(i+1);
if(s.charAt(i+1)=='B'){
s.setCharAt(i+1, 'W');
}
else{
s.setCharAt(i+1, 'B');
}
}
}
if(s.charAt(n-1)=='B'){
System.out.println(l.size());
for(Integer x: l){
System.out.print(x+" ");
}
}
else{
l=new ArrayList<>();
for(i=0;i<n-1;i++){
if(t.charAt(i)=='B'){
t.setCharAt(i, 'W');
l.add(i+1);
if(t.charAt(i+1)=='B'){
t.setCharAt(i+1, 'W');
}
else{
t.setCharAt(i+1, 'B');
}
}
}
if(t.charAt(n-1)=='W'){
System.out.println(l.size());
for(Integer x: l){
System.out.print(x+" ");
}
}
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 | def rev(s):
if s=="B":
return "W"
else:
return "B"
n=int(input())
s=list(input())
ans=[]
if n%2==0:
if s.count("B")%2==1:
print (-1)
exit()
if s.count("B")%2==0:
i=0
while i<n-1:
if s[i]=="B":
ans.append(i+1)
s[i+1]=rev(s[i+1])
i+=1
else:
i=0
while i<n-1:
if s[i]=="W":
ans.append(i+1)
s[i+1]=rev(s[i+1])
i+=1
print (len(ans))
if len(ans)!=0:
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 | # for t in range(int(input())):
# s = input()
# i, j = 0, 0
# cnt = 0
# ans = float('inf')
# dic = {}
# while j < len(s):
# if len(dic) < 3:
# dic[s[j]] = dic.get(s[j], 0) + 1
# # print(j)
# # print(dic)
# while len(dic) == 3:
# ans = min(ans, j - i + 1)
# dic[s[i]] -= 1
# if dic[s[i]] == 0:
# del dic[s[i]]
# i += 1
#
# j += 1
# print((0, ans)[ans < float('inf')])
# for t in range(int(input())):
# n = int(input())
# s = list(map(int, input().split()))
# dp = [1] * n
# for i in range(n):
# k = 2
# while (i + 1) * k <= n:
# j = (i + 1) * k
# if s[i] < s[j - 1]:
# dp[j - 1] = max(dp[j - 1], dp[i] + 1)
# k += 1
# print(max(dp))
# for T in range(int(input())):
# t = input()
# z, o = 0, 0
# for ch in t:
# z = z + 1 if ch == '0' else z
# o = o + 1 if ch == '1' else o
# if z > 0 and o > 0:
# print('01' * len(t))
# elif o > 0 and not z:
# print('1' * len(t))
# else:
# print('0' * len(t))
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# ans = []
# while a:
# ans.append(str(a.pop(len(a) // 2)))
# print(' '.join(ans))
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# cnt = 0
# p = set()
# l, r = 0, sum(a)
# left, right = {}, {}
# for i in a:
# right[i] = right.get(i, 0) + 1
# for i in range(n - 1):
# l += a[i]
# left[a[i]] = left.get(a[i], 0) + 1
# r -= a[i]
# right[a[i]] = right.get(a[i], 0) - 1
# if not right[a[i]]:
# del right[a[i]]
# j = n - i - 1
# if (2 + i) * (i + 1) // 2 == l and (j + 1) * j // 2 == r:
# if len(left) == i + 1 and len(right) == j:
# cnt += 1
# p.add((i + 1, n - i - 1))
# print(cnt)
# if cnt:
# for el in p:
# print(*el)
# for t in range(int(input())):
# n = int(input())
# G = []
# taken = [False] * n
# girl = -1
# for i in range(n):
# g = list(map(int, input().split()))
# k = g[0]
# single = True
# for j in range(1, k + 1):
# if not taken[g[j] - 1]:
# taken[g[j] - 1] = True
# single = False
# break
# if single:
# girl = i
# if girl == -1:
# print('OPTIMAL')
# else:
# print('IMPROVE')
# print(girl + 1, taken.index(False) + 1)
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# odd, even = [], []
# for i in range(2 * n):
# if a[i] % 2:
# odd.append(i + 1)
# else:
# even.append(i + 1)
# for i in range(n - 1):
# if len(odd) >= len(even):
# print(odd.pop(), odd.pop())
# else:
# print(even.pop(), even.pop())
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# ans, i, j = 0, 0, 1
# while j < n:
# if a[i] < a[j]:
# ans += 1
# i += 1
# j += 1
# else:
# while j < n and a[i] == a[j]:
# i += 1
# j += 1
# print(ans + 1)
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# got = False
#
# b = 1
# while not got and b < 2 * n - 1:
# if b % 2:
# i, j = (b - 1) // 2, (b + 1) // 2
# else:
# i, j = b // 2 - 1, b // 2 + 1
# left, right = set(a[:i]), set(a[j:])
# if left & right:
# got = True
# b += 1
# print('YES' if got else 'NO')
# n, m, k = list(map(int, input().split()))
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# ans = 0
# a, b = [0], [0]
# for el in A:
# a.append(a[-1] + el)
# for el in B:
# b.append(b[-1] + el)
# d = [(i, k//i) for i in range(1, int(k**0.5)+1) if k % i == 0]
# d += [(j, i) for i, j in d if i != j]
# for i in range(n):
# for j in range(m):
# for q, p in d:
# if i + q <= n and j + p <= m:
# if a[i + q] - a[i] == q and b[j + p] - b[j] == p:
# ans += 1
# print(ans)
# for t in range(int(input())):
# n = int(input())
# s = input()
# dic, se = {s: 1}, {s}
# for k in range(2, n):
# p = s[k - 1:] + (s[:k - 1], s[:k - 1][::-1])[(n % 2) == (k % 2)]
# # print(k, p)
# if p not in dic:
# # print(dic, p)
# dic[p] = k
# se.add(p)
# if s[::-1] not in dic:
# dic[s[::-1]] = n
# se.add(s[::-1])
# # print(dic)
# ans = min(se)
# print(ans)
# print(dic[ans])
# for t in range(int(input())):
# a, b, p = list(map(int, input().split()))
# s = input()
# road = [a if s[0] == 'A' else b]
# st = [0]
# for i in range(1, len(s) - 1):
# if s[i] != s[i - 1]:
# road.append(road[-1] + (a, b)[s[i] == 'B'])
# st.append(i)
# # print(road)
# pay = road[-1]
# j = 0
# while pay > p and j < len(st):
# pay = road[-1] - road[j]
# j += 1
# # print(j)
# print(st[j] + 1 if j < len(st) else len(s))
# for t in range(int(input())):
# n, x, y = list(map(int, input().split()))
# print(max(1, min(x + y - n + 1, n)), min(n, x + y - 1))
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# print(' '.join(map(str, sorted(a, reverse=True))))
# s = input()
# open, close = [], []
# i = 0
# for i in range(len(s)):
# if s[i] == '(':
# open.append(i)
# else:
# close.append(i)
# i, j = 0, len(close) - 1
# ans = []
# while i < len(open) and j >= 0 and open[i] < close[j]:
# ans += [open[i] + 1, close[j] + 1]
# i += 1
# j -= 1
# ans.sort()
# print('0' if not ans else '1\n{}\n{}'.format(len(ans), ' '.join([str(idx) for idx in ans])))
import collections
# n, m = list(map(int, input().split()))
# a = list(input() for i in range(n))
# dic = {}
# for w in a:
# dic[w] = dic.get(w, 0) + 1
# l, r = '', ''
# for i in range(n):
# for j in range(i + 1, n):
# # print(i, j, a)
# if a[i] == a[j][::-1] and dic[a[i]] and dic[a[j]]:
# l += a[i]
# r = a[j] + r
# dic[a[i]] -= 1
# dic[a[j]] -= 1
# c = ''
# for k, v in dic.items():
# if v and k == k[::-1]:
# if c and c[-m] == k or not c:
# c += k
# print(f'{len(l) + len(c) + len(r)}\n{l + c + r}')
# for t in range(int(input())):
# n, g, b = list(map(int, input().split()))
# d = n // 2 + n % 2
# full, inc = divmod(d, g)
# ans = (g + b) * (full - 1, full)[inc > 0] + (g, inc)[inc > 0]
# print(ans if ans >= n else n)
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# print(a[n] - a[n - 1])
# for t in range(int(input())):
# n, x = list(map(int, input().split()))
# s = input()
# cntz = s.count('0')
# total = 2 * cntz - n
# bal = 0
# ans = 0
# for i in range(n):
# if not total:
# if bal == x:
# ans = -1
# elif not abs(x - bal) % abs(total):
# if (x - bal) // total >= 0:
# ans += 1
# bal += 1 if s[i] == '0' else -1
# print(ans)
# n = int(input())
# ans = 0
# for i in range(1, n + 1):
# ans += 1 / i
# print(ans)
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# p, s = 0, n - 1
# for i in range(n):
# if a[i] < i:
# break
# p = i
# for i in range(n - 1, -1, -1):
# if a[i] < n - i - 1:
# break
# s = i
# print('Yes' if s <= p else 'No')
# n, k = list(map(int, input().split()))
# a = [input() for i in range(n)]
# c = set(a)
# b = set()
# for i in range(n):
# for j in range(i + 1, n):
# third = ''
# for c1, c2 in zip(a[i], a[j]):
# if c1 == c2:
# third += c1
# else:
# if c1 != 'S' and c2 != 'S':
# third += 'S'
# elif c1 != 'E' and c2 != 'E':
# third += 'E'
# else:
# third += 'T'
# if third in c:
# b.add(frozenset([a[i], a[j], third]))
# print(len(b))
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# total, curr = sum(a), 0
# ans, i, start = 'YES', 0, 0
# while ans == 'YES' and i < n:
# if curr > 0:
# curr += a[i]
# else:
# curr = a[i]
# start = i
# # print(curr, i, start, total)
# if i - start + 1 < n and curr >= total:
# ans = 'NO'
# i += 1
# print(ans)
# for t in range(int(input())):
# n, p, k = list(map(int, input().split()))
# a = list(map(int, input().split()))
# a.sort(reverse=True)
# odd, even = 0, 0
# i, j = len(a) - 1, len(a) - 2
# curr = 0
# while curr < p and i >= 0:
# curr += a[i]
# if curr <= p:
# odd += 1
# i -= 2
# curr = 0
# while curr < p and j >= 0:
# curr += a[j]
# if curr <= p:
# even += 1
# j -= 2
# print(max(odd * 2 - 1, even * 2))
# for t in range(int(input())):
# s, c = input().split()
# s = list(ch for ch in s)
# sor = sorted(s)
# for i in range(len(s)):
# if s[i] != sor[i]:
# j = max(j for j, v in enumerate(s[i:], i) if v == sor[i])
# s = s[:i] + [s[j]] + s[i + 1:j] + [s[i]] + s[j + 1:]
# break
# s = ''.join(s)
# print(s if s < c else '---')
# for t in range(int(input())):
# n, s = list(map(int, input().split()))
# a = list(map(int, input().split()))
# if sum(a) <= s:
# print(0)
# else:
# curr, i, j = 0, 0, 0
# for i in range(n):
# if a[i] > a[j]:
# j = i
# s -= a[i]
# if s < 0:
# break
# print(j + 1)
# for t in range(int(input())):
# a, b = list(map(int, input().split()))
# a, b = (b, a) if b > a else (a, b)
# if not ((1 + 8 * (a - b))**0.5 - 1) % 2 and ((1 + 8 * (a - b))**0.5 - 1) // 2 >= 0:
# ans = ((1 + 8 * (a - b))**0.5 - 1) // 2
# print(int(ans))
# else:
# n1 = int(((1 + 8 * (a - b))**0.5 - 1) // 2) + 1
# while (n1 * (n1 + 1) // 2) % 2 != (a - b) % 2:
# n1 += 1
# print(n1)
# for t in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# a.sort()
# ans = 0
# l = 0
# dic = {}
# for i in range(n - 1, -1, -1):
# if not a[i] % 2:
# l, r = 0, 30
# while l < r:
# m = (l + r) // 2
# # print(l, r, m, a[i] % 2**m)
# if a[i] % 2**m:
# r = m
# else:
# l = m + 1
# dic[a[i] // 2**(l - 1)] = max(dic.get(a[i] // 2**(l - 1), 0), l - 1)
# print(sum(list(dic.values())))
n = int(input())
s = input()
b = s.count('B')
w = n - b
if b % 2 and w % 2:
print(-1)
elif not b or not w:
print(0)
else:
ans = []
if not b % 2:
for i in range(n - 1):
if s[i] != 'W':
ans += [str(i + 1)]
s = s[:i] + 'W' + 'BW'[s[i + 1] == 'B'] + s[i + 2:]
elif not w % 2:
for i in range(n - 1):
if s[i] != 'B':
ans += [str(i + 1)]
s = s[:i] + 'B' + 'WB'[s[i + 1] == 'W'] + s[i + 2:]
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 | #include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int64_t n;
cin >> n;
string s, a, b;
cin >> s;
a = s, b = s;
vector<int64_t> black, white;
for (int64_t i = 0; i < n - 1; ++i) {
if (a[i] == 'B') {
white.push_back(i + 1);
a[i] = 'W';
a[i + 1] = a[i + 1] == 'W' ? 'B' : 'W';
}
}
int64_t c = count((a).begin(), (a).end(), 'W');
if (c == n and int64_t(white.size()) <= 3 * n) {
cout << int64_t(white.size()) << '\n';
for (auto xy : white) cout << xy << ' ';
cout << '\n';
return 0;
}
for (int64_t i = 0; i < n - 1; ++i) {
if (b[i] == 'W') {
black.push_back(i + 1);
b[i] = 'B';
b[i + 1] = b[i + 1] == 'W' ? 'B' : 'W';
}
}
c = count((b).begin(), (b).end(), 'B');
if (c == n and int64_t(black.size()) <= 3 * n) {
cout << int64_t(black.size()) << '\n';
for (auto xy : black) cout << xy << ' ';
cout << '\n';
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 factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush
import os
import sys
from io import BytesIO, IOBase
# 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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')
# ------------------------------
def main():
n = N()
arr = list(input())
w = arr.count('W')
b = n-w
if w%2==1 and b%2==1:
print(-1)
else:
tar = 'W' if w%2==0 else 'B'
res = []
for i in range(n-1):
if arr[i]==tar:
if arr[i+1]!=tar:
arr[i], arr[i+1] = arr[i+1], arr[i]
else:
arr[i] = arr[i+1] = 'B' if tar=='W' else 'W'
res.append(i+1)
print(len(res))
print(*res)
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 | import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main implements Runnable {
static Scanner in;
// static FastReader in;
static PrintWriter out;
static int[][] dirs8 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}};
static int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static int[][] E;
// static List<int[]>[] adj;
static List<Integer>[] adj;
static Map<String, Integer> map;
static int[] vis;
static long INFL = (long) 1e15 + 7;
static int INF = (int) 1e7 + 9;
static int T, n, m, k, count;
static boolean flag;
static int[] a;
static int[][] dp;
static void solve() {
n = in.nextInt();
a = new int[n];
String input = in.next();
for (int i = 0; i < n; i++) {
a[i] = (input.charAt(i) == 'W' ? 0 : 1);
count += a[i];
}
int d = count - (n - count);
int res;
if (Math.abs(d - n) % 4 == 0) {res = 1;}
else if (Math.abs(d + n) % 4 == 0) {res = 0;}
else {out.println(-1); return;}
count = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
if (a[i] != res) {a[i] ^= 1; a[i + 1] ^= 1; list.add(i + 1); count++;}
}
out.println(count);
for (Integer i : list) {out.print(i + " ");}
out.println();
}
static class Node {
Node left, right, par;
int val, idx;
int size = 1;
Node() {}
Node(int i, int c) {idx = i; val = c;}
}
public static void main(String[] args) throws Exception{
in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// in = new FastReader();
out = new PrintWriter(System.out);
// int T = in.nextInt();
// for (int tt = 1; tt <= T; tt++) {
// Thread t = new Thread(null, new Main(), "Main", 1 << 28);
// t.start();
// t.join();
// // solve();
// }
// Thread t = new Thread(null, new Main(), "Main", 1 << 28);
// t.start();
// t.join();
solve();
out.flush();
out.close();
}
@Override
public void run() {solve();}
static class FastReader {
public 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;
}
}
}
| 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>
const long long MOD = 1e9 + 7;
using namespace std;
void rev(char &c) {
if (c == 'W')
c = 'B';
else
c = 'W';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
bool b = 1;
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
b = 0;
break;
}
}
if (b) {
cout << 0 << '\n';
exit(0);
}
vector<int> res;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
rev(s[i]);
rev(s[i + 1]);
res.push_back(i);
}
}
b = 1;
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
b = 0;
break;
}
}
if (b) {
cout << res.size() << '\n';
for (int i = 0; i < res.size(); i++) cout << res[i] + 1 << ' ';
exit(0);
}
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
rev(s[i]);
rev(s[i + 1]);
res.push_back(i);
}
}
b = 1;
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
b = 0;
break;
}
}
if (b) {
cout << res.size() << '\n';
for (int i = 0; i < res.size(); i++) cout << res[i] + 1 << ' ';
exit(0);
} else
cout << -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 | n = int(input())
a = input()
def go(s, x):
b = [1 if c == 'B' else 0 for c in s]
o = []
n = len(b)
for i in range(n):
if b[i] != x:
if i + 1 >= n:
return 0
o += i + 1,
b[i + 1] = b[i + 1] ^ 1
print(len(o))
print(' '.join(map(str, o)))
return 1
if not go(a, 0) and not go(a, 1):
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.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
public class Solution
{
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;
}
}
public static String sortString(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
public static void main (String[] args)throws IOException
{
FastReader in=new FastReader();
int t=1;
int i,j;
while(t-->0)
{
int n=in.nextInt();
String s=in.next();
int a=0,b=0,k=0;
for(i=0;i<n;i++)
{
if(s.charAt(i)=='B')
a++;
}
b=n-a;
ArrayList<Integer>x=new ArrayList<Integer>();
char ar[]=s.toCharArray();
if(b%2!=0 && a%2!=0)
System.out.println(-1);
else
{
if(a%2==0 && b%2!=0)// B is even and W is odd, so B -> W conversion
{
for(i=0;i<n-1;i++)
{
if(ar[i]=='B' && ar[i+1]=='W')
{
ar[i]='W';
ar[i+1]='B';
k++;
x.add(i+1);
}
else if(ar[i]=='B' && ar[i+1]=='B')
{
ar[i]='W';
ar[i+1]='W';
k++;
x.add(i+1);
}
else
continue;
}
}
else // W is even and B is odd or both are even, so W -> B conversion
{
for(i=0;i<n-1;i++)
{
if(ar[i]=='W' && ar[i+1]=='B')
{
ar[i]='B';
ar[i+1]='W';
k++;
x.add(i+1);
}
else if(ar[i]=='W' && ar[i+1]=='W')
{
ar[i]='B';
ar[i+1]='B';
k++;
x.add(i+1);
}
else
continue;
}
}
System.out.println(k);
for(i=0;i<x.size();i++)
System.out.print(x.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 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int mod = 998244353;
const double PI = acos(-1);
int main() {
int n;
cin >> n;
string s;
cin >> s;
int c = 0;
vector<int> v;
for (int i = (0); i < (n - 1); i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
v.push_back(i + 1);
c += 1;
} else if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
v.push_back(i + 1);
c++;
}
}
if (s[n - 1] == 'B') {
if (n % 2 == 0) {
cout << -1;
return 0;
} else {
for (int i = 0; i < n - 1; i += 2) {
v.push_back(i + 1);
c++;
}
}
}
cout << c << endl;
for (auto i : v) {
cout << 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 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
char s[300];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
int w = 0, b = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (w % 2 == 1 && b % 2 == 1)
printf("-1\n");
else if (w == 0 || b == 0) {
printf("0\n");
} else {
int d[300];
memset(d, 0, sizeof(d));
int op = b % 2 == 0 ? 1 : 2;
int tot = 0;
for (int i = 0; i + 1 < n; i++) {
if (op == 1 && s[i] == 'B') {
s[i] = 'W', s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B', d[tot++] = i + 1;
} else if (op == 2 && s[i] == 'W') {
s[i] = 'B', s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W', d[tot++] = i + 1;
}
}
printf("%d\n", tot);
for (int i = 0; i < tot; i++) {
if (i == 0)
printf("%d", d[i]);
else
printf(" %d", d[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 | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
String s = sc.next();
int m = 0;
for(int i = 0;i<n;i++){
if(s.charAt(i)=='B'){
m+=1;
}
}
// System.out.println("m" + m + "n" + n);
if(((m%2)==0)||((m%2)==(n%2))){
int a[] = new int[n];
if((m%2)==(n%2)){
// System.out.println("option 1");
for(int i = 0;i<n;i++){
if(s.charAt(i)=='B'){
a[i]=0;
}
else{
a[i]=1;
}
}
}
else{
// System.out.println("option 2");
for(int i = 0;i<n;i++){
if(s.charAt(i)=='W'){
a[i]=0;
}
else{
a[i]=1;
}
}
}
ArrayList<Integer> ops = new ArrayList<Integer>();
int j = 0;
while(j<n-1){
if(a[j]==0){
j++;
continue;
}
else{
ops.add(j);
a[j]=0;
a[j+1] = (a[j+1]+1)%2;
j++;
}
}
int k = ops.size();
System.out.println(k);
for(int c : ops){
System.out.print((c+1) + " " );
}
System.out.println();
}
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 | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
public static void main(String[] args) {
// int test = fs.nextInt();
int test = 1;
for (int cases = 0; cases < test; cases++) {
int n = fs.nextInt();
String s = fs.next();
boolean ans1 = false;
boolean ans2 = false;
StringBuilder ss = new StringBuilder(s);
ArrayList<Integer> ops1 = new ArrayList<Integer>();
ArrayList<Integer> ops2 = new ArrayList<Integer>();
for (int i = 0; i < ss.length() - 1; i++) {
if (ss.charAt(i) == 'W') {
continue;
} else {
ss.setCharAt(i, 'W');
ops1.add(i + 1);
if (ss.charAt(i + 1) == 'W') {
ss.setCharAt(i + 1, 'B');
} else {
ss.setCharAt(i + 1, 'W');
}
}
}
if (ss.charAt(n - 1) == 'W') {
ans1 = true;
} else {
StringBuilder sss = new StringBuilder(s);
for (int i = 0; i < sss.length() - 1; i++) {
if (sss.charAt(i) == 'B') {
continue;
} else {
sss.setCharAt(i, 'B');
ops2.add(i + 1);
if (sss.charAt(i + 1) == 'W') {
sss.setCharAt(i + 1, 'B');
} else {
sss.setCharAt(i + 1, 'W');
}
}
}
if (sss.charAt(n - 1) == 'B') {
ans2 = true;
}
}
if (ans1) {
op.print(ops1.size() + "\n");
if (!ops1.isEmpty()) {
for (Integer integer : ops1) {
op.print(integer + " ");
}
}
} else if (ans2) {
op.print(ops2.size() + "\n");
if (!ops2.isEmpty()) {
for (Integer integer : ops2) {
op.print(integer + " ");
}
}
} else {
op.print(-1);
}
op.flush();
}
}
/*
* array list
*
* ArrayList<Integer> al=new ArrayList<>(); creating BigIntegers
*
* BigInteger a=new BigInteger(); BigInteger b=new BigInteger();
*
* hash map
*
* HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int
* i=0;i<ar.length;i++) { Integer c=hm.get(ar[i]); if(hm.get(ar[i])==null) {
* hm.put(ar[i],1); } else { hm.put(ar[i],++c); } }
*
* while loop
*
* int t=sc.nextInt(); while(t>0) { t--; }
*
* array input
*
* int n = sc.nextInt(); int ar[] = new int[n]; for (int i = 0; i < ar.length;
* i++) { ar[i] = sc.nextInt(); }
*/
static class Graph {
HashMap<Character, LinkedList<Character>> hm = new HashMap<Character, LinkedList<Character>>();
private void addVertex(char vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(char source, char dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean isCyclicUtil(char ch, boolean visited[], char parent, HashMap<Character, Integer> index) {
visited[index.get(ch)] = true;
Iterator<Character> it = hm.get(ch).iterator();
while (it.hasNext()) {
Character character = (Character) it.next();
if (!visited[index.get(character)]) {
if (isCyclicUtil(character, visited, ch, index))
return true;
} else if (character != ch)
return true;
}
return false;
}
private boolean isCyclic(HashMap<Character, Integer> index) {
boolean visited[] = new boolean[hm.size()];
Set<Character> set = hm.keySet();
for (Character character : set) {
if (!visited[index.get(character)]) {
if (isCyclicUtil(character, visited, '-', index))
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
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 class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
for (int i : ar) {
op.print(i + " ");
}
op.flush();
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
//
// function to find largest prime factor
}/*
* 1 5 -2 -3 -1 -4 -6till here
*/
// while (t > 0) {
// int a[] = getintarray(3);
// int b[] = getintarray(3);
// int ans = getans(a, b);
// System.out.println(ans);
// } | 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.math.*;
import java.util.*;
public class blocksthatarebw {
public static void main(String[] args) throws IOException {
FastScanner scan = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(scan.nextToken());
char[] blocks = new char[n];
String s = scan.nextToken();
for (int i = 0; i < n; i++) {
blocks[i] = s.charAt(i);
}
long ans = 0;
String answer = "";
for (int i = 0; i < n-1; i++) {
if (blocks[i] != 'W') {
blocks[i] = f(blocks[i]);
blocks[i+1] = f(blocks[i+1]);
if (ans != 0) {
answer += " " + (i+1);
}
else {
answer += "" + (i+1);
}
ans++;
}
//System.out.println(blocks[i]);
}
if (blocks[n-1] == 'W') {
out.println(ans);
out.println(answer);
}
else {
for (int i = 0; i < n; i++) {
blocks[i] = s.charAt(i);
}
ans = 0;
answer = "";
for (int i = 0; i < n-1; i++) {
if (blocks[i] != 'B') {
blocks[i] = f(blocks[i]);
blocks[i+1] = f(blocks[i+1]);
if (ans != 0) {
answer += " " + (i+1);
}
else {
answer += "" + (i+1);
}
ans++;
}
}
if (blocks[n-1] == 'B') {
out.println(ans);
out.println(answer);
}
else {
out.println(-1);
}
}
out.close();
}
public static char f(char c) {
if (c == 'B')
return 'W';
return 'B';
}
public static void shuffle(int[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int rPos = rgen.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[rPos];
arr[rPos]=temp;
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
st = null;
try {
return br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | 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 |
// template : secondThread
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class main2 {
public static void main(String[] args) {
try {
PrintWriter fop = new PrintWriter(System.out);
FastScanner fsca = new FastScanner();
int n = fsca.nextInt();
String s = fsca.next();
char A[] = s.toCharArray() ;
char B[] = s.toCharArray() ;
int ct = 0 ;
StringBuilder sb = new StringBuilder() ;
// white conversion
for (int i = 0; i <n-1 ; i++) {
if (A[i] != 'W'){
A[i] = 'W' ;
ct++ ;
sb.append((i+1)+ " ") ;
if (A[i+1] == 'W')
A[i+1] = 'B' ;
else A[i+1] = 'W' ;
}
}
if (A[n-1] == 'W'){
System.out.println(ct);
if (ct != 0)
System.out.println(sb);
return;
}
ct = 0 ;
StringBuilder sb1 = new StringBuilder() ;
for (int i = 0; i <n-1 ; i++) {
if (B[i] != 'B'){
B[i] = 'B' ;
ct++ ;
sb1.append((i+1) + " ") ;
if (B[i+1] == 'W')
B[i+1] = 'B' ;
else B[i+1] = 'W' ;
}
}
if (B[n-1] == 'B'){
System.out.println(ct);
if (ct != 0)
System.out.println(sb1);
return;
}
System.out.println(-1);
fop.flush();
fop.close();
} catch (Exception e) {
return;
}
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
static void sieve(int n) {
boolean[] flag = new boolean[n];
for (int i = 2; i * i < n; i++) {
if (flag[i])
continue;
else
for (int j = i * i; j <= n; j += i) {
flag[j] = true;
}
}
}
static int gcd(int a, int b) {
if (b > a) {
int tenp = b;
b = a;
a = tenp;
}
int temp = 0;
while (b != 0) {
a %= b;
temp = b;
b = a;
a = temp;
}
return a;
}
static long gcd_long(long a, long b) {
if (b > a) {
long tenp = b;
b = a;
a = tenp;
}
long temp = 0;
while (b != 0) {
a %= b;
temp = b;
b = a;
a = temp;
}
return a;
}
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(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.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Stack;
import javax.swing.event.ListSelectionEvent;
public class ROUGH{
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception r) {
r.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception r) {
r.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static int mod = (int) (1e9+7);
static long cf = 998244353;
static final long MAX = (long) 1e15;
public static List<Integer>[] edges;
public static int[][] parent;
public static int col = 32;
public static int[] Bit;
public static void main(String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
char[] s = sc.next().toCharArray();
boolean[] a = new boolean[n];
for(int i=0;i<s.length;++i) if(s[i] == 'W') a[i] = true;
List<Integer> ans = new ArrayList<>();
boolean[] b = a.clone();
int op = solve(a,true,ans);
if(op <= 3*n && check(a,true)) {
out.println(ans.size());
for(int x : ans) out.print(x+" ");
}else {
ans.clear();
op = solve(b,false,ans);
if(op <= 3*n && check(b,false)) {
out.println(ans.size());
for(int x : ans) out.print(x+" ");
}else out.print(-1);
}
out.close();
}
private static boolean check(boolean[] a, boolean b) {
for(boolean x : a) if(x != b) return false;
return true;
}
private static int solve(boolean[] a, boolean b, List<Integer> ans) {
// TODO Auto-generated method stub
int x = 0;
for(int i=0;i+1<a.length;++i) {
if(a[i] != b) {
++x;
a[i] = !a[i];
a[i+1] = !a[i+1];
ans.add(i+1);
}
}
return x;
}
} | 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 = 1000000007;
using pii = pair<long long, long long>;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
string b;
cin >> b;
string a = b;
vector<long long> ans;
for (long long i = 0; i + 1 < n; i++) {
if (a[i] == 'W') {
a[i + 1] = a[i + 1] == 'W' ? 'B' : 'W';
ans.push_back(i + 1);
}
}
if (a[n - 1] == 'W') {
ans.clear();
a = b;
for (long long i = 0; i + 1 < n; i++) {
if (a[i] == 'B') {
a[i + 1] = a[i + 1] == 'W' ? 'B' : 'W';
ans.push_back(i + 1);
}
}
if (a[n - 1] == 'B') {
cout << "-1\n";
return 0;
}
}
cout << ans.size() << "\n";
for (long long x : ans) cout << x << " ";
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int w = 0;
int b = 0;
int c = 0;
int f = 0;
vector<int> vec;
for (int i = 0; i < n; i++) {
if (s[i] == 'B') {
b++;
} else {
w++;
}
}
if (w % 2 != 0 && b % 2 != 0) {
cout << -1 << endl;
f = 1;
} else if (w % 2 == 0) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
vec.push_back(i + 1);
if (s[i] == 'B') {
s[i] = 'W';
} else {
s[i] = 'B';
}
if (s[i + 1] == 'B') {
s[i + 1] = 'W';
} else {
s[i + 1] = 'B';
}
c++;
}
}
} else {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
vec.push_back(i + 1);
if (s[i] == 'B') {
s[i] = 'W';
} else {
s[i] = 'B';
}
if (s[i + 1] == 'B') {
s[i + 1] = 'W';
} else {
s[i + 1] = 'B';
}
c++;
}
}
}
if (f == 0) {
if (c == 0) {
cout << c << endl;
} else {
cout << c << endl;
for (int i = 0; i < c - 1; i++) {
cout << vec[i] << " ";
}
cout << vec[c - 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 | def main():
N = int(input())
P = list(map(lambda x: 0 if x == "W" else 1, input()))
for c in range(2):
PP = P.copy()
res = []
for i in range(N-1):
if PP[i] != c:
res.append(i+1)
PP[i+1] = 1 - PP[i+1]
if PP[N-1] == c:
print(len(res))
print(" ".join(str(i) for i in res))
return
print("-1")
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 | import java.io.*;
import java.util.*;
public class B {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String st = sc.nextLine();
char s1[] = st.toCharArray();
char s2[] = new char[st.length()];
s2 = s1.clone();
ArrayList<Integer> ans = new ArrayList<Integer>();
for(int i=0;i<(n-1);i++) {
if(s1[i]=='B') {
ans.add(i+1);
s1[i]='W';
if(s1[i+1]=='B') {
s1[i+1]='W';
}
else {
s1[i+1]='B';
}
}
}
Boolean flag=true;
for(int i=0;i<n;i++) {
if(s1[i]!='W') {
flag=false;
}
}
if(flag) {
System.out.println(ans.size());
ListIterator<Integer> it = ans.listIterator();
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
return;
}
ans.clear();
for(int i=0;i<(n-1);i++) {
if(s2[i]=='W') {
ans.add(i+1);
s2[i]='B';
if(s2[i+1]=='W') {
s2[i+1]='B';
}
else {
s2[i+1]='W';
}
}
}
flag=true;
for(int i=0;i<n;i++) {
if(s2[i]!='B') {
flag=false;
}
}
if(flag) {
System.out.println(ans.size());
ListIterator<Integer> it = ans.listIterator();
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
return;
}
System.out.print(-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=input().strip()
if s.count('W')==0 or s.count('B')==0:
print(0)
elif s.count('W')%2==1 and s.count('B')%2==1:
print(-1)
else:
if s.count('B')%2==0:
x=[]
for i in range(n):
if s[i]=='B':
x.append(i)
an=[]
for i in range(0,len(x),2):
for j in range(x[i],x[i+1]):
an.append(j+1)
print(len(an))
print(*an)
else:
x = []
for i in range(n):
if s[i] == 'W':
x.append(i)
an = []
for i in range(0, len(x), 2):
for j in range(x[i], x[i + 1]):
an.append(j + 1)
print(len(an))
print(*an) | 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())
d={'B':'W','W':'B'}
ans=[]
for i in range(1,len(l)-1):
if l[i-1]!=l[i]:
l[i]=d[l[i]]
l[i+1]=d[l[i+1]]
ans.append(i+1)
f=0
if l[-2]!=l[-1]:
if n&1:
for i in range(n-2,0,-2):
ans.append(i)
else:
f=1
print(-1)
if f==0:
if not ans:
print(0)
else:
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 | import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class BTask {
private static final String QUICK_ANSWER = "NO";
private final Input in;
private final StringBuilder out;
public BTask(Input in, StringBuilder out) {
this.in = in;
this.out = out;
}
public void solve() throws QuickAnswer {
int n = nextInt();
char[] blocks = nextString().toCharArray();
boolean[] b = new boolean[n];
for (int i = 0; i < n; i++) {
b[i] = blocks[i] == 'W';
}
int cnt = 0;
for (boolean bb : b) {
if (bb) ++cnt;
}
if (n % 2 == 0 && cnt % 2 == 1) {
print(-1);
return;
}
boolean res = cnt % 2 == 1;
List<Integer> result = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
if (b[i] == res) continue;
result.add(i + 1);
b[i+1] = !b[i+1];
}
println(result.size());
for (int i : result) {
printsp(i);
}
}
// Common functions
private static class Input {
private final BufferedReader in;
private StringTokenizer tokenizer = null;
public Input(BufferedReader in) {
this.in = in;
}
String nextToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(in.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
return "";
}
}
}
void quickAnswer(String answer) throws QuickAnswer {
throw new QuickAnswer(answer);
}
void quickAnswer() throws QuickAnswer {
quickAnswer(QUICK_ANSWER);
}
static class QuickAnswer extends Exception {
private String answer;
public QuickAnswer(String answer) {
this.answer = answer;
}
}
void print(Object... args) {
String prefix = "";
for (Object arg : args) {
out.append(prefix);
out.append(arg);
prefix = " ";
}
}
void println(Object... args) {
print(args);
out.append("\n");
}
void printsp(Object... args) {
print(args);
out.append(" ");
}
int nextInt() {
return in.nextInt();
}
long nextLong() {
return in.nextLong();
}
String nextString() {
return in.nextLine();
}
int[] nextInts(int count) {
int[] res = new int[count];
for (int i = 0; i < count; ++i) {
res[i] = nextInt();
}
return res;
}
int[][] nextInts(int count, int n) {
int[][] res = new int[n][count];
for (int i = 0; i < count; ++i) {
for (int j = 0; j < n; j++) {
res[j][i] = nextInt();
}
}
return res;
}
long[] nextLongs(int count) {
long[] res = new long[count];
for (int i = 0; i < count; ++i) {
res[i] = nextLong();
}
return res;
}
long[][] nextLongs(int count, int n) {
long[][] res = new long[n][count];
for (int i = 0; i < count; ++i) {
for (int j = 0; j < n; j++) {
res[j][i] = nextLong();
}
}
return res;
}
public static void main(String[] args) {
doMain(System.in, System.out);
}
static void doMain(InputStream inStream, PrintStream outStream) {
Input in = new Input(new BufferedReader(new InputStreamReader(inStream)));
StringBuilder totalOut = new StringBuilder();
int count = 1;
//count = in.nextInt();
while (count-- > 0) {
try {
StringBuilder out = new StringBuilder();
new BTask(in, out).solve();
totalOut.append(out.toString());
} catch (QuickAnswer e) {
totalOut.append(e.answer);
}
if (count > 0) {
totalOut.append("\n");
}
}
outStream.print(totalOut.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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sparsh Sanchorawala
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BBlocks solver = new BBlocks();
solver.solve(1, in, out);
out.close();
}
static class BBlocks {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int n = s.nextInt();
String str = s.next();
char[] a = str.toCharArray();
ArrayList<Integer> res = new ArrayList<>();
int i = 0;
while (i < n - 1) {
if (a[i] == 'W' && a[i + 1] == 'W') {
res.add(i);
a[i] = 'B';
a[i + 1] = 'B';
i += 2;
} else if (a[i] == 'W' && a[i + 1] == 'B') {
res.add(i);
a[i] = 'B';
a[i + 1] = 'W';
i += 1;
} else
i++;
}
boolean check = true;
for (int j = 0; j < n; j++)
if (a[j] == 'W')
check = false;
if (check) {
w.println(res.size());
for (int j : res)
w.print((j + 1) + " ");
w.println();
return;
}
res.clear();
a = str.toCharArray();
i = 0;
while (i < n - 1) {
if (a[i] == 'B' && a[i + 1] == 'B') {
res.add(i);
a[i] = 'W';
a[i + 1] = 'W';
i += 2;
} else if (a[i] == 'B' && a[i + 1] == 'W') {
res.add(i);
a[i] = 'W';
a[i + 1] = 'B';
i += 1;
} else
i++;
}
check = true;
for (int j = 0; j < n; j++)
if (a[j] == 'B')
check = false;
if (check) {
w.println(res.size());
for (int j : res)
w.print((j + 1) + " ");
w.println();
return;
}
w.println(-1);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(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 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 String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextString();
}
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 | n=int(input())
s=list(input().strip())
b=s.count('B')
w=s.count('W')
res=[]
if(b%2 and w%2):
print(-1)
elif(b%2==0):
i=n-1
while i-1>=0:
#print(i,s[i])
if(s[i]=='W'):
i-=1
elif(s[i]=='B' and s[i-1]=='B'):
res.append(i)
s[i]='W'
s[i-1]='W'
i-=2
else:
res.append(i)
s[i]='W'
s[i-1]='B'
i-=1
print(len(res))
if(len(res)):
print(*res)
else:
i=n-1
while i-1>=0:
if(s[i]=='B'):
i-=1
elif(s[i]=='W' and s[i-1]=='W'):
res.append(i)
s[i]='B'
s[i-1]='B'
i-=2
else:
res.append(i)
s[i]='B'
s[i-1]='W'
i-=1
print(len(res))
if(len(res)):
print(*res)
| 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, ans;
cin >> s;
ans = s;
vector<int> v;
for (int i = 0; i < ans.length() - 1; i++) {
if (ans[i] != 'B') {
v.push_back(i + 1);
ans[i] = 'B';
if (ans[i + 1] == 'B')
ans[i + 1] = 'W';
else
ans[i + 1] = 'B';
}
}
int f = 0;
for (int i = 0; i < ans.length() - 1; i++) {
if (ans[i] != ans[i + 1]) {
f = 1;
break;
}
}
if (f == 0) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
} else {
ans = s;
int y = 0;
vector<int> x;
for (int i = 0; i < (ans.length() - 1); i++) {
if (ans[i] != 'W') {
x.push_back(i + 1);
ans[i] = 'W';
if (ans[i + 1] == 'W')
ans[i + 1] = 'B';
else
ans[i + 1] = 'W';
}
}
for (int i = 0; i < ans.length() - 1; i++) {
if (ans[i] != ans[i + 1]) {
y = 1;
break;
}
}
if (y == 0) {
cout << x.size() << endl;
for (int i = 0; i < x.size(); i++) cout << x[i] << " ";
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 | import java.io.*;
import java.util.*;
public class HelloWorld
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'B') ls.add(i+1);
}
if(ls.size() == 0) writer.println(0);
else if(ls.size()%2 != 0)
{
ls = new ArrayList<Integer>();
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'W') ls.add(i+1);
}
if(ls.size() == 0) writer.println(0);
else if(ls.size()%2 != 0) writer.println(-1);
else
{
ArrayList<Integer> ls1 = new ArrayList<>();
for(int i = 0; i < ls.size()-1; i += 2)
{
for(int j = ls.get(i+1)-1; j >= ls.get(i); j--)
{
ls1.add(j);
}
}
writer.println(ls1.size());
for(int i = 0; i < ls1.size(); i++) writer.print(ls1.get(i)+" ");
}
}
else
{
ArrayList<Integer> ls1 = new ArrayList<>();
for(int i = 0; i < ls.size()-1; i += 2)
{
for(int j = ls.get(i+1)-1; j >= ls.get(i); j--)
{
ls1.add(j);
}
}
writer.println(ls1.size());
for(int i = 0; i < ls1.size(); i++) writer.print(ls1.get(i)+" ");
}
br.close();
writer.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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Blocks {
public static String opp(String s) {
if (s.equals("B"))
return "W";
else
return "B";
}
public static void main(String[] args) throws IOException {
int n, wc = 0, bc = 0;
int count=0;
String[] b;
String s = "";
List<Integer> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
b = br.readLine().split("");
for (String value : b) {
if (value.equals("W"))
wc++;
else
bc++;
}
if (bc % 2 == 1 && wc % 2 == 1)
System.out.println(-1);
else if (bc == 0 || wc == 0)
System.out.println(0);
else {
if (bc % 2 == 0 && wc % 2 == 0) {
if (bc < wc)
s = "B";
else
s = "W";
} else if (bc % 2 == 0)
s = "B";
else
s = "W";
for (int i = 0; i < b.length - 1; i++) {
if (b[i].equals(s)) {
b[i] = opp(b[i]);
b[i+1] = opp(b[i+1]);
count++;
list.add(i+1);
}
}
System.out.println(count);
System.out.println(list.toString().replace(",","").replace("[","").replace("]",""));
}
}
}
| 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.*;
public class practice {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
int n=scn.nextInt();
scn.nextLine();
String s=scn.nextLine();
int white=0,black=0;
for(int i=0;i<n;i++) {
char ch=s.charAt(i);
if(ch=='W') {
white++;
}else {
black++;
}
}
if((white%2!=0 && black%2!=0)) {
System.out.println("-1");
}else {
char ch='B';
if((white%2 )!=0) {
ch='W';
}
int back=doit(ch,n,s);
}
}
public static int doit(char ch,int n,String s) {
int count=0;
StringBuilder sb=new StringBuilder("");
Stack<Integer>stk=new Stack();
for(int i=0;i<n;i++) {
if(s.charAt(i)!=ch) {
if(!stk.isEmpty()) {
int left=stk.pop();
count+=i-left;
for(int j=i-1;j>=left;j--) {
sb.insert(0,((j+1)+" "));
}
}
else {
stk.push(i);
}
}
}
System.out.println(count);
System.out.println(sb);
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.io.*;
import java.util.*;
public final class code
// class code
// public class Solution
{
static void solve()throws IOException
{
int n=nextInt();
char s[]=(" "+nextLine()).toCharArray();
int w=0,b=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='B')
b++;
else
w++;
}
if(w%2!=0 && b%2!=0)
{
out.println(-1);
return;
}
char x=w%2==0?'W':'B';
ArrayList<Integer> list=new ArrayList<>();
for(int i=1;i<=n;i++)
{
if(i!=n && s[i]==x)
{
list.add(i);
s[i]=(char)(s[i]^'W'^'B');
s[i+1]=(char)(s[i+1]^'W'^'B');
}
}
out.println(list.size());
for(int i:list)
out.print(i+" ");
}
///////////////////////////////////////////////////////////
public static void main(String args[])throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(new BufferedOutputStream(System.out));
random=new Random();
solve();
// int t=nextInt();
// for(int i=1;i<=t;i++)
// {
// // out.print("Case #"+i+": ");
// solve();
// }
out.close();
}
static final long mod=(long)(1e9+7);
static final int inf=(int)(1e9+1);
// static final long inf=(long)(1e18);
static class Pair implements Comparable<Pair>
{
int first,second;
Pair(int a,int b)
{
first=a;
second=b;
}
public int compareTo(Pair p)
{
return this.second-p.second;
}
public boolean equals(Object p)
{
Pair p1=(Pair)p;
return (first==p1.first && second==p1.second);
}
public String toString()
{
return this.first+" "+this.second;
}
public int hashCode()
{
return (int)((1l*(inf+1)*this.first+this.second)%mod);
}
}
static int max(int ... a)
{
int ret=a[0];
for(int i=1;i<a.length;i++)
ret=Math.max(ret,a[i]);
return ret;
}
static void debug(Object ... a)
{
System.out.print("> ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void debug(int a[]){debuga(Arrays.stream(a).boxed().toArray());}
static void debug(long a[]){debuga(Arrays.stream(a).boxed().toArray());}
static void debuga(Object a[])
{
System.out.print("> ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static Random random;
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken()throws IOException
{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static String nextLine()throws IOException
{
return br.readLine();
}
static int nextInt()throws IOException
{
return Integer.parseInt(nextToken());
}
static long nextLong()throws IOException
{
return Long.parseLong(nextToken());
}
static double nextDouble()throws IOException
{
return Double.parseDouble(nextToken());
}
} | 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 | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
/* Name of the class has to be "Main" only if the class is public. */
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;
}
}
public static int findB(String s, ArrayList<Integer> index, int ind){
if(s.length()==0){
return 0;
}
if(s.charAt(0)=='B')
return findB(s.substring(1), index, ind+1);
if(s.length()==1){
return -1;
}
if(s.charAt(1)=='B'){
int ans = findB("W"+s.substring(2), index, ind+1);
if(ans!=-1){
index.add(ind+1);
return ans+1;
}
else return ans;
}
else{
int ans = findB("B"+s.substring(2), index, ind+1);
if(ans!=-1){
index.add(ind+1);
return ans+1;
}
else return ans;
}
}
public static int findW(String s, ArrayList<Integer> index, int ind){
if(s.length()==0){
return 0;
}
if(s.charAt(0)=='W')
return findW(s.substring(1), index, ind+1);
if(s.length()==1){
return -1;
}
if(s.charAt(1)=='W'){
int ans = findW("B"+s.substring(2), index, ind+1);
if(ans!=-1){
index.add(ind+1);
return ans+1;
}
else return ans;
}
else{
int ans =findW("W"+s.substring(2), index, ind+1);
if(ans!=-1){
index.add(ind+1);
return ans+1;
}
else return ans;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
FastReader s = new FastReader();
PrintWriter p = new PrintWriter(System.out);
int n = s.nextInt();
String str = s.next();
ArrayList<Integer> arr1 = new ArrayList<Integer>();
int op1 = findB(str, arr1, 0);
ArrayList<Integer> arr2 = new ArrayList<Integer>();
int op2 = findW(str, arr2, 0);
//p.println(op1);
if(op1!=-1&&op2!=-1){
if(op1>op2){
p.println(op2);
for(int j:arr2){
p.print(j+" ");
}
}
else{
p.println(op1);
for(int j:arr1){
p.print(j+" ");
}
}
}
else if(op1!=-1){
p.println(op1);
for(int j:arr1){
p.print(j+" ");
}
}
else if(op2!=-1){
p.println(op2);
for(int j:arr2){
p.print(j+" ");
}
}
else p.println(-1);
// String str1 = str;
// for(;j<n;){
// if(str1.charAt(j)=="B"){
// j++;
// }
// else{
// if(j+1<n){
// op1++;
// arr.add(j);
// if(str1.charAt(j+1)=="W"){
// str1 = str1.substring(0, j) + "BB" + str1.substring(j+2);
// }
// else{
// str1 = str1.substring(0, j) + "BW" + str1.substring(j+2);
// }
// }
// else op1 = -1;
// }
// }
p.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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static int check(char[] ch) {
int B = 0, W = 0;
for (int i = 0; i < ch.length; i++)
if (ch[i] == 'B') B++;
else W++;
if (B == 0 || W == 0) return 1;
return 0;
}
public static void main(String[] args) {
FastReader fs = new FastReader();
int a = fs.nextInt();
String s = fs.next();
char[] ch = s.toCharArray();
int l = s.length();
int B = 0, W = 0;
for (int i = 0; i < l; i++) {
if (s.charAt(i) == 'B') B++;
else W++;
}
if (W == 0 || B == 0) System.out.println(0);
else if (B % 2 != 0 && W % 2 != 0) System.out.println(-1);
else {
int cnt1 = 0;
List<Integer> list1 = new ArrayList<>();
int z = 0;
while (check(ch) != 1) {
z++;
if (z > 50) break;
for (int i = 0; i < l; i++) {
if (ch[i] == 'W' && i != l - 1) {
ch[i] = 'B';
if (ch[i + 1] == 'B') ch[i + 1] = 'W';
else ch[i + 1] = 'B';
cnt1++;
list1.add(i + 1);
}
}
}
while (check(ch) != 1) {
for (int i = 0; i < l; i++) {
if (ch[i] == 'B' && i != l - 1) {
ch[i] = 'W';
if (ch[i + 1] == 'B') ch[i + 1] = 'W';
else ch[i + 1] = 'B';
cnt1++;
list1.add(i + 1);
}
}
}
System.out.println(cnt1);
for (int i = 0; i < cnt1; i++) {
if (i != 0) System.out.print(" ");
System.out.print(list1.get(i));
}
}
}
public 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;
}
}
}
| 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.*;
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 && w % 2 != 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';
// System.out.println(s);
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) + " ");
}
}
} else {
for (int i = 0; i < n; i++) {
if ( ch[i]== 'W') {
ch[i]='B';
ch[i+1]='B';
st.append((i + 1) + " ");
moves++;
i++;
}
}
}
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 | #include <bits/stdc++.h>
using namespace std;
long long n, arr[200 + 5];
vector<long long> ans;
string s;
bool tf = 1;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> s;
for (long long i = 0; i < n; i++) {
if (s[i] == 'B')
arr[i] = 0;
else
arr[i] = 1;
}
for (long long i = 1; i < n - 1; i++) {
if (arr[i] != arr[i - 1]) {
arr[i] = !arr[i];
arr[i + 1] = !arr[i + 1];
ans.push_back(i + 1);
}
}
for (long long i = n - 2; i > 0; i--) {
if (arr[i] != arr[i + 1]) {
arr[i] = !arr[i];
arr[i - 1] = !arr[i - 1];
ans.push_back(i);
}
}
for (long long i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) tf = 0;
}
if (!tf)
cout << -1 << '\n';
else {
cout << ans.size() << '\n';
for (long long i : ans) {
cout << i << " ";
}
cout << '\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 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
map<char, char> m;
int a[N];
int main() {
int n, cnt = 0, t;
string s, s1;
cin >> n >> s;
m['B'] = 'W';
m['W'] = 'B';
s1 = s;
t = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
a[cnt++] = i + 1;
s[i] = m[s[i]];
s[i + 1] = m[s[i + 1]];
}
}
for (int i = 0; i < n; i++) {
if (s[i] == 'W') continue;
t = 1;
}
if (t == 0) {
cout << cnt << endl;
for (int i = 0; i < cnt; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
t = cnt = 0;
for (int i = 0; i < n - 1; i++) {
if (s1[i] == 'W') {
a[cnt++] = i + 1;
s1[i] = m[s1[i]];
s1[i + 1] = m[s1[i + 1]];
}
}
for (int i = 0; i < n; i++) {
if (s1[i] == 'B') continue;
t = 1;
}
if (t == 0) {
cout << cnt << endl;
for (int i = 0; i < cnt; i++) printf("%d ", a[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 | import java.util.ArrayList;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
char[] s = sc.next().toCharArray();
if(allSameCharacters(s)){
System.out.println(0);
return;
}
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n-1;i++){
if(s[i]=='B'){
s[i]='W';
if(s[i+1]=='W'){
s[i+1] = 'B';
}else {
s[i+1] = 'W';
}
list.add(i+1);
}
}
if(allSameCharacters(s)){
System.out.println(list.size());
for(int a: list){
System.out.printf(a+ " ");
}
}else {
for(int i=n-1;i>0;i--){
if(s[i]=='W'){
s[i]='B';
if(s[i-1]=='W'){
s[i-1] = 'B';
}else {
s[i-1] = 'W';
}
list.add(i);
}
}
if(allSameCharacters(s)){
System.out.println(list.size());
for(int a: list){
System.out.printf(a+ " ");
}
}else {
System.out.printf("-1");
}
}
}
private static boolean allSameCharacters(char[] s) {
for(int i=0;i<s.length-1;i++){
if(s[i]!=s[i+1]){
return false;
}
}
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 | n=int(input())
a=input().strip()
a1=list(a)
c1=0
c2=0
x=[]
c=0
for i in a:
if i=="B":
c1+=1
elif i=="W":
c2+=1
if c1==0 or c2==0 :
print(0)
elif c1%2==1 and c2%2==1:
print(-1)
elif c2>=c1:
for i in range(n-1):
if a1[i]=="W" and a1[i]==a1[i+1]:
a1[i]="B"
a1[i+1]="B"
c+=1
x.append(i+1)
if a1[i]!=a1[i+1] and a1[i]=="W":
a1[i]="B"
a1[i+1]="W"
x.append(i+1)
c+=1
if a1[n-1]!=a1[n-2]:
for jj in range(n-1,0,-1):
if jj%2:
x.append(jj)
c+=1
if a1[1]!=a1[0]:
for jj in range(1,n):
if jj%2==0:
x.append(jj)
c+=1
print(c)
for i in x:
print(i,end=" ")
elif c1>c2:
for i in range(n-1):
if a1[i]=="B" and a1[i]==a1[i+1]:
a1[i]="W"
a1[i+1]="W"
c+=1
x.append(i+1)
if a1[i]!=a1[i+1] and a1[i]=="B":
a1[i]="W"
a1[i+1]="B"
x.append(i+1)
c+=1
if a1[n-1]!=a1[n-2]:
for jj in range(n-1,0,-1):
if jj%2:
x.append(jj)
c+=1
if a1[1]!=a1[0]:
for jj in range(1,n):
if jj%2==0:
x.append(jj)
c+=1
print(c)
for i in x:
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 | /***
* βββββββ=====ββββββββ====ββββββββ====βββββββ=
* ββββββββ====ββββββββ====ββββββββ====ββββββββ
* βββ==βββ====ββββββ======ββββββ======ββββββββ
* βββ==βββ====ββββββ======ββββββ======βββββββ=
* ββββββββ====ββββββββ====ββββββββ====βββ=====
* βββββββ=====ββββββββ====ββββββββ====βββ=====
* ============================================
*/
import sun.font.DelegatingShape;
import sun.misc.IOUtils;
import sun.nio.cs.KOI8_U;
import javax.smartcardio.ATR;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.math.*;
import java.lang.*;
public class AA implements Runnable {
public void run() {
InputReader sc = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int i=0,j=0,k=0;
int t=0;
//t=sc.nextInt();
for(int testcase = 0;testcase < t; testcase++)
{
}
int n=sc.nextInt();
String str=sc.next();
char W='W';
char B='B';
ArrayList<Integer> wind=new ArrayList<>();
ArrayList<Integer> bind=new ArrayList<>();
for (i=0;i<n;i++)
{
if (str.charAt(i)=='W')
wind.add(i+1);
else
bind.add(i+1);
}
int bindsize=bind.size();
int windsize=wind.size();
if (bindsize%2==0)
{
if (bindsize==0)
out.println("0");
else
{
StringBuilder sb=new StringBuilder();
int count=0;
for (i=1;i<bindsize;i+=2)
{
count+=bind.get(i)-bind.get(i-1);
for (j=bind.get(i-1);j<bind.get(i);j++)
sb.append(j+" ");
}
out.println(count);
out.println(sb);
}
}
else if (windsize%2==0)
{
if (windsize==0)
out.println("0");
else
{
StringBuilder sb=new StringBuilder();
int count=0;
for (i=1;i<windsize;i+=2)
{
count+=wind.get(i)-wind.get(i-1);
for (j=wind.get(i-1);j<wind.get(i);j++)
sb.append(j+" ");
}
out.println(count);
out.println(sb);
}
}
else
{
if (n==Math.max(bindsize,windsize))
out.println("0");
else
out.println("-1");
}
//================================================================================================================================
out.flush();
out.close();
}
//================================================================================================================================
public static int[] sa(int n,InputReader sc)
{
int inparr[]=new int[n];
for (int i=0;i<n;i++)
inparr[i]=sc.nextInt();
return inparr;
}
public static long gcd(long a,long b){
return (a%b==0l)?b:gcd(b,a%b);
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
public int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public int countChar(String str, char c)
{
int count = 0;
for(int i=0; i < str.length(); i++)
{ if(str.charAt(i) == c)
count++;
}
return count;
}
static int binSearch(Integer[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static int binSearch(int[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
String get()
{
return a+" "+b;
}
String getrev()
{
return b+" "+a;
}
}
static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
static long factorial(long n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
//================================================================================================================================
static class InputReader
{
BufferedReader br;
StringTokenizer st;
public InputReader()
{
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;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new AA(),"Main",1<<27).start();
}
}
| 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.*;
public class Main {
public static void printAns(Vector<Integer> v) {
System.out.println(v.size());
for(Integer i : v)
System.out.print(i + " ");
}
public static void checkInvert(Vector<Integer> v, boolean[] a, int i, int j, int k) {
if(a[i] != a[j]) {
a[i] = !a[i];
a[k] = !a[k];
v.add(Math.min(i, k) + 1);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String s = scanner.next();
Vector<Integer> v = new Vector<Integer>();
boolean a[] = new boolean[n];
for(int i = 0; i < n; i++)
a[i] = (s.charAt(i) == 'B');
for(int i = 1; i < n - 1; i++)
checkInvert(v, a, i, i - 1, i + 1);
for(int i = n - 2; i > 0; i--)
checkInvert(v, a, i, i + 1, i - 1);
boolean ans = true;
for(int i = 1; i < n; i++)
if(a[i] != a[i - 1])
ans = false;
if(ans)
printAns(v);
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 | import java.io.*;
import java.util.*;
public class Main {
static int s, n, c;
static long[] mem;
static int[] arr;
static class pair implements Comparable<pair>{
int f;int s;
public pair(int ff,int ss){
f=ff;
s=ss;
}
@Override
public int compareTo(pair o) {
return s-o.s;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
long res = 1;
long gcd = gcd(a, b);
res *= a / gcd;
return res *= b;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st;
int n=Integer.parseInt(br.readLine());
char[]in=br.readLine().toCharArray();
char[]trying=new char[n];
for (int i = 0; i <n ; i++) {
trying[i]=in[i];
}
ArrayList<Integer>res=new ArrayList<>();
boolean valid=true;
for (int i = 0; i <n ; i++) {
if(trying[i]=='W')
continue;
if(i==n-1){
valid=false;
break;
}
res.add(i+1);
trying[i]='W';
if(trying[i+1]=='B')
trying[i+1]='W';
else trying[i+1]='B';
}
if(valid){
out.println(res.size());
for (int i = 0; i <res.size() ; i++) {
out.print(res.get(i));
if(i<n-1)
out.print(" ");
}
}
else{
valid=true;
for (int i = 0; i <n ; i++) {
trying[i]=in[i];
}
res.clear();
for (int i = 0; i <n ; i++) {
if(trying[i]=='B')
continue;
if(i==n-1){
valid=false;
break;
}
res.add(i+1);
trying[i]='B';
if(trying[i+1]=='B')
trying[i+1]='W';
else trying[i+1]='B';
}
if(valid){
out.println(res.size());
for (int i = 0; i <res.size() ; i++) {
out.print(res.get(i));
if(i<n-1)
out.print(" ");
}
}
else out.println(-1);
}
out.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| 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 | from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from sys import stdin,stdout
from collections import Counter
from math import ceil
from bisect import bisect_left
from bisect import bisect_right
import math
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def lessThanX(a, x):
i = bisect_left(a, x)
if i:
return (i-1)
else:
return -1
def BinarySearch(a, x):
i = bisect_right(a, x)
if i != len(a)+1 and a[i-1] == x:
return (i-1)
else:
return -1
def main():
n = ip()
p = input()
w = p.count('W')
b = p.count('B')
p = [i for i in p]
li = []
c = 0
if w%2==0:
for i in range(n-1):
if p[i] == 'W':
p[i]='B'
li.append(i+1)
c+=1
if p[i+1]=="B":
p[i+1]='W'
else:
p[i+1]="B"
print(c)
print(*li)
elif b%2==0:
for i in range(n-1):
if p[i] == 'B':
p[i]='W'
c += 1
li.append(i+1)
if p[i+1]=='W':
p[i+1]='B'
else:
p[i+1] = 'W'
print(c)
print(*li)
else:
print(-1)
# 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)
ip = lambda: int(sys.stdin.readline().rstrip("\r\n"))
ai = lambda: list(map(int, sys.stdin.readline().split()))
ei = lambda: map(int, sys.stdin.readline().split())
# endregion
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 | #! /usr/bin/env python
# -*- coding: utf-8 -*-
def abc(l: list):
def slip(n):
if l[n] == 'B':
l[n] = 'W'
else:
l[n] = 'B'
if l[n+1] == 'B':
l[n+1] = 'W'
else:
l[n+1] = 'B'
def move1():
for i in range(long-1):
if l[i] == choice:
slip(i)
out.append(i+1)
out = []
long = len(l)
b = l.count('B')
w = l.count('W')
if b % 2 and w % 2:
return -1
if b % 2:
choice = 'W'
else:
choice = 'B'
move1()
return out
n = int(input())
s = list(input())
if n == 2:
if s[0] == s[1]:
print(0)
else:
print(-1)
elif n == 3:
if s[0] == s[1]:
if s[1] == s[2]:
print(0)
else:
print('1\n1')
else:
if s[1] == s[2]:
print('1\n2')
else:
print('2\n1 2')
else:
if s.count(s[0]) == len(s):
print(0)
else:
ans = abc(s)
if abc(s) == -1:
print(-1)
else:
print(len(ans))
print(' '.join(list(map(str, 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 org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int k =0;
int n = sc.nextInt();
StringBuilder str = new StringBuilder(sc.next());
ArrayList<Integer> moves = new ArrayList<>();
if(n==2) {
if (str.charAt(0) != str.charAt(1)) {
System.out.println(-1);
} else {
System.out.println(0);
}
}else{
for(int i=0; i<n-1; i++) {
if (i != n - 2) {
if (str.charAt(i) != str.charAt(i + 1)) {
//inverting colors
k++;
moves.add(i+2);
if (str.charAt(i + 1) == 'B') {
str.setCharAt(i + 1, 'W');
} else {
str.setCharAt(i + 1, 'B');
}
if (str.charAt(i + 2) == 'B') {
str.setCharAt(i + 2, 'W');
} else {
str.setCharAt(i + 2, 'B');
}
}
}else {
if (str.charAt(i)!=str.charAt(i+1)){
if(n%2!=0){
str.setCharAt(i+1,str.charAt(i));
for(int j=n-2; j>0; j-=2){
k++;
moves.add(j);
}
}else{
break;
}
}
}
}
boolean check =true;
for(int i=0; i<n-1; i++){
if(str.charAt(i)!= str.charAt(i+1)){
check=false;
break;
}
}
if(k>(3*n)){
check=false;
}
if(check){
System.out.println(k);
int size = moves.size();
for(int i=0; i<size; i++){
if(i==size-1){
System.out.println(moves.get(i));
}else {
System.out.print(moves.get(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 | a=int(input())
b=input().replace('',' ').split()
c="";k=[]
for i in range(a-1):
if b[i]=='B':c+='W';b[i]='W';b[i+1]='BW'[b[i+1]!='W'];k+=[i+1]
else:c+='W'
if len(c)!=len(b):c+=b[-1]
b=''.join(c)
if b[-1]=='W':print(len(k));print(*k)
else:
if (a-1)%2:print(-1)
else:print(len(k)+(a-1)//2);print(*k,*range(1,a,2)) | 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>
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);
int* nums = (int*)malloc(sizeof(int) * 2 * 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 | from fractions import Fraction as F
import math
import sys
import random
import time
random.seed(time.time())
fi = sys.stdin
fo = sys.stdout
fe = sys.stderr
exit = sys.exit
readline = raw_input
def readargs(tp=None):
if tp is not None:
return map(tp, readline().split())
return readline().split()
def yesno(flag, yes='', no=''):
if flag:
print yes if yes else 'YES'
else:
print no if no else 'NO'
truefalse = lambda flag : yesno(flag, yes='TRUE', no='FALSE')
n = int(readline())
box = list(readline().strip())
b = box.count('B')
w = box.count('W')
ans = []
if b % 2 == 1 and w % 2 == 1:
print -1
exit(0)
elif b % 2 == 1:
# Turn all W to B
for i in range(len(box) - 1):
if box[i] == 'W':
ans.append(i + 1)
box[i] = 'B'
box[i + 1] = 'W' if box[i + 1] == 'B' else 'B'
assert box.count('W') == 0
else:
# Turn all B to W
for i in range(len(box) - 1):
if box[i] == 'B':
ans.append(i + 1)
box[i] = 'W'
box[i + 1] = 'W' if box[i + 1] == 'B' else 'B'
assert box.count('B') == 0
print len(ans)
print ' '.join(map(str, ans))
| 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 | n = int(input())
s = input()
b = s.count("B")
w = s.count("W")
f = 0
res = 0
ans = []
if b % 2 != 0 and w % 2 != 0:
print(-1)
else:
if b % 2 != 0:
i = 0
while i < (n - 1):
if s[i] == "W":
f = 1
if f==1:
res += 1
ans.append(i + 1)
if s[i + 1] == "W":
i += 1
f = 0
i += 1
else:
i = 0
while i < (n - 1):
if s[i] == "B":
f = 1
if f==1:
res += 1
ans.append(i + 1)
if s[i + 1] == "B":
i += 1
f = 0
i += 1
print(res)
print(*ans,sep=" ") | 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;
char s[222];
int n;
vector<int> r;
bool colit(int type) {
for (int i = 1; i < n - 1; i++)
if (s[i] != type) {
r.push_back(i + 1);
s[i] ^= 1, s[i + 1] ^= 1;
}
if (s[n - 1] != type) return 0;
return 1;
}
int main() {
scanf("%d%s", &n, s);
for (int i = 0; i < n; i++)
if (s[i] == 'B')
s[i] = 1;
else
s[i] = 0;
int flag = 0;
flag |= colit(s[0]);
if (!flag) {
r.push_back(1);
s[0] ^= 1, s[1] ^= 1;
flag |= colit(s[0]);
}
if (!flag)
printf("-1\n");
else {
printf("%d\n", r.size());
for (int k : r) printf("%d ", k);
}
}
| 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() {
long long int n, b = 0, w = 0, ans = 0;
cin >> n;
string str;
cin >> str;
vector<long long int> vec;
for (long long int i = 0; i < n; ++i) {
if (str[i] == 'B')
++b;
else
++w;
}
if (b % 2 == 1 && w % 2 == 1)
cout << "-1";
else {
if (b % 2 == 1) {
for (long long int i = 0; i < n - 1; ++i) {
if (str[i] == 'W' && str[i + 1] == 'W') {
++ans;
vec.push_back(i + 1);
str[i] = 'B';
str[i + 1] = 'B';
}
}
long long int flag = 0;
for (long long int i = 0; i < n; ++i) {
if (flag == 0 && str[i] == 'W') {
flag = 1;
vec.push_back(i + 1);
++ans;
} else if (flag == 1 && str[i] == 'B') {
++ans;
vec.push_back(i + 1);
} else if (flag == 1 && str[i] == 'W') {
flag = 0;
}
}
} else {
for (long long int i = 0; i < n - 1; ++i) {
if (str[i] == 'B' && str[i + 1] == 'B') {
++ans;
vec.push_back(i + 1);
str[i] = 'W';
str[i + 1] = 'W';
}
}
long long int flag = 0;
for (long long int i = 0; i < n; ++i) {
if (flag == 0 && str[i] == 'B') {
flag = 1;
vec.push_back(i + 1);
++ans;
} else if (flag == 1 && str[i] == 'W') {
++ans;
vec.push_back(i + 1);
} else if (flag == 1 && str[i] == 'B') {
flag = 0;
}
}
}
cout << ans << "\n";
for (long long int i = 0; i < vec.size(); ++i) {
cout << vec[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 | #!/usr/bin/env python3
import sys
input = sys.stdin.readline
n = int(input())
s = input().rstrip()
bcnt = s.count("B")
wcnt = s.count("W")
if bcnt % 2 == 1 and wcnt % 2 == 1:
print(-1)
exit()
s_l = list(s)
ans = []
for i in range(n-1):
if s_l[i] == "W":
ans.append(i + 1)
s_l[i] = "B"
if s_l[i+1] == "B":
s_l[i+1] = "W"
else:
s_l[i+1] = "B"
if "W" in s_l:
for i in range(n-1):
if s_l[i] == "B":
ans.append(i + 1)
s_l[i] = "W"
if s_l[i+1] == "B":
s_l[i+1] = "W"
else:
s_l[i+1] = "B"
print(len(ans))
print(*ans) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.