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 | t = int(input())
string = input()
changes = []
adopted = [i for i in string]
count = 0
i = 0
while(i < t-1 ):
if adopted[i] == 'B':
i += 1
else:
adopted[i] = 'B'
count += 1
changes.append(i+1)
if adopted[i+1] == 'W':
adopted[i+1] = 'B'
else:
adopted[i+1] = 'W'
if adopted[t-1] == 'B':
print(count)
for i in changes:
print(i ,end = ' ')
else:
del changes,adopted
adopted = [i for i in string]
changes = []
check = -1
count = 0
i = 0
while(i < t-1 ):
if adopted[i] == 'W':
i += 1
else:
adopted[i] = 'W'
count += 1
changes.append(i+1)
if adopted[i+1] == 'W':
adopted[i+1] = 'B'
else:
adopted[i+1] = 'W'
if adopted[t-1] == 'W':
print(count)
for i in changes:
print(i,end = ' ')
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
string s;
cin >> s;
char arr[N + 1];
strcpy(arr, s.c_str());
vector<int> v;
unordered_map<char, int> um;
for (int i = 0; i < N; i++) {
um[arr[i]]++;
}
if (um['B'] % 2 == 1 && um['W'] % 2 == 1) {
cout << "-1\n";
} else if (um['B'] == 0 || um['W'] == 0)
cout << "0\n";
else {
char ch;
if (um['B'] % 2 == 0)
ch = 'W';
else
ch = 'B';
for (int i = 0; i < N; i++) {
if (arr[i] != ch) {
if (i + 1 < N && arr[i + 1] != ch) {
arr[i] = ch;
arr[i + 1] = ch;
v.push_back(i + 1);
} else if (i + 1 < N && arr[i + 1] == ch) {
char t = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = t;
v.push_back(i + 1);
}
}
}
cout << v.size() << "\n";
for (int i = 0; i < v.size() - 1; i++) cout << v[i] << " ";
cout << v[v.size() - 1];
}
}
| 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 python
import os
import sys
from io import BytesIO, IOBase
def solve(n, sarr):
ans = []
if len(sarr) <= 1:
return 0
def flip(x, y):
ans.append(x + 1)
sarr[x] = 'B' if sarr[x] == 'W' else 'W'
sarr[y] = 'B' if sarr[y] == 'W' else 'W'
for i, c in enumerate(sarr[1:-1]):
i = i + 1
if sarr[i] != sarr[i - 1]:
flip(i, i + 1)
if sarr[-1] == sarr[-2]:
return ans
if n % 2 == 0:
return -1
ans.extend([i + 1 for i in range(0, n - 1, 2)])
return ans
def main():
n = int(input().strip())
sarr = list(input().strip())
ans = solve(n, sarr)
if type(ans) is int and ans <= 0:
print(ans)
else:
print(len(ans))
print(' '.join(list(map(str, ans))))
# Bootstrap https://github.com/cheran-senthil/PyRival/blob/master/tests/misc/test_bootstrap.py
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO, self).read()
def readline(self):
while self.newlines == 0:
s = self._fill();
self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self, a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero=0):
conv = ord if py2 else lambda x: x
A = [];
numb = zero;
sign = 1;
i = 0;
s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0'[0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-'[0]:
sign = -1
elif s[i] != b'\r'[0]:
A.append(sign * numb)
numb = zero;
sign = 1
i += 1
except:
pass
if s and s[-1] >= b'0'[0]:
A.append(sign * numb)
return A
if __name__ == "__main__":
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
co = []
for i in range(0,n):
if s[i] == 'B':
co.append(0)
else:
co.append(1)
bl = co.copy()
wl = co.copy()
abl = []
awl = []
for i in range(0,n-1):
if bl[i] == 0:
pass
else:
bl[i] = 0
abl.append(i+1)
bl[i+1] = (bl[i+1]+1)%2
for i in range(0,n-1):
if wl[i] == 1:
pass
else:
wl[i] = 1
awl.append(i+1)
wl[i+1] = (wl[i+1]+1)%2
if bl.count(1)%2 == 0:
if bl.count(1) == 0:
pass
else:
last = bl.index(1)
for i in range(last,n,2):
abl += [i+1]
print(len(abl))
print(*abl)
elif wl.count(0)%2 == 0:
if wl.count(0) == 0:
pass
else:
last = wl.index(0)
for i in range(last,n,2):
awl += [i+1]
print(len(awl))
print(*awl)
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
#input = sys.stdin.readline
def main():
n = int( input())
S = list( input())
b = 0
w = 0
T = [0]*n
for i in range(n):
if S[i] == 'B':
b += 1
else:
w += 1
T[i] = 1
if b%2 == 1 and w%2 == 1:
print(-1)
return
count = 0
ans = []
if b%2 == 1:
for i in range(n-1):
if T[i] == 0:
continue
T[i] = 0
T[i+1] = 1-T[i+1]
count += 1
ans.append(i+1)
else:
for i in range(n-1):
if T[i] == 1:
continue
T[i] = 1
T[i+1] = 1-T[i+1]
count += 1
ans.append(i+1)
print(count)
if count == 0:
return
print(" ".join( map(str, ans)))
if __name__ == '__main__':
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | /*package whatever //do not write package name here */
import java.util.*;
import java.io.*;
public class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
char[] S = sc.nextLine().toCharArray();
int B = 0, W = 0;
for(int i = 0; i < n ; i++){
B += (S[i] == 'B') ? 1 : 0;
W += (S[i] == 'W') ? 1 : 0;
}
if((B%2 == 1) && (W % 2 == 1)){
System.out.println(-1);
}else{
StringBuilder ans = new StringBuilder();
boolean flag = false;
int cnt = 0;
for(int i = 1; i < n; i++){
if(i == n-1){
if(S[i] != S[i-1]){
flag = true;
}
}else if(S[i] == S[i+1] && S[i] == S[i-1]){
i += 1;
}else if((S[i] != S[i+1] && S[i] != S[i-1])){
ans.append(i+1);
ans.append(" ");
cnt++;
char temp = S[i];
S[i] = S[i+1];
S[i+1] = temp;
}else if(S[i] == S[i+1] && S[i] != S[i-1]){
ans.append(i+1);
ans.append(" ");
S[i] = S[i-1];
S[i+1] = S[i];
cnt++;
i += 1;
}
}
if(flag){
for(int i = 1; i < n; i+=2){
ans.append(i);
ans.append(" ");
cnt++;
}
}
System.out.println(cnt);
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 | #include <bits/stdc++.h>
using namespace std;
char s[300010];
vector<int> ans;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
cin >> s + 1;
for (int i = 1; i < n; i++) {
if (s[i] == 'B') {
s[i] = 'W';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
ans.push_back(i);
}
}
if (s[n] == 'W') {
cout << ans.size() << endl;
for (auto i : ans) cout << i << " ";
} else {
if (n & 1) {
cout << ans.size() + (n - 1) / 2 << endl;
for (auto i : ans) cout << i << " ";
for (int i = 1; i <= n - 1; i += 2) cout << i << " ";
} else
cout << -1 << endl;
}
return 0;
}
| 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 B_608{
public static void main(String[] args)
{
Scanner omkar = new Scanner(System.in);
int size = omkar.nextInt();
int[] arr = new int[size];
int[] arr2 = new int[size];
String colors = omkar.next();
for(int i = 0; i < size; i++)
{
if(colors.charAt(i) == 'B')
{
arr[i] = 0;
arr2[i] = 0;
}
else
{
arr[i] = 1;
arr2[i] = 1;
}
}
int[] moves = new int[size-1];
int count = 0;
for(int i = 0; i < size-1; i++)
{
if(arr[i] == 1)
{
arr[i] = 0;
arr[i+1] = 1 ^ arr[i+1];
moves[i] = 1;
count++;
}
else
{
moves[i] = 0;
}
}
if(arr[size-1] == 0)
{
System.out.println(""+count);
for(int i = 0; i < size-1; i++)
{
if(moves[i] == 1)
{
System.out.print((i+1)+" ");
}
}
System.out.println("");
return;
}
count = 0;
for(int i = 0; i < size-1; i++)
{
if(arr2[i] == 0)
{
arr2[i] = 1;
arr2[i+1] = 1 ^ arr2[i+1];
moves[i] = 1;
count++;
}
else
{
moves[i] = 0;
}
}
if(arr2[size-1] == 1)
{
System.out.println(""+count);
for(int i = 0; i < size-1; i++)
{
if(moves[i] == 1)
{
System.out.print((i+1)+" ");
}
}
System.out.println("");
return;
}
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 | from copy import copy
def arr_float_inp():
return [float(s) for s in input().split()]
def arr_int_inp():
return [int(s) for s in input().split()]
def int_inp():
return int(input())
def float_inp():
return float(input())
def comp(a):
return a[0]
if __name__ == '__main__':
k = int_inp()
arr = [el == 'W' for el in input()]
out = []
s = sum(arr)
if s == 0 or s == k:
print(0)
else:
if (s % 2) == 0:
for i in range(k - 1):
if arr[i]:
arr[i] = not arr[i]
arr[i + 1] = not arr[i + 1]
out.append(i)
elif (k - s) % 2 == 0:
for i in range(k - 1):
if not arr[i]:
arr[i] = not arr[i]
arr[i + 1] = not arr[i + 1]
out.append(i)
if len(out) == 0 or (sum(arr) != 0 and sum(arr) != k):
print(-1)
else:
print(len(out))
for el in out:
print(el + 1, end=' ')
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int l[n];
int wh = 0;
int bl = 0;
for (int i = 0; i < n; i++) {
l[i] = s.at(i) == 'W' ? 1 : 0;
if (l[i] == 0) {
bl++;
} else {
wh++;
}
}
if (bl % 2 == 1 && wh % 2 == 1) {
cout << -1;
return 0;
}
int m;
if (wh % 2 == 0) {
m = 0;
} else {
m = 1;
}
vector<int> ans;
int ind = -1;
for (int i = 0; i < n; i++) {
if (l[i] != m) {
ind = i;
break;
}
}
while (ind != -1) {
ans.push_back(ind + 1);
l[ind] = l[ind] == 1 ? 0 : 1;
l[ind + 1] = l[ind + 1] == 1 ? 0 : 1;
ind = -1;
for (int i = 0; i < n; i++) {
if (l[i] != m) {
ind = i;
break;
}
}
}
cout << ans.size() << "\n";
for (int x : ans) cout << x << " ";
}
| 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;
long long mod = 1e9 + 7;
long long power(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % mod;
if (n % 2 == 0) {
long long y = power(x, n / 2) % mod;
return (y * y) % mod;
}
if (n & 1) {
long long y = power(x, n - 1);
return (x % mod * y % mod) % mod;
}
return 0;
}
long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const long long maxn = 200005;
void solve() {
long long n, i, j, k, m;
cin >> n;
string s;
cin >> s;
vector<long long> ans;
i = 0;
if (s[0] == 'W') i++;
for (i; i < n - 1; i++) {
if (s[i] == 'B') {
ans.push_back(i);
s[i] = 'W';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
if (s[n - 1] == 'W') {
cout << (long long)ans.size() << '\n';
for (long long x : ans) cout << x + 1 << " ";
exit(0);
}
i = 0;
if (s[0] == 'B') i++;
for (i; i < n - 1; i++) {
if (s[i] == 'W') {
ans.push_back(i);
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
if (s[n - 1] == 'B') {
cout << (long long)ans.size() << '\n';
for (long long x : ans) cout << x + 1 << " ";
exit(0);
}
cout << -1;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
| 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())
s = input()
w = s.count('W')
b = s.count('B')
if w == 0 or b == 0:
print(0)
return
s_s = [i for i in s]
res = []
for i in range(1,len(s_s) - 1):
if s_s[i] != s_s[i-1]:
s_s[i] = invert(s_s[i])
s_s[i+1] = invert(s_s[i+1])
res.append(str(i+1))
for i in range(len(s_s) - 2, 0, -1):
if s_s[i] != s_s[i+1]:
s_s[i] = invert(s_s[i])
s_s[i-1] = invert(s_s[i-1])
res.append(str(i))
if s_s[-1] != s_s[0]:
print(-1)
return
length = len(res)
res = ' '.join(res)
if length == 0:
print(length)
else:
print(length)
print(res)
def invert(c):
if c == 'W':
c = 'B'
else:
c = 'W'
return c
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 sys
input = lambda: sys.stdin.readline().strip('\r\n')
n = int(input())
s = [(0 if c=='B' else 1) for c in input()]
c = [s.count(0), s.count(1)]
for i in 0, 1:
if c[i]%2 == 0:
t = i
break
else:
print(-1)
exit()
out = []
for i in range(n-1):
if s[i] == t:
s[i] = 1-s[i]
s[i+1] = 1-s[i+1]
out.append(i+1)
print(len(out))
print(' '.join(str(x) for x in out))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
l = list(input()) #list of B and W
def isDone(l, target):
return l == [target]*len(l)
#first considering making each element equal to the first element
count = 0
steps=''
target = l[0]
for i in range(len(l)-1):
#print(i, l[i])
if l[i]!=target:
count +=1
steps+=str(i+1)+' '
l[i]=target
if l[i+1]=='B':
l[i+1]='W'
else:
l[i+1]='B'
#print(l)
if isDone(l, target):
#print(l)
break
if isDone(l, target)==False:
l.reverse()
target = l[0]
for i in range(len(l)-1):
#print(i, l[i])
if l[i]!=target:
count +=1
steps+=str(len(l)-i-1)+' '
l[i]=target
if l[i+1]=='B':
l[i+1]='W'
else:
l[i+1]='B'
#print(l)
if isDone(l, target):
#print(l)
break
if isDone(l, target)==False:
print(-1)
else:
print(count)
print(steps[:-1]) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
char ar[n];
for (int i = 0; i < n; ++i) {
cin >> ar[i];
}
int co = 0;
vector<int> po;
for (int i = 0; i < n - 1; ++i) {
if (ar[i] == 'W') {
ar[i] = 'B';
co++;
po.push_back(i + 1);
if (ar[i + 1] == 'B') {
ar[i + 1] = 'W';
} else {
ar[i + 1] = 'B';
}
}
}
if (ar[n - 1] == 'B') {
cout << co << endl;
for (int i = 0; i < po.size(); ++i) {
cout << po[i] << " ";
}
cout << endl;
} else {
if (n % 2 == 0) {
cout << -1 << endl;
} else {
cout << co + (n - 1) / 2 << endl;
for (int i = 0; i < po.size(); ++i) {
cout << po[i] << " ";
}
for (int i = 0; i < (n - 1) / 2; ++i) {
cout << 2 * i + 1 << " ";
}
cout << endl;
}
}
return 0;
}
| 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/python3
import os
import sys
def main():
N = read_int()
S = inp()
ans = solve(N, S)
if ans is None:
print('-1')
return
print(len(ans))
if ans:
print(*[x + 1 for x in ans])
def solve(N, S):
A = [0 if c == 'W' else 1 for c in S]
ans = []
for i in range(1, N - 1):
if A[i - 1] != A[i]:
ans.append(i)
A[i] = 1 - A[i]
A[i + 1] = 1 - A[i + 1]
if A[-1] == A[-2]:
return ans
A = [0 if c == 'W' else 1 for c in S]
A[0] = 1 - A[0]
A[1] = 1 - A[1]
ans = [0]
for i in range(1, N - 1):
if A[i - 1] != A[i]:
ans.append(i)
A[i] = 1 - A[i]
A[i + 1] = 1 - A[i + 1]
if A[-1] == A[-2]:
return ans
return None
###############################################################################
DEBUG = 'DEBUG' in os.environ
def inp():
return sys.stdin.readline().rstrip()
def read_int():
return int(inp())
def read_ints():
return [int(e) for e in inp().split()]
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
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 | /* package codechef; // 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 Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t=1;
while(t-->0)
{
int n=Integer.parseInt(bf.readLine());
String s=bf.readLine();
char a[]=s.toCharArray();
int black=0;
int white=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='W')
white++;
else
black++;
}
if(black%2!=0 && white%2!=0)
{
System.out.println(-1);
}
else
{
// System.out.println(Arrays.toString(a));
HashSet<Integer> hs=new HashSet<>();
char z;
if(white%2!=0)
z='W';
else
z='B';
int count=0;
for(int i=0;i<n-1;i++)
{
if(a[i]==z)
{
}
else
{
count++;
hs.add(i+1);
// hs.add(i+2);
if(a[i]=='W')
a[i]='B';
else
a[i]='W';
if(a[i+1]=='W')
a[i+1]='B';
else
a[i+1]='W';
}
}
System.out.println(count);
for(int i:hs)
{
System.out.print(i+" ");
}
System.out.println();
// System.out.println(Arrays.toString(a));
}
}
}
} | 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 os
import sys
from io import BytesIO, IOBase
from collections import Counter
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)
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) / gcd(a, b)
def main():
n=int(input())
s=input()
b=0
w=0
a=[]
sec=[]
for i in s:
if i=='B' or i=='W':
a.append(i)
sec.append(i)
ans=[]
# b
for i in range(len(a)-1):
if a[i]=='W':
a[i]='B'
ans.append(i+1)
if a[i+1]=='B':
a[i+1]='W'
else:
a[i+1]='B'
f=1
for i in a:
if i=='W':
f=0
break
if f==1:
print(len(ans))
print(*ans)
return
# W
ans=[]
for i in range(len(a)-1):
if sec[i]=='B':
sec[i]='W'
ans.append(i+1)
if sec[i+1]=='B':
sec[i+1]='W'
else:
sec[i+1]='B'
f=1
for i in sec:
if i=='B':
f=0
break
if f:
print(len(ans))
print(*ans)
return
print(-1)
return
if __name__ == "__main__":
main() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
x='B'
def check(l):
prev=l[0]
for i in range(len(l)):
if l[i]!=prev:
return False
return True
count=0
ans=[i for i in s]
l=[]
for i in range(n-1):
if s[i]!=x:
s[i]=x
if s[i+1]=='B':
s[i+1]='W'
else:
s[i+1]='B'
l.append(i+1)
count+=1
if count<=3*n and check(s):
print(count)
print(*l)
else:
x='W'
count=0
l=[]
s=[i for i in ans]
for i in range(n-1):
if s[i]!=x:
s[i]=x
if s[i+1]=='B':
s[i+1]='W'
else:
s[i+1]='B'
l.append(i+1)
count+=1
if count<=3*n and check(s):
print(count)
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 | #include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
int main() {
file();
fast();
int n;
cin >> n;
string str;
cin >> str;
int flib = 0;
map<char, int> mp;
mp['B'] = 0, mp['W'] = 1;
vector<int> show;
vector<int> st(n + 9);
for (int i = 0; i < n - 1; i++) {
flib = st[i];
int x = mp[str[i]];
if (flib) x = 1 - x;
if (!x) continue;
show.push_back(i + 1);
st[i + 1] = 1 - st[i + 1];
}
int s = mp[str.back()];
if (st[n - 1]) s = 1 - s;
if (!s) {
cout << (int)(show.size()) << "\n";
for (auto it : show) cout << it << ' ';
return cout << "\n"
<< "\n",
0;
}
show.clear();
flib = 0;
st = vector<int>(n + 9);
for (int i = 0; i < n - 1; i++) {
flib = st[i];
int x = mp[str[i]];
if (flib) x = 1 - x;
if (x == 1) continue;
show.push_back(i + 1);
st[i + 1] = 1 - st[i + 1];
}
s = mp[str.back()];
if (st[n - 1]) s = 1 - s;
if (s) {
cout << (int)(show.size()) << "\n";
for (auto it : show) cout << it << ' ';
return cout << "\n"
<< "\n",
0;
}
cout << -1 << "\n";
return 0;
}
| 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 BLOCK{
public static void main(String arggs[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String block = sc.next();
char [] a = block.toCharArray();
LinkedList<Integer> k = new LinkedList<Integer>();
for(int i = 0 ;i<n-1;i++){
if(a[i]=='W'){
a[i]='B';
k.add(i);
if(a[i+1]=='B')
a[i+1]='W';
else
a[i+1]='B';
}
}
if(a[n-1]=='B'){
System.out.println(k.size());
for(int p=0; p < k.size(); p++){
System.out.print(k.get(p)+1+" ");
}
System.out.println();
}
else if(a[n-1]=='W' && n%2==0)
System.out.println("-1");
else{
System.out.println(k.size()+((n-1)/2));
for(int p=0; p < k.size(); p++){
System.out.print(k.get(p)+1+" ");
}
for(int p= 1; p<n ;p=p+2)
System.out.print(p+" ");
System.out.println();
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long ans, sum, tot, cnt, mx, mn, idx, chk, rem, ret, tmp, mid, lft, rgt,
pos;
long long tc, n, m, a, b, c, d, g, h, l, r, x, y, i, j, k;
cin >> n;
string s;
cin >> s;
long long w;
w = b = 0;
for (i = 0; i < n; ++i) {
if (s[i] == 66)
b++;
else
w++;
}
if (b & 1 && w & 1) {
cout << -1 << endl;
return 0;
}
if (b == 0 || w == 0) {
cout << 0 << endl;
return 0;
}
vector<long long> v;
char cc;
if (b % 2 == 0 && w % 2 == 0)
cc = s[0];
else if (b & 1)
cc = 'B';
else
cc = 'W';
for (i = 0; i < n; ++i) {
if (s[i] != cc) {
swap(s[i], s[i + 1]);
v.push_back(i + 1);
}
if (s[i] == s[i + 1]) i++;
}
cout << v.size() << endl;
for (auto it : v) cout << it << ' ';
return 0;
}
| 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
import math
import heapq
import collections
def inputnum():
return(int(input()))
def inputnums():
return(map(int,input().split()))
def inputlist():
return(list(map(int,input().split())))
def inputstring():
return([x for x in input()])
def inputstringnum():
return([ord(x)-ord('a') for x in input()])
def inputmatrixchar(rows):
arr2d = [[j for j in input().strip()] for i in range(rows)]
return arr2d
def inputmatrixint(rows):
arr2d = []
for _ in range(rows):
arr2d.append([int(i) for i in input().split()])
return arr2d
n = inputnum()
s = inputstring()
ans = []
for i in range(n-1):
if s[i] == 'B':
s[i] = 'W'
if s[i+1] == 'B':
s[i+1] = 'W'
else:
s[i+1] = 'B'
ans.append(i+1)
if s[n-1] == 'W':
print(len(ans))
print(*ans)
elif n%2 == 1:
for i in range(0, n-1, 2):
ans.append(i+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 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int main() {
int n;
string s;
cin >> n >> s;
vector<int> b;
string t = s;
for (int i = 0; i < (int)(n - 1); i++) {
if (t[i] == 'W') {
t[i] = 'B';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
b.push_back(i);
}
}
bool ok = true;
for (int i = 0; i < (int)(n); i++) {
if (t[i] != 'B') ok = false;
}
if (ok) {
cout << b.size() << endl;
for (int i = 0; i < (int)(b.size()); i++) cout << b[i] + 1 << " ";
cout << endl;
return 0;
}
vector<int> w;
t = s;
for (int i = 0; i < (int)(n - 1); i++) {
if (t[i] == 'B') {
t[i] = 'W';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
w.push_back(i);
}
}
ok = true;
for (int i = 0; i < (int)(n); i++) {
if (t[i] != 'W') ok = false;
}
if (ok) {
cout << w.size() << endl;
for (int i = 0; i < (int)(w.size()); i++) cout << w[i] + 1 << " ";
cout << endl;
return 0;
}
cout << -1 << endl;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
b=[]
a=[]
for i in range(n):
a.append(s[i])
b.append(s[i])
c=[]
for i in range(n-1):
if(a[i]=="W"):
continue
else:
a[i]="W"
c.append(i+1)
if(a[i+1]=="W"):
a[i+1]="B"
else:
a[i+1]="W"
if(a.count("W")==n):
print(len(c))
print(*c)
else:
d=[]
for i in range(n-1):
if(b[i]=="B"):
continue
else:
b[i]="B"
d.append(i+1)
if(b[i+1]=="W"):
b[i+1]="B"
else:
b[i+1]="W"
if(b.count("B")==n):
print(len(d))
print(*d)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | # --------------------------------------- START OF LICENSE NOTICE ------------------------------------------------------
# Copyright (c) 2019 Soroco Private Limited. All rights reserved.
#
# NO WARRANTY. THE PRODUCT IS PROVIDED BY SOROCO "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL SOROCO BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE PRODUCT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# ---------------------------------------- END OF LICENSE NOTICE -------------------------------------------------------
#
# Primary Author: ANSHUL JAIN <[email protected]>
#
# Purpose: A short description of the purpose of this source file ...
n = int(input())
s = list(input())
l = []
def make_white(s):
for i in range(len(s)-1):
if s[i]=='W':
continue
else:
l.append(i+1)
s[i]='W'
if s[i+1]=='W':
s[i+1]='B'
else:
s[i+1]='W'
if s[-1] == 'W':
return True
else:
return False
def make_black(s):
for i in range(len(s)-1):
if s[i]=='B':
continue
else:
l.append(i+1)
s[i]='B'
if s[i+1]=='W':
s[i+1]='B'
else:
s[i+1]='W'
if s[-1] == 'B':
return True
else:
return False
if make_white(s) and len(l)<(3*n):
print(len(l))
print(*l)
elif make_black(s) and len(l)<(3*n):
print(len(l))
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 os
from math import*
def checker(s,s1,ans):
for i in range(n-1):
if s[i]==s1:
if s[i]=="B":
s[i]="W"
else:
s[i]="B"
ans.append(i+1)
if s[i+1]=="B":
s[i+1]="W"
else:
s[i+1]="B"
return ans
n=int(input())
s=input()
s=list(s)
if "B" not in s or "W" not in s:
print("0")
else:
ans=[]
ans=checker(s,"W",ans)
#print(s)
if len(set(s))==1:
print(len(ans))
for x in ans:
print(x,end=' ')
else:
ans=checker(s,"B",ans)
# print(s)
if len(set(s))==1:
print(len(ans))
for x in ans:
print(x,end=' ')
else:
print("-1")
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(str(input()))
ans = []
countW = 0
countB = 0
bol = False
if len(set(s)) == 1:
print(0)
exit(0)
for i in range(n):
if s[i] == 'B':
countB += 1
else:
countW += 1
if (countB & 1) == 1 and (countW & 1) == 1:
print(-1)
exit(0)
i = 0
while i < n - 1:
if i == 0 and s[i] == 'B':
i += 1
continue
elif i == 0 and s[i] == 'W':
if s[i + 1] == 'B':
s[i] = 'B'
s[i + 1] = 'W'
ans.append(i + 1)
i += 1
elif s[i + 1] == 'W':
s[i] = 'B'
s[i + 1] = 'B'
ans.append(i + 1)
i += 2
continue
if s[i] == 'W':
if s[i + 1] == 'B':
s[i] = 'B'
s[i + 1] = 'W'
ans.append(i + 1)
i += 1
elif s[i + 1] == 'W':
s[i] = 'B'
s[i + 1] = 'B'
ans.append(i + 1)
i += 2
continue
else:
i += 1
continue
if len(set(s)) == 1:
print(len(ans))
for i in ans:
print(i, end=" ")
else:
i = 0
while i < n - 1:
if i == 0 and s[i] == 'W':
i += 1
continue
elif i == 0 and s[i] == 'B':
if s[i + 1] == 'W':
s[i] = 'W'
s[i + 1] = 'B'
ans.append(i + 1)
i += 1
elif s[i + 1] == 'B':
s[i] = 'W'
s[i + 1] = 'W'
ans.append(i + 1)
i += 2
continue
if s[i] == 'B':
if s[i + 1] == 'W':
s[i] = 'W'
s[i + 1] = 'B'
ans.append(i + 1)
i += 1
elif s[i + 1] == 'B':
s[i] = 'W'
s[i + 1] = 'W'
ans.append(i + 1)
i += 2
continue
else:
i += 1
continue
if len(set(s)) == 1:
print(len(ans))
for i in ans:
print(i, end=" ")
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=str(input())
l=list(s)
lt=list(s)
l1=[]
l2=[]
for i in range(n-1):
if(l[i]=="W"):
l1.append(i+1)
l[i]="B"
if(l[i+1]=="W"):
l[i+1]="B"
else:
l[i+1]="W"
if(l[n-1]=="B"):
print(len(l1))
for i in l1:
print(i,end=" ")
print()
else:
for i in range(n-1):
if(lt[i]=="B"):
l2.append(i+1)
lt[i]="W"
if(lt[i+1]=="B"):
lt[i+1]="W"
else:
lt[i+1]="B"
if(lt[n-1]=="W"):
print(len(l2))
for i in l2:
print(i,end=" ")
print()
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
public class B {
final static int MOD = 1000000007;
static final char BLACK = 'B';
static final char WHITE = 'W';
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
int t = in.nextInt();
String s = in.nextString();
exec(s.toCharArray());
}
static void exec(char[] arr) {
int blackCount = countBlack(arr);
int whiteCount = arr.length - blackCount;
if (blackCount % 2 == 0) {
inverse(arr, BLACK);
return;
}
if (whiteCount % 2 == 0) {
inverse(arr, WHITE);
return;
}
System.out.println(-1);
}
static void inverse(char[] arr, char target) {
List<Integer> seq = new LinkedList<>();
int p = 0;
while (p < arr.length - 1) {
if (arr[p] != target) {
p++;
continue;
}
// arr[p] == target
swap(arr, p);
seq.add(p);
p++;
}
System.out.println(seq.size());
StringBuilder sb = new StringBuilder();
for (int i : seq) {
sb.append(i + 1).append(" ");
}
if (!seq.isEmpty()) {
System.out.println(sb);
}
}
static void swap(char[] arr, int p) {
assert p < arr.length - 1;
if (arr[p] == BLACK) {
arr[p] = WHITE;
} else {
arr[p] = BLACK;
}
if (arr[p + 1] == BLACK) {
arr[p + 1] = WHITE;
} else {
arr[p + 1] = BLACK;
}
}
private static int countBlack(char[] arr) {
int count = 0;
for (char c : arr) {
if (c == BLACK) {
count++;
}
}
return count;
}
static class FastReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
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 == ',') {
c = read();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String nextLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String nextLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return nextLine();
} else {
return readLine0();
}
}
public BigInteger nextBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
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=str(input())
s1=list(s)
b="B"
w="W"
if(s1.count(b)%2!=0 and s1.count(w)%2!=0):
print(-1)
else:
if((s1.count(b)%2!=0 and s1.count(w)%2==0)):
k=s1.count(w)
a=[]
i=0
while(i<len(s)-2):
if(s1[i]=="W" and s1[i+1]!="W"):
temp=s1[i]
s1[i]=s1[i+1]
s1[i+1]=temp
a.append(i+1)
elif(s1[i]=="W" and s1[i+1]=="W"):
i=i+2
continue
i=i+1
for j in range(0,k//2):
p=s1.index(w)
s1[p]=b
s1[p+1]=b
a.append(p+1)
print(len(a))
for j in range(0,len(a)):
print(a[j],end=" ")
elif((s1.count(b)%2==0 and s1.count(w)%2!=0)):
k=s1.count(b)
a=[]
i=0
while(i<len(s)-2):
if(s1[i]=="B" and s1[i+1]!="B"):
temp=s1[i]
s1[i]=s1[i+1]
s1[i+1]=temp
a.append(i+1)
elif(s1[i]=="B" and s1[i+1]=="B"):
i=i+2
continue
i=i+1
for j in range(0,k//2):
p=s1.index(b)
s1[p]=w
s1[p+1]=w
a.append(p+1)
print(len(a))
for j in range(0,len(a)):
print(a[j],end=" ")
else:
k=s1.count(b)
a=[]
i=0
while(i<len(s)-2):
if(s1[i]=="B" and s1[i+1]!="B"):
temp=s1[i]
s1[i]=s1[i+1]
s1[i+1]=temp
a.append(i+1)
elif(s1[i]=="B" and s1[i+1]=="B"):
i=i+2
continue
i=i+1
for j in range(0,k//2):
p=s1.index(b)
s1[p]=w
s1[p+1]=w
a.append(p+1)
print(len(a))
for j in range(0,len(a)):
print(a[j],end=" ")
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
s=s.replace('B','0')
s=s.replace('W','1')
# print(s)
zcount = s.count('0')
ocount = s.count('1')
if(zcount==0 or ocount==0):
print(0)
elif(zcount%2!=0 and ocount%2!=0):
print(-1)
else:
ans = []
idx = []
bit = '0'
if(zcount%2==0 and ocount%2==0):
if(zcount<ocount):
bit = '0'
else:
bit = '1'
elif(zcount%2==0):
bit='0'
else:
bit='1'
for x in range(len(s)):
if(s[x]==bit):
idx.append(x)
for y in range(len(idx)-1,-1,-2):
for k in range(idx[y]-1,idx[y-1]-1,-1):
ans.append(str(k+1))
if(len(ans)<=3*len(s)):
print(len(ans))
print(" ".join(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 = list(input())
def DP_(color1, color2):
DP = [[None for _ in range(2)] for _ in range(len(s))]
DP[-1][0] = 0 if s[-1] == color1 else 10 ** 10
DP[-1][1] = 0 if s[-1] == color2 else 10 ** 10
parents = {}
for i in range(len(s) - 2, -1, -1):
DP[i][0] = DP[i + 1][0] if s[i] == color1 else DP[i + 1][1] + 1
DP[i][1] = DP[i + 1][0] if s[i] == color2 else DP[i + 1][1] + 1
parents[(i, 0)] = (i + 1, 0, "F") if s[i] == color1 else (i + 1, 1, "T")
parents[(i, 1)] = (i + 1, 0, "F") if s[i] == color2 else (i + 1, 1, "T")
return DP, parents
def print_solution(parents_dict):
current = (0, 0)
changed = []
for i in range(len(s)):
a, b, c = parents_dict[current]
current = (a, b)
if c == "T":
changed.append(i)
if a == len(s) - 1:
break
return changed
DP1, parents1 = DP_("W", "B")
DP2, parents2 = DP_("B", "W")
if DP1[0][0] < 10 ** 9:
print(DP1[0][0])
print(" ".join([str(x + 1) for x in print_solution(parents1)]))
elif DP2[0][0] < 10 ** 9:
print(DP2[0][0])
print(" ".join([str(x + 1) for x in print_solution(parents2)]))
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
int a[1000];
int cnt = 0;
for (int i = 1; i <= n - 2; i++) {
if (s[i] != s[i - 1]) {
if (s[i] == 'B')
s[i] = 'W';
else
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
a[cnt++] = i;
}
}
if (s[0] != s[n - 1])
if ((n - 1) % 2 == 0) {
for (int i = 0; i <= n - 3; i += 2) a[cnt++] = i;
s[n - 1] = s[0];
}
if (s[0] != s[n - 1])
puts("-1");
else {
cout << cnt << endl;
for (int i = 0; i < cnt; i++) cout << a[i] + 1 << ' ';
}
return 0;
}
| 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())
it=list(input())
tt=it[:]
ans=[]
for i in range(n-1):
if it[i]=="W":
it[i]="B"
if it[i+1]=="B":
it[i+1]="W"
else:
it[i+1]="B"
ans.append(i+1)
if it[-1]=="B":
print(len(ans))
print(*ans)
else:
it=tt[:]
ans=[]
for i in range(n-1):
if it[i]=="B":
it[i]="W"
if it[i+1]=="B":
it[i+1]="W"
else:
it[i+1]="B"
ans.append(i+1)
if it[-1]=="W":
print(len(ans))
print(*ans)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Blocks { // Template for CF
public static class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> l1, List<Integer> l2) {
for (int i = 0; i < l1.size(); ++i) {
if (l1.get(i).compareTo(l2.get(i)) != 0) {
return l1.get(i).compareTo(l2.get(i));
}
}
return 0;
}
}
public static void main(String[] args) throws IOException {
// Check for int overflow!!!!
// Should you use a long to store the sum or smthn?
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(f.readLine());
String str = f.readLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
sb.append(str.charAt(i));
}
boolean check = true;
char match = sb.charAt(0);
for (int i = 1; i < sb.length(); i++) {
if (match != sb.charAt(i)) {
check = false;
break;
}
}
if (check) {
out.println(0);
out.close();
}
StringBuilder sb2 = new StringBuilder(sb.toString());
List<Integer> list1 = new ArrayList<>();
for (int i = 0; i < sb.length() - 1; i++) {
if (sb.charAt(i) != 'B') {
sb.setCharAt(i, 'B');
if (sb.charAt(i + 1) == 'B') {
sb.setCharAt(i + 1, 'W');
} else {
sb.setCharAt(i + 1, 'B');
}
list1.add(i);
}
}
List<Integer> list2 = new ArrayList<>();
for (int i = 0; i < sb2.length() - 1; i++) {
if (sb2.charAt(i) != 'W') {
sb2.setCharAt(i, 'W');
if (sb2.charAt(i + 1) == 'W') {
sb2.setCharAt(i + 1, 'B');
} else {
sb2.setCharAt(i + 1, 'W');
}
list2.add(i);
}
}
check = true;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) != 'B') {
check = false;
break;
}
}
boolean check2 = true;
for (int i = 0; i < sb2.length(); i++) {
if (sb2.charAt(i) != 'W') {
check2 = false;
break;
}
}
if (check) {
out.println(list1.size());
for (int i = 0; i < list1.size(); i++) {
out.print((list1.get(i) + 1) + " ");
}
out.println();
} else if (check2) {
out.println(list2.size());
for (int i = 0; i < list2.size(); i++) {
out.print((list2.get(i) + 1) + " ");
}
out.println();
} else {
out.println(-1);
}
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;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
int nb = 0, nw = 0;
for (auto ai : s) {
if (ai == 'B') {
nb++;
} else {
nw++;
}
}
if (nb & 1 && nw & 1) {
cout << -1;
return 0;
}
vector<int> ve;
if (!(nb & 1) && !(nw & 1)) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'B') {
ve.emplace_back(i);
i++;
} else if (s[i] == 'B') {
swap(s[i], s[i + 1]);
ve.emplace_back(i);
}
}
} else if (nb & 1) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = s[i + 1] = 'W';
ve.emplace_back(i++);
} else if (s[i] == 'B') {
swap(s[i], s[i + 1]);
ve.emplace_back(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W' && s[i + 1] == 'W') {
ve.emplace_back(i);
i++;
}
}
} else {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = s[i + 1] = 'B';
ve.emplace_back(i++);
} else if (s[i] == 'W') {
swap(s[i], s[i + 1]);
ve.emplace_back(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'B') {
ve.emplace_back(i);
i++;
}
}
}
cout << ve.size() << '\n';
for (auto ai : ve) {
cout << ai + 1 << ' ';
}
return 0;
}
| 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()
b=list(a)
e=-1
m=0
if(a.count("W")==len(a) or a.count("B")==len(a)):
print(0)
m=1
elif(a=="BWB" or a=="WBW"):
print(2)
print(2,1)
m=1
elif(a.count("W")%2!=0 and a.count("B")%2!=0):
print(-1)
m=1
elif(a.count("W")%2==0):
ans=[]
while(b.count("W")>0):
c=b.index("W",e+1)
d=b.index("W",c+1)
for i in range(c,d+1):
b[i]="B"
for i in range(c,d):
ans.append(i+1)
#print(b,ans)
elif(a.count("B")%2==0):
ans=[]
while(b.count("B")>0):
c=b.index("B",e+1)
d=b.index("B",c+1)
for i in range(c,d+1):
b[i]="W"
for i in range(c,d):
ans.append(i+1)
if(m!=1):
print(len(ans))
for i in ans:
print(i,end=" ")
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(input())
b = s.count('B')
w = s.count('W')
order = []
if b % 2 and w % 2:
print(-1)
else:
cur = 'B' if b % 2 else 'W'
notcur = 'B' if cur == 'W' else 'W'
for i in range(n-1):
if s[i] == cur:
continue
order.append(i+1)
s[i+1] = cur if s[i+1] != cur else notcur
print(len(order))
if len(order):
print(' '.join(list(map(str, order))))
| 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;
void solve() {
int n;
string s;
cin >> n >> s;
int white = count(s.begin(), s.end(), 'W');
int black = count(s.begin(), s.end(), 'B');
if (white % 2 && black % 2) {
cout << "-1" << endl;
return;
}
if (white == s.size() || black == s.size()) {
cout << "0" << endl;
return;
}
vector<int> v;
if (white % 2) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
v.push_back(i + 1);
if (s[i + 1] == 'B') {
i++;
} else {
s[i + 1] = 'B';
}
}
}
} else {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
v.push_back(i + 1);
if (s[i + 1] == 'W') {
i++;
} else {
s[i + 1] = 'W';
}
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
return 0;
}
| 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 b {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
char[] in = stdin.next().toCharArray();
int w = 0, b = 0;
for (int i = 0; i < n; i ++) {
if (in[i] == 'W') w ++;
else b ++;
}
if (w % 2 != 0 && b % 2 != 0) System.out.println("-1");
else {
ArrayList<Integer> arr = new ArrayList<>();
if (w % 2 == 0) {
for (int i = 0; i < n; i ++) {
if (in[i] == 'W') {
in[i] = 'B';
if (in[i+1] == 'W') in[i+1] = 'B';
else in[i+1] = 'W';
arr.add(i+1);
}
}
}
else {
for (int i = 0; i < n; i ++) {
if (in[i] == 'B') {
in[i] = 'W';
if (in[i+1] == 'W') in[i+1] = 'B';
else in[i+1] = 'W';
arr.add(i+1);
}
}
}
System.out.println(arr.size());
for (int i : arr) System.out.print(i + " ");
System.out.println();
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(input())
ans = []
if s.count('B') % 2 != 0 and s.count('W') % 2 != 0:
print(-1)
else:
if s.count('B') % 2 == 0:
now = 'B'
else:
now = 'W'
for i in range(n - 1):
if s[i] == now:
if now == 'B':
s[i] = 'W'
else:
s[i] = 'B'
if s[i + 1] == 'B':
s[i + 1] = 'W'
else:
s[i + 1] = 'B'
ans.append(i + 1)
print(len(ans))
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | """
// Author : snape_here - Susanta Mukherjee
"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
def gcd(x, y):
while y:
x, y = y, x % y
return x
def read():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
import math
mod=100000007
def main():
n=ii()
s=si()
a=s.count('B')
b=s.count('W')
if a%2==1 and b%2==1:
print(-1)
exit()
if b%2==0:
x='W'
else:
x='B'
q=0
c=0
a=[]
for i in range(n):
if s[i]==x and q==0 or s[i]!=x and q==1:
q=1
c+=1
a.append(i+1)
else:
q=0
print(c)
print(*a)
# region fastio#
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
main()
#Comment read()
| 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 | import sys
n=int(sys.stdin.readline().strip())
a=[0]*n
cnt=[0,0]
for i,v in enumerate(sys.stdin.readline().strip()):
if v=='B':
a[i]=1
cnt[1]+=1
else:
a[i]=0
cnt[0]+=1
if cnt[0]%2==1 and 1==cnt[1]%2:
print(-1)
else:
total=0
ans=[]
for i in range(n-1):
if a[i]==0:
total+=1
ans.append(i+1)
a[i]=1
if a[i+1]==1:
a[i+1]=0
else:
a[i+1]=1
cnt1=a.count(1)
cnt0=a.count(0)
if cnt0>0 and cnt1%2==0:
total+=cnt1//2
for i in range(1,cnt1+1,2):
ans.append(i)
print(total)
# print(a)
for i in ans:
print(i,end=' ') | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
a = list(input())
if a.count('B') % 2 and a.count('W') % 2:
print(-1)
else:
if a.count('W') % 2:
for q in range(len(a)):
if a[q] == 'W':
a[q] = 'B'
else:
a[q] = 'W'
ans = []
for q in range(len(a)-1):
if a[q] == 'W' and a[q+1] == 'W':
ans.append(q+1)
a[q] = a[q+1] = 'B'
elif a[q] == 'W':
ans.append(q+1)
a[q], a[q+1] = 'B', 'W'
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
string str;
cin >> str;
int cw = 0, cb = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'B') {
cb++;
} else {
cw++;
}
}
if (cb % 2 != 0 && cw % 2 != 0) {
cout << -1;
return 0;
}
int tr = 0;
vector<int> ans;
bool f = false;
if (cb % 2 == 0) {
f = true;
}
while (tr < 3 * n) {
for (int i = 0; i < n - 1; i++) {
if (cb == 0 || cw == 0) {
cout << ans.size() << "\n";
for (int j = 0; j < ans.size(); j++) {
cout << ans[j] << " ";
}
return 0;
}
if (f) {
if (str[i] == 'B') {
str[i] = 'W';
cb--;
cw++;
ans.push_back(i + 1);
tr++;
if (str[i + 1] == 'B') {
str[i + 1] = 'W';
cb--;
cw++;
} else {
str[i + 1] = 'B';
cw--;
cb++;
}
}
} else {
if (str[i] == 'W') {
str[i] = 'B';
cb++;
cw--;
ans.push_back(i + 1);
tr++;
if (str[i + 1] == 'B') {
str[i + 1] = 'W';
cb--;
cw++;
} else {
str[i + 1] = 'B';
cw--;
cb++;
}
}
}
}
}
cout << -1;
}
| 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.List;
import java.util.Scanner;
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) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
String s=sc.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));
}
}
}
}
| 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())
arr = [i for i in input()]
counter = 0
ans = []
for i in range(n - 1):
if arr[i] == "B":
arr[i] = "W"
if arr[i + 1] == "B":
arr[i + 1] = "W"
else:
arr[i + 1] = "B"
counter += 1
ans.append(i + 1)
if len(set(arr)) != 1:
for i in range(n - 1):
if arr[i] == "W":
arr[i] = "B"
if arr[i + 1] == "W":
arr[i + 1] = "B"
else:
arr[i + 1] = "W"
counter += 1
ans.append(i + 1)
if len(set(arr)) != 1:
print(-1)
else:
print(counter)
print(*ans)
else:
print(counter)
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
arr = list(input())
ori = arr[:]
temp = []
for i in range(n-1):
if arr[i] == 'W':
temp += [i+1]
arr[i] = 'B'
if arr[i+1] == 'W':
arr[i+1] = 'B'
else:
arr[i+1] = 'W'
if arr[-1] == 'B':
print(len(temp))
print(*temp)
exit()
temp = []
arr = ori[:]
for i in range(n-1):
if arr[i] == 'B':
temp += [i+1]
arr[i] = 'W'
if arr[i+1] == 'W':
arr[i+1] = 'B'
else:
arr[i+1] = 'W'
if arr[-1] == 'W':
print(len(temp))
print(*temp)
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 | n=int(input())
s=input()
ss=s
s=list(s)
ss=list(ss)
a=[]
b=[]
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'
a.append(i+1)
for i in range(n-1):
if(ss[i]=='B'):
ss[i]='W'
if(ss[i+1]=='B'):
ss[i+1]='W'
else:
ss[i+1]='B'
b.append(i+1)
if('W' not in s):
print(len(a))
print(*a)
elif('B' not in ss):
print(len(b))
print(*b)
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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
char[] copy = in.next().toCharArray();
for (int iter = 0; iter < 2; iter++) {
char find = (iter == 0 ? 'W' : 'B');
ArrayList<Integer> move = new ArrayList<>();
char[] c = copy.clone();
for (int i = N - 1; i >= 0; i--) {
if (c[i] == find) {
for (int j = i - 1; j >= 0; j--) {
move.add(j);
if (c[j] == find) {
c[i] = '$';
c[j] = '$';
i = j;
break;
}
}
}
}
boolean flag = false;
for (char ch : c) {
if (ch == find) {
flag = true;
break;
}
}
if (flag || move.size() >= 3 * N) {
continue;
} else {
out.println(move.size());
for (int i : move) {
out.print((i + 1) + " ");
}
return;
}
}
out.println(-1);
}
}
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 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;
void fragmentarA() {
long long h1, h2, h3, h4, h5, h6, h7, h8, h9;
h1 = h2 + h3 + h4;
h2 = h1 + h3 + h5 + 9;
h3 = h4 + h1 + h6;
h3 = h5 + h6;
h5 = h6 + h2;
h1 = h2 + h5 + h6;
h6 = h1 + h2;
h5 = h2 + h3;
h9 = h2 + h6;
h5 = h6 + h7;
h2 = h6 + h7 + h5;
h1 = h2 + h3 + h6;
return;
}
void fragmentarB() {
long long u1, u2, u3, u4, u5, u6, u7, u8, u9;
u1 = u1 + u2 + u3;
u3 + u2 + u4 + u2;
u7 = u1 + u5;
u8 = u1 + u2;
u9 = u2 + u5 + u9;
u2 = u1 + u2 + u3;
u5 = u1 + u4;
u6 = u1 + u2 + u3 + u4;
u7 = u1 + u7 + u8 + u6;
u3 = u1 + u2 + u3 + u4 + u5 + u7;
u1 = u6 + u4 + u6 + u7 + u8;
return;
}
void fragmentarC() {
long long y1, y2, y3, y4, y5, y6, y7, y8, y9;
y3 = y1 = y6 + y7 + y8 + y9 + y3;
y1 = y2 + y4 + y7 + y8;
y5 = y2 + y4 + y6 + y8;
y6 = y2 + y4 + y5 + y7 + y8;
y8 = y1 + y2 + y4 + y6 + y8;
y4 = y1 + y2 + y4 + y5;
y2 = y7 + y8 + y9 + y5;
y6 = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8;
y2 = y1 + y2 + y3 + y4 + y6 + y7;
y2 = y3 + y4 + y6 + y7;
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
string s;
cin >> s;
long long sum1 = 0, sum2 = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W') sum1++;
if (s[i] == 'B') sum2++;
}
long long min1 = min(sum1, sum2);
string t = s;
vector<long long> v1, v2;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
v1.push_back(i + 1);
}
}
fragmentarC();
fragmentarC();
fragmentarB();
fragmentarA();
fragmentarB();
fragmentarA();
for (long long i = 0; i < n - 1; i++) {
if (t[i] == 'B') {
t[i] = 'W';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
v2.push_back(i + 1);
}
}
fragmentarC();
fragmentarC();
fragmentarB();
fragmentarA();
fragmentarB();
fragmentarA();
if (t[n - 1] == 'B' && s[n - 1] == 'W')
cout << -1 << "\n";
else if (s[n - 1] == 'B') {
cout << v1.size() << "\n";
for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " ";
} else {
cout << v2.size() << "\n";
for (long long i = 0; i < v2.size(); i++) cout << v2[i] << " ";
}
fragmentarC();
fragmentarC();
fragmentarB();
fragmentarA();
fragmentarB();
fragmentarA();
return 0;
}
| 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() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
string s;
cin >> s;
string p = s;
long long ans1 = 0, ans2 = 0;
vector<long long> v1, v2;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
if (s[i + 1] == 'B') {
s[i + 1] = 'W';
} else
s[i + 1] = 'B';
v1.push_back(i);
}
}
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
continue;
else {
ans1 = -1;
break;
}
}
if (ans1 != -1) {
cout << v1.size() << endl;
for (auto x : v1) cout << x + 1 << " ";
cout << endl;
return 0;
}
s = p;
if (ans1 == -1) {
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
if (s[i + 1] == 'B') {
s[i + 1] = 'W';
} else
s[i + 1] = 'B';
v2.push_back(i);
}
}
for (int i = 0; i < n; i++) {
if (s[i] == 'W')
continue;
else {
ans2 = -1;
break;
}
}
}
if (ans2 != -1) {
cout << v2.size() << endl;
for (auto x : v2) cout << x + 1 << " ";
cout << endl;
} else
cout << "-1" << endl;
return 0;
}
| 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(raw_input())
s=list(raw_input())
nw=s.count("W")
nb=n-nw
if nw==0 or nb==0:
print 0
exit()
if nw%2==1 and nb%2==1:
print -1
exit()
if nw%2==1:
# make all to be white
ans=""
tmp=s[:]
x=0
for i in xrange(n-1):
if tmp[i]=="B":
ans=ans+" "+str(i+1)
x+=1
tmp[i]="W"
if tmp[i+1]=="W": tmp[i+1]="B"
else: tmp[i+1]="W"
print x
print ans.strip()
else:
# make all to be black
ans=""
tmp=s[:]
x=0
for i in xrange(n-1):
if tmp[i]=="W":
ans=ans+" "+str(i+1)
x+=1
tmp[i]="B"
if tmp[i+1]=="W": tmp[i+1]="B"
else: tmp[i+1]="W"
print x
print ans.strip()
| PYTHON |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from sys import stdin, stdout
n = int(input())
s = list(input())
tmp = list()
tmp += s
if len(set(s)) == 1:
print(0)
else:
ans = []
for i in range(n-1):
if s[i] == 'B':
s[i] = 'W'
s[i+1] = 'W' if s[i+1] == 'B' else 'B'
ans.append(str(i+1))
if len(set(s)) == 1:
print(len(ans))
print(' '.join(ans))
else:
s = tmp
ans = []
for i in range(n-1):
if s[i] == 'W':
s[i] = 'B'
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
ans.append(str(i+1))
if len(set(s)) == 1:
print(len(ans))
print(' '.join(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 java.util.*;
import java.lang.*;
import java.io.*;
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 readArray(FastReader scan, int[] a, int N) {
for (int i = 0; i < N; i++) {
a[i] = scan.nextInt();
}
}
public static void main(String[] args) {
B();
}
public static void B() {
FastReader scan = new FastReader();
StringBuilder output = new StringBuilder();
int N = scan.nextInt();
String S = scan.next();
int moves = 0;
int[] s = new int[N];
for(int i = 0; i < N; i++) {
if(S.charAt(i) == 'B') {
s[i] = 1;
}
else {
s[i] = 2;
}
}
for(int i = 0; i + 1 < N; i++) {
if(s[i] == 2) {
output.append(i + 1).append(' ');
s[i] = 1;
if(s[i + 1] == 2) {
s[i + 1] = 1;
}
else{
s[i + 1] = 2;
}
moves++;
}
}
if(s[N - 1] == 1) {
System.out.println(moves);
if(moves != 0) {
System.out.println(output);
}
}
else if(N % 2 == 0) {
System.out.println(-1);
}
else {
for(int i = 0; i < N - 1; i += 2) {
output.append(i + 1).append(' ');
moves++;
}
System.out.println(moves);
System.out.println(output);
}
}
}
| 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())
sequence = []
for i in range(n-1):
if s[i] =='B' and s[i+1] == 'W':
s[i] = 'W'
s[i+1] ='B'
sequence.append(i)
if s[i] =='B' and s[i+1] == 'B':
s[i] = 'W'
s[i+1] ='W'
sequence.append(i)
if s[n-1] == 'B':
if n %2 ==0:
sequence = [-1]
else:
sequence.extend([i for i in range(0, n-1, 2)])
if len(sequence) == 1 and sequence[0] == -1:
print (-1)
else:
if len(sequence)==0:
print(0)
else:
print(len(sequence))
print(*list(map(lambda x: x+1, sequence))) | 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;
string z;
cin >> n >> z;
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) {
if (z[i] == 'W')
c1++;
else
c2++;
}
if (c1 % 2 != 0 && c2 % 2 != 0) {
cout << -1;
return 0;
}
if (c1 % 2 == 0) {
vector<int> ans;
for (int i = 0; i < n - 1; i++) {
if (z[i] == 'W') {
if (z[i + 1] == 'W') {
ans.push_back(i + 1);
i++;
} else {
ans.push_back(i + 1);
z[i + 1] = 'W';
}
}
}
int counter = ans.size();
cout << counter << "\n";
for (int i = 0; i < counter; i++) {
cout << ans[i] << " ";
}
return 0;
}
if (c2 % 2 == 0) {
vector<int> ans;
for (int i = 0; i < n - 1; i++) {
if (z[i] == 'B') {
if (z[i + 1] == 'B') {
ans.push_back(i + 1);
i++;
} else {
ans.push_back(i + 1);
z[i + 1] = 'B';
}
}
}
int counter = ans.size();
cout << counter << "\n";
for (int i = 0; i < counter; i++) {
cout << ans[i] << " ";
}
return 0;
}
return 0;
}
| 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 minKBitFlips(A):
K=2
N = len(A)
hint = [0] * N
# ans = flip = 0
flip=0
ans=[]
# When we flip a subarray like A[i], A[i+1], ..., A[i+K-1]
# we can instead flip our current writing state, and put a hint at
# position i+K to flip back our writing state.
for i, x in enumerate(A):
flip ^= hint[i]
if x ^ flip == 0: # If we must flip the subarray starting here...
ans.append(i+1) # We're flipping the subarray from A[i] to A[i+K-1]
if i+K > N: return -1 # If we can't flip the entire subarray, its impossible
flip ^= 1
if i+K < N: hint[i + K] ^= 1
return ans
import sys
# # t=int(input())
# t=1
# for i in range(t):
# n,sx,sy=list(map(int,.split()))
n=int(input())
a=sys.stdin.readline().strip()
A=[]
B=[]
for i in range(n):
if(a[i]=="B"):
A.append(1)
B.append(0)
else:
A.append(0)
B.append(1)
# print(A,B,minKBitFlips(A),minKBitFlips(B))
x=minKBitFlips(A)
y=minKBitFlips(B)
if(x==-1 and y==-1):
print(-1)
else:
if(x==-1):
print(len(y))
print(" ".join(list(map(str,y))))
else:
print(len(x))
print(" ".join(list(map(str,x))))
# if(x)
| 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()
l = list(s)
protocol = []
i = 0
while i < n - 1:
if l[i:i + 2] == ['W', 'B'] and i < n - 2 and l[i + 2] == 'W':
l[i + 1] = 'W'
l[i + 2] = 'B'
protocol.append(i + 2)
i += 2
elif l[i:i + 2] == ['B', 'W']:
l[i] = 'W'
l[i + 1] = 'B'
protocol.append(i + 1)
i += 1
elif l[i:i + 2] == ['B', 'B']:
l[i] = 'W'
l[i + 1] = 'W'
protocol.append(i + 1)
i += 1
else:
i += 1
if l[n - 1] == 'B':
i = 0
while i < n - 1:
if l[i:i + 2] == ['B', 'W'] and i < n - 2 and l[i + 2] == 'B':
l[i + 1] = 'B'
l[i + 2] = 'W'
protocol.append(i + 2)
i += 2
elif l[i:i + 2] == ['W', 'B']:
l[i] = 'B'
l[i + 1] = 'W'
protocol.append(i + 1)
i += 1
elif l[i:i + 2] == ['W', 'W']:
l[i] = 'B'
l[i + 1] = 'B'
protocol.append(i + 1)
i += 1
else:
i += 1
if l[n - 1] == 'W':
print(-1)
else:
print(len(protocol))
print(' '.join(map(str, protocol)))
else:
print(len(protocol))
print(' '.join(map(str, protocol)))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int pos[N];
int main() {
int n;
cin >> n;
string s;
cin >> s;
s = " " + s;
string b = s;
int cnt = 0;
int i;
int s1 = 0, s2 = 0;
for (i = 1; i <= s.size(); i++) {
if (s[i] == 'W') s1++;
}
if (s1 == n || s1 == 0) {
cout << 0 << endl;
return 0;
}
for (i = 2; i <= s.size(); i++) {
if (s[i] == 'W' && s[i - 1] == 'W') {
pos[cnt++] = i - 1;
s[i] = s[i - 1] = 'B';
}
}
int sum = 0;
for (i = 1; i <= s.size(); i++) {
if (s[i] == 'W') sum++;
}
if (sum % 2) {
s = b;
int cnt = 0;
int i;
for (i = 2; i <= s.size(); i++) {
if (s[i] == 'B' && s[i - 1] == 'B') {
pos[cnt++] = i - 1;
s[i] = s[i - 1] = 'W';
}
}
int sum = 0;
for (i = 1; i <= s.size(); i++) {
if (s[i] == 'B') sum++;
}
if (sum % 2) {
cout << -1 << endl;
} else {
for (i = 1; i < s.size(); i++) {
if (s[i] == 'B') {
pos[cnt++] = i;
s[i] = 'W';
s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W';
}
}
if (cnt > 3 * n) {
cout << -1 << endl;
return 0;
}
cout << cnt << endl;
for (i = 0; i < cnt - 1; i++) {
cout << pos[i] << " ";
}
cout << pos[i] << endl;
}
return 0;
} else {
for (i = 1; i < s.size(); i++) {
if (s[i] == 'W') {
pos[cnt++] = i;
s[i] = 'B';
s[i + 1] = s[i + 1] == 'B' ? 'W' : 'B';
}
}
if (cnt > 3 * n) {
cout << -1 << endl;
return 0;
}
cout << cnt << endl;
for (i = 0; i < cnt - 1; i++) {
cout << pos[i] << " ";
}
cout << pos[i] << endl;
}
}
| 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 os
import sys
from io import BytesIO, IOBase
import math
import itertools
import bisect
import heapq
def main():
pass
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 binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c=0
while(n%2==0):
n//=2
c+=1
return c
def seive(n):
primes=[True]*(n+1)
primes[1]=primes[0]=False
for i in range(2,n+1):
if(primes[i]):
for j in range(i+i,n+1,i):
primes[j]=False
p=[]
for i in range(0,n+1):
if(primes[i]):
p.append(i)
return(p)
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def denofactinverse(n,m):
fac=1
for i in range(1,n+1):
fac=(fac*i)%m
return (pow(fac,m-2,m))
def numofact(n,m):
fac = 1
for i in range(1, n + 1):
fac = (fac * i) % m
return(fac)
def solve(ch):
ind=[]
for i in range(0,n):
if(s[i]==ch):
ind.append(i+1)
for i in range(1,len(ind),2):
for j in range(ind[i],ind[i-1],-1):
ans.append(j-1)
n=int(input())
s=list(input())
cnt1=s.count("W")
if(cnt1%2==1 and (n-cnt1)%2==1):
print(-1)
else:
ans=[]
if(cnt1%2==0):
solve("W")
else:
solve("B")
print(len(ans))
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<char> z;
long long x, y, dl, mi, ma, b, s, m, k, w, a[1000], ko;
char p;
int main() {
cin >> dl;
for (int i = 0; i < dl; i++) {
cin >> p;
z.push_back(p);
if (p == 'W')
w++;
else
b++;
}
if (w % 2 != 0 && b % 2 != 0) {
cout << -1;
return 0;
} else if (w % 2 == 0) {
for (int i = 0; i < dl; i++) {
if (z[i] == 'W' && z[i + 1] == 'W') {
a[ko] = i;
z[i] = 'B';
z[i + 1] = 'B';
ko++;
} else if (z[i] == 'W' && z[i + 1] == 'B') {
z[i] = 'B';
z[i + 1] = 'W';
a[ko] = i;
ko++;
}
}
cout << ko << endl;
for (int i = 0; i < ko; i++) cout << a[i] + 1 << " ";
} else if (b % 2 == 0) {
for (int i = 0; i < dl; i++) {
if (z[i] == 'B' && z[i + 1] == 'B') {
z[i] = 'W';
z[i + 1] = 'W';
a[ko] = i;
ko++;
} else if (z[i] == 'B' && z[i + 1] == 'W') {
z[i] = 'W';
z[i + 1] = 'B';
a[ko] = i;
ko++;
}
}
cout << ko << endl;
for (int i = 0; i < ko; i++) cout << a[i] + 1 << " ";
}
return 0;
}
| 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 = list(input())
w = a.count('W')
b = n-w
flag = True
if (w-b)%4==(n%4):
flag = False
c = 'W'
elif (b-w)%4==(n%4):
flag = False
c = 'B'
if flag:
print(-1)
else:
p = []
for i in range(n-1):
if a[i]!=c:
p.append(str(i+1))
a[i]=c
if(a[i+1]=='W'):
a[i+1]='B'
else:
a[i+1]='W'
print(len(p))
print(' '.join(p)) | 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 c = 0;
unordered_map<string, bool> memo;
bool sb(int i, string s, vector<int> moves, vector<int>& sol, int mreset) {
string rem = s;
if (memo.find(rem) != memo.end()) return memo.at(rem);
if (s.find('W') == string::npos || s.find('B') == string::npos) {
sol = moves;
memo[rem] = true;
return true;
}
if (i >= s.size() - 1) {
if (mreset == 0) {
memo[rem] = false;
return false;
}
mreset = 0;
i = 0;
}
if (moves.size() >= s.size() * 3) {
moves.clear();
memo[rem] = false;
return false;
}
if (s[i] == s[i + 1]) {
s[i] = (s[i] == 'B') ? 'W' : 'B';
s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B';
moves.push_back(i);
mreset++;
if (sb(i + 2, s, moves, sol, mreset)) {
return true;
}
}
if (sb(i + 1, s, moves, sol, mreset)) {
return true;
}
if (true) {
s[i] = (s[i] == 'B') ? 'W' : 'B';
s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B';
moves.push_back(i);
mreset++;
if (sb(i + 1, s, moves, sol, mreset)) {
return true;
}
}
memo[rem] = false;
return false;
}
int main() {
int n;
string s;
cin >> n >> s;
vector<int> sol;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
sol.push_back(i);
} else if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
sol.push_back(i);
}
}
const int nW = count(begin(s), end(s), 'W');
const int nB = count(begin(s), end(s), 'B');
if (nW % 2 != 0 && nB % 2 != 0) {
cout << -1 << endl;
} else {
if (nW % 2 != 0) {
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
sol.push_back(i);
}
}
} else {
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
sol.push_back(i);
}
}
}
cout << sol.size() << endl;
for (int i = 0; i < sol.size(); ++i) {
cout << sol[i] + 1;
if (i == sol.size() - 1)
cout << endl;
else
cout << " ";
}
}
return 0;
}
| 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())
r=list(input())
for t in range(len(r)):
if r[t]=='B':
r[t]=1
else:
r[t]=-1
g=0
m=0
l=[]
jk=[]
o=list(r)
for i in range(0,n-1):
if r[i]==-1:
r[i]=-r[i]
r[i+1]=-r[i+1]
l+=[i+1]
if r[n-1]!=1:
g=1
for q in range(0,n-1):
if o[q]==1:
o[q]=-o[q]
o[q+1]=-o[q+1]
jk+=[q+1]
if o[n-1]!=-1:
m=1
if m==1 and g==1:
print(-1)
if m!=1 and g==1:
print(len(jk))
for ty in jk:
print(ty,end=' ')
if m==1 and g!=1:
print(len(l))
for tyh in l:
print(tyh,end=' ')
if m!=1 and g!=1:
if len(l)<len(jk):
print(len(l))
for tye in l:
print(tye,end=' ')
else:
print(len(jk))
for tyw in jk:
print(tyw,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 | def check(target, a):
return target ^ 1 not in a
n = int(input())
a = [0 if ch == 'W' else 1 for ch in input()]
for target in (0, 1):
ret = []
t = a[::]
for i in range(n - 2):
sub = t[i:i + 3]
if sub == [target, target ^ 1, target ^ 1]:
ret.append(i + 2)
t[i + 1] = t[i + 2] = target
elif sub == [target ^ 1, target ^ 1, target]:
ret.append(i + 1)
t[i] = t[i + 1] = target
elif sub == [target ^ 1, target, target ^ 1]:
ret.extend([i + 1, i + 2])
t[i] = t[i + 2] = target
elif sub == [target ^ 1] * 3:
ret.append(i + 1)
t[i] = t[i + 1] = target
elif sub == [target, target ^ 1, target]:
ret.append(i + 2)
t[i + 1] = target
t[i + 2] = target ^ 1
if check(target, t):
print(len(ret))
print(*ret)
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 | n=int(input())
f=list(str(input()))
s=f*1
i=0
b=[];w=[]
while i<n-1:
if s[i]=='B' and s[i+1]=='B':
b.append(i+1)
s[i]='W';s[i+1]='W'
i+=2
elif s[i]=='B' and s[i+1]=='W':
b.append(i+1)
s[i],s[i+1]=s[i+1],s[i]
i+=1
else:
i+=1
if s.count('B')!=0:
b=[9]*10**5
s=f*1;i=0
while i<n-1:
if s[i]=='W' and s[i+1]=='W':
w.append(i+1)
s[i]='B';s[i+1]='B'
i+=2
elif s[i]=='W' and s[i+1]=='B':
w.append(i+1)
s[i],s[i+1]=s[i+1],s[i]
i+=1
else:
i+=1
if s.count('W')!=0:
w=[9]*10**5
if len(b)*len(w)==10**10:
print(-1)
elif len(b)*len(w)==0:
print(0)
else:
print(min(len(b),len(w)))
if len(b)<len(w):
for j in range(len(b)):
print(b[j],end=' ')
else:
for j in range(len(w)):
print(w[j],end=' ') | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout,setrecursionlimit
from itertools import repeat
# sys.stdin = open('input')
# n, k = map(int, raw_input().split())
# da = map(int, raw_input().split())
# db = map(int, raw_input().split())
def main():
n = map(int, raw_input().split())[0]
st = raw_input().strip()
ct = Counter(st)
ans = []
if ct['W'] == 0 or ct['B']==0:
print 0
return
if ct['B']%2==0:
idx = 0
while idx<n:
if st[idx] == 'B':
while st[idx+1]!='B':
ans.append(idx+1)
idx += 1
ans.append(idx+1)
idx += 2
else:
idx += 1
print len(ans)
print ' '.join(map(str, ans))
elif ct['W']%2==0:
idx = 0
while idx<n:
if st[idx] == 'W':
while st[idx+1]!='W':
ans.append(idx+1)
idx += 1
ans.append(idx+1)
idx += 2
else:
idx += 1
print len(ans)
print ' '.join(map(str, ans))
else:
print -1
main()
| 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;
int main() {
int n;
scanf("%d", &n);
string s, s1;
cin >> s;
s1 = s;
int i, j, k, l;
vector<int> v;
for (i = 0; i < n; i++) {
if (i + 1 < n && s[i] == 'B') {
v.push_back(i + 1);
s[i] = 'W';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
if (s[n - 1] == 'W') {
cout << v.size() << "\n";
for (i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
v.clear();
for (i = 0; i < n; i++) {
if (i + 1 < n && s1[i] == 'W') {
v.push_back(i + 1);
s1[i] = 'B';
if (s1[i + 1] == 'B')
s1[i + 1] = 'W';
else
s1[i + 1] = 'B';
}
}
if (s1[n - 1] == 'B') {
cout << v.size() << "\n";
for (i = 0; i < v.size(); i++) cout << v[i] << " ";
} else {
cout << "-1\n";
}
return 0;
}
| 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.*;
import java.io.*;
public class Solution{
long mod = 1000000007;
public Solution(){
Scanner sc = new Scanner(System.in);
int tests = 1;
for(int t=0; t<tests; t++){
int n = sc.nextInt();
String s = sc.next();
char[] arr = s.toCharArray();
int bCount = 0;
int wCount = 0;
for(int i=0; i<n; i++){
if(arr[i] == 'B'){
bCount++;
}
else{
wCount++;
}
}
if(bCount%2 == 1 && wCount%2 == 1){
System.out.println("-1");
continue;
}
ArrayList<Integer> res = new ArrayList<Integer>();
if(wCount%2 == 0){
for(int i=0; i<(n-1); i++){
if(arr[i] == 'W' && arr[i+1] == 'B'){
arr[i] = 'B';
arr[i+1] = 'W';
res.add(i+1);
}
else if(arr[i] == 'W' && arr[i+1] == 'W'){
arr[i] = 'B';
arr[i+1] = 'B';
res.add(i+1);
}
}
}
else{
for(int i=0; i<(n-1); i++){
if(arr[i] == 'B' && arr[i+1] == 'W'){
arr[i] = 'W';
arr[i+1] = 'B';
res.add(i+1);
}
else if(arr[i] == 'B' && arr[i+1] == 'B'){
arr[i] = 'W';
arr[i+1] = 'W';
res.add(i+1);
}
}
}
System.out.println(res.size());
if(res.size() > 0){
for(int i=0; i<(res.size()-1); i++){
System.out.print(res.get(i)+" ");
}
System.out.println(res.get(res.size()-1));
}
}
}
public static void main(String[] args){
new Solution();
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arrp[n];
int arrn[n];
char t;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
cin >> t;
if (t == 'B') {
b++;
arrp[i] = -1;
arrn[i] = -1;
} else {
w++;
arrp[i] = 1;
arrn[i] = 1;
}
}
if (n % 2 == 0 && b % 2 != 0) {
cout << -1;
return 0;
}
if (b == 0 || w == 0) {
cout << 0;
return 0;
}
vector<int> ind;
ind.reserve(n);
int changes = 0;
bool noChange = true;
bool solved = true;
for (int i = 0; i < n; i++) {
if (arrp[i] == -1) {
if (i == n - 1) {
solved = false;
} else {
arrp[i] *= -1;
changes++;
arrp[i + 1] *= -1;
ind.push_back(i + 1);
}
}
}
if (!solved) {
solved = true;
ind.clear();
changes = 0;
for (int i = 0; i < n; i++) {
if (arrn[i] == 1) {
if (i == n - 1) {
solved = false;
} else {
arrn[i] *= -1;
arrn[i + 1] *= -1;
ind.push_back(i + 1);
changes++;
}
}
}
}
if (solved) {
cout << changes << endl;
for (auto i : ind) {
cout << i << " ";
}
} else {
cout << -1;
}
return 0;
}
| 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B_Blocks {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder(br.readLine());
int b = 0;
int w = 0;
for(int i = 0; i < N; i++) {
char c = sb.charAt(i);
if(c == 'W') w++;
}
b = N - w;
StringBuilder result = new StringBuilder();
int cnt = 0;
if(w % 2 == 1 && b % 2 == 1) {
System.out.println(-1);
return;
}
else if(w == 0 || b == 0) {
System.out.println(0);
return;
}
else {
char color = 'W';
if(w % 2 == 0 && b % 2 == 0) {
if(w < b) color = 'B';
}
else if(w % 2 == 0) {
color = 'B';
}
for(int i = 0; i < N-1; i++) {
if(sb.charAt(i) != color) {
cnt++;
result.append(i+1 + " ");
char tmp = sb.charAt(i+1);
if(tmp == 'W') sb.setCharAt(i+1, 'B');
else sb.setCharAt(i+1, 'W');
}
}
result.deleteCharAt(result.length()-1);
}
System.out.println(cnt);
System.out.println(result);
}
}
| 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>
int main() {
int n;
std::cin >> n;
std::vector<char> str(n);
int cnt[2] = {0, 0};
for (int i = 0; i < n; i++) {
std::cin >> str[i];
str[i] = str[i] == 'B' ? 0 : 1;
cnt[str[i]]++;
}
if (cnt[0] % 2 && cnt[1] % 2) {
printf("-1\n");
return 0;
}
bool need = (cnt[0] % 2) ? 0 : 1;
std::vector<int> ans;
for (int i = 0; i < n; i++) {
if (str[i] != need) {
str[i] = !str[i];
str[i + 1] = !str[i + 1];
ans.push_back(i);
}
}
printf("%d\n", (int)ans.size());
for (auto x : ans) {
printf("%d ", x + 1);
}
puts("");
return 0;
}
| 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())
l=list(input())
b=0
w=0
for i in l:
if i=='B':
b+=1
else:
w+=1
if b%2==w%2==1:
print(-1)
elif b==n or w==n:
print(0)
else:
ans=[]
s=l
if w%2==0:
for i in range(n-1):
if s[i]=='W':
if s[i+1]=='B':
ans.append(i+1)
s[i+1]='W'
elif s[i+1]=='W':
s[i+1]='B'
ans.append(i+1)
print(len(ans))
print(*ans)
else:
for i in range(n-1):
if s[i]=='B':
if s[i+1]=='W':
ans.append(i+1)
s[i+1]='B'
elif s[i+1]=='B':
s[i+1]='W'
ans.append(i+1)
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
l = [int(c == 'B') for c in input()]
res = []
for e in [1, 0]:
for i in range(n - 1):
if l[i] == e:
l[i] ^= 1
l[i + 1] ^= 1
res.append(i + 1)
if sum(l) == (e ^ 1) * n:
print(len(res))
print(*res, sep=' ')
exit(0)
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int ans[1000];
int ans2[1000];
int main() {
int n;
scanf("%d", &n);
string a;
cin >> a;
string b = a;
int flag1 = 1;
int flag2 = 1;
int cnt1 = 0;
int cnt2 = 0;
for (int i = 0; i < a.length() - 1; i++) {
if (a[i] == 'B') {
ans[++cnt1] = i + 1;
a[i] = 'W';
if (a[i + 1] == 'B')
a[i + 1] = 'W';
else
a[i + 1] = 'B';
}
}
for (int i = 0; i < a.length(); i++) {
if (a[i] == 'B') {
flag1 = 0;
break;
}
}
for (int i = 0; i < b.length() - 1; i++) {
if (b[i] == 'W') {
ans2[++cnt2] = i + 1;
b[i] = 'B';
if (b[i + 1] == 'B')
b[i + 1] = 'W';
else
b[i + 1] = 'B';
}
}
for (int i = 0; i < b.length(); i++) {
if (b[i] == 'W') {
flag2 = 0;
break;
}
}
if (!flag1 && !flag2) {
printf("-1");
return 0;
}
if (flag1) {
printf("%d\n", cnt1);
for (int i = 1; i <= cnt1; i++) printf("%d ", ans[i]);
return 0;
}
if (flag2) {
printf("%d\n", cnt2);
for (int i = 1; i <= cnt2; i++) printf("%d ", ans2[i]);
}
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
w=s.count('W')
b=n-w
f = lambda c: 'B' if c=='W' else 'W'
if not b&1:
l = []
for i in range(n-1):
if s[i]=='B':
s[i],s[i+1]=f(s[i]),f(s[i+1])
l.append(i+1)
le=len(l)
print(le)
if le:
print(*l)
elif not w&1:
l = []
for i in range(n-1):
if s[i]=='W':
s[i],s[i+1]=f(s[i]),f(s[i+1])
l.append(i+1)
le=len(l)
print(le)
if le:
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 sys
import collections
import heapq
import math
import bisect
input = sys.stdin.readline
def rints(): return map(int, input().strip().split())
def rstr(): return input().strip()
def rint(): return int(input().strip())
def rintas(): return [int(i) for i in input().strip().split()]
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a%b)
n = rint()
s = [i for i in rstr()]
ans = []
counts = collections.Counter(s)
# if n&1 == 0 and ((counts['B'] + (counts['W']//2))&1) and (counts['W'] + (counts['B']//2))&1:
# print(-1)
# exit()
for i in range(n):
if i+1 < n:
if s[i] == 'B' and s[i+1] == 'W':
s[i], s[i+1] = 'W', 'B'
ans.append(i)
elif s[i] == 'B' and s[i+1] == 'B':
s[i], s[i+1] = 'W', 'W'
ans.append(i)
# print(s)
if s[-1] == 'B':
for i in range(0, n-1, 2):
if s[i] == 'W' and i+2 < n:
s[i], s[i+1] = 'B', 'B'
ans.append(i)
# print(s)
cnts = collections.Counter(s)
if cnts['B'] != n and cnts['W'] != n:
print(-1)
exit()
print(len(ans))
print(" ".join([str(i+1) for i in ans]))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | a = int(input())
b = input()
sum = 0
line = []
for i in range(a):
if b[i] == 'B':
sum+=1
line.append(1)
else:
line.append(0)
if sum % 2 == a % 2:
ans = []
if a % 2 == 1:
for i in range(a-1):
if line[i] == 0:
line[i] = 1
line[i+1] = abs(line[i+1]-1)
ans.append(i+1)
for i in range(1, a, -1):
if line[i] == 0:
line[i] = 1
line[i+1] = abs(line[i+1]-1)
ans.append(i+1)
else:
for i in range(a-1):
if line[i] == 1:
line[i] = 0
line[i+1] = abs(line[i+1]-1)
ans.append(i+1)
for i in range(1, a, -1):
if line[i] == 1:
line[i] = 0
line[i+1] = abs(line[i+1]-1)
ans.append(i+1)
print(len(ans))
print(*ans)
elif sum % 2 == 0 and a % 2 == 1:
ans = []
for i in range(a-1):
if line[i] == 1:
line[i] = 0
line[i+1] = abs(line[i+1]-1)
ans.append(i+1)
for i in range(1, a, -1):
if line[i] == 1:
line[i] = 0
line[i+1] = abs(line[i+1]-1)
ans.append(i+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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string str;
cin >> str;
int cw = 0, cb = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'W')
cw++;
else
cb++;
}
if (cb % 2 != 0 && cw % 2 != 0) {
cout << "-1" << endl;
return 0;
}
vector<int> ans;
while (!(cb == 0 || cw == 0)) {
if (cb < cw) {
for (int i = 0; i < n - 1; i++) {
if (str[i] == 'W') {
ans.push_back(i + 1);
str[i] = 'B';
if (str[i + 1] == 'B') {
str[i + 1] = 'W';
} else {
str[i + 1] = 'B';
}
}
}
cw = cb = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'W')
cw++;
else
cb++;
}
} else {
for (int i = 0; i < n - 1; i++) {
if (str[i] == 'B') {
ans.push_back(i + 1);
str[i] = 'W';
if (str[i + 1] == 'B') {
str[i + 1] = 'W';
} else {
str[i + 1] = 'B';
}
}
}
cw = cb = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'W')
cw++;
else
cb++;
}
}
}
cout << ans.size() << endl;
for (auto c : ans) cout << c << " ";
return 0;
}
| 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=[0]*n
cnt=[0,0]
for i,v in enumerate(input()):
if v=='B':
a[i]=1
cnt[1]+=1
else:
a[i]=0
cnt[0]+=1
if cnt[0]%2==1 and 1==cnt[1]%2:
print(-1)
else:
total=0
ans=[]
for i in range(n-1):
if a[i]==0:
total+=1
ans.append(i+1)
a[i]=1
if a[i+1]==1:
a[i+1]=0
else:
a[i+1]=1
cnt1=a.count(1)
cnt0=a.count(0)
if cnt0>0 and cnt1%2==0:
total+=cnt1//2
for i in range(1,cnt1+1,2):
ans.append(i)
print(total)
# print(a)
for i in ans:
print(i,end=' ') | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
ilo=int(input())
kloc=list(input())
iloW=0
iloB=0
kol="W"
iloZ=0
zmia=''
for i in range(ilo):
if kloc[i]=='W':
iloW+=1
else:
iloB+=1
if iloW==ilo:
print(0)
sys.exit()
elif iloB==ilo:
print(0)
sys.exit()
if (iloW%2==1)and(iloB%2==1):
print(-1)
sys.exit()
if iloB%2==1:
kol="B"
for i in range(ilo-1):
if kloc[i]!=kol:
iloZ+=1
zmia+=(str(i+1)+' ')
kloc[i]=kol
if kloc[i+1]=='B':
kloc[i+1]='W'
else:
kloc[i+1]='B'
print(iloZ)
print(zmia)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
String str = sc.next();
int cntW = 0, cntB = 0;
char[] ch = new char[n];
int[] num = new int[n];
for(int i = 0; i < n; i++) {
ch[i] = str.charAt(i);
if(ch[i] == 'W') cntW++;
else cntB++;
}
if(cntW % 2 == 0 && cntB % 2 == 0) {
int k = 0;
for(int i = 0; i < n - 1; i++) {
if(ch[i] == 'B') {
ch[i+1] = ((ch[i+1] == 'W')? 'B' : 'W');
num[k] = i+1;
k++;
}
}
System.out.println(k);
if(k > 0) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < k; i++) {
sb.append(num[i]);
sb.append(" ");
}
System.out.println(sb.toString().trim());
}
} else if(cntW % 2 == 0) {
int k = 0;
boolean flag = true;
for(int i = 0; i < n - 1; i++) {
if(ch[i] == 'W') {
ch[i+1] = ((ch[i+1] == 'W')? 'B' : 'W');
num[k] = i+1;
k++;
}
}
System.out.println(k);
if(k > 0) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < k; i++) {
sb.append(num[i]);
sb.append(" ");
}
System.out.println(sb.toString().trim());
}
} else if(cntB % 2 == 0) {
int k = 0;
boolean flag = true;
for(int i = 0; i < n - 1; i++) {
if(ch[i] == 'B') {
ch[i+1] = ((ch[i+1] == 'W')? 'B' : 'W');
num[k] = i+1;
k++;
}
}
System.out.println(k);
if(k > 0) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < k; i++) {
sb.append(num[i]);
sb.append(" ");
}
System.out.println(sb.toString().trim());
}
} 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 sys
# sys.setrecursionlimit(10**6)
from sys import stdin, stdout
import bisect #c++ upperbound
import math
import heapq
def modinv(n,p):
return pow(n,p-2,p)
def cin():
return map(int,sin().split())
def ain(): #takes array as input
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
import math
def Divisors(n) :
l = []
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
return l
"""*******************************************************"""
def main():
n=inin()
s=sin()
a=[]
j=k=0
for i in s:
if(i=="W"):
a.append(1)
j+=1
else:
a.append(0)
k+=1
ans=[]
if(j%2==1 and k%2==1):
print(-1)
else:
for i in range(1,n-1):
# print(a,i,i-1)
if(a[i]!=a[i-1]):
ans.append(i+1)
a[i]^=1
a[i+1]^=1
for i in range(n-2,0,-1):
if(a[i]!=a[i+1]):
ans.append(i)
a[i]^=1
a[i-1]^=1
print(len(ans))
if(len(ans)>0):
print(*ans)
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'0' [0]:
A.append(sign*numb)
return A
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 sys
def rev(s):
if s == "W":
return "B"
else:
return "W"
n = int(input())
p = list(input())
w = 0
b = 0
for i in p:
if i == "B":
b += 1
else:
w += 1
if w % 2 == 1 and b % 2 == 1:
print (-1)
sys.exit()
if b % 2 == 1: #all b
ans = []
for i in range(n-1):
if p[i] == "W":
ans.append(i+1)
p[i+1] = rev(p[i+1])
else:
ans = []
for i in range(n-1):
if p[i] == "B":
ans.append(i+1)
p[i+1] = rev(p[i+1])
print (len(ans))
print (" ".join(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 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
bool a[n];
char c;
for (int i = 0; i < n; ++i) {
cin >> c;
a[i] = (c == 'W');
}
bool b[2][n];
for (int i = 0; i < n; ++i) {
b[0][i] = 0;
b[1][i] = 1;
}
vector<int> ans;
for (int z = 0; z < 2; ++z) {
ans.clear();
for (int i = 0; i < n - 1; ++i) {
if (b[z][i] != a[i]) {
b[z][i] = !b[z][i];
b[z][i + 1] = !b[z][i + 1];
ans.push_back(i + 1);
}
}
if (b[z][n - 1] != a[n - 1]) {
continue;
}
cout << ans.size() << '\n';
for (int i : ans) {
cout << i << ' ';
}
cout << '\n';
return 0;
}
cout << -1 << '\n';
return 0;
}
| 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.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.math.*;
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) throws java.lang.Exception {
FastReader sc=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int t=1;//sc.nextInt();
while(t-->0){
int n=sc.nextInt();
String s=sc.next();
char c[]=s.toCharArray();
char ch=(c[0]=='B')?'W':'B';
List<Integer> l=new ArrayList<>();
int index=s.indexOf(ch),temp=0;
if(index==-1)pw.println(0);
else
{
for(int i=index;i<n-1;i++)
{
if(c[i]==ch)
{
l.add(i+1);
c[i]=(ch=='W')?'B':'W';
c[i+1]=(c[i+1]=='W')?'B':'W';
}
}
if(c[n-1]==ch)
{
if((n-1)%2==0){
for(int i=0;i<n-1;i+=2)
l.add(i+1);
}
else
{
pw.println(-1);temp=1;
}
}
if(temp==0)
{
pw.println(l.size());
for(int w: l) pw.print(w+" ");
}
}
}
pw.close();
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
bk = list(input())
if bk.count('W') % 2 == 1 and bk.count('B') % 2 == 1:
print(-1)
else:
ans = []
if bk.count('W') % 2 == 0 and bk.count('B') % 2 == 0:
if bk.count('W') > bk.count('B'):
c = 'W'
else:
c = 'B'
elif bk.count('W') % 2 == 0:
c = 'B'
else:
c = 'W'
for i in range(n - 1):
if bk[i] != c:
ans.append(i + 1)
if bk[i] == 'W':
bk[i] = 'B'
else:
bk[i] = 'W'
if bk[i + 1] == 'W':
bk[i + 1] = 'B'
else:
bk[i + 1] = 'W'
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 | i1=int(input())
ar=list(input())
if(ar.count('W')%2!=0 and ar.count('B')%2!=0):
print(-1)
else:
if(ar.count('W')%2==0):
count=0
swap=[]
for i in range(len(ar)-1):
if(ar[i]=='W' and ar[i+1]=='W'):
count+=1
swap.append(str(i+1))
ar[i]='B'
ar[i+1]='B'
elif(ar[i]=='W' and ar[i+1]=='B'):
ar[i]='B'
ar[i+1]='W'
count+=1
swap.append(str(i+1))
elif(ar.count('B')%2==0):
count=0
swap=[]
for i in range(len(ar)-1):
if(ar[i]=='B' and ar[i+1]=='B'):
count+=1
swap.append(str(i+1))
ar[i]='W'
ar[i+1]='W'
elif(ar[i]=='B' and ar[i+1]=='W'):
ar[i]='W'
ar[i+1]='B'
count+=1
swap.append(str(i+1))
print(count)
print(' '.join(swap))
| 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()
if s.count("W")%2 == 1 and s.count("B")%2 == 1:
print(-1)
exit()
l = []
for i in range(n):
l.append(s[i])
if l.count("W")%2 == 1:
t = "B"
t1 = "W"
else:
t = "W"
t1 = "B"
ans = []
# print(l)
# while l.count(t1) == len(t1)
for i in range(n-1):
# print(*l)
if t == "B" and l[i] == "B":
# print("--",i)
l[i] = "W"
if l[i+1] == "B":
l[i+1] = "W"
else:
l[i+1] = "B"
# print("--",*l)
ans.append(i+1)
elif t == "W" and l[i] == "W":
l[i] = "B"
if l[i+1] == "B":
l[i+1] = "W"
else:
l[i+1] = "B"
ans.append(i+1)
# print(l)
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.*;
public class CF608B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
char[] blocks = br.readLine().toCharArray();
List<Integer> ans = solve(blocks);
if (ans == null) bw.write("-1\n");
else {
bw.write(ans.size() + "\n");
StringBuilder seq = new StringBuilder();
for (int pos : ans) {
if (seq.length() > 0) seq.append(" ");
seq.append(String.valueOf(pos));
}
if (seq.length() > 0) seq.append("\n");
bw.write(seq.toString());
}
bw.flush();
}
private static List<Integer> solve(char[] blocks) {
int countB = 0;
int countW = 0;
for (int ii = 0 ; ii < blocks.length; ii++) {
if (blocks[ii] == 'B') countB++;
else countW++;
}
if (countB%2 == 1 && countW%2 == 1) return null;
List<Integer> ret = new ArrayList<>();
char c = countW%2 == 0 ? 'W' : 'B';
for (int ii = 0; ii < blocks.length-1; ii++) {
if (blocks[ii] == c && blocks[ii+1] == c) ret.add(++ii);
else if (blocks[ii] == c) {
blocks[ii+1] = c;
ret.add(ii+1);
}
}
return ret;
}
}
/*
BBWW
*/ | 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(NULL);
cout.tie(NULL);
long long T = 1;
while (T--) {
long long n, b = 0, w = 0;
cin >> n;
string s;
cin >> s;
for (long long i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b % 2 && w % 2) {
cout << "-1\n";
continue;
}
vector<long long> v;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
v.push_back(i + 1);
s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B';
}
}
if (s[n - 1] == 'W')
for (long long i = 0; i < n - 2; i += 2) v.push_back(i + 1);
cout << v.size() << "\n";
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << "\n";
}
return 0;
}
| 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;
void solve() {
long long n;
cin >> n;
string str;
cin >> str;
vector<char> sv, sb;
for (long long i = 0; i < n; i++) {
sv.push_back(str[i]);
sb.push_back(str[i]);
}
vector<long long> ans;
for (long long i = 0; i < n - 1; i++) {
if (sv[i] == 'B') {
ans.push_back(i + 1);
sv[i] = 'W';
if (str[i + 1] == 'B')
sv[i + 1] = 'W';
else
sv[i + 1] = 'B';
}
}
if (sv[n - 1] == 'W') {
cout << ans.size() << endl;
for (long long i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return;
}
ans.clear();
for (long long i = 0; i < n - 1; i++) {
if (sb[i] == 'W') {
ans.push_back(i + 1);
sb[i] = 'B';
if (sb[i + 1] == 'W')
sb[i + 1] = 'B';
else
sb[i + 1] = 'W';
}
}
if (sb[n - 1] == 'B') {
cout << ans.size() << endl;
for (long long i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return;
}
cout << -1;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string c;
cin >> c;
int cntb = 0, cntw = 0;
for (int i = 0; i < n; i++) {
if (c[i] == 'B')
cntb++;
else
cntw++;
}
if (cntb % 2 == 1 && cntw % 2 == 1) {
cout << -1;
return 0;
}
vector<int> p;
char swp = 'W', neg = 'B';
if (cntw % 2) swp = 'B', neg = 'W';
for (int i = 1; i < n; i++) {
if (c[i] == c[i - 1] && c[i] == swp) {
c[i] = c[i - 1] = neg;
p.push_back(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (c[i] == swp) {
while (c[i + 1] != swp && i < n - 1) {
c[i] = neg;
c[i + 1] = swp;
i++;
p.push_back(i);
}
c[i] = c[i + 1] = neg;
p.push_back(i + 1);
}
}
cerr << c << '\n';
cout << p.size() << '\n';
for (auto i : p) cout << i << " ";
return 0;
}
| 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;
unsigned long long gcd(unsigned long long n, unsigned long long m) {
if (m == 0)
return n;
else
return gcd(m, n % m);
}
int longestsub(string x, string y, int n, int m) {
int lcs[n + 1][m + 1];
int result = 0;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < m + 1; j++) {
if (i == 0 || j == 0) {
lcs[i][j] = 0;
} else if (x[i - 1] == y[j - 1]) {
lcs[i][j] = 1 + lcs[i - 1][j - 1];
result = max(result, lcs[i][j]);
} else
lcs[i][j] = 0;
}
}
return result;
}
unsigned long long fast_pow(unsigned long long a, unsigned long long p) {
unsigned long long res = 1;
while (p) {
if (p % 2 == 0) {
a = a * 1ll * a % 1000000007;
p /= 2;
} else {
res = res * 1ll * a % 1000000007;
p--;
}
}
return res;
}
unsigned long long fact(unsigned long long n) {
unsigned long long res = 1;
for (int i = 1; i <= n; i++) {
res = res * 1ll * i % 1000000007;
}
return res;
}
unsigned long long C(unsigned long long n, unsigned long long k) {
return fact(n) * 1ll * fast_pow(fact(k), 1000000007 - 2) % 1000000007 * 1ll *
fast_pow(fact(n - k), 1000000007 - 2) % 1000000007;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
while (t--) {
int n;
cin >> n;
string s, z;
cin >> s;
z = s;
vector<int> v, l;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else if (s[i + 1] == 'B')
s[i + 1] = 'W';
v.push_back(i);
}
if (z[i] == 'W') {
z[i] = 'B';
if (z[i + 1] == 'W')
z[i + 1] = 'B';
else if (z[i + 1] == 'B')
z[i + 1] = 'W';
l.push_back(i);
}
}
if (s[n - 1] == 'B' and z[n - 1] == 'W')
cout << -1 << '\n';
else {
if (s[n - 1] != 'B') {
cout << (int)v.size() << '\n';
for (auto i : v) {
cout << i + 1 << " ";
}
cout << '\n';
} else {
cout << (int)l.size() << '\n';
for (auto i : l) {
cout << i + 1 << " ";
}
cout << '\n';
}
}
}
}
| 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())
stri = input()
stringa = []
for i in range(len(stri)):
stringa.append(stri[i])
counta = 0
countb = 0
for i in range(len(stringa)):
if(stringa[i] == 'W'):
counta += 1
else:
countb += 1
if(counta%2 == 1 and countb%2 == 1):
print(-1)
elif(counta == n or countb == n):
print(0)
else:
if(counta%2 == 0):
ans = []
for i in range(len(stringa)-1):
if(stringa[i] == 'W'):
ans.append(i)
if(stringa[i+1] == 'W'):
stringa[i+1] = 'B'
else:
stringa[i+1] = 'W'
elif(countb%2 == 0):
ans = []
for i in range(len(stringa)-1):
if(stringa[i] == 'B'):
ans.append(i)
if(stringa[i+1] == 'W'):
stringa[i+1] = 'B'
else:
stringa[i+1] = 'W'
for i in range(len(ans)):
ans[i] = ans[i] + 1
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
void IO() {}
template <class T>
void unisort(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void reverse(string& string) {
long long n = string.length();
for (long long i = 0; i < n / 2; i++) swap(string[i], string[n - i - 1]);
}
long long fact(long long nb) {
long long res = 1;
for (long long i = 2; i <= nb; i++) res *= i;
return res;
}
bool sortbysec(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
if (a.second == b.second) {
return a.first < b.first;
}
return (a.second < b.second);
}
vector<string> v;
void subsequences(const string& a, const string& suffix) {
if (a.length() >= 1) {
v.push_back(a);
}
for (size_t i = 0; i < suffix.length(); ++i)
subsequences(a + suffix[i], suffix.substr(i + 1));
}
int32_t main() {
IO();
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
long long n, b, w;
string s;
char c;
cin >> n >> s;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (w % 2 && b % 2) {
cout << -1;
return 0;
}
vector<long long> v;
if (w % 2 == 0)
c = 'B';
else
c = 'W';
w = 0;
for (long long i = 0; i < s.size(); i++) {
if (w % 2 == 1) v.push_back(i);
if (s[i] != c) w++;
}
cout << v.size() << "\n";
for (long long i = 0; i < v.size(); i++) cout << v[i] << ' ';
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili=lambda:list(map(int,sys.stdin.readlines()))
li = lambda:list(map(int,input().split()))
#for deque append(),pop(),appendleft(),popleft(),count()
I=lambda:int(input())
S=lambda:input().strip()
mod = 1000000007
def swap(a):
if(a=='W'):
return('B')
return('W')
n=I()
s=S()
s=list(s)
t=s.copy()
ans=[]
ans1=[]
f=0
g=0
for i in range(0,n-1):
if(s[i]=='B'):
s[i]='W'
s[i+1]=swap(s[i+1])
ans.append(i+1)
if(s[n-1]=='W'):
f=1
for i in range(0,n-1):
if(t[i]=='W'):
t[i]='B'
t[i+1]=swap(t[i+1])
ans1.append(i+1)
if(t[n-1]=='B'):
g=1
if(f):
print(len(ans))
if(len(ans)):
print(*ans)
elif(g):
print(len(ans1))
if(len(ans1)):
print(*ans1)
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 minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int,minp().split())
def change(s, i):
if s[i] == 'W':
s[i] = 'B'
else:
s[i] = 'W'
def check(s, c):
r = []
for i in range(len(s)-1):
if s[i] != c:
r.append(i+1)
change(s, i)
change(s, i+1)
if s == [c]*len(s):
return r
else:
return None
n = mint()
s = list(minp())
r = check(s.copy(), 'B')
if r == None:
r = check(s.copy(), 'W')
if r != None:
print(len(r))
print(*r)
else:
print(-1) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.