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 | n=int(input())
s=list(input())
count=0
l=[]
cb=0
cw=0
def swap(char ):
if char=="W":
return "B"
else:
return "W"
for i in range(len(s)):
if s[i]=="B":
cb+=1
else:
cw+=1
if (cb%2!=0) and (cw%2!=0):
print(-1)
elif( cb==0 or cw==0):
print(0)
else:
for i in range(len(s)-1):
#print(s, cb, cw, "-----1")
if s[i]=="B":
s[i]=swap(s[i])
cw+=1
cb-=1
s[i+1]=swap(s[i+1])
l.append(i+1)
if s[i+1]=="B":
cb+=1
cw-=1
else:
cw+=1
cb-=1
if(cb==0 or cw==0):
break
if(cb!=0 and cw!=0):
for i in range(len(s)-1):
#print(s, cb, cw, "-----0")
if s[i]=="W":
s[i]=swap(s[i])
cw-=1
cb+=1
s[i+1]=swap(s[i+1])
l.append(i+1)
if s[i+1]=="B":
cb+=1
cw-=1
else:
cw+=1
cb-=1
if(cb==0 or cw==0):
break
print(len(l))
print(*l) | 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
def flip_colors(s, i):
if s[i] == 'W':
s[i] = 'B'
else:
s[i] = 'W'
if __name__ == '__main__':
n = int(input())
s = list(input())
wb = Counter(s)
if wb['W'] < wb['B']:
min_wb = wb['W']
min_wb_color = 'W'
else:
min_wb = wb['B']
min_wb_color = 'B'
if n % 2 == 0 and min_wb % 2 == 1:
print(-1)
else:
ans = 0
ans_seq = []
for i in range(1, n):
if s[i - 1] == min_wb_color:
ans_seq.append(i - 1 + 1)
ans += 1
flip_colors(s, i - 1)
flip_colors(s, i)
if s[i - 1] != s[i]:
for i in range(0, n - 1, 2):
ans_seq.append(i + 1)
ans += 1
print(ans)
print(' '.join(map(str, ans_seq)))
| 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<int> v;
int main() {
int numOfBlocks, white = 0, black = 0;
string block;
cin >> numOfBlocks >> block;
for (int i = 0; i < numOfBlocks; i++) {
if (block[i] == 'W')
white++;
else
black++;
}
if (white == 0 || black == 0) {
cout << 0;
return 0;
}
if (white % 2 == 0 || black % 2 == 0) {
int j;
if (black % 2 != 0) {
for (int i = 0; i < numOfBlocks; i++) {
if (block[i] == 'W') {
j = i + 1;
while (block[j] != 'W') {
v.push_back(j);
if (block[j - 1] == 'B')
block[j - 1] = 'W';
else
block[j - 1] = 'B';
if (block[j] == 'B')
block[j] = 'W';
else
block[j] = 'B';
j++;
}
v.push_back(j);
if (block[j - 1] == 'B')
block[j - 1] = 'W';
else
block[j - 1] = 'B';
if (block[j] == 'B')
block[j] = 'W';
else
block[j] = 'B';
}
}
} else {
for (int i = 0; i < numOfBlocks; i++) {
if (block[i] == 'B') {
j = i + 1;
while (block[j] != 'B') {
v.push_back(j);
if (block[j - 1] == 'B')
block[j - 1] = 'W';
else
block[j - 1] = 'B';
if (block[j] == 'B')
block[j] = 'W';
else
block[j] = 'B';
j++;
}
v.push_back(j);
if (block[j - 1] == 'B')
block[j - 1] = 'W';
else
block[j - 1] = 'B';
if (block[j] == 'B')
block[j] = 'W';
else
block[j] = 'B';
}
}
}
cout << v.size() << "\n";
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
} else {
cout << -1;
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 | import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;
public class temp {
void solve() throws IOException {
FastReader sc = new FastReader();
int n = sc.nextInt();
char s[] = sc.next().toCharArray();
char p[] = new char[n];
for(int i=0;i<n;i++)
p[i] = s[i];
ArrayList<Integer> B = new ArrayList<>();
ArrayList<Integer> W = new ArrayList<>();
boolean w = true , b = true;
for(int i=0;i<n;i++)
{
if(s[i]=='W')
{
if(i==(n-1))
b = false;
else
{
s[i] = 'B';
s[i+1] = (s[i+1]=='B' ? 'W' : 'B');
B.add((i+1));
}
}
}
for(int i=0;i<n;i++)
{
if(p[i]=='B')
{
if(i==(n-1))
w = false;
else
{
p[i] = 'W';
p[i+1] = (p[i+1]=='B' ? 'W' : 'B');
W.add((i+1));
}
}
}
if(!w && !b)
System.out.println(-1);
else if(w && b)
{
int min = Math.min(W.size(),B.size());
System.out.println(min);
if(W.size()==min)
{
for(int i : W)
System.out.print(i+" ");
}
else
{
for(int i : B)
System.out.print(i+" ");
}
}
else
{
if(w)
{
System.out.println(W.size());
for(int i : W)
System.out.print(i+" ");
}
else
{
System.out.println(B.size());
for(int i : B)
System.out.print(i+" ");
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new temp().solve();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | l = int(input())
s = list(input())
counts = {'B':0, 'W':0}
for i in range(l):
counts[s[i]] += 1
if counts['B']%2==1 and counts['W']%2==1:
print(-1)
exit(0)
if counts['B']%2==0:
c = 'B'
ci = 'W'
elif counts['W']%2==0:
c = 'W'
ci = "B"
# print(c, ci)
actions = []
for i in range(l-1):
if s[i] == c:
actions.append(i+1)
s[i] = ci
if s[i+1] == c:
s[i+1] = ci
elif s[i+1] == ci:
s[i+1] = c
# print(s)
print(len(actions))
if len(actions)>0:
print(*actions)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import os
import sys
from io import BytesIO, IOBase
def solution(s, n):
ss, res = list(s), []
for i in range(n - 1):
if ss[i] == 'B':
ss[i] = 'W'
ss[i + 1] = 'W' if s[i + 1] == 'B' else 'B'
res.append(i + 1)
if 'B' not in ss:
write(len(res))
write(*res)
return
ss, res = list(s), []
for i in range(n - 1):
if ss[i] == 'W':
ss[i] = 'B'
ss[i + 1] = 'B' if s[i + 1] == 'W' else 'W'
res.append(i + 1)
if 'W' not in ss:
write(len(res))
write(*res)
return
write(-1)
def main():
n = r_int()
s = input()
solution(s, n)
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 input():
return sys.stdin.readline().rstrip("\r\n")
def write(*args, end='\n'):
for x in args:
sys.stdout.write(str(x) + ' ')
sys.stdout.write(end)
def r_array():
return [int(x) for x in input().split()]
def r_int():
return int(input())
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())
a = list(input())
q = a.copy()
ans=[]
count = 0
for i in range(n-1):
if a[i] == 'W' and a[i+1] =='B':
a[i] ='B'
a[i+1] = 'W'
ans.append(i+1)
count = count +1
elif a[i] == 'W' and a[i+1] =='W':
a[i] ='B'
a[i+1] = 'B'
ans.append(i+1)
count =count+1
if a[n-1] == 'B':
print(count)
ans = ' '.join(list(map(str,ans)))
print(ans)
else:
count = 0
ans = []
a= q
for i in range(n-1):
if a[i] == 'B' and a[i+1] =='W':
a[i] ='W'
a[i+1] = 'B'
ans.append(i+1)
count =count+1
elif a[i] == 'B' and a[i+1] =='B':
a[i] ='W'
a[i+1] = 'W'
ans.append(i+1)
count =count+1
if a[n-1] == 'W':
print(count)
ans = ' '.join(list(map(str,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 | from sys import stdin, stdout
#import math
from collections import deque
input = stdin.readline
n = int(input())
seqB = list(input())
seqW = seqB.copy()
for i in range(0, n):
if seqB[i] == 'B':
seqB[i] = True
seqW[i] = False
else:
seqB[i] = False
seqW[i] = True
opsB = deque()
opsW = deque()
for i in range(0, n - 1):
if not seqB[i]:
seqB[i] = not seqB[i]
seqB[i+1] = not seqB[i+1]
opsB.append(i + 1)
for i in range(0, n - 1):
if not seqW[i]:
seqW[i] = not seqW[i]
seqW[i + 1] = not seqW[i + 1]
opsW.append(i + 1)
if not seqB[n - 1]:
if not seqW[n - 1]:
print(-1)
else:
print(len(opsW))
print(*opsW)
else:
print(len(opsB))
print(*opsB) | 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 primeFactors(long long int n) {
long long int count = 0;
long long int answer = 1;
while (n % 2 == 0) {
count++;
n = n / 2;
}
if (count % 2 == 1) answer = 2;
for (long long int i = 3; i <= sqrt(n); i = i + 2) {
count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
if (count % 2 == 1) answer *= i;
}
if (n > 2) answer = n * answer;
}
int main() {
long long int n;
cin >> n;
string s;
cin >> s;
vector<long long int> jk;
for (long long int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
if (s[i + 1] == 'W') {
s[i + 1] = 'B';
} else
s[i + 1] = 'W';
jk.push_back(i + 1);
}
}
if (s[n - 1] == 'W') {
cout << jk.size() << endl;
for (long long int i = 0; i < jk.size(); i++) cout << jk[i] << " ";
cout << endl;
} else {
for (long long int 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';
jk.push_back(i + 1);
}
}
if (s[n - 1] == 'W')
cout << -1 << endl;
else {
cout << jk.size() << endl;
for (long long int i = 0; i < jk.size(); i++) cout << jk[i] << " ";
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 | def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
n=int(input())
s=input()
#with every move, the black parity and white parity don't change
if s.count('B')%2!=n%2 and s.count('W')%2!=n%2: #impossible
print(-1)
else:
#make everything B. B counts must have same parity as n
if s.count('B')%2!=n%2: #flip
s2=[]
for x in s:
if x=='B':
s2.append('W')
else:
s2.append('B')
s=''.join(s2)
arr=list(s)
ans=[]
for i in range(n-1):
if arr[i]=='W': #flip
ans.append(i+1)
if arr[i+1]=='W':
arr[i+1]='B'
else:
arr[i+1]='W'
print(len(ans))
oneLineArrayPrint(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.*;
public class Blocks {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
String str = input.readLine();
char[] blocks = new char[n];
for (int i = 0; i < n; i++) blocks[i] = str.charAt(i);
boolean[] changed = new boolean[n - 1];
for (int i = 0; i < n - 1; i++) {
if (blocks[i] == 'B') {
blocks[i] = 'W';
blocks[i + 1] = blocks[i + 1] == 'B' ? 'W' : 'B';
changed[i] = !changed[i];
}
}
if (blocks[n - 1] == 'B') {
if ((n - 1) % 2 == 0) {
for (int i = 0; i < n - 1; i += 2) changed[i] = !changed[i];
int sum = 0;
for (int i = 0; i < n - 1; i++) sum += changed[i] ? 1 : 0;
System.out.println(sum);
for (int i = 0; i < n - 1; i++) if (changed[i]) System.out.print((i + 1) + " ");
} else {
System.out.println(-1);
}
} else {
int sum = 0;
for (int i = 0; i < n - 1; i++) sum += changed[i] ? 1 : 0;
System.out.println(sum);
for (int i = 0; i < n - 1; i++) if (changed[i]) System.out.print((i + 1) + " ");
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
Nb=0
Nw=0
for i in s:
if i=='W':Nw+=1
elif i=='B':Nb+=1
if Nb%2==1 and Nw%2==1:print(-1)
elif Nb==0 or Nw==0:print(0)
else:
ans=''
count=0
WB=['W','B']
tmp=[0,1][Nb%2==0]
for i in range(n-1):
if s[i] == WB[tmp]:
s[i]=WB[1-tmp]
count+=1
ans+=str(i+1)+" "
s[i+1]=WB[s[i+1]=='W']
print(count)
print(ans[:-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 | #!/usr/bin/env python
from __future__ import division, print_function
import os
import 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
# 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)
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
input = lambda: sys.stdin.readline().rstrip("\r\n")
read = lambda: list(map(int, input().strip().split(" ")))
def solve():
def invert(i):
dic = {"W": "B", "B":"W"}
s[i] = dic[s[i]]
s[i+1] = dic[s[i+1]]
n = int(input()); s = list(input())
c = Counter(s)
def invert(i):
dic = {"W": "B", "B":"W"}
s[i] = dic[s[i]]
s[i+1] = dic[s[i+1]]
if c["W"] == n:
print(0); return
elif c["B"] == n:
print(0); return
elif c["W"]%2 and c["B"]%2:
print(-1);return
elif not(c["W"]%2 or c["B"]%2):
k = 0; ans = []
for i in range(n):
if s[i] != "B":
invert(i)
k += 1; ans.append(i+1)
print(k); print(*ans)
else:
col = "B" if c["B"]%2 == 1 else "W"
# print(col, "**")
k = 0; ans = []
for i in range(n):
if s[i] != col:
invert(i)
k += 1; ans.append(i+1)
print(k); print(*ans)
if __name__ == "__main__":
solve() | 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())
p = list(input())
pp = p[:]
ans = []
for i in range(n-1):
if pp[i]=='W':
ans.append(i+1)
if pp[i+1]=='W':
pp[i+1] = 'B'
else:
pp[i+1] = 'W'
if pp[-1]=='B':
print(len(ans))
print(*ans)
exit(0)
pp = p[:]
ans = []
for i in range(n-1):
if pp[i]=='B':
ans.append(i+1)
if pp[i+1]=='W':
pp[i+1] = 'B'
else:
pp[i+1] = 'W'
if pp[-1]=='W':
print(len(ans))
print(*ans)
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 |
I = input
n=int(I())
s=list(I())
b=[]
w=[]
for i in range(n):
if s[i]=='W':
w.append(i+1)
else:
b.append(i+1)
bc=len(b)
wc=len(w)
ans=[]
q=-1
if bc==0 and bc==n:
print(0)
elif wc==0 and wc==n:
print(0)
else:
if bc%2!=0:
if wc%2!=0:
q=0
print(-1)
else:
if bc==1:
for i in range(b[0]-1,0,-1):
ans.append(i)
for i in range(2,n,2):
ans.append(i)
else:
for i in range(1,bc,2):
st = b[i]
en = b[i + 1]
for j in range(en - 1, st - 1, -1):
ans.append(j)
for i in range(b[0] - 1, 0, -1):
ans.append(i)
for i in range(2,n,2):
ans.append(i)
else:
for i in range(0,bc,2):
st=b[i]
en=b[i+1]
for j in range(en-1,st-1,-1):
ans.append(j)
if q==-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 | from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value(): return tuple(map(int, input().split())) # multiple values
def arr(): return [int(i) for i in input().split()] # array input
def sarr(): return [int(i) for i in input()] #array from string
def starr(): return [str(x) for x in input().split()] #string array
def inn(): return int(input()) # integer input
def svalue(): return tuple(map(str, input().split())) #multiple string values
def parr(): return [(value()) for i in range(n)] # array of pairs
mo = 1000000007
# ----------------------------CODE------------------------------#
n=inn()
a=[str(x) for x in input()]
ans=[]
i=0
res=a[0]
flag=0
while(i<n and a[i]==res):
i+=1
while(i<n):
if(a[i]==res):
i+=1
continue
val=a[i]
j=i
while(i<n and val==a[i]):
i+=1
if((i-j)%2==0):
ans+=[k+1 for k in range(j,i,2)]
a[i-1]=res
else:
#print(j,i)
ans+=[k+1 for k in range(j,min(i-1,n-1),2)]
#print(ans)
if(j<n-1 and i<n):
#print(i,n-1)
ans+=[i]
#print(ans)
while(i<n-1 and a[i]==res):
ans+=[i+1]
i += 1
#print(i,a[i])
if(i==n-1):
if(a[i]==res):
flag=1
break
else:
#ans+=[i]
a[i]=res
#a[i-1]=res
i+=1
#print(len(ans))
if(flag==0 and a[-1]==res):
print(len(ans))
print(*ans)
else:
if(len(a)%2==0):
print(-1)
else:
ans+=[k+1 for k in range(0,n-1,2)]
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() {
long long int n;
cin >> n;
char a[n + 1];
for (long long int i = 0; i < n; ++i) {
cin >> a[i];
}
char ini = a[0];
long long int count = 0;
vector<long long int> v1;
for (long long int i = 1; i < n; ++i) {
if (a[i] != ini) {
if (i != n - 1) {
a[i] = ini;
if (a[i + 1] == 'W')
a[i + 1] = 'B';
else
a[i + 1] = 'W';
count++;
v1.push_back(i + 1);
}
}
}
long long int flag = 1;
if (a[n - 1] != a[0]) {
if (n % 2 == 0) {
flag = 0;
} else {
for (long long int i = 0; i < n - 1; i = i + 2) {
v1.push_back(i + 1);
count++;
}
}
}
if (flag == 0) {
cout << "-1" << endl;
} else {
cout << count << endl;
for (long long int i = 0; i < v1.size(); ++i) {
cout << v1[i];
cout << " ";
}
cout << endl;
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
word = input()
arr = list(map(str,word))
if word==n*'B':
print(0)
elif word==n*'W':
print(0)
else:
ans = []
for i in range(n-1):
if arr[i]=='B':
ans.append(i+1)
arr[i] = 'W'
if arr[i+1]=='W':
arr[i+1] = 'B'
else:
arr[i+1] = 'W'
final = ''.join(str(x)for x in arr)
if final=='W'*n:
print(len(ans))
print(*ans)
else:
for i in range(n-1):
if arr[i]=='W':
ans.append(i+1)
arr[i]='B'
if arr[i+1]=='B':
arr[i+1] = 'W'
else:
arr[i+1]='B'
final = ''.join(str(x)for x in arr)
if final=='B'*n:
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>
#pragma warning(disable : 4996)
using namespace std;
char a[205];
void solve() {
int n;
cin >> n;
int w = 0, b = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 'W') w++;
if (a[i] == 'B') b++;
}
int cnt = 0;
vector<int> ans;
ans.clear();
if (w % 2 && b % 2) {
cout << -1 << '\n';
return;
} else if (w % 2 && b % 2 == 0) {
for (int i = 1; i <= n - 1; i++) {
if (a[i] == 'B' && a[i + 1] == 'B') {
ans.push_back(i);
i++;
cnt++;
} else if (a[i] == 'B') {
swap(a[i], a[i + 1]);
ans.push_back(i);
;
cnt++;
}
}
} else if (b % 2 && w % 2 == 0) {
for (int i = 1; i <= n - 1; i++) {
if (a[i] == 'W' && a[i + 1] == 'W') {
ans.push_back(i);
i++;
cnt++;
} else if (a[i] == 'W') {
swap(a[i], a[i + 1]);
ans.push_back(i);
cnt++;
}
}
} else {
for (int i = 1; i <= n - 1; i++) {
if (a[i] == 'B' && a[i + 1] == 'B') {
ans.push_back(i);
i++;
cnt++;
} else if (a[i] == 'B') {
swap(a[i], a[i + 1]);
ans.push_back(i);
cnt++;
}
}
}
if (ans.empty())
cout << 0 << '\n';
else {
cout << cnt << '\n';
for (int i = 0; i < (int)(ans).size(); i++) {
cout << ans[i] << ' ';
}
}
}
int main() {
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;
char a1[10000];
int s = 0, p = 0, k = 0;
int a2[1000];
void chnage_fun12(int i, int g) {
if (a1[i] == 'W')
a1[i] = 'B';
else
a1[i] = 'W';
if (a1[g] == 'W')
a1[g] = 'B';
else
a1[g] = 'W';
}
void chnage_fun(int i, int g) {
s--;
a2[p] = i + 1;
p++;
if (a1[i] == 'W')
a1[i] = 'B';
else
a1[i] = 'W';
if (a1[g] == 'W')
a1[g] = 'B';
else
a1[g] = 'W';
int w, b;
w = b = 0;
for (int j = 0; j < strlen(a1); j++) {
if (a1[j] == 'W')
w++;
else
b++;
}
if (w == 0 || b == 0) s = 0;
}
int main() {
int n, t;
scanf("%d %s", &n, &a1);
n = strlen(a1);
s = n * 3;
int w1, b1;
w1 = b1 = 0;
for (int j = 0; j < strlen(a1); j++) {
if (a1[j] == 'W')
w1++;
else
b1++;
}
if (w1 == 0 || b1 == 0) {
printf("0\n");
return 0;
}
int a, c, d, e, f, g;
if (n == 1) {
printf("0\n");
return 0;
}
if (n == 2) {
if (a1[0] == a1[1]) {
printf("1\n1\n");
return 0;
} else {
printf("-1\n");
return 0;
}
}
if (n == 3) {
int i = 0;
if (a1[i] == a1[i + 1]) {
printf("1\n1\n");
return 0;
}
}
for (int j = 0; j < s; j++) {
for (int i = 0; i < strlen(a1) - 2; i++) {
if (a1[i] != a1[i + 1]) {
chnage_fun(i + 1, i + 2);
}
}
}
int w, b;
w = b = 0;
for (int j = 0; j < strlen(a1); j++) {
if (a1[j] == 'W')
w++;
else
b++;
}
if (w == 0 || b == 0)
s = 0;
else
s = -1;
if (s != -1) {
printf("%d\n", p);
for (int i = 0; i < p; i++) {
printf("%d ", a2[i]);
}
} else {
if (w % 2 == 0 && b == 1 || b % 2 == 0 && w == 1) {
if (b % 2 == 0) w = b;
if (w % 2 == 0) {
if (k == 0) k = p;
while (w) {
a2[p] = (w - 2) + 1;
chnage_fun12(w - 2, w - 1);
p++;
w = w - 2;
}
printf("%d\n", p);
k = 0;
for (int i = k; i < p; i++) {
printf("%d ", a2[i]);
}
return 0;
}
}
printf("-1\n");
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=input()
ar=list(s)
br=list(s)
b=0
w=0
for i in(ar):
if i=="B":
b+=1
else:
w+=1
ans=[]
ans1=[]
count=0
flag=0
if b==0 or w==0:
print("0")
else:
for i in range(n):
if(i==n-1):
break
if(ar[i]==ar[i+1]):
if(ar[i]=="B"):
continue
else:
ar[i]="B"
ar[i+1]="B"
count+=1
ans.append(i+1)
elif ar[i]=="W":
ar[i]="B"
ar[i+1]="W"
ans.append(i+1)
count+=1
for i in range(n):
if(i==n-1):
break
if(br[i]==br[i+1]):
if(br[i]=="W"):
continue
else:
br[i]="W"
br[i+1]="W"
flag+=1
ans1.append(i+1)
elif br[i]=="B":
br[i]="W"
br[i+1]="B"
ans1.append(i+1)
flag+=1
if ar[n-1]!=ar[n-2] and br[n-1]!=br[n-2]:
print("-1")
elif ar[n-1]==ar[n-2]:
print(count)
for i in(ans):
print(i,end=" ")
else:
print(flag)
for i in(ans1):
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 | from math import ceil
from math import factorial
from collections import Counter
from operator import itemgetter
ii = lambda: int(input())
iia = lambda: list(map(int,input().split()))
isa = lambda: list(input().split())
n = ii()
s = input()
s = list(s)
if(s.count('B')%2==1 and s.count('W')%2==1):
print(-1)
else:
i = 0
ans = []
while(i<n-2):
if(s[i]!=s[i+1]):
s[i+1] = s[i]
if(s[i+2]=='B'):
s[i+2]='W'
else:
s[i+2]='B'
ans.append(i+2)
i+=1
if(s.count('W')!=n and s.count('B')!=n):
for i in range(0,n-2,2):
ans.append(i+1)
if s[i]=='W':
s[i]='B'
s[i+1]='B'
else:
s[i]='W'
s[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 | import java.io.*;
import java.util.*;
public class Main{
static int mod = (int)(Math.pow(10, 9) + 7);
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n =sc.nextInt();
String s= sc.next();
List<Integer> blackMoves = new ArrayList<Integer>();
List<Integer> whiteMoves = new ArrayList<Integer>();
char[] c = s.toCharArray();
boolean validB = solve(blackMoves, c, 'B');
boolean validW = solve(whiteMoves, c, 'W');
if (validB) {
print(blackMoves);
}
else if (validW) print(whiteMoves);
else out.println(-1);
out.close();
}
static void print(List<Integer> a) {
out.println(a.size());
for (int x: a) out.print(x + " ");
out.println();
}
static boolean solve(List<Integer> m, char[] c, char t) {
int n = c.length;
char[] ch = new char[c.length];
for (int i = 0; i < n; i++) ch[i] = c[i];
for (int i = 0; i < n-1; i++) {
if (ch[i] != t && ch[i+1] != t) {
swap(ch, i);
swap(ch, i+1);
m.add(i+1);
}
}
for (int i = 0; i < n-2; i++) {
if (ch[i] != t && ch[i+1] == t && ch[i] != t) {
swap(ch, i);
swap(ch, i+2);
m.add(i+1);
m.add(i+2);
}
}
for (int i = 0; i < n-1; i++) {
if (ch[i] != t) {
swap(ch, i);
swap(ch, i+1);
m.add(i+1);
}
}
for (int i = 0; i <n ; i++) {
if (ch[i] != t) return false;
}
return true;
}
static void swap(char[] c, int i) {
if (c[i]== 'B') c[i] = 'W';
else c[i] = 'B';
}
static long pow(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a;
else {
long R = pow(a,N/2);
if (N % 2 == 0) {
return R*R;
}
else {
return R*R*a;
}
}
}
static long powMod(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a % mod;
else {
long R = powMod(a,N/2) % mod;
R *= R % mod;
if (N % 2 == 1) {
R *= a % mod;
}
return R % mod;
}
}
static void mergeSort(int[] A){ // low to hi sort, single array only
int n = A.length;
if (n < 2) return;
int[] l = new int[n/2];
int[] r = new int[n - n/2];
for (int i = 0; i < n/2; i++){
l[i] = A[i];
}
for (int j = n/2; j < n; j++){
r[j-n/2] = A[j];
}
mergeSort(l);
mergeSort(r);
merge(l, r, A);
}
static void merge(int[] l, int[] r, int[] a){
int i = 0, j = 0, k = 0;
while (i < l.length && j < r.length && k < a.length){
if (l[i] < r[j]){
a[k] = l[i];
i++;
}
else{
a[k] = r[j];
j++;
}
k++;
}
while (i < l.length){
a[k] = l[i];
i++;
k++;
}
while (j < r.length){
a[k] = r[j];
j++;
k++;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | x = int(input())
y = input()
out = list()
printo = list()
for i in range(len(y)):
if y[i] == "W":
out.append(1)
else:
out.append(0)
if out.count(0) % 2 == 0 or out.count(1) % 2 == 0:
for j in range(len(out) - 1):
if out[j] % 2 != out[0] % 2:
out[j] += 1
out[j + 1] += 1
printo.append(str(j + 1))
for k in range(len(out) - 1, 0, -1):
if out[k] % 2 != out[len(out) - 1] % 2:
out[k] += 1
out[k - 1] += 1
printo.append(str(k))
print(len(printo))
print(" ".join(printo))
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;
void faster() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
if (0) {
freopen(
"input"
".in",
"r", stdin);
freopen(
"input"
".out",
"w", stdout);
}
}
int n;
string s;
bool w[2100], b[2100], good;
vector<int> ans;
int main() {
faster();
cin >> n >> s;
for (int i = 0; i < int(n); i++)
if (s[i] == 'B')
w[i] = 1;
else
b[i] = 1;
for (int i = 0; i < int(n - 1); i++) {
if (w[i]) {
ans.push_back(i + 1);
w[i + 1] ^= 1;
}
}
if (w[n - 1]) {
ans.clear();
for (int i = 0; i < int(n - 1); i++) {
if (b[i]) {
ans.push_back(i + 1);
b[i + 1] ^= 1;
}
}
if (!b[n - 1]) good = 1;
} else
good = 1;
if (!good) {
cout << -1;
return 0;
}
cout << ans.size() << endl;
for (int i = 0; i < int(ans.size()); i++) cout << ans[i] << ' ';
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int resultbool = 1;
string x = s;
vector<int> v1, v2;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
v1.push_back(i + 1);
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
}
}
for (int i = 0; i < n; i++) {
if (s[i] != 'W') {
resultbool = 3;
}
}
if (resultbool == 1) {
cout << v1.size() << endl;
for (int i = 0; i < v1.size(); i++) cout << v1[i] << " ";
cout << endl;
return 0;
}
resultbool = 2;
s = x;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
v2.push_back(i + 1);
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
}
}
for (int i = 0; i < n; i++) {
if (s[i] != 'B') {
resultbool = 3;
}
}
if (resultbool == 2) {
cout << v2.size() << endl;
for (int i = 0; i < v2.size(); i++) cout << v2[i] << " ";
cout << endl;
return 0;
}
if (resultbool == 3) 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 | # Whatever the mind of man can conceive and believe, it can achieve. Napoleon Hill
# by : Blue Edge - Create some chaos
n=int(input())
s=input()
b=s.count("B")
w=n-b
if b&1 and w&1 and n%2==0:
print(-1)
elif n&1:
d={"W":"B","B":"W"}
c={"W":w,"B":b}
s=list(s)
if b&1:
t="W"
else:
t="B"
i=0
p=[]
while c[t]!=0:
i%=(n-1)
if s[i]==t:
if s[i]==s[i+1]:
c[t]-=2
# c[d[t]]+=
s[i]=d[t]
s[i+1]=d[s[i+1]]
p+=i+1,
i+=1
print(len(p))
print(*p)
else:
d={"W":"B","B":"W"}
c={"W":w,"B":b}
s=list(s)
if b>w:
t="W"
else:
t="B"
i=0
p=[]
while c[t]!=0:
i%=(n-1)
if s[i]==t:
if s[i]==s[i+1]:
c[t]-=2
# c[d[t]]+=
s[i]=d[t]
s[i+1]=d[s[i+1]]
p+=i+1,
i+=1
print(len(p))
print(*p)
# print("".join(s))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import collections
n=int(input())
a=input()
d=collections.Counter(a)
if d['B']%2!=0 and d['W']%2!=0:
print("-1")
else:
if d['B']==0 or d['W']==0:
print(0)
else:
# doing for W
ans=list(a)
ansf=[]
for i in range(0,n-1):
if ans[i]=='B':
ans[i]='W'
if ans[i+1]=='W':
ans[i+1]='B'
else:
ans[i+1]='W'
ansf.append(i+1)
d=collections.Counter(ans)
#print(ans)
if d['B']==0:
print(len(ansf))
print(*ansf)
else:
# doing for B
ans=list(a)
ansf=[]
for i in range(0, n - 1):
if ans[i] == 'W':
ans[i]='B'
if ans[i+1] == 'W':
ans[i+1]='B'
else:
ans[i+1]='W'
ansf.append(i + 1)
d = collections.Counter(ans)
#print(ans)
if d['W'] == 0:
print(len(ansf))
print(*ansf)
| 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 javafx.beans.NamedArg;
import java.io.*;
import java.util.Comparator;
import java.util.*;
/**
* @author Yuxuan Wu
*/
public class Main {
public static final int RANGE = 1000000;
static int MAX = Integer.MAX_VALUE;
static int MIN = Integer.MIN_VALUE;
static int MODULO = 1000000007;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
B1217();
/*
int T = sc.nextInt();
for(int t = 1; t <= T; ++t){
solve();
}
*/
}
private static void solve() {
int n = sc.nextInt();
String s = sc.next();
int[] str = new int[n];
int black = 0;
int white = 0;
for (int i = 0; i < n; i++) {
if(s.charAt(i) == 'B'){
str[i] = 1;
black++;
}
else{
str[i] = 0;
white++;
}
}
if(black % 2 == 1 && white % 2 == 1){
System.out.println(-1);
return;
}
else{
Queue<Integer> ans = new LinkedList<>();
int cnt = 0;
int toClear = 0;
if(white % 2 == 1){
toClear = 1;
}
if(n == 1){
System.out.println(-1);
return;
}
for (int i = 1; i < n; ) {
if(str[i] == str[i-1] && str[i] == toClear){
str[i] = 1;
str[i-1] = 1;
ans.add(i);
cnt++;
i += 2;
}else {
++i;
}
}
int p = 0;
while(p <= n-1){
while (p <= n-1 && str[p] != toClear){
++p;
}
if(p >= n-1){
break;
}
while(p <= n-1 && str[p+1] != toClear){
ans.add(p+1);
cnt++;
++p;
}
ans.add(p+1);
cnt++;
if(p + 1 >= n-1){
break;
}
else {
p += 2;
}
}
System.out.println(cnt);
for (int i = 0; i < cnt; i++) {
System.out.print(ans.poll() + " ");
}
}
}
private static void B1217() {
int n = sc.nextInt();
String s = sc.next();
int[] str = new int[n];
int black = 0;
int white = 0;
for (int i = 0; i < n; i++) {
if(s.charAt(i) == 'B'){
str[i] = 1;
black++;
}
else{
str[i] = 0;
white++;
}
}
if(black % 2 == 1 && white % 2 == 1){
System.out.println(-1);
return;
}
else{
Queue<Integer> ans = new LinkedList<>();
int cnt = 0;
int toClear = 0;
if(white % 2 == 1){
toClear = 1;
}
if(n == 1){
System.out.println(-1);
return;
}
for (int i = 0; i < n-1; i++) {
if(str[i] == toClear){
ans.add(i+1);
cnt++;
str[i+1] = 1 - str[i+1];
}
}
if(str[n-1] == toClear){
System.out.println(-1);
return;
}
System.out.println(cnt);
for (int i = 0; i < cnt; i++) {
System.out.print(ans.poll() + " ");
}
}
}
/**
* 返回最大公约数
* 时间复杂度 O(Log min(n1, n2))
*/
public static long gcd(long n1, long n2) {
if (n2 == 0) {
return n1;
}
return gcd(n2, n1 % n2);
}
/**
* 返回该数的位数
*/
public static int getDigit(long num){
int ans = 0;
while(num > 0){
num /= 10;
ans++;
}
return ans;
}
/**
* 判断是否为回文串
*/
public static boolean palindrome(String s) {
int n = s.length();
for(int i = 0; i < n; i++) {
if(s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
/**
* 判断是否为完全平方数
*/
public static boolean isSquare(int num) {
double a = 0;
try {
a = Math.sqrt(num);
} catch (Exception e) {
return false;
}
int b = (int) a;
return a - b == 0;
}
public static class Pairs<K, V> implements Comparable<Pairs> {
private K key;
private V value;
public K getKey() { return key; }
public V getValue() { return value; }
public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
public Pairs(@NamedArg("key") K key, @NamedArg("value") V value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return key + " = " + value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pairs<?, ?> pairs = (Pairs<?, ?>) o;
return Objects.equals(key, pairs.key) &&
Objects.equals(value, pairs.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
@Override
public int compareTo(Pairs o) {
return 0;
}
}
static class CompInt implements Comparator<Pairs>{
@Override
public int compare(Pairs o1, Pairs o2) {
if(o1.key instanceof Integer&&o2.key instanceof Integer){
if((int)o1.key>(int)o2.key){
return 1;
}else{
return -1;
}
}
return 0;
}
}
static class CompLong implements Comparator<Pairs>{
@Override
public int compare(Pairs o1, Pairs o2) {
return Long.compare((long)o1.key, (long)o2.key);
}
}
static class CompDouble implements Comparator<Pairs>{
@Override
public int compare(Pairs o1, Pairs o2) {
if(o1.key instanceof Double && o2.key instanceof Double){
return Double.compare((double) o1.key, (double) o2.key);
}
return 0;
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null) {
return;
}
din.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 | t = int(input())
s = list(input())
l1 = []
l2 = []
B = 0
W = 0
j = 0
p = []
q = []
for i in s:
l1.append(i)
l2.append(i)
if l1[j] == "W":
W += 1
else:
B += 1
j += 1
if W==0 or B==0:
print(0)
else:
for k in range(t-1):
if l1[k] != 'B':
l1[k] = 'B'
p.append(k + 1)
if l1[k+1] == 'B':
l1[k+1] = 'W'
elif l1[k+1] == 'W':
l1[k+1] = 'B'
if l2[k] != 'W':
l2[k] = 'W'
q.append(k + 1)
if l2[k+1] == 'W':
l2[k+1] = 'B'
elif l2[k+1] == 'B':
l2[k+1] = 'W'
if l1[-1] == 'B':
print(len(p))
while p:
print(p[-1])
p.pop()
elif l2[-1] == 'W':
print(len(q))
while q:
print(q[-1])
q.pop()
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 | def calculate_p(l):
p = []
for i in range(0, len(l), 2):
if l[i + 1] - l[i] == 1:
p.append(l[i] + 1) # 1-indexed
else:
p.extend([j + 1 for j in range(l[i], l[i + 1])]) # 1-indexed
return p
def blocks(n, s):
b = []
w = []
for i in range(n):
if s[i] == 'B':
b.append(i)
else:
w.append(i)
even_b = (len(b) % 2 == 0)
even_w = (len(w) % 2 == 0)
if (not even_w and not b) or (not even_b and not w):
return []
if not even_w and not even_b:
return -1
return calculate_p(b) if even_b else calculate_p(w)
def test():
assert blocks(8, "BWWWWWWB") == [1, 2, 3, 4, 5, 6, 7]
assert blocks(4, "BWBB") == -1
assert blocks(5, "WWWWW") == []
assert blocks(3, "BWB") == [1, 2]
assert blocks(7, "WWBBBWW") == [1, 6]
if __name__ == '__main__':
n = int(input())
s = input()
res = blocks(n, s)
if res == -1:
print(-1)
else:
print(len(res))
if len(res) > 0:
print(" ".join(map(str, res)))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author John Martin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BBlocks solver = new BBlocks();
solver.solve(1, in, out);
out.close();
}
static class BBlocks {
public void solve(int testNumber, InputReader c, OutputWriter w) {
int n = c.readInt();
char s[] = c.readString().toCharArray();
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
cnt1++;
} else {
cnt2++;
}
}
ArrayList<Integer> res;
if (cnt1 % 2 == 0) {
res = solve_(n, s, cnt1);
} else if (cnt2 % 2 == 0) {
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
s[i] = 'B';
} else {
s[i] = 'W';
}
}
res = solve_(n, s, cnt2);
} else {
w.printLine(-1);
return;
}
w.printLine(res.size());
for (int i : res) {
w.print(i + " ");
}
w.printLine();
}
private ArrayList<Integer> solve_(int n, char[] s, int cnt) {
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
ans.add(i + 1);
s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W');
}
}
return ans;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine() {
writer.println();
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(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())
a = list(input())
k = 0;
result = []
for i in range(n - 1):
if a[i] == 'W':
k += 1
result.append(i + 1)
a[i] = 'B'
if a[i + 1] == 'W':
a[i + 1] = 'B'
else:
a[i + 1] = 'W'
if a[n - 1] == 'B':
print(k)
print(*result)
else:
if (n - 1) % 2 == 0:
print(k + (n - 1) // 2)
for i in range(1, n - 1, 2):
result.append(i)
print(*result)
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() {
long long n, i, c1 = 0, c2 = 0, cnt = 0;
cin >> n;
string s;
cin >> s;
vector<long long> v;
vector<long long> ans;
for (i = 0; i < s.length(); i++) {
if (s[i] == 'W') {
v.push_back(0);
c1++;
} else {
v.push_back(1);
c2++;
}
}
if ((c1 % 2 == 1 && c2 % 2 == 0) || (c1 % 2 == 0 && c2 % 2 == 0)) {
for (i = 0; i < v.size() - 1; i++) {
if (v[i] == 1) {
v[i] = 0;
v[i + 1] = v[i + 1] ^ 1;
cnt++;
ans.push_back(i + 1);
}
}
cout << cnt << endl;
for (i = 0; i < ans.size(); i++) cout << ans[i] << " ";
cout << endl;
} else if (c1 % 2 == 0 && c2 % 2 == 1) {
for (i = 0; i < v.size() - 1; i++) {
if (v[i] == 0) {
v[i] = 1;
v[i + 1] = v[i + 1] ^ 1;
cnt++;
ans.push_back(i + 1);
}
}
cout << cnt << endl;
for (i = 0; i < ans.size(); i++) cout << ans[i] << " ";
cout << endl;
} else
cout << -1 << endl;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
aa=input()
a=[i for i in aa]
b=0
w=0
for i in a:
if i=='W':
w += 1
else:
b += 1
if w==0 or b==0:
print(0)
else:
ans=[]
for i in range(n-1):
if a[i]=='B':
ans.append(i+1)
a[i]='W'
if a[i+1]=='B':
a[i+1]='W'
else:
a[i+1]='B'
b=0
w=0
for i in a:
if i=='W':
w += 1
else:
b += 1
ok=False
if b==0:
ok=True
print(len(ans))
print(" ".join(map(str,ans)))
if ok==False:
a=[i for i in aa]
ans=[]
for i in range(n-1):
if a[i]=='W':
ans.append(i+1)
a[i]='B'
if a[i+1]=='B':
a[i+1]='W'
else:
a[i+1]='B'
b=0
w=0
for i in a:
if i=='W':
w += 1
else:
b += 1
if w==0:
ok=True
print(len(ans))
print(" ".join(map(str,ans)))
if ok==False:
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 | from sys import stdin, stdout
from collections import Counter, defaultdict
pr=stdout.write
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return (map(int,stdin.read().split()))
range = xrange # not for python 3.0+
# main code
def fun(x):
if x=='B':
return 1
else:
return 0
n=ni()
s=map(fun,list(raw_input().strip()))
c0,c1=0,0
for i in range(n):
if s[i]:
c1+=1
else:
c0+=1
if c0%2==0:
ans=[]
prev=-1
for i in range(n):
if s[i]==0:
if prev>=0:
prev=-1
else:
prev=i
if prev>=0:
ans.append(i+1)
pn(len(ans))
pa(ans)
exit()
if c1%2==0:
ans=[]
prev=-1
for i in range(n):
if s[i]==1:
if prev>=0:
prev=-1
else:
prev=i
if prev>=0:
ans.append(i+1)
pn(len(ans))
pa(ans)
exit()
pn(-1)
| PYTHON |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
def white(arr1):
arr = arr1.copy()
ans = []
for i in range(1,len(arr)):
if arr[i-1] == 0:
arr[i-1]^=1
arr[i]^=1
ans.append(i)
if arr[-1] == 1:
return ans
else:
return 0
def black(arr1):
arr = arr1.copy()
ans = []
for i in range(1,len(arr)):
if arr[i-1] == 1:
arr[i-1]^=1
arr[i]^=1
ans.append(i)
if arr[-1] == 0:
return ans
else:
return 0
n = int(input())
arr = [1 if i=='W' else 0 for i in input()]
if white(arr)!=0:
print(len(white(arr)))
print(' '.join([str(i) for i in white(arr)]))
elif black(arr)!=0:
print(len(black(arr)))
print(' '.join([str(i) for i in black(arr)]))
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
String a = sc.next();
char arr[] = a.toCharArray();
int x[] = new int[n];
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'W') {
x[i] = 0;
} else {
x[i] = 1;
}
}
Vector<Integer> ans = new Vector<Integer>();
for (int j = 1; j < n - 1; j++) {
if (x[j] != x[j - 1]) {
ans.add(j);
x[j] = 1 - x[j];
x[j + 1] = 1 - x[j + 1];
}
}
if (x[n - 2] != x[n - 1]) {
if (n % 2 == 0) {
System.out.println(-1);
break;
}
for (int i = 0; i < n - 1; i += 2) {
ans.add(i);
}
}
System.out.println(ans.size());
for (Integer c : ans) {
System.out.print((c + 1) + " ");
}
System.out.println();
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.io.*;
public class problem2 {
public static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
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 {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static long factorialNumInverse[];
public static long naturalNumInverse[];
public static long fact[];
public static int N = 100007;
public static char change(char a) {
if(a == 'W')return 'B';
else return 'W';
}
public static void InverseofNumber(long p) {
naturalNumInverse = new long [N+1];
naturalNumInverse[0] = 1;
naturalNumInverse[1] = 1;
for(int i = 2;i<=N;i++) {
naturalNumInverse[i] = naturalNumInverse[(int)(p % i)] * (p - p / i) % p;
}
}
public static void InverseofFactorial(long p) {
factorialNumInverse = new long [N+1];
factorialNumInverse[0] = 1;
factorialNumInverse[1] = 1;
for(int i = 2;i<=N;i++) {
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
}
public static void factorial(long p) {
fact = new long [N+1];
fact[0] = 1;
for(int i = 1;i<=N;i++) {
fact[i] = (fact[i - 1] * i) % p;
}
}
public static long Binomial(long N, long R, long p) {
long ans = ((fact[(int)N] * factorialNumInverse[(int)R])
% p * factorialNumInverse[(int)(N - R)])
% p;
return ans;
}
public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
String str1 = in.nextLine();
char arr[] = str1.toCharArray();
int c1 = 0, c2 = 0;
for(int i=0;i<n;i++) {
if(arr[i] == 'W')c1++;
else c2++;
}
if(c1%2 == 1 && c2%2 == 1) {
System.out.println(-1);
}
else {
char del;
int xp = c1%2;
if(xp == 1) {
del = 'B';
}
else {
del = 'W';
}
ArrayList<Integer> arr1 = new ArrayList<>();
for(int i=0;i+1<n;++i) {
if(arr[i] == del) {
arr[i] = change(arr[i]);
arr[i+1] = change(arr[i+1]);
arr1.add(i+1);
}
}
System.out.println(arr1.size());
Iterator<Integer>it = arr1.iterator();
while(it.hasNext()) {
System.out.println(it.next() + " ");
}
System.out.println();
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
static int INF = 1000000007;
public static void main(String[] args) {
// int test = fs.nextInt();
int test = 1;
for (int cases = 0; cases < test; cases++) {
int n = fs.nextInt();
char ar[] = fs.next().toCharArray();
int b = 0, w = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] == 'B') {
++b;
} else {
++w;
}
}
if (b == 0 || w == 0) {
System.out.println(0);
} else {
// w
ArrayList<Integer> al1 = new ArrayList<Integer>();
ArrayList<Integer> al2 = new ArrayList<>();
char ar2[] = Arrays.copyOf(ar, n);
for (int i = 0; i < n - 1; i++) {
if (ar[i] == 'W') {
continue;
} else {
ar[i] = 'W';
ar[i + 1] = ar[i + 1] == 'W' ? 'B' : 'W';
al1.add(i + 1);
}
}
for (int i = 0; i < n - 1; i++) {
if (ar2[i] == 'B') {
continue;
} else {
ar2[i] = 'B';
ar2[i + 1] = ar2[i + 1] == 'B' ? 'W' : 'B';
al2.add(i + 1);
}
}
if (ar[n - 1] != ar[n - 2] && ar2[n - 1] != ar2[n - 2]) {
System.out.println(-1);
} else if (ar[n - 1] == ar[n - 2] && ar2[n - 1] == ar2[n - 2]) {
int min = Integer.min(al1.size(), al2.size());
if (min > 3 * n) {
System.out.println(-1);
} else {
if (min == al1.size()) {
System.out.println(al1.size());
for (Integer integer : al1) {
System.out.print(integer + " ");
}
} else {
System.out.println(al2.size());
for (Integer integer : al2) {
System.out.print(integer + " ");
}
}
}
} else if (ar[n - 1] == ar[n - 2]) {
if (al1.size() > 3 * n) {
System.out.println(-1);
} else {
System.out.println(al1.size());
for (Integer integer : al1) {
System.out.print(integer + " ");
}
}
} else {
if (al2.size() > 3 * n) {
System.out.println(-1);
} else {
System.out.println(al2.size());
for (Integer integer : al2) {
System.out.print(integer + " ");
}
}
}
}
}
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long nCrModPFermat(long n, long r, long p) {
long ans1 = 1;
long i = n;
long k = r;
while (k > 0) {
ans1 = mul(ans1, i, p);
i--;
k--;
}
long ans2 = 1;
while (r > 0) {
ans2 = mul(ans2, r, p);
r--;
}
r = modInverse(ans2, p);
ans1 = mul(ans1, r, p);
return ans1;
}
static long facCalc(long total) {
long ans = 1;
for (long i = 2; i <= total; i++) {
ans = mul(ans, i, INF);
}
return ans;
}
static long mul(long a, long b, long p) {
return ((a % p) * (b % p)) % p;
}
static void sieve() {
boolean prime[] = new boolean[101];
Arrays.fill(prime, true);
prime[1] = false;
for (int i = 2; i * i <= prime.length - 1; i++) {
for (int j = i * i; j <= prime.length - 1; j += i) {
prime[j] = false;
}
}
}
public static int[] radixSort(int[] f) {
return radixSort(f, f.length);
}
public static int[] radixSort(int[] f, int n) {
int[] to = new int[n];
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
static void printArray(int ar[]) {
System.out.println(Arrays.toString(ar));
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class Pair2 {
int first;
Pair second;
public Pair2(int first, Pair second) {
this.first = first;
this.second = second;
}
}
static class LongPair {
long first, second;
public LongPair(long first, long second) {
this.first = first;
this.second = second;
}
}
static long expmodulo(long a, long b, long c) {
long x = 1;
long y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y) % c;
}
y = (y * y) % c; // squaring the base
b /= 2;
}
return (long) x % c;
}
// static int modInverse(int a, int m) {
// int m0 = m;
// int y = 0, x = 1;
//
// if (m == 1)
// return 0;
//
// while (a > 1) {
// int q = a / m;
// int t = m;
// m = a % m;
// a = t;
// t = y;
// y = x - q * y;
// x = t;
// }
// if (x < 0)
// x += m0;
// return x;
// }
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<String, Integer> hm) {
List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static boolean ispowerof2(long num) {
if ((num & (num - 1)) == 0)
return true;
return false;
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sq = (long) Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class Graph {
HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();
private void addVertex(int vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(int source, int dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
visited.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, i))
return true;
} else if (integer != parent) {
return true;
}
}
return false;
}
private boolean uniCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, -1)) {
return true;
}
}
}
return false;
}
private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
if (countered.contains(i))
return true;
if (visited.contains(i))
return false;
visited.add(i);
countered.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
countered.remove(i);
return false;
}
Boolean isReachable(int s, int d, int k) {
if (hm.isEmpty()) {
return false;
}
LinkedList<Integer> temp;
boolean visited[] = new boolean[k];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[s] = true;
queue.add(s);
Iterator<Integer> i;
while (queue.size() != 0) {
s = queue.poll();
int n;
i = hm.get(s).listIterator();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
while (i.hasNext()) {
n = i.next();
// If this adjacent node is the destination node,
// then return true
if (n == d)
return true;
// Else, continue to do BFS
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
// If BFS is complete without visited d
return false;
}
private boolean isbiCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> countered = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=input()
l=[]
i=0
s=list(s)
x=s.copy()
tag=True
while i<len(s):
if i==len(s)-1:
if s[i]=='W':
tag=False
break
else:
break
elif s[i]=='W':
l.append(i+1)
if s[i+1]=='W':
s[i+1]='B'
else:
s[i+1]='W'
s[i]='B'
i=i+1
else:
i+=1
if tag:
print(len(l))
print(*l)
else:
i=0
tag=True
l=[]
while i<len(s):
if i==len(s)-1:
if x[i]=='W':
tag=False
break
else:
break
elif x[i]=='B':
l.append(i+1)
if x[i+1]=='B':
x[i+1]='W'
else:
x[i+1]='B'
x[i]='W'
i=i+1
else:
i+=1
if tag:
print(-1)
else:
print(len(l))
print(*l) | 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 mod = 1000000007;
long long int inf = 1000000000000000000;
vector<int> ans, from, to;
string s;
int n;
void suka(char c, char cc) {
int sc = 0;
for (int i = 0; i < n; i++) {
if (s[i] == s[i + 1] && s[i] == c) sc++, i++;
}
for (int i = 0; i < n; i++) {
if (s[i] == s[i + 1] && s[i] == c)
ans.push_back(i + 1), s[i] = cc, s[i + 1] = cc, i++, sc--;
}
for (int i = 0; i < n; i++) {
if (s[i] == c) {
if (to.size() < from.size())
to.push_back(i);
else
from.push_back(i);
}
}
reverse(from.begin(), from.end());
reverse(to.begin(), to.end());
while (!from.empty()) {
while (from[from.size() - 1] != to[to.size() - 1])
ans.push_back(++from[from.size() - 1]);
from.pop_back();
to.pop_back();
}
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cin >> s;
s += ' ';
s += ' ';
s += ' ';
int a[2]{0};
for (int i = 0; i < n; i++) {
if (s[i] == 'W')
a[0]++;
else
a[1]++;
}
if (a[0] == n || a[1] == n) {
cout << 0;
return 0;
}
if (a[0] % 2 && a[1] % 2) {
cout << "-1";
return 0;
}
if (a[0] % 2)
suka('B', 'W');
else
suka('W', 'B');
cout << ans.size() << "\n";
for (auto it : ans) cout << it << " ";
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class B{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Pair
{
int p;
int q;
Pair(int p,int q)
{
this.p = p;
this.q = q;
}
}
static int gcd(int a,int b)
{
if(a == 0) return b;
if(b == 0) return a;
return gcd(b,a%b);
}
public static void main(String[] args)
{
OutputStream outputStream = System.out;
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
int n = sc.nextInt();
char ar[] = sc.nextLine().toCharArray();
char cpy[] = new char[n];
for(int i = 0; i < n; i++)
{
cpy[i] = ar[i];
}
int bc = 0;
int wc = 0;
for(int i = 0; i < n; i++)
{
if(ar[i] == 'B') bc++;
else wc++;
}
if(bc == 0 || wc == 0)
{
out.println(0);
out.close();
return;
}
//b
ArrayList<Integer> brl = new ArrayList<>();
for(int i = 0; i < n-1; i++)
{
if(ar[i] == 'W')
{
ar[i] = 'B';
if(ar[i+1] == 'W') ar[i+1] = 'B';
else ar[i+1] = 'W';
brl.add(i);
}
}
if(ar[n-1] == 'W')
{
ArrayList<Integer> wrl = new ArrayList<>();
for(int i = 0; i < n-1; i++)
{
if(cpy[i] == 'B')
{
cpy[i] = 'W';
if(cpy[i+1] == 'W') cpy[i+1] = 'B';
else cpy[i+1] = 'W';
wrl.add(i);
}
}
if(cpy[n-1] == 'B')
{
out.println(-1);
}
else
{
out.println(wrl.size());
for(int r:wrl)
{
out.print((r+1)+" ");
}
}
}
else
{
out.println(brl.size());
for(int r:brl)
{
out.print((r+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 | def solve(n,ar):
track = []
cnt = 0
s = ar[:]
##make everything white blocks
for i in range(n-1):
if s[i] == 'W':
continue
else:
s[i] = 'W'
if s[i+1] == 'B': s[i+1] = 'W'
else: s[i+1] = 'B'
cnt += 1
track.append(i+1)
if s[n-1] == 'W':
print(cnt)
print(" ".join(map(str, track)))
return
#make everything Black
s = ar[:]
track = []
cnt = 0
for i in range(n-1):
if s[i] == 'B': continue
else:
s[i] = 'B'
if s[i + 1] == 'B': s[i + 1] = 'W'
else: s[i + 1] = 'B'
cnt += 1
track.append(i + 1)
if s[n-1] == 'B':
print(cnt)
print(" ".join(map(str, track)))
return
else:
print(-1)
return
if __name__ == '__main__':
n = int(input())
s = list(input())
solve(n,s)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.IllegalCharsetNameException;
import java.sql.SQLOutput;
import java.util.*;
public class Main {
static int[] arr;
static boolean[] v;
static long[] ans;
static int n;
static int[] parity;
public static void main(String[] args) throws IOException{
Reader.init(System.in);
int n = Reader.nextInt();
String s = Reader.next();
int[] arr = new int[n];
int cnt1 = 0;
int cnt2 = 0;
Deque<Integer> q = new LinkedList<>();
Deque<Integer> qq = new LinkedList<>();
for (int i = 0 ; i < n ; i++){
if (s.charAt(i)=='B'){
arr[i] = 1;
cnt1++;
q.addLast(i);
}
else{
qq.addLast(i);
}
}
int r = 0;
Deque<Integer> ans = new LinkedList<>();
cnt2 = n - cnt1;
if (cnt1%2==0 || cnt2%2==0){
if (cnt1%2==0){
while (q.isEmpty()==false){
int first = q.removeFirst();
int second = q.removeFirst();
for (int i = second -1 ; i >= first ; i--){
r++;
ans.addLast(i);
}
}
}
else{
while (qq.isEmpty()==false){
int first = qq.removeFirst();
int second = qq.removeFirst();
for (int i = second -1 ; i >= first ; i--){
r++;
ans.addLast(i);
}
}
}
System.out.println(r);
while (ans.isEmpty()==false){
System.out.print((ans.removeFirst()+1) + " ");
}
}
else{
System.out.println(-1);
}
}
static long fun(int pp , int i , int hops){
if (i<0 || i >= n){
return Long.MAX_VALUE;
}
if (v[i]==true){
if ((pp ^ parity[i])==1){
return hops;
}
else{
if (ans[i]==Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
else{
return ans[i] + 1;
}
}
}
if ((pp ^ parity[i])==1){
return 1;
}
v[i] = true;
long num1 = fun(parity[i] , i + arr[i] , 1);
long num2 = fun(parity[i] , i - arr[i] , 1);
ans[i] = Math.min(num1,num2);
if (ans[i]==Long.MAX_VALUE){
return ans[i];
}
else{
return ans[i] + 1;
}
}
/*static int find(int x) {
return parent[x] == x ? x : (parent[x] = find(parent[x]));
}
static void merge(int x, int y) {
x = find(x);
y = find(y);
if ( x > y ){
parent[y] = x;
}
else if (y > x){
parent[x] = y;
}
}*/
static Deque<Integer>[] graph ;
static void solve()throws IOException{
int n = Reader.nextInt();
}
static int dfs(int start , int cannot){
if (start==cannot){
return 0;
}
v[start] = true;
Iterator i = graph[start].iterator();
int cnt = 1;
while (i.hasNext()){
int next = (int)i.next();
if (!v[next]){
cnt+=dfs(next,cannot);
}
}
return cnt;
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static class Node{
long a;
int i;
Node(long a, int i){
this.a = a;
this.i = i;
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
}
class Node implements Comparable<Node>{
int a;
int b;
Node (int a , int b){
this.a = a;
this.b = b;
}
public int compareTo(Node o) {
if ((this.a%2) == (o.a%2)){
return (this.b - o.b);
}
else{
return this.a - o.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 | n=int(input())
s=input()
c1=s.count('W')
def swap(s):
if s=='W':return 'B'
else:return'W'
if 'W' not in s or 'B' not in s:print(0)
elif (n-c1)%2 and c1%2:print(-1)
else:
L=list(s)
sol=[]
for i in range(1,n-1):
if L[i]!=L[i-1]:
L[i],L[i+1]=swap(L[i]),swap(L[i+1])
sol.append(i+1)
if L[n-1]!=L[n-2]:
for i in range(1, n, 2):sol.append(i)
print(len(sol))
for i in sol: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 java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException {
Reader.init();
int n=Reader.nextInt();
String S=Reader.next();
int w=0;
int b=0;
for(int i=0;i<n;i++){
if(S.charAt(i)=='W'){
w++;
}
else{
b++;
}
}
if(w%2==1 && b%2==1){
println(-1);
}
else{
ArrayList<Integer> A=invert(S, n, 'B');
if(A==null){
A=invert(S, n, 'W');
}
println(A.size());
for(int i=0;i<A.size();i++){
print(A.get(i)+" ");
}
}
}
static ArrayList<Integer> invert(String S, int n, char target){
ArrayList<Integer> A=new ArrayList<Integer>();
char ch[]=S.toCharArray();
for(int i=0;i<n-1;){
if(ch[i]==target){
flip(ch, i);
flip(ch, i+1);
A.add(i+1);
if(ch[i+1]==ch[i]){
i=i+2;
}
else{
i=i+1;
}
}
else{
i++;
}
}
if(match(ch, n)){
return A;
}
return null;
}
static boolean match(char ch[], int n){
for(int i=0;i<n-1;i++){
if(ch[i]!=ch[i+1]){
return false;
}
}
return true;
}
static void flip(char ch[], int i){
if(ch[i]=='W'){
ch[i]='B';
}
else{
ch[i]='W';
}
}
static int mod=1000000007;
public static void read(int arr[], int N) throws IOException{ for(int i=0;i<N;i++){ arr[i]=Reader.nextInt(); } }
public static void read(long arr[], int N) throws IOException{ for(int i=0;i<N;i++){ arr[i]=Reader.nextLong(); } }
public static void read(String arr[], int N) throws IOException{ for(int i=0;i<N;i++){ arr[i]=Reader.next(); } }
public static void print(Object O){ System.out.print(O); }
public static void println(Object O){ System.out.println(O); }
public static void display(int arr[]){ System.out.println(Arrays.toString(arr)); }
public static void display(int arr[][], int N){ for(int i=0;i<N;i++){ System.out.println(Arrays.toString(arr[i])); } }
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init() {
reader=new BufferedReader(new InputStreamReader(System.in));
tokenizer=new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens() ) {
tokenizer=new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
static String nextLine() throws IOException { return reader.readLine(); }
} | 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 typing import List
def solve_for(c: bool, blocks: List[bool]):
flipped = []
for i in range(len(blocks) - 1):
if blocks[i] != c:
blocks[i] = not blocks[i]
blocks[i+1] = not blocks[i+1]
flipped.append(i)
if blocks[-1] == c:
return flipped
else:
return None
def main():
n: int = int(input())
blocks: List[bool] = [c == 'B' for c in input()] # [True, True, False, True, False ... ]
for c in [True, False]:
res = solve_for(c, blocks.copy())
if res is not None:
print(len(res))
print(' '.join(str(x+1) for x in res))
break
else:
print('-1')
if __name__ == '__main__':
main() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | //package com.company;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main{
public static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public char nextChar() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char)c;
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,b=0,W=0;
t=in.nextInt();
String s=in.next();
char c[]=s.toCharArray();
int check[]=new int[t];
int ans[]=new int[3*t];
for(i=0;i<t;i++){
if(c[i]=='B'){
b++;
check[i]=1;
}else{
W++;
check[i]=0;
}
}
if(W%2==1&&b%2==1){
w.println(-1);
w.close();
return;
}
int temp,count=0;
while(count<3*t){
if(W%2==0){
W=0;
b=0;
for(i=0;i<t-1;i++){
if(check[i]==0&&check[i+1]==1){
temp=check[i];
check[i]=check[i+1];
check[i+1]=temp;
ans[count]=i+1;
count++;
}
else if(check[i]==0&&check[i+1]==0){
check[i]=1;
check[i+1]=1;
ans[count]=i+1;
count++;
}
}
for(i=0;i<t;i++){
if(check[i]==0){
W++;
}else{
b++;
}
}
if(W==t||b==t){
break;
}
}if(b%2==0){
b=0;
W=0;
for(i=0;i<t-1;i++){
if(check[i]==1&&check[i+1]==0){
temp=check[i];
check[i]=check[i+1];
check[i+1]=temp;
ans[count]=i+1;
count++;
}
else if(check[i]==1&&check[i+1]==1){
check[i]=0;
check[i+1]=0;
ans[count]=i+1;
count++;
}
}
for(i=0;i<t;i++){
if(check[i]==0){
W++;
}else{
b++;
}
}
if(W==t||b==t){
break;
}
}
}
w.println(count);
for(i=0;i<count;i++){
w.print(ans[i]+" ");
}
w.close();
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from typing import List
def make_equal_to(x: bool, blocks):
flips = []
for i in range(len(blocks)-1):
# blocks[i]
if blocks[i] != x:
# flip i, i+1
blocks[i] = not blocks[i]
blocks[i+1] = not blocks[i+1]
flips.append(i+1)
if blocks[-1] != x:
return None
else:
return flips
def main():
n = int(input())
blocks = input()
blocks: List[bool] = [x == 'B' for x in blocks] # [True, False, True, True ...]
for k in [True, False]:
flips = make_equal_to(k, blocks.copy())
if flips is not None:
print(len(flips))
print(' '.join(str(x) for x in flips))
return
print(-1)
if __name__ == '__main__':
main() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | //package dec15;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
find(in,out);
out.close();
}
private static char getColor(String word){
int w = 0;
int b = 0;
for(char ch : word.toCharArray()){
if(Character.toLowerCase(ch) == 'w'){
w+=1;
}else{
b+=1;
}
}
if( w % 2 == 0){
return 'W';
}else if(b % 2 == 0){
return 'B';
}else{
return 'c';
}
}
private static void find(InputReader in, PrintWriter out) {
int length = in.nextInt();
String word = in.next();
char color = getColor(word);
if(color == 'c'){
out.println(-1);
return;
}
List<Integer> sequence = new ArrayList<>();
int numOfOperations = 0;
for(int i=0;i<word.length()-1;i++){
if(word.charAt(i) == color){
if(word.charAt(i+1) == color){
if(color == 'W'){
word = word.substring(0,i)+"BB"+word.substring(i+2);
sequence.add(i+1);
}else{
word = word.substring(0,i)+"WW"+word.substring(i+2);
sequence.add(i+1);
}
}else{
if(color == 'B'){
word = word.substring(0,i)+"WB"+word.substring(i+2);
}else{
word = word.substring(0,i)+"BW"+word.substring(i+2);
}
sequence.add(i+1);
}
numOfOperations+=1;
}
}
out.println(numOfOperations);
for(Integer num : sequence){
out.print(num+" ");
}
}
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;
int main() {
int n;
cin >> n;
string s, c;
cin >> s;
vector<int> vw, vb;
c = s;
int a = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
vw.push_back(i + 1);
} else if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
vw.push_back(i + 1);
}
}
if (s[n - 1] == 'W') {
cout << vw.size() << "\n";
for (int i = 0; i < vw.size(); i++) {
cout << vw[i] << " ";
}
a++;
} else {
for (int i = 0; i < n - 1; i++) {
if (c[i] == 'W' && c[i + 1] == 'B') {
c[i] = 'B';
c[i + 1] = 'W';
vb.push_back(i + 1);
} else if (c[i] == 'W' && c[i + 1] == 'W') {
c[i] = 'B';
c[i + 1] = 'B';
vb.push_back(i + 1);
}
}
if (c[n - 1] == 'B') {
cout << vb.size() << "\n";
for (int i = 0; i < vb.size(); i++) {
cout << vb[i] << " ";
}
a++;
}
}
if (a == 0) {
cout << "-1"
<< " ";
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class ProbB
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=0;
List<Integer> l = new ArrayList<>();
n = sc.nextInt();
String s = sc.next();
char a[] = s.toCharArray();
int w=0,b=0;
for(int i=0;i<a.length;i++)
{
if(a[i]=='W')
w++;
else
b++;
}
// System.out.println(a);
if(w%2==1 && b%2==1)
{
System.out.println("-1");
}
else
{
int swaps=0;
char goal = a[a.length-1];
boolean flag=false;
for(int i=n-2;i>=1;i--)
{
if(a[i]!=goal)
{
swaps++;
flag=true;
if(a[i]=='B')
a[i]='W';
else
a[i]='B';
if(a[i-1]=='W')
a[i-1]='B';
else
a[i-1]='W';
l.add(i);
// System.out.print(i+" ");
}
}
goal = (goal=='B')?'W':'B';
//System.out.println("GOALLLLLL "+goal);
//System.out.println(new String(a));
if(new String(a).contains(goal+""))
{
swaps=0;
a=s.toCharArray();
//System.out.println("SSSS");
l = new ArrayList<Integer>();
flag=false;
for(int i=a.length-1;i>=1;i--)
{
if(a[i]!=goal)
{
swaps++;
flag=true;
if(a[i]=='B')
a[i]='W';
else
a[i]='B';
if(a[i-1]=='W')
a[i-1]='B';
else
a[i-1]='W';
l.add(i);
// System.out.print(i+" ");
}
}
}
if(!flag)
System.out.println(0);
else
{
System.out.println(swaps);
for(int i=0;i<l.size();i++)
{
System.out.print(l.get(i)+" ");
}
}
}
//System.out.println(new String(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 | from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
def solve():
n = ri()
S = list(input())
S2 = list(S)
target = S[0]
moves = []
swap = {"W":"B", "B":"W"}
for i in range(1, len(S) - 1):
if S[i] != target:
moves.append(i + 1)
S[i+1] = swap[S[i+1]]
S2[0] = swap[S2[0]]
S2[1] = swap[S2[1]]
target = S2[0]
moves2 = [1]
for i in range(1, len(S2) - 1):
if S2[i] != target:
moves2.append(i + 1)
S2[i+1] = swap[S2[i+1]]
if S[-1] != S[0] and S2[-1] != S2[0]:
print (-1)
elif S[-1] == S[0]:
print (len(moves))
print (*moves)
else:
print (len(moves2))
print (*moves2)
mode = 's'
if mode == 'T':
t = ri()
for i in range(t):
solve()
else:
solve()
| 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()
b,w = s.count('B'),s.count('W')
if b==n or w==n:
print(0)
elif b%2==1 and w%2==1:
print(-1)
else:
c,p = 0,0
ans = []
if w%2==0:
x = "W"
else:
x = "B"
for i in range(n):
if (s[i]==x and p==0) or (s[i]!=x and p==1):
p = 1
c += 1
ans.append(i+1)
else:
p = 0
print(c)
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 | // package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class BE2 {
public static void main(String[] Args){
FastReader scan=new FastReader();
int n=scan.nextInt();
StringBuilder s=new StringBuilder(scan.next());
int bc=0;
int wc=0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='B'){
bc++;
}
else{
wc++;
}
}
if(bc%2==0||wc%2==0){
int m=1;
if(bc%2==0){
m=0;
}
int moves=0;
ArrayList<Integer> ans=new ArrayList<>();
for(int i=0;i<n-1;i++){
if(m==0){
if(s.charAt(i)=='B'){
moves++;
if(s.charAt(i+1)=='W'){
s.setCharAt(i+1,'B');
}
else{
s.setCharAt(i+1,'W');
}
ans.add(i+1);
}
}
else{
if(s.charAt(i)=='W'){
moves++;
if(s.charAt(i+1)=='W'){
s.setCharAt(i+1,'B');
}
else{
s.setCharAt(i+1,'W');
}
ans.add(i+1);
}
}
}
System.out.println(moves);
for(Integer j:ans){
System.out.print(j+" ");
}
}
else{
System.out.println(-1);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | 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)
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 N;
string s;
int arr[210];
vector<int> res;
int main() {
ios_base::sync_with_stdio(0);
cin >> N >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'B')
arr[i] = 1;
else
arr[i] = 0;
}
for (int i = 1; i + 1 < s.length(); i++) {
if (arr[i] != arr[i - 1]) {
res.push_back(i);
arr[i] = 1 - arr[i];
arr[i + 1] = 1 - arr[i + 1];
}
}
if (arr[N - 2] != arr[N - 1]) {
if (N % 2 == 0) {
cout << "-1\n";
return 0;
}
for (int i = 0; i + 1 < N; i += 2) res.push_back(i);
}
cout << res.size() << "\n";
for (int x : res) cout << x + 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 | #!/usr/bin/env python3
import sys
#lines = stdin.readlines()
def rint():
return map(int, sys.stdin.readline().split())
def input():
return sys.stdin.readline().rstrip('\n')
def oint():
return int(input())
n = oint()
s = list(input())
bc = s.count('B')
wc = s.count('W')
if bc %2 and wc %2:
print(-1)
exit()
s0 = s[0]
cnt = 0
ans = []
for i in range(1, n-1):
if s0 != s[i]:
s[i] = s0
if s[i+1] == 'B':
s[i+1] = 'W'
else:
s[i+1] = 'B'
cnt += 1
ans.append(i+1)
#print(s)
if s[-1] != s0:
for i in range(n//2):
cnt += 1
ans.append(i*2+1)
print(cnt)
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.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String str=s.next();
char[] arr=new char[n];
for(int i=0;i<n;i++)
{
arr[i]=str.charAt(i);
}
ArrayList<Integer> list=new ArrayList<>();
for(int i=1;i<n-1;i++)
{
if(arr[i]!=arr[0])
{
if(arr[0]=='B')
{
arr[i]='B';
if(arr[i+1]=='B')
{
arr[i+1]='W';
}
else
{
arr[i+1]='B';
}
list.add(i+1);
}
else
{
arr[i]='W';
if(arr[i+1]=='B')
{
arr[i+1]='W';
}
else
{
arr[i+1]='B';
}
list.add(i+1);
}
}
}
int b=0;
int w=0;
for(int i=0;i<n;i++)
{
if(arr[i]=='B')
b++;
else
{
w++;
}
}
if(b%2==0)
{
for(int i=0;i<n;i++)
{
if(arr[i]=='B')
{
list.add(i+1);
arr[i]='W';
arr[i+1]='W';
}
}
if(list.size()<=3*n)
{
System.out.println(list.size());
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i)+" ");
}
}
else
{
System.out.println(-1);
}
}
else if(w%2==0)
{
for(int i=0;i<n;i++)
{
if(arr[i]=='W')
{
list.add(i+1);
arr[i]='B';
arr[i+1]='B';
}
}
if(list.size()<=3*n)
{
System.out.println(list.size());
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i)+" ");
}
}
else
{
System.out.println(-1);
}
}
else
{
System.out.println(-1);
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void change(char &x) {
if (x == 'B')
x = 'W';
else
x = 'B';
}
int main() {
string s;
int n;
cin >> n;
cin >> s;
int w = 0, b = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b % 2 != 0 && w % 2 != 0) {
cout << -1;
return 0;
}
string c = s;
vector<int> vec1, vec2;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
vec1.push_back(i + 1);
if (s[i] == 'B') change(s[i]);
change(s[i + 1]);
}
}
if (s[n - 1] == 'W') {
cout << vec1.size() << endl;
for (int i = 0; i < vec1.size(); i++) cout << vec1[i] << " ";
return 0;
}
for (int i = 0; i < n - 1; i++) {
if (c[i] == 'W') {
vec2.push_back(i + 1);
change(c[i]);
change(c[i + 1]);
}
}
if (s[n - 1] == 'B') {
cout << vec2.size() << endl;
for (int i = 0; i < vec2.size(); i++) cout << vec2[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 | import java.util.*;
import java.io.*;
public class Codeforces
{
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
static StringBuilder sb=new StringBuilder();
static long MOD = (long)(998244353);
// Main Class Starts Here
public static void main(String args[])throws IOException
{
// Write your code.
int t=1;
while(t-->0) {
int n=in();
char a[]=sn().toCharArray();
char x = a[0];
Vector<Integer> v=new Vector<>();
int count = 0 ;
for(int i=1;i<n-1;i++) {
if(a[i] == x)
continue;
v.add(i+1);
a[i] = x;
if(a[i+1] == 'B')
a[i+1] = 'W';
else
a[i+1] = 'B';
count++;
}
if(a[n-1] != x) {
if((n-1) % 2 != 0)app(-1+"\n");
else {
x = a[n-1];
for(int i=0;i<n-1;i+=2) {
a[i] = x;
a[i+1] = x;
count++;
v.add(i+1);
}
app(count+"\n");
for(int xx:v)app(xx+" ");
}
}
else {
app(count+"\n");
for(int xx:v)app(xx+" ");
}
}
out.printLine(sb);
out.close();
}
static class Pair
{
int x;
int y;
public Pair(int x,int y) {
this.x=x;
this.y=y;
}
}
public static long rev(long n) {
if(n<10L)
return n;
long rev = 0L;
while(n != 0L) {
long a = n % 10L;
rev = a + rev*10L;
n /= 10L;
}
return rev;
}
public static long pow(long a,long b) {
if(b==0)
return 1L;
if(b==1)
return a % MOD;
long f = a * a;
if(f > MOD)
f %= MOD;
long val = pow(f, b/2) % MOD;
if(b % 2 == 0)
return val;
else {
long temp = val * (a % MOD);
if(temp > MOD)
temp = temp % MOD;
return temp;
}
}
public static long lcm(long a,long b) {
long lcm = (a*b)/gcd(a,b);
return lcm;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b, a%b);
}
public static int[] an(int n) {
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=in();
return ar;
}
public static long[] lan(int n) {
long ar[]=new long[n];
for(int i=0;i<n;i++)
ar[i]=lin();
return ar;
}
public static String atos(Object ar[]) {
return Arrays.toString(ar);
}
public static int in() {
return in.readInt();
}
public static long lin() {
return in.readLong();
}
public static String sn() {
return in.readString();
}
public static void prln(Object o) {
out.printLine(o);
}
public static void prn(Object o) {
out.print(o);
}
public static int getMax(int a[]) {
int n = a.length;
int max = a[0];
for(int i=1; i < n; i++) {
max = Math.max(max, a[i]);
}
return max;
}
public static int getMin(int a[]) {
int n = a.length;
int min = a[0];
for(int i=1; i < n; i++) {
min = Math.min(min, a[i]);
}
return min;
}
public static void display(int a[]) {
out.printLine(Arrays.toString(a));
}
public static HashMap<Integer,Integer> hm(int a[]){
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0; i < a.length;i++) {
int keep = (int)map.getOrDefault(a[i],0);
map.put(a[i],++keep);
}
return map;
}
public static void app(Object o) {
sb.append(o);
}
public static long calcManhattanDist(int x1,int y1,int x2,int y2) {
long xa = Math.abs(x1-x2);
long ya = Math.abs(y1-y2);
return (xa+ya);
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
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 readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> ans;
for (int i = 0; i < n; i++) {
if (i < n - 1) {
if (s[i] == 'W') continue;
if (s[i + 1] == 'B') {
ans.push_back(i + 1);
s[i] = 'W';
s[i + 1] = 'W';
} else {
ans.push_back(i + 1);
s[i] = 'W';
s[i + 1] = 'B';
}
}
}
if (s[n - 1] == 'B') {
if (n & 1) {
for (int i = 1; i < n; i += 2) ans.push_back(i);
} else {
cout << -1;
return 0;
}
}
cout << ans.size() << "\n";
for (int it : ans) cout << it << ' ';
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.ArrayList;
/**
* Created by Ayushi on 15/12/2019.
*/
public class B {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String[] a = r.readLine().split(" ");
int n = Integer.parseInt(a[0]);
a = r.readLine().split("");
r.close();
int b, w;
b = w = 0;
for (int i = 0; i < n; i++) {
if (a[i].equals("B")) b++;
else w++;
}
ArrayList<Integer> ans = new ArrayList<>();
String x, y;
if (b % 2 == 1 && w % 2 == 1) {
System.out.println(-1);
}
else {
if (b % 2 == 0) {
x = "B";
y = "W";
}
else {
x = "W";
y = "B";
}
for (int j = 0; j < n-1; j++){
if (a[j].equals(x)) {
ans.add(j+1);
a[j] = y;
if (a[j+1].equals(x)) a[j+1] = y;
else a[j+1] = x;
}
}
System.out.println(ans.size());
for (int k = 0; k < ans.size(); k++) {
System.out.print(ans.get(k) + " ");
}
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def flip(s,i):
s[i] = 'B' if s[i] == 'W' else 'W'
n = int(raw_input())
s = list(raw_input())
color = s[0]
ops = []
for i in xrange(1,n-1):
if s[i] != color:
ops.append(i+1)
flip(s,i)
flip(s,i+1)
if s[-1] == color:
# we are done!
print len(ops)
for op in ops:
print op,
else:
# oh no, the last one must be wrong!
if n%2 == 0:
print -1
else:
# flush it again!
color = 'B' if color=='W' else 'W'
for i in xrange(0,n-1):
if s[i] != color:
ops.append(i+1)
flip(s,i)
flip(s,i+1)
print len(ops)
for op in ops:
print op, | 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 |
/**
* Date: 15 Dec, 2019
* Link: https://codeforces.com/contest/1271/problem/B
*
* @author Prasad Chaudhari
* @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/
* @git: https://github.com/Prasad-Chaudhari
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.LinkedList;
public class B_1271 {
public static void main(String[] args) throws IOException {
// TODO code application logic here
FastIO in = new FastIO(args);
int n = in.ni();
int index = 0;
int a[] = new int[n];
int sum = 0;
for (char c : in.next().toCharArray()) {
a[index++] = c == 'W' ? 1 : 0;
sum += a[index - 1];
}
int b[] = Arrays.copyOf(a, n);
if (sum == 0 || sum == n) {
System.out.println(0);
return;
}
LinkedList<Integer> l = new LinkedList<>();
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1] && a[i + 1] == 1) {
a[i] ^= 1;
a[i + 1] ^= 1;
l.add(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (a[i] == 1) {
a[i] ^= 1;
a[i + 1] ^= 1;
l.add(i);
}
}
boolean p = true;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
p = false;
}
}
if (p) {
System.out.println(l.size());
for (int i : l) {
System.out.print((i + 1) + " ");
}
} else {
a = b;
for (int i = 0; i < n; i++) {
a[i] ^= 1;
}
l = new LinkedList<>();
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1] && a[i + 1] == 1) {
a[i] ^= 1;
a[i + 1] ^= 1;
l.add(i);
}
}
for (int i = 0; i < n - 1; i++) {
if (a[i] == 1) {
a[i] ^= 1;
a[i + 1] ^= 1;
l.add(i);
}
}
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
System.out.println(-1);
return;
}
}
System.out.println(l.size());
for (int i : l) {
System.out.print((i + 1) + " ");
}
}
in.bw.flush();
}
static class Data implements Comparable<Data> {
int a, b;
public Data(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Data o) {
if (a == o.a) {
return Integer.compare(b, o.b);
}
return Integer.compare(a, o.a);
}
public static void sort(int a[]) {
Data d[] = new Data[a.length];
for (int i = 0; i < a.length; i++) {
d[i] = new Data(a[i], 0);
}
Arrays.sort(d);
for (int i = 0; i < a.length; i++) {
a[i] = d[i].a;
}
}
}
static class FastIO {
private final BufferedReader br;
private final BufferedWriter bw;
private String s[];
private int index;
public FastIO(String[] args) throws IOException {
if (args.length > 1) {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(args[0]))));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(args[1]))));
} else {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
}
s = br.readLine().split(" ");
index = 0;
}
public int ni() throws IOException {
return Integer.parseInt(nextToken());
}
public double nd() throws IOException {
return Double.parseDouble(nextToken());
}
public long nl() throws IOException {
return Long.parseLong(nextToken());
}
public String next() throws IOException {
return nextToken();
}
public String nli() throws IOException {
try {
return br.readLine();
} catch (IOException ex) {
}
return null;
}
public int[] gia(int n) throws IOException {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public int[] gia(int n, int start, int end) throws IOException {
validate(n, start, end);
int a[] = new int[n];
for (int i = start; i < end; i++) {
a[i] = ni();
}
return a;
}
public double[] gda(int n) throws IOException {
double a[] = new double[n];
for (int i = 0; i < n; i++) {
a[i] = nd();
}
return a;
}
public double[] gda(int n, int start, int end) throws IOException {
validate(n, start, end);
double a[] = new double[n];
for (int i = start; i < end; i++) {
a[i] = nd();
}
return a;
}
public long[] gla(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public long[] gla(int n, int start, int end) throws IOException {
validate(n, start, end);
long a[] = new long[n];
for (int i = start; i < end; i++) {
a[i] = nl();
}
return a;
}
public int[][][] gwtree(int n) throws IOException {
int m = n - 1;
int adja[][] = new int[n + 1][];
int weight[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
int cost[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = i + 1;
to[i] = ni();
cost[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
weight[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][count[from[i]] - 1] = to[i];
adja[to[i]][count[to[i]] - 1] = from[i];
weight[from[i]][count[from[i]] - 1] = cost[i];
weight[to[i]][count[to[i]] - 1] = cost[i];
count[from[i]]--;
count[to[i]]--;
}
return new int[][][] { adja, weight };
}
public int[][][] gwg(int n, int m) throws IOException {
int adja[][] = new int[n + 1][];
int weight[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
int cost[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = ni();
to[i] = ni();
cost[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
weight[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][count[from[i]] - 1] = to[i];
adja[to[i]][count[to[i]] - 1] = from[i];
weight[from[i]][count[from[i]] - 1] = cost[i];
weight[to[i]][count[to[i]] - 1] = cost[i];
count[from[i]]--;
count[to[i]]--;
}
return new int[][][] { adja, weight };
}
public int[][] gtree(int n) throws IOException {
int adja[][] = new int[n + 1][];
int from[] = new int[n - 1];
int to[] = new int[n - 1];
int count[] = new int[n + 1];
for (int i = 0; i < n - 1; i++) {
from[i] = i + 1;
to[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
}
for (int i = 0; i < n - 1; i++) {
adja[from[i]][--count[from[i]]] = to[i];
adja[to[i]][--count[to[i]]] = from[i];
}
return adja;
}
public int[][] gg(int n, int m) throws IOException {
int adja[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = ni();
to[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][--count[from[i]]] = to[i];
adja[to[i]][--count[to[i]]] = from[i];
}
return adja;
}
public void print(String s) throws IOException {
bw.write(s);
}
public void println(String s) throws IOException {
bw.write(s);
bw.newLine();
}
public void print(int s) throws IOException {
bw.write(s + "");
}
public void println(int s) throws IOException {
bw.write(s + "");
bw.newLine();
}
public void print(long s) throws IOException {
bw.write(s + "");
}
public void println(long s) throws IOException {
bw.write(s + "");
bw.newLine();
}
public void print(double s) throws IOException {
bw.write(s + "");
}
public void println(double s) throws IOException {
bw.write(s + "");
bw.newLine();
}
private String nextToken() throws IndexOutOfBoundsException, IOException {
if (index == s.length) {
s = br.readLine().split(" ");
index = 0;
}
return s[index++];
}
private void validate(int n, int start, int end) {
if (start < 0 || end >= n) {
throw new IllegalArgumentException();
}
}
}
}
| 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 | '''input
3
BWB
'''
n = int(input())
string = input()
temp = []
for ele in string:
if ele == 'B':
temp.append(0)
else:
temp.append(1)
string = temp[::]
ans = []
temp = string[::]
for i in range(n - 1):
if temp[i] == 0:
temp[i] ^= 1
temp[i + 1] ^= 1
ans.append(i + 1)
# print(string, temp)
if temp[n - 1] == 0:
ans = []
temp = string[::]
for i in range(n - 1):
if temp[i] == 1:
temp[i] ^= 1
temp[i + 1] ^= 1
ans.append(i + 1)
if temp[n - 1] == 1:
print(-1)
else:
print(len(ans))
print(*ans)
else:
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5 + 10;
int main() {
int n;
string s, s1;
cin >> n >> s;
s1 = s;
int i = n - 1;
vector<int> v1, v2;
while (i > 0) {
while (i >= 1 && s1[i] == 'B') i--;
if (i >= 1) {
s1[i] = 'B';
if (s1[i - 1] == 'W')
s1[i - 1] = 'B';
else
s1[i - 1] = 'W';
v1.push_back(i);
i--;
} else
break;
}
i = n - 1;
while (i > 0) {
while (i >= 1 && s[i] == 'W') i--;
if (i >= 1) {
s[i] = 'W';
if (s[i - 1] == 'W')
s[i - 1] = 'B';
else
s[i - 1] = 'W';
v2.push_back(i);
i--;
} else
break;
}
sort(s.begin(), s.end());
sort(s1.begin(), s1.end());
if (s[0] == s.back()) {
cout << v2.size() << endl;
for (auto i : v2) cout << i << " ";
} else if (s1[0] == s1.back()) {
cout << v1.size() << endl;
for (auto i : v1) cout << i << " ";
} else
cout << "-1";
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
cw=0
cb=0
l=[]
for i in range(n-1):
if s[i]=='B':
s[i]='W'
s[i+1]='W' if s[i+1]=='B' else 'B'
l.append(i+1)
if s[n-1]=='W':
print(len(l))
if len(l)>0:
print(*l,sep=" ")
else:
if n%2==1:
for i in range(0,n-1,2):
l.append(i+1)
print(len(l))
if len(l)>0:
print(*l,sep=" ")
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.*;
public class vk18
{
public static void main(String[]st) throws Exception
{
Scanner scan=new Scanner(System.in);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String[] s;
int n;
String na;
n=scan.nextInt();
na=scan.next();
char c[]=new char[n];
c=na.toCharArray();
int a[]=new int[n];
int p=0,b=0,w=0,i;
for(i=0;i<n;i++) { if(c[i]=='B') b++; else w++; }
//System.out.println(b+" "+w);
if(b%2!=0 && w%2!=0) {System.out.println("-1"); System.exit(0);}
if(b%2==0)
{
for(i=0;i<n-1;i++)
{
if(c[i]=='B')
{
c[i]='W';
a[p++]=i+1;
if(c[i+1]=='B') c[i+1]='W';
else c[i+1]='B';
}
}
}
else
{
//System.out.print("IN THE ELSE");
for(i=0;i<n-1;i++)
{
if(c[i]=='W')
{
c[i]='B';
a[p++]=i+1;
if(c[i+1]=='B') c[i+1]='W';
else c[i+1]='B';
}
}
}
System.out.print(p);
System.out.println();
for(i=0;i<p;i++) System.out.print(a[i]+" ");
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
input = sys.stdin.readline
N = int(input())
S = [1 if a == 'W' else 0 for a in list(input().rstrip())]
ans = []
for i in range(1,N-1):
if S[i-1] != S[i]:
S[i] ^= 1
S[i+1] ^= 1
ans.append(i+1)
if S[N-2] != S[N-1] and N%2 == 0:
print(-1)
else:
if S[N-2] != S[N-1]:
for i in range(N-1):
if i%2 == 0:
ans.append(i+1)
print(len(ans))
for a in ans:
print(a, 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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class P9 {
public static void main(String args[])
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String a=br.readLine();
if(Blocks(a)==null)
System.out.println(-1);
else
{
System.out.println(Blocks(a).size());
for(int x:Blocks(a))
{
System.out.print(x+" ");
}
System.out.println();
}
}
catch(Exception e)
{
}
}
private static ArrayList<Integer> Blocks(String a) {
// TODO Auto-generated method stub
ArrayList<Integer> al=new ArrayList<Integer>();
int n =a.length();
char arr[]=a.toCharArray();
for(int i=1;i<n-1;i++)
{
if(arr[i]!=arr[i-1])
{
arr[i]=(arr[i]=='W')?'B':'W';
arr[i+1]=(arr[i+1]=='W')?'B':'W';
al.add(i+1);
}
}
String s=new String(arr);
if(s.replace(String.valueOf(s.charAt(0)),"").length()==0)
return al;
else
{
arr=a.toCharArray();
al=new ArrayList<Integer>();
arr[0]=(arr[0]=='W')?'B':'W';
arr[1]=(arr[1]=='W')?'B':'W';
al.add(1);
for(int i=1;i<n-1;i++)
{
if(arr[i]!=arr[i-1])
{
arr[i]=(arr[i]=='W')?'B':'W';
arr[i+1]=(arr[i+1]=='W')?'B':'W';
al.add(i+1);
}
}
s=new String(arr);
if(s.replace(String.valueOf(s.charAt(0)),"").length()==0)
return al;
}
return null;
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def swap(a):
if(a=='B'):
return 'W'
return 'B'
n = int(input())
s = list(input())
w = s.count('W')
b = len(s)-w
ans = 0
arr = []
if(b%2==0):
for i in range(len(s)-1):
if(s[i]=='B'):
s[i]=swap(s[i])
s[i+1]=swap(s[i+1])
ans+=1
arr.append(i)
elif(w%2==0):
for i in range(len(s)-1):
if(s[i]=='W'):
s[i]=swap(s[i])
s[i+1]=swap(s[i+1])
ans+=1
arr.append(i)
else:
ans = -1
print(ans)
for i in arr:
print(i+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;
struct FASTINPUT {
void f(string& s) {
char buf[400005];
long long a = scanf("%s", buf);
a++;
s = buf;
}
void f(long long& x) {
long long a = scanf("%lld", &x);
a++;
}
void f(int& x) {
long long a = scanf("%d", &x);
a++;
}
void f(double& x) {
long long a = scanf("%lf", &x);
a++;
}
template <typename A, typename B>
void f(pair<A, B>& p) {
f(p.first);
f(p.second);
}
template <typename A>
void f(vector<A>& x) {
for (auto& y : x) f(y);
}
void read() {}
template <typename Head, typename... Tail>
void read(Head& H, Tail&... T) {
f(H);
read(T...);
}
};
struct FASTOUTPUT {
void f(string s) { printf("%s", s.c_str()); }
void f(long long x) { printf("%lld", x); }
void f(int x) { printf("%d", x); }
void f(double x) { printf("%.20f", x); }
void f(char x) { printf("%c", x); }
void f(const char* a) { printf("%s", a); }
template <typename A>
void f(vector<A>& x) {
for (auto& y : x) {
f(y);
f(" ");
}
}
void print() {}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
f(H);
print(T...);
}
};
struct DEBUG {
string open = "[", close = "]", sep = ", ";
string f(string s) { return s; }
string f(char c) { return string(1, c); }
string f(int x) { return to_string(x); }
string f(long long x) { return to_string(x); }
string f(double x) { return to_string(x); }
string f(const char* a) { return a; }
template <typename A, typename B>
string f(pair<A, B> p) {
return open + f(p.first) + sep + f(p.second) + close;
}
template <typename A>
string f(A v) {
string s = open;
for (auto a : v) {
if (s != open) {
s += sep;
}
s += f(a);
}
s += close;
return s;
}
void show() { cout << endl; }
template <typename Head, typename... Tail>
void show(Head H, Tail... T) {
cout << " " << f(H);
show(T...);
}
};
vector<long long> di = {0, 0, 1, -1, 1, -1, 1, -1};
vector<long long> dj = {1, -1, 0, 0, -1, -1, 1, 1};
long long ob(long long i, long long n) { return i < 0 || i >= n; }
long long tp(long long x) { return (1LL << x); }
long long roundup(long long a, long long b) {
return a % b ? a / b + 1 : a / b;
}
template <typename A, typename B>
long long exist(A a, B b) {
return a.find(b) != a.end();
}
vector<string> ssplit(string s) {
vector<string> ans;
stringstream ss;
ss << s;
while (ss >> s) ans.push_back(s);
return ans;
}
void makemod(long long& x, long long m) {
x %= m;
x += m;
x %= m;
}
long long powmod(long long a, long long b, long long m) {
if (b == 0) return 1;
long long h = powmod(a, b / 2, m);
long long ans = h * h % m;
return b % 2 ? ans * a % m : ans;
}
bool isvowel(char ch) {
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
long long sign(long long x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
void upmin(long long& x, long long v) { x = min(x, v); }
void upmax(long long& x, long long v) { x = max(x, v); }
const vector<long long> mods = {(long long)1e9 + 7, 998244353,
(long long)1e6 + 3, (long long)1e18 + 5};
const long long mod = mods[0];
const long long inf = mods[3];
const double eps = 1e-10;
const double pi = acos(0) * 2;
const string nl = "\n";
void flip(char& c) {
if (c == 'W')
c = 'B';
else
c = 'W';
}
long long flag = 0;
vector<long long> f(string s, char target) {
flag = 0;
vector<long long> ans;
for (long long i = 0; i < (long long)s.length(); i++) {
if (s[i] != target) {
if (i == (long long)s.length() - 1) {
flag = 1;
return {-1};
}
ans.push_back(i + 1);
flip(s[i]);
flip(s[i + 1]);
}
}
return ans;
}
void solve() {
long long n;
string s;
do {
FASTINPUT inp;
inp.read(n, s);
} while (false);
vector<long long> ans = f(s, 'B');
if (!flag) {
do {
FASTOUTPUT out;
out.print((long long)ans.size(), nl);
} while (false);
do {
FASTOUTPUT out;
out.print(ans, nl);
} while (false);
return;
}
ans = f(s, 'W');
if (!flag) {
do {
FASTOUTPUT out;
out.print((long long)ans.size(), nl);
} while (false);
do {
FASTOUTPUT out;
out.print(ans, nl);
} while (false);
return;
}
do {
FASTOUTPUT out;
out.print(-1, nl);
} while (false);
return;
}
signed main() {
long long t = 1;
for (long long i = 0; i < t; i++) 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;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
long long fact(long long n);
long long nCr(long long n, long long r) {
return fact(n) / (fact(r) * fact(n - r));
}
long long fact(long long n) {
long long res = 1;
for (long long i = 2; i <= n; i++) res = res * i;
return res;
}
long long dif(long long a, long long b) {
if (a > b) return a - b;
return b - a;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
long long k = gcd(a, b);
return (a * b) / k;
}
int main() {
int t;
t = 1;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
w++;
} else {
b++;
}
}
if (b % 2 == 1 && w % 2 == 1) {
cout << -1 << endl;
} else if (w % 2 == 0) {
vector<int> v;
int flag = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W' && flag == 0) {
v.push_back(i);
flag = 1;
} else if (flag == 1 && s[i] == 'B') {
v.push_back(i);
} else if (flag == 1 && s[i] == 'W') {
flag = 0;
}
}
cout << v.size() << endl;
for (int x : v) cout << x + 1 << " ";
cout << endl;
} else {
vector<int> v;
int flag = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B' && flag == 0) {
v.push_back(i);
flag = 1;
} else if (flag == 1 && s[i] == 'W') {
v.push_back(i);
} else if (flag == 1 && s[i] == 'B') {
flag = 0;
}
}
cout << v.size() << endl;
for (int x : v) cout << x + 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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author anand.oza
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BBlocks solver = new BBlocks();
solver.solve(1, in, out);
out.close();
}
static class BBlocks {
int[] b;
List<Integer> answer;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
b = new int[n];
char[] input = in.next().toCharArray();
for (int i = 0; i < n; i++) {
b[i] = input[i] == 'B' ? 1 : 0;
}
int sum = 0;
for (int x : b)
sum += x;
if (sum % 2 == 1 && n % 2 == 0) {
out.println(-1);
return;
}
answer = new ArrayList<>();
int target;
if (sum % 2 == 0) {
target = 0;
} else {
target = 1;
}
for (int i = 0; i + 1 < n; i++) {
if (b[i] != target)
op(i);
}
out.println(answer.size());
out.println(Util.join(answer));
}
private void op(int i) {
b[i] ^= 1;
b[i + 1] ^= 1;
answer.add(i + 1);
}
}
static class Util {
public static String join(Iterable i) {
StringBuilder sb = new StringBuilder();
for (Object o : i) {
sb.append(o);
sb.append(" ");
}
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
}
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 | n = int(input())
sq = input().strip()
sq = list(sq)
b, w = (0, 0)
for ch in sq:
if ch == 'B':
b += 1
else:
w += 1
# only single type
if b == 0 or w == 0:
print(0)
exit(0)
# if both of them is odd
elif b & 1 == 1 and w & 1 == 1:
print(-1)
exit(0)
if b & 1 == 0:
f = 'W'
s = 'B'
else:
f = 'B'
s = 'W'
ans = []
while True:
no_swap = True
for i in range(1, n):
# push all B towards the end
if sq[i-1] != sq[i] and sq[i-1] == s:
# swap
sq[i-1] = f
sq[i] = s
ans.append(i-1)
no_swap = False
if sq[i-1] == sq[i] and sq[i-1] == s:
sq[i-1] = sq[i] = f
ans.append(i-1)
if no_swap:
s_count = sq.count(s)
for i in range(n-s_count, n, 2):
ans.append(i)
print(len(ans))
ans = list(map(lambda x: x + 1, ans))
print(*ans)
exit(0)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from collections import Counter as C,defaultdict as D,deque as Q
from operator import itemgetter as I
from itertools import product as P,permutations as PERMUT
from bisect import bisect_left as BL,bisect_right as BR,insort as INSORT
from heapq import heappush as HPUSH,heappop as HPOP
from math import floor as MF,ceil as MC, gcd as MG,factorial as F,sqrt as SQRT, inf as INFINITY,log as LOG
from sys import stdin, stdout
INPUT=stdin.readline
PRINT=stdout.write
L=list;M=map
def Player1():
print("")
def Player2():
print("")
def Yes():
PRINT("Yes\n")
def No():
PRINT("No\n")
def IsPrime(n):
for i in range(2,MC(SQRT(n))+1):
if n%i==0:
return False
return True
def Factors(x):
ans=[]
for i in range(1,MC(SQRT(x))+1):
if x%i==0:
ans.append(i)
if x%(x//i)==0:
ans.append(x//i)
return ans
def CheckPath(source,destination,g):
visited=[0]*101
q=Q()
q.append(source)
visited[source]=1
while q:
node=q.popleft()
if node==destination:
return 1
for v in g[node]:
if not visited[v]:
q.append(v)
visited[v]=1
return 0
def Sieve(n):
prime=[1]*(n+1)
p=2
while p*p<=n:
if prime[p]:
for i in range(p*p,n+1,p):
prime[i]=0
p+=1
primes=[]
for p in range(2,n+1):
if prime[p]:
primes.append(p)
return primes
def Prefix(a,n):
p=[]
for i in range(n):
if i==0:
p.append(a[0])
else:
p.append(p[-1]+a[i])
return p
def Suffix(a,n):
s=[0]*n
for i in range(n-1,-1,-1):
if i==n-1:
s[i]=a[i]
else:
s[i]=s[i+1]+a[i]
return s
def Spf(n):
spf=[0 for i in range(n)]
spf[1]=1
for i in range(2,n):
spf[i]=i
for i in range(4,n,2):
spf[i]=2
for i in range(3,MC(SQRT(n))+1):
if spf[i]==i:
for j in range(i*i,n,i):
if spf[j]==j:
spf[j]=i
return spf
def DFS(g,s,visited,ans):
visited[s]=1
for u,c in g[s]:
if visited[u]:
continue
if c==ans[s]:
if c==1:
ans[u]=2
else:
ans[u]=1
else:
ans[u]=c
DFS(g,u,visited,ans)
def lcm(a,b):
return (a*b)//(MG(a,b))
def Kadane(numbers):
max_so_far=-INFINITY
max_ending_here=0
max_element=-INFINITY
for i in range(len(numbers)):
max_ending_here=max(max_ending_here+numbers[i],0)
max_so_far=max(max_ending_here,max_so_far)
max_element=max(max_element,numbers[i])
if max_so_far==0:
max_so_far=max_element
return max_so_far
def Main():
'''
10
WWBBBWBBWB
'''
n=int(INPUT())
x=INPUT()
a=[0]*n
for i in range(n):
if x[i]=='W':
a[i]=1
nexxt=a[0]
ans=[]
for i in range(n-1):
if nexxt!=1:
nexxt=a[i+1]^1
ans.append(i+1)
else:
nexxt=a[i+1]
if nexxt==1:
PRINT("%d\n"%(len(ans)))
for v in ans:
PRINT("%d "%(v))
else:
nexxt=a[0]
ans=[]
for i in range(n-1):
if nexxt!=0:
nexxt=a[i+1]^1
ans.append(i+1)
else:
nexxt=a[i+1]
if nexxt==0:
PRINT("%d\n"%(len(ans)))
for v in ans:
PRINT("%d "%(v))
else:
PRINT("%d"%(-1))
Main()
'mysql -u root -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 | n=int(input())
s=input()
arr=[]
for i in s:
if i=='B':
arr.append(1)
else:
arr.append(0)
b=sum(arr)
w=n-b
if w*b == 0:
print(0)
elif w%2 and b%2:
print(-1)
else:
if b%2:
target=0
else:
target=1
ans=[]
for i in range(n-1):
if arr[i]==target:
ans.append(i+1)
arr[i+1]=1-arr[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())
s = input()
b = w = 0
for c in s:
if c == 'B':
b += 1
else:
w += 1
if w % 2 == 1 and b % 2 == 1:
print(-1)
else:
ans = list()
s = list(s)
if w % 2 == 1:
for i in range(len(s)-1):
if s[i] != 'W':
ans.append(i + 1)
s[i] = 'W'
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
else:
for i in range(len(s)-1):
if s[i] != 'B':
ans.append(i + 1)
s[i] = 'B'
s[i+1] = 'W' if s[i+1] == 'B' else 'B'
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() {
int n;
cin >> n;
string s, t;
cin >> s;
t = s;
vector<int> ans, an;
for (int i = int(0); i <= int(n - 2); i++) {
if (s[i] == 'W') continue;
s[i] = 'W';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
ans.push_back(i + 1);
}
if (s[n - 1] != 'B') {
cout << ans.size() << "\n";
for (auto it : ans) cout << it << " ";
return 0;
}
for (int i = int(0); i <= int(n - 2); i++) {
if (t[i] == 'B') continue;
t[i] = 'B';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
an.push_back(i + 1);
}
if (t[n - 1] != 'W') {
cout << an.size() << "\n";
for (auto it : an) cout << it << " ";
return 0;
}
cout << -1;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
string s;
int main() {
ios::sync_with_stdio(false);
long long n, num = 0;
cin >> n >> s;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == 'B') num++;
}
if (n % 2 == 0 && num % 2 == 1) {
cout << -1;
return 0;
}
if (n % 2 == 0) {
vector<long long> v;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1] && s[i] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
v.push_back(i + 1);
} else if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
v.push_back(i + 1);
}
}
cout << v.size() << endl;
for (long long i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
} else {
if (num % 2 == 0) {
vector<long long> v;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1] && s[i] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
v.push_back(i + 1);
} else if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
v.push_back(i + 1);
}
}
cout << v.size() << endl;
for (long long i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
} else {
vector<long long> v;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1] && s[i] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
v.push_back(i + 1);
} else if (s[i] == 'W' && s[i + 1] == 'B') {
s[i] = 'B';
s[i + 1] = 'W';
v.push_back(i + 1);
}
}
cout << v.size() << endl;
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 | #include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long mod = (1e9) + 7;
void solve() {
int n, i, a = 0, b = 0;
string s;
vector<int> v;
cin >> n >> s;
for (i = 0; i < n; i++) {
if (s[i] == 'W')
a++;
else
b++;
}
if (a % 2 == 1 && b % 2 == 1) {
cout << "-1";
return;
}
if (a >= b) {
a = 'W';
b = 'B';
} else {
a = 'B';
b = 'W';
}
for (i = 0; i + 1 < n; i++) {
if (s[i] == b) {
s[i] = a;
v.push_back(i + 1);
if (s[i + 1] == b)
s[i + 1] = a;
else
s[i + 1] = b;
}
}
if (s[n - 1] != s[n - 2]) {
for (i = 0; i + 2 < n; i += 2) {
if (s[i] == s[i + 1]) {
v.push_back(i + 1);
(s[i] == 'W') ? (s[i] = s[i + 1] = 'B') : (s[i] = s[i + 1] = 'W');
}
}
}
cout << v.size() << "\n";
if (v.size() > 0)
for (auto x : v) cout << x << " ";
}
int 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 | from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline
def main():
n = int(input())
s = list(input())
b, w = 0, 0
for i in range(n):
if s[i] == "B":
b += 1
else:
w += 1
if b % 2 == 1 and w % 2 == 1:
print(-1)
elif b == 0 or w == 0:
print(0)
else:
ans = 0
ansl = []
ss = s
for i in range(n-1):
if ss[i] == "B":
ansl.append(i+1)
ans+=1
ss[i] = "W"
if ss[i + 1] == "B":
ss[i + 1] = "W"
else:
ss[i + 1] = "B"
#print(ss)
if ss.count("W") != n:
for i in range(n-1):
if ss[i] == "W":
ans += 1
ansl.append(i+1)
ss[i] = "B"
if ss[i + 1] == "B":
ss[i + 1] = "W"
else:
ss[i + 1] = "B"
print(ans)
print(*ansl)
# zans = 0
# zansl = []
# ss = s
# for i in range(n-1):
# if ss[i] == "W":
# zansl.append(i+1)
# zans += 1
# ss[i] = "B"
# if ss[i + 1] == "B":
# ss[i + 1] = "W"
# else:
# ss[i + 1] = "B"
# #print(ss)
# if ss.count("B") != n:
# for i in range(n-1):
# if ss[i] == "B":
# zans += 1
# zansl.append(i+1)
# ss[i] = "W"
# if ss[i + 1] == "B":
# ss[i + 1] = "W"
# else:
# ss[i + 1] = "B"
if __name__ == '__main__':
main()
# t = int(input())
# for _ in range(t):
# ss, tt = input().split()
# ss = list(ss)
# tt = list(tt)
# l = []
# bo = -1
# if len(ss) > len(tt):
# l = ["0"] * (len(ss) - len(tt))
# bo = 1
# elif len(ss) < len(tt):
# l = ["0"] * (len(tt) - len(ss))
# bo = 2
# lss = []
# ltt = []
# if bo == 1:
# lss = ss
# ltt = tt + l
# elif bo == 2:
# lss = ss + l
# ltt = tt
# else:
# lss = ss
# ltt = tt
# f = False
# cnt = 0
# for i in range(max(len(tt),len(ss))):
# if lss[i] == ltt[i]:
# cnt += 1
# elif lss[i] < ltt[i] and cnt == i:
# f = True
# if f:
# print("".join(map(str, ss)))
# else:
# c = ""
# for i in range(min(len(tt), len(ss))):
# if ss[i] > tt[i]:
# for j in range(len(ss)-1,i,-1):
# if ss[j] < tt[i]:
# c = ss[j]
# break
# if c != "":
# ss[i], ss[j] = ss[j], ss[i]
# if ss < tt:
# print("".join(map(str, ss)))
# else:
# print("---")
# break
# if c == "":
# print("---")
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.ArrayList;
import java.util.Scanner;
public class Blocks {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String str = s.next();
char[] a = str.toCharArray();
ArrayList<Integer> index = new ArrayList<>();
// try white first
for(int i = 0; i < a.length - 1; i++) {
if(a[i] == 'B') {
a[i] = 'W';
if(a[i + 1] == 'W') a[i + 1] = 'B';
else if(a[i + 1] == 'B') a[i + 1] = 'W';
index.add(i);
}
}
if(a[a.length - 2] == 'W' && a[a.length - 1] == 'W') {
System.out.println(index.size());
for(int i : index) System.out.print((i + 1) + " ");
return;
}
a = str.toCharArray();
index.clear();
// try black next
for(int i = 0; i < a.length - 1; i++) {
if(a[i] == 'W') {
a[i] = 'B';
if(a[i + 1] == 'W') a[i + 1] = 'B';
else if(a[i + 1] == 'B') a[i + 1] = 'W';
index.add(i);
}
}
if(a[a.length - 2] == 'B' && a[a.length - 1] == 'B') {
System.out.println(index.size());
for(int i : index) System.out.print((i + 1) + " ");
}
else System.out.println(-1);
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | a=int(input())
b=input()
oper=[]
operation=0
blackwhite=[0,0]
for i in b:
if i=='B':
blackwhite[0]+=1
else:
blackwhite[1]+=1
if (a-blackwhite[0])%2==0:
glavcvet='B'
zapcvet='W'
elif (a-blackwhite[1])%2==0:
glavcvet='W'
zapcvet='B'
else:
glavcvet='Z'
print(-1)
if glavcvet!='Z':
for schet,i in enumerate(b):
if ((i==zapcvet)&(operation==1))|((i==glavcvet)&(operation==0)):
operation=0
else:
operation=1
oper.append(str(schet+1))
print(len(oper))
print(' '.join(oper))
| 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.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String str = s.next();
int b = 0,w = 0;
ArrayList<Integer> black = new ArrayList<>();
ArrayList<Integer> white = new ArrayList<>();
for(int i = 0; i < n; i++){
if(str.charAt(i) == 'B'){
b++;
black.add(i + 1);
}
else{
w++;
white.add(i + 1);
}
}
if(b % 2 == 1 && w % 2 == 1){
System.out.println(-1);
return;
}
ArrayList<Integer> ans = new ArrayList<>();
if(b % 2 == 1){
for(int i = 1; i < white.size(); i = i + 2){
for(int start = white.get(i) - 1; start >= white.get(i - 1); start--){
ans.add(start);
}
}
}
else{
for(int i = 1; i < black.size(); i = i + 2){
for(int start = black.get(i) - 1; start >= black.get(i - 1); start--){
ans.add(start);
}
}
}
System.out.println(ans.size());
for(int num : ans){
System.out.print(num + " ");
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.*;
import java.lang.*;
public class Codechef {
static long mod = 1000000007;
PrintWriter out;
StringTokenizer st;
BufferedReader br;
class Pair
{
int f;
int s;
public Pair(int t, int r) {
f = t;
s = r;
}
}
String ns() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
int nextInt() {
return Integer.parseInt(ns());
}
long nextLong() {
return Long.parseLong(ns());
}
double nextDouble() {
return Double.parseDouble(ns());
}
public static void main(String args[]) throws IOException {
new Codechef().run();
}
void run() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
out.close();
}
long power(long x,long y)
{
long ans=1;
while(y!=0)
{
if(y%2==1)
ans*=x;
x=x*x;
y/=2;
}
return ans;
}
void solve()
{
int n =nextInt();
String str = ns();
char a[]=str.toCharArray();
int b=0,w=0;
for(int i=0;i<n;i++)
{
if(a[i]=='B')
b++;
else
w++;
}
ArrayList<Integer> list= new ArrayList<>();
int count=0;
if(n==2&&w%2==1)
out.println(-1);
else if(n%2==0&&b%2!=0)
out.println(-1);
else
{
char c=a[0];
for(int i=1;i<n-1;i++)
{
if(a[i]==c)
continue;
else
{
a[i]=c;
if(a[i+1]=='W')
a[i+1]='B';
else
a[i+1]='W';
count++;
list.add(i+1);
}
}
if(a[n-1]!=c)
{
for(int i=0;i<n-1;i+=2)
{
list.add(i+1);
count++;
}
}
out.println(count);
for(int i=0;i<list.size();i++)
out.print(list.get(i)+" ");
//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 | def turn(blocks, frc, toc):
res=[]
for i in range(len(blocks) - 1):
if blocks[i] == frc:
res.append(i + 1)
blocks[i] = toc
if blocks[i+1] == frc:
blocks[i + 1] = toc
else:
blocks[i + 1] = frc
print(len(res))
print(*res)
n = int(input())
blocks = input()
wc = 0
bc = 0
for b in blocks:
if b=='W':
wc+=1
else:
bc+=1
if wc % 2 != 0 and bc % 2 != 0:
print(-1)
elif wc % 2 == 0 and bc % 2 != 0:
turn(list(blocks), 'W', 'B')
elif wc % 2 != 0 and bc % 2 == 0:
turn(list(blocks),'B', 'W')
else:
if wc <= bc:
if wc > 0:
turn(list(blocks),'W', 'B')
else:
print(0)
else:
if bc > 0:
turn(list(blocks),'B', 'W')
else:
print(0)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class Blocks {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int N = Integer.parseInt(in.readLine());
boolean[] blocks = new boolean[N];
String blockString = in.readLine();
for(int i = 0; i < N; ++i) {
blocks[i] = blockString.charAt(i) == 'B';
}
List<Integer> swaps = new ArrayList<Integer>();
for(int i = 1; i < N - 1; ++i) {
if(blocks[i] ^ blocks[i - 1]) {
blocks[i] = !blocks[i];
blocks[i + 1] = !blocks[i + 1];
swaps.add(i + 1);
}
}
if(blocks[N - 1] ^ blocks[N - 2] && N % 2 == 0) {
out.println(-1);
} else {
if(blocks[N - 1] ^ blocks[N - 2]) {
for(int i = 0; i < N - 1; i += 2) {
swaps.add(i + 1);
}
}
out.println(swaps.size());
if(swaps.size() > 0) {
out.print(swaps.get(0));
for(int i = 1; i < swaps.size(); ++i) {
out.print(" " + swaps.get(i));
}
}
}
out.close();
in.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 | // No sorceries shall previal. //
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.*;
import java.util.Arrays;
public class _InVoker_ {
public static void sort(int arr[], int start, int end) {
if(start>=end)
return;
int mid=(start+end)/2;
sort(arr,start,mid);
sort(arr,mid+1,end);
merge(arr,start,mid,end);
}
private static void merge(int arr[], int start, int mid, int end) {
int i, j=mid+1,c=0;
int temp[]= new int[end-start+1];
for(i=start;i<=mid && j<=end;c++) {
if(arr[i]<=arr[j]) {
temp[c]=arr[i];
i++;
}
else {
temp[c]=arr[j];
j++;
}
}
while(i<=mid) {
temp[c]=arr[i];
i++;c++;
}
while(j<=end) {
temp[c]=arr[j];
j++;c++;
}
c=0;
for(i=start;i<=end;i++,c++)
arr[i]=temp[c];
}
static class ListNode {
ListNode(){
data=0;
next=null;
}
ListNode(int data){
this.data=data;
}
ListNode(char a){
this.a=a;
}
private char a;
private int data;
private ListNode next;
public char getCharData() {
return a;
}
public void setCharData(char x) {
a=x;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String args[]) {
Scanner inp=new Scanner(System.in);
PrintWriter out= new PrintWriter(System.out);
int n=inp.nextInt();
char s[]=inp.next().toCharArray();
int gg=0;
int b=0,w=0;
for(int i=0;i<n;i++) {
if(s[i]=='B')
b++;
else w++;
}
if(n%2==0 && w%2==1)
out.println(-1);
else {
for(int i=1;i<n-1;i++) {
if(s[i]=='B' && s[i-1]!='B') {
s[i]='W';
if(s[i+1]=='W')
s[i+1]='B';
else
s[i+1]='W';
out.print((i+1)+" ");
gg++;
}
else if(s[i]=='W' && s[i-1]!='W') {
s[i]='B';
if(s[i+1]=='W')
s[i+1]='B';
else
s[i+1]='W';
out.print((i+1)+" ");
gg++;
}
}
for(int i=n-2;i>0;i--) {
if(s[i]=='B' && s[i+1]!='B') {
s[i]='W';
if(s[i-1]=='W')
s[i-1]='B';
else
s[i-1]='W';
out.print((i)+" ");
gg++;
}
else if(s[i]=='W' && s[i+1]!='W') {
s[i]='B';
if(s[i-1]=='W')
s[i-1]='B';
else
s[i-1]='W';
out.print((i)+" ");
gg++;
}
}
System.out.println(gg);
}
inp.close();
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 |
import java.math.*;
import java.util.*;
public class BruteForce {
public static Scanner in = new Scanner(System.in);
public static boolean ok(char[] z) {
boolean ans = true;
StringBuilder build = new StringBuilder();
for (char c : z) {
build.append(c);
}
if (build.toString().contains("W") && build.toString().contains("B")) {
ans = false;
}
return ans;
}
public static void main(String[] args) {
int n = in.nextInt();
char[] b = in.next().toCharArray();
char[] w = new char[b.length];
for (int i = 0; i < n; i++) {
w[i] = b[i];
}
if (ok(b)) {
System.out.println("0");
} else {
ArrayList<Integer> sol1 = null;
ArrayList<Integer> sol2 = null;
{
//make white
sol1 = new ArrayList<Integer>();
int count = 0;
while (!ok(w) && count <= (3 * n)) {
count++;
for (int i = 0; i < w.length - 1; i++) {
if (w[i] == 'B') {
w[i] = 'W';
sol1.add((i + 1));
if (w[i + 1] == 'B') {
w[i + 1] = 'W';
} else {
w[i + 1] = 'B';
}
}
}
}
}
{
//make black
sol2 = new ArrayList<Integer>();
int count = 0;
while (!ok(b) && count <= (3 * n)) {
count++;
for (int i = 0; i < b.length - 1; i++) {
if (b[i] == 'W') {
b[i] = 'B';
sol2.add((i + 1));
if (b[i + 1] == 'B') {
b[i + 1] = 'W';
} else {
b[i + 1] = 'B';
}
}
}
}
}
if (ok(w) && sol1.size() <= (3 * n)) {
System.out.println(sol1.size());
for (int val : sol1) {
System.out.print(val + " ");
}
System.out.println("");
} else if (ok(b) && sol2.size() <= (3 * n)) {
System.out.println(sol2.size());
for (int val : sol2) {
System.out.print(val + " ");
}
System.out.println("");
} else {
System.out.println("-1");
}
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, e, f;
int n;
string s;
void kaisa() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cin >> s;
s = " " + s;
int dem1 = 0, dem2 = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == 'B')
dem1++;
else
dem2++;
}
if (dem1 == 0 || dem2 == 0) {
cout << 0;
return;
}
if (dem1 % 2 == 0) {
vector<int> res;
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';
res.push_back(i);
}
}
cout << res.size() << "\n";
for (int i : res) {
cout << i << " ";
}
return;
}
if (dem2 % 2 == 0) {
vector<int> res;
for (int i = 1; i < n; i++) {
if (s[i] == 'W') {
s[i] = 'B';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
res.push_back(i);
}
}
cout << res.size() << "\n";
for (int i : res) {
cout << i << " ";
}
return;
} else {
cout << -1;
}
}
int main() {
kaisa();
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 Main {
public static PrintStream out = new PrintStream(System.out);;
public static void main(String[] args){
FastScanner sc = new FastScanner();
int countW = 0; int countB = 0;
int n = sc.nextInt();
String s = sc.nextLine();
char[] arr = s.toCharArray();
int l = arr.length;
//try white
boolean whiteok = false;
ArrayList<Integer> v = new ArrayList();
boolean blackok = false;
ArrayList<Integer> v2 = new ArrayList();
for(int i = 0; i < l; i++){
if(i == l - 1){
if(arr[i] == 'W') whiteok = true;
} else{
if(arr[i] != 'W'){
arr[i] = 'W';
if(arr[i + 1] == 'B') arr[i + 1] = 'W';
else arr[i + 1] = 'B';
v.add(i + 1);
}
}
}
if(whiteok){
out.println(v.size());
for(int i : v)
out.print(i + " ");
out.println();
}
else{
//try black
arr = s.toCharArray();
for(int i = 0; i < l; i++){
if(i == l - 1){
if(arr[i] == 'B') blackok = true;
} else{
if(arr[i] != 'B'){
arr[i] = 'B';
if(arr[i + 1] == 'B') arr[i + 1] = 'W';
else arr[i + 1] = 'B';
v2.add(i + 1);
}
}
}
if(blackok){
out.println(v2.size());
for(int i : v2)
out.print(i + " ");
out.println();
}
else out.println("-1");
}
}
private static int factorial(int n){
int fac = 1;
for(int i=2;i<=n;i++){
fac *= i;
}
return fac;
}
private static boolean isPrime(int n){
if(n == 0 || n == 1){
return false;
}else if(n%2 == 0){
return false;
}else{
int x = (int)(Math.sqrt(n)) + 1;
for(int i=3;i<=x;i+=2){
if(n % i == 0){
return false;
}
}
return true;
}
}
private static long pow(int base,int exp){
if(exp == 0){
return 1;
}else if(exp == 1){
return base;
}else{
long a = pow(base,exp/2);
if(exp%2 == 0){
return a * a;
}else{
return a * a * base;
}
}
}
private static int gcd(int a,int b){
if(a == 0){
return b;
}
return gcd(b%a,a);
}
private static int lcm(int a,int b){
return (a * b)/gcd(a,b);
}
private static ArrayList<Integer> SieveOfEratosthenes(int n){
boolean b[] = new boolean[n+1];
b[0] = true;
b[1] = true;
for(int i=2;i<=5;i++){
int j = 2;
while(i * j <= n && !b[i]){
b[i * j] = true;
j++;
}
}
ArrayList<Integer> arr = new ArrayList<>();
for(int i=2;i<n+1;i++){
if(!b[i]){
arr.add(i);
}
}
return arr;
}
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
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;
}
}
//class Node
//{
// public int V;
// public Node(int v)
// {
// this.V=v;
// }
//}
//class Node implements Comparable<Node>
//{
// public int V,E;
// public Node(int v, int e)
// {
// this.V=v;
// this.E=e;
// }
// public int compareTo( Node N ){
// return E - N.E;
// }
//}
// class Graph{
// int V;
// LinkedList<Integer>[] adjListArray;
//
// Graph(int V) {
// this.V = V;
// // define the size of array as
// // number of vertices
// adjListArray = new LinkedList[V + 5];
//
// // Create a new list for each vertex
// // such that adjacent nodes can be stored
//
// for(int i = 0; i < V + 5; i++){
// adjListArray[i] = new LinkedList<Integer>();
// }
// }
//
// void addEdge( int src, int dest) {
// adjListArray[src].add(dest);
// adjListArray[dest].add(src);
// }
//
// void DFSUtil(int v, boolean[] visited) {
// // Mark the current node as visited and print it
// visited[v] = true;
// q.add(v);
// //System.out.print(v+" ");
// // Recur for all the vertices
// // adjacent to this vertex
// for (int x : adjListArray[v]) {
// if(!visited[x]) DFSUtil(x,visited);
// }
//
// }
//
// void connectedComponents(){
// boolean[] visited = new boolean[V + 5];
// boolean found = true;
// for(int v = 1; v <= V; ++v) {
// if(!visited[v]) {
// // print all reachable vertices
// // from v
// q = new LinkedList();
// DFSUtil(v,visited);
// found = true;
// if(q.size() < 3){
// found = false;
// continue;
// }
// for(int i = 0; i < q.size(); i++){
// if(adjListArray[q.get(i)].size() != q.size() - 1){
// found = false;
// System.out.println("yo");
// System.out.println("i: " + i + " size: " + adjListArray[i].size());
// break;
// }
// }
// if(found){
// ok = true;
// break;
// }
// //System.out.println();
// //System.out.println("size: " + q.size());
// }
// }
//
// }
// }
// N = sc.nextInt();
// M = sc.nextInt();
// adj=new LinkedList[N + 5];
// for(int i = 0; i <= N + 4; i++)
// adj[i] = new LinkedList();
//
// for(int i = 0; i < M; i++){
// int u = sc.nextInt(); int v = sc.nextInt(); int e = sc.nextInt();
// adj[u].add(new Node(v, e)); adj[v].add(new Node(u, e));
// }
// vis = new int[N + 5];
// for(int i = 0; i < N + 5; i++)
// vis[i] = Integer.MAX_VALUE;
// vis[1] = 0;
// pq.offer(new Node(1, 0));
// ArrayList<Integer> path = new ArrayList();
// path.add(1);
// while(pq.size() > 0){
// Node n = pq.poll();
// Iterator<Node> i = adj[n.V].listIterator();
// while(i.hasNext())
// {
// Node v=i.next();
// if(n.E+v.E<vis[v.V])
// {
// vis[v.V]=n.E+v.E;
// pq.offer(new Node(v.V,vis[v.V]));
// path.add(v.V);
// }
// }
//
// }
// path.add(N);
// for(int i = 0; i < path.size(); i++)
// System.out.print(path.get(i) + " ");
// System.out.println(); | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = input()
a = raw_input()
if a.count('B')%2!=0 and a.count('W')%2!=0:
print -1
exit(0)
if a.count('B')%2==0:
l = []
s = 0
ans = []
for i in range(n):
if l==[]:
l.append(a[i])
s+=1
continue
if l[-1]=='W':
l.append(a[i])
s+=1
continue
if l[-1]=='B' and a[i]=='B':
l.pop()
l.append('W')
l.append('W')
ans.append(s)
s+=1
elif l[-1]=='B' and a[i] == 'W':
l.pop()
l.append('W')
l.append('B')
ans.append(s)
s+=1
print len(ans)
if len(ans)!=0:
for x in ans:
print x,
print
else:
l = []
s = 0
ans = []
for i in range(n):
if l==[]:
l.append(a[i])
s+=1
continue
if l[-1]=='B':
l.append(a[i])
s+=1
continue
if l[-1]=='W' and a[i]=='W':
l.pop()
l.append('B')
l.append('B')
ans.append(s)
s+=1
elif l[-1]=='W' and a[i] == 'B':
l.pop()
l.append('B')
l.append('W')
ans.append(s)
s+=1
print len(ans)
if len(ans)!=0:
for x in ans:
print x,
print
| 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;
cin >> n;
string s;
cin >> s;
for (int i = 0; i <= 1; i++) {
char x = "WB"[i];
string t = s;
vector<int> ve;
for (int j = 0; j + 1 < n; j++) {
if (t[j] != x) {
t[j] ^= 'w' ^ 'B';
t[j + 1] ^= 'W' ^ 'B';
ve.push_back(j + 1);
}
}
if (t.back() == x) {
cout << ve.size() << endl;
for (auto v : ve) {
cout << v << " ";
}
cout << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 ⋅ n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 ≤ n ≤ 200) — the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 ≤ k ≤ 3 ⋅ n) — the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≤ p_j ≤ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | /*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public final class GFG {
public static void main (String[] args) {
Scanner in =new Scanner(System.in);
int n=in.nextInt();in.nextLine();
String input=in.nextLine();
char[] line=input.toCharArray();
int ans=0; int[] op=new int[(4*n)];
int top=-1,tmp=0;
boolean found=true;
//to black
while(tmp<4&&found){
found=false;
for(int i=0;i<n-1;i++){
if(line[i]=='W'){
found=true;
line[i]='B';
if(line[i+1]=='W'){
line[i+1]='B';
}
else{
line[i+1]='W';
}
ans++;
top++;
op[top]=i+1;
}
}
if(line[n-1]=='W'){
found=true;
}
tmp++;
}
if(tmp<4&&!found){
System.out.println(ans);
if(ans==0){
return;
}
String s="";
for(int i=0;i<=top;i++){
if(i==0)
s+=""+op[i];
else
s+=" "+op[i];
}
System.out.println(s);
return;
}
line=input.toCharArray();
//to white
tmp=0;found=true;
ans=0;top=-1;
while(tmp<4&&found){
found=false;
for(int i=0;i<n-1;i++){
if(line[i]=='B'){
found=true;
line[i]='W';
if(line[i+1]=='W'){
line[i+1]='B';
}
else{
line[i+1]='W';
}
ans++;
top++;
op[top]=i+1;
}
}
if(line[n-1]=='B'){
found=true;
}
tmp++;
}
if(tmp<4&&!found){
System.out.println(ans);
if(ans==0){
return;
}
String s="";
for(int i=0;i<=top;i++){
if(i==0)
s+=""+op[i];
else
s+=" "+op[i];
}
System.out.println(s);
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 | #include <bits/stdc++.h>
using namespace std;
int n, a[205], cnt, b, w, t[205];
string x;
int main() {
cin >> n;
cin >> x;
for (int i = 0; i < x.size(); i++) {
if (x[i] == 'B')
t[i] = 0, b++;
else
t[i] = 1, w++;
}
if (b % 2 & w % 2) {
cout << -1;
return 0;
}
b = b % 2 ? 0 : 1;
for (int i = 0; i < n - 1; i++) {
if (t[i] ^ b) {
t[i] ^= 1;
t[i + 1] ^= 1;
a[cnt++] = i + 1;
}
}
cout << cnt << endl;
for (int i = 0; i < cnt; i++) cout << a[i] << ' ';
return 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;
char s[300];
int a[300], n;
vector<int> ans;
bool check1() {
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] == 1) cnt++;
return cnt == n;
}
bool check2() {
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] == 0) cnt++;
return cnt == n;
}
void work(int op) {
int cnt = 0;
while (1) {
if (op) {
if (check1()) break;
} else {
if (check2()) break;
}
for (int i = 1; i <= n; i++) {
if (a[i] != op) {
if (a[i + 1] != a[i])
swap(a[i], a[i + 1]);
else
a[i] = a[i + 1] = op;
ans.push_back(i);
break;
}
}
cnt++;
}
cout << cnt << endl;
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cin >> (s + 1);
for (int i = 1; i <= n; i++) a[i] = (s[i] == 'B') ? 1 : 0;
int cnt1 = 0, cnt0 = 0;
for (int i = 1; i <= n; i++) {
if (a[i])
cnt1++;
else
cnt0++;
}
if (cnt1 == 0 || cnt0 == 0) {
cout << 0 << endl;
} else if (n % 2 == 0 && (cnt1 & 1)) {
cout << -1 << endl;
} else {
if (cnt1 % 2 == 0)
work(0);
else
work(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 | /* package codechef; // don't place package name! */
import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String s= sc.next();
int arr[]=new int[n];
int w=0,b=0;
for(int i=0;i<n;i++){
if(s.charAt(i)=='B'){
arr[i]=0;
b++;
}
else{
arr[i]= 1;
w++;
}
}
if(w%2!=0&&b%2!=0){
System.out.println("-1");
return;
}
int color=0;
if(w%2!=0){
color=1;
}
if(w%2==0&&b%2==0){
if(arr[0]==1){
color=1;
}
}
int steps=0;
ArrayList<Integer> ans= new ArrayList<Integer>();
{
for(int i=0;i<n;i++){
if(arr[i]==color){
}
else{
arr[i]=Math.abs(color-1);
arr[i+1]=Math.abs(arr[i+1]-1);
steps++;
ans.add(i+1);
}
}
}
System.out.println(steps);
for(int i=0;i<ans.size();i++){
System.out.print(ans.get(i)+" ");
}
}
// your code goes here
}
| JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.