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())
B,W = 0,0
s = input()
ss = []
for i in range(0,len(s)):
ss.append(s[i])
if(s[i] == 'B'):
B = B + 1
else:
W = W + 1
# print(B,W)
if(W % 2 == 1 and B % 2 == 1):
print(-1)
else:
a = []
flag = 0
# print(len(s))
if(B % 2 == 0):
flag = 1
elif(W % 2 == 0):
flag = 2
for i in range(0,len(ss)-1):
if(ss[i] == 'B'):
if(flag == 1):
ss[i] = 'W'
if(ss[i+1] == 'B'):
ss[i+1] = 'W'
else:
ss[i+1] = 'B'
a.append(i)
elif(ss[i] == 'W'):
if(flag == 2):
ss[i] = 'B'
if(ss[i+1] == 'B'):
ss[i+1] = 'W'
else:
ss[i+1] = 'B'
a.append(i)
# print(ss)
print(len(a))
for i in range(0,len(a)):
print(a[i] + 1,end = ' ')
print() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (b % 2 == 1 && w % 2 == 1) {
cout << "-1";
return 0;
}
vector<int> v;
if (b % 2 == 1) {
int i = 0;
while (i < n - 1) {
if (s[i] == 'B' && s[i + 1] == 'B')
i += 2;
else if (s[i] == 'W' && s[i + 1] == 'W') {
v.push_back(i + 1);
i = i + 2;
} else if (s[i] == 'B' && s[i + 1] == 'W') {
i = i + 1;
} else {
v.push_back(i + 1);
s[i + 1] = 'W';
i = i + 1;
}
}
cout << v.size() << " ";
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
int i = 0;
while (i < n - 1) {
if (s[i] == 'W' && s[i + 1] == 'W')
i += 2;
else if (s[i] == 'B' && s[i + 1] == 'B') {
v.push_back(i + 1);
i = i + 2;
} else if (s[i] == 'W' && s[i + 1] == 'B') {
i = i + 1;
} else {
v.push_back(i + 1);
s[i + 1] = 'B';
i = i + 1;
}
}
cout << v.size() << " ";
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | # code by RAJ BHAVSAR
n = int(input())
s = list(str(input()))
temp = s[:]
ans = []
for i in range(n-1):
if(s[i] == 'B'):
ans.append(i+1)
s[i] = 'W'
s[i+1] = 'W' if s[i+1] == 'B' else 'B'
if(len(set(s)) == 1):
print(len(ans))
print(*ans)
else:
ans = []
for i in range(n-1):
if(temp[i] == 'W'):
ans.append(i+1)
temp[i] = 'B'
temp[i+1] = 'W' if temp[i+1] == 'B' else 'B'
if(len(set(temp)) == 1):
print(len(ans))
print(*ans)
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void resolve() {
int n;
string s;
cin >> n >> s;
int w = 0, b = 0;
for (int i = 0; i < s.size(); i++) w += (s[i] == 'W'), b += (s[i] == 'B');
if (w == 0 || b == 0)
cout << 0;
else if (w % 2 == 1 && b % 2 == 1)
cout << -1;
else {
vector<int> ans;
char c;
if (w % 2 == 0)
c = 'W';
else
c = 'B';
for (int i = 0; i < s.size() - 1; i++)
if (s[i] == c) {
ans.push_back(i + 1);
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
}
cout << ans.size() << '\n';
for (int e : ans) cout << e << ' ';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
for (int i = 0; i < t; i++) {
resolve();
cout << '\n';
}
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.*;
public class blocks {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
String blockString = in.readLine();
char[] blocks = blockString.toCharArray();
List<Integer> swaps = new ArrayList<Integer>();
for (int i = 1; i < n - 1; ++i) {
if ((blocks[i] == 'B' && blocks[i - 1] == 'W') || (blocks[i] == 'W' && blocks[i - 1] == 'B')) {
if (blocks[i] == 'W')
blocks[i] = 'B';
else
blocks[i] = 'W';
if (blocks[i + 1] == 'W')
blocks[i + 1] = 'B';
else
blocks[i + 1] = 'W';
swaps.add(i + 1);
}
}
if (((blocks[n - 1] == 'W' && blocks[n - 2] == 'B') || (blocks[n - 1] == 'B' && blocks[n - 2] == 'W')) && (n % 2 == 0)) {
System.out.println(-1);
}
else{
if ((blocks[n - 1] == 'W' && blocks[n - 2] == 'B') || (blocks[n - 1] == 'B' && blocks[n - 2] == 'W')) {
for (int i = 0; i < n-1; i += 2)
swaps.add(i + 1);
}
System.out.println(swaps.size());
if (swaps.size() > 0) {
for (int i = 0; i < swaps.size(); i++)
System.out.print(swaps.get(i) + " ");
}
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def affichage(L):
for i in range(len(L)):
L[i]=str(L[i])
return(' '.join(L))
def executeB(L,n):
L2=[]
for i in range(n):
if L[i]!='B':
L[i]='W'
if L[i+1]=='B':
L[i+1]='W'
else:
L[i+1]='B'
L2.append(i+1)
print(len(L2))
if len(L2)>0:
print(affichage(L2))
def executeW(L,n):
L2=[]
for i in range(n):
if L[i]!='W':
L[i]='B'
if L[i+1]=='W':
L[i+1]='B'
else:
L[i+1]='W'
L2.append(i+1)
print(len(L2))
if len(L2)>0:
print(affichage(L2))
n=int(input())
ch=input()
L=[0]*n
for i in range(n):
L[i]=ch[i]
nbB=0
nbW=0
for i in L:
if i=='B':
nbB+=1
else:
nbW+=1
if (nbB%2==1)and(nbW%2==1):
print(-1)
elif (nbB%2==1) and (nbW%2==0):
executeB(L,n)
elif (nbB%2==0) and (nbW%2==1):
executeW(L,n)
else:
L1=ch.split('B')
L2=ch.split('W')
nb1=0
nb2=0
for i in (L1):
if (len(i)>0)and(len(i)%2==0):
nb1+=1
for i in (L2):
if (len(i)>0)and(len(i)%2==0):
nb2+=1
if nb1>nb2:
executeB(L,n)
else:
executeW(L,n)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> v;
int b = count(s.begin(), s.end(), 'B');
int w = count(s.begin(), s.end(), 'W');
if (b % 2 == 1 && w % 2 == 1) {
cout << "-1";
return 0;
}
if (b % 2 == 0) {
for (int i = 0; i < (n - 1); i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
v.push_back(i + 1);
s[i] = 'W';
s[i + 1] = 'B';
} else if (s[i] == 'B' && s[i + 1] == 'B') {
v.push_back(i + 1);
s[i] = 'W';
s[i + 1] = 'W';
}
}
} else if (w % 2 == 0) {
for (int i = 0; i < n; i++) {
if (s[i] == 'W' && s[i + 1] == 'B') {
v.push_back(i + 1);
s[i] = 'B';
s[i + 1] = 'W';
} else if (s[i] == 'W' && s[i + 1] == 'W') {
v.push_back(i + 1);
s[i] = 'B';
s[i + 1] = 'B';
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict
from math import factorial as f
from fractions import gcd as g
n = int (raw_input ())
s = raw_input ()
l = []
w, b = 0, 0
result = []
for i in s:
if i == "B":
l.append(0)
b += 1
else:
l.append(1)
w += 1
if w % 2 == 1 and b % 2 == 1:
print -1
else:
for i in range(n - 1):
if l[i] == 0 and w % 2 == 1:
result.append(i + 1)
l[i] = 1 - l[i]
l[i + 1] = 1 - l[i + 1]
elif l[i] == 1 and w % 2 == 0:
result.append(i + 1)
l[i] = 1 - l[i]
l[i + 1] = 1 - l[i + 1]
print len(result)
print " ".join(str(i) for i in result)
| PYTHON |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
import java.lang.management.*;
import static java.lang.Math.*;
@SuppressWarnings("unchecked")
public class P1271B {
int n, k;
StringBuilder ans;
void change(char [] s, char c) {
k = 0;
ans = new StringBuilder();
for (int i = n - 1; i > 0; i--) {
if (s[i] != c) {
s[i + 0] = (s[i + 0] == 'B') ? 'W' : 'B';
s[i - 1] = (s[i - 1] == 'B') ? 'W' : 'B';
ans.append(i).append(' ');
k++;
}
}
ans = (s[0] == s[1]) ? ans : null;
}
public void run() throws Exception {
n = nextInt();
char [] s = next().toCharArray();
change(Arrays.copyOf(s, n), 'B');
if (ans != null) {
println(k + "\n" + ans);
} else {
change(s, 'W');
println((ans != null) ? (k + "\n" + ans) : -1);
}
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P1271B().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
long gct = 0, gcc = 0;
for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
gct += garbageCollectorMXBean.getCollectionTime();
gcc += garbageCollectorMXBean.getCollectionCount();
}
System.err.println("[GC time : " + gct + " ms, count = " + gcc + "]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { print("" + o); }
}
void printsp(int [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); }
void printsp(long [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); }
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print(o); println(); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
int gcd(int a, int b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Long.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
boolean isPrime(int v) {
if (v <= 1) { return false; }
if (v <= 3) { return true; }
if (((v & 1) == 0) || ((v % 3) == 0)) { return false; }
for (int m = 5, n = (int)sqrt(v); m <= n; m += 6) { if (((v % m) == 0) || ((v % (m + 2)) == 0)) { return false; } }
return true;
}
void rshuffle(int [] a) { // RANDOM shuffle
Random r = new Random();
for (int i = a.length - 1, j, t; i >= 0; j = r.nextInt(a.length), t = a[i], a[i] = a[j], a[j] = t, i--);
}
void qshuffle(int [] a) { // QUICK shuffle
int m = new Random().nextInt(10) + 2;
for (int i = 0, n = a.length, j = m % n, t; i < n; t = a[i], a[i] = a[j], a[j] = t, i++, j = (i * m) % n);
}
void shuffle(long [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(Object [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i]; a[i] = a[j]; a[j] = t;
}
}
int [] sort(int [] a) {
final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1;
int n = a.length, ta [] = new int [n], ai [] = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++);
int [] t = a; a = ta; ta = t;
ai = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++);
return ta;
}
void flush() {
pw.flush();
}
void pause() {
flush(); System.console().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 | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const long long INF = 1e15 + 7;
const long long N = 2e5 + 10;
const long long mod = 998244353;
long long pow(long long x, long long y) {
long long res = 1;
while (y > (long long)0) {
if (y & (long long)1) res = ((res) * (x));
y = y >> (long long)1;
x = ((x) * (x));
}
return res;
}
long long powm(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > (long long)0) {
if (y & (long long)1) res = (res * x) % p;
y >>= (long long)1;
x = (x * x) % p;
}
return res;
}
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
long long w = 0, b = 0;
vector<long long> v;
for (long long i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b % 2 != 0 && w % 2 != 0) {
cout << -1 << endl;
return;
}
if (b % 2 == 0) {
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
v.push_back(i + 1);
}
}
} else if (w % 2 == 0) {
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
v.push_back(i + 1);
}
}
}
cout << v.size() << endl;
for (auto i : v) cout << i << " ";
cout << endl;
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long ts = 1;
while (ts--) {
solve();
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
char S[210];
int b = 0, w = 0, pos[210], tot = 0;
int main() {
int n, i, k;
scanf("%d%s", &n, S + 1);
for (i = 1; i <= n; i++)
if (S[i] == 'B')
b++;
else if (S[i] == 'W')
w++;
if (b == 0 || w == 0) {
printf("0\n");
return 0;
} else if ((b % 2) && (w % 2)) {
printf("-1\n");
return 0;
} else if (b % 2 == 0) {
i = 1;
while (i <= n) {
if (S[i] == 'B') {
pos[++tot] = i;
if (i + 1 <= n && S[i + 1] == 'B')
i += 2;
else if (i + 1 <= n && S[i + 1] == 'W')
S[i + 1] = 'B', i += 1;
} else
i++;
}
} else if (w % 2 == 0) {
i = 1;
while (i <= n) {
if (S[i] == 'W') {
pos[++tot] = i;
if (i + 1 <= n && S[i + 1] == 'W')
i += 2;
else if (i + 1 <= n && S[i + 1] == 'B')
S[i + 1] = 'W', i += 1;
} else
i++;
}
}
printf("%d\n", tot);
for (i = 1; i <= tot; i++)
if (i == 1)
printf("%d", pos[i]);
else
printf(" %d", pos[i]);
printf("\n");
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author xylenox
*/
public class b {
public static void main(String[] args) {
FS in = new FS(System.in);
int n = in.nextInt();
char[] arr = in.next().toCharArray();
ArrayList<Integer> res = new ArrayList<>();
for(int i = 0; i < n-1; i++) {
if(arr[i] == 'B') {
arr[i] = 'W';
if(arr[i+1] == 'B') arr[i+1] = 'W';
else arr[i+1] = 'B';
res.add(i+1);
}
}
if(arr[n-1] == 'B') {
for(int i = 0; i < n-1; i++) {
if(arr[i] == 'W') {
arr[i] = 'B';
if(arr[i+1] == 'B') arr[i+1] = 'W';
else arr[i+1] = 'B';
res.add(i+1);
}
}
if(arr[n-1] == 'W') {
System.out.println("-1");
return;
}
}
System.out.println(res.size());
for(int e : res) System.out.print(e + " ");
System.out.println();
}
static class FS {
BufferedReader in;
StringTokenizer token;
public FS(InputStream st) {
in = new BufferedReader(new InputStreamReader(st));
}
public String next() {
if(token == null || !token.hasMoreElements()) {
try {
token = new StringTokenizer(in.readLine());
} catch(Exception e) {}
return next();
}
return token.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 | import math
from collections import defaultdict
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:input()
mod=1e9+7
"""========main code==============="""
t=1
#t=ii()
for _ in range(t):
n=ii()
s=ip()
a=s.count('B')
b=s.count('W')
k='N'
if(a%2==0):
k='B'
elif(b%2==0):
k='W'
if(k!='N'):
lol=[]
for i in range(n):
if(s[i]==k):
lol.append(i+1)
ans=[]
for i in range(0,len(lol)-1,2):
for j in range(lol[i],lol[i+1]):
ans.append(j)
print(len(ans))
print(*ans)
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int32_t main() {
vector<long long> ans;
long long n;
cin >> n;
string a, b;
cin >> a;
b = a;
for (long long i = 0; i < n - 1; i++) {
if (b[i] == 'W') {
ans.push_back(i + 1);
b[i] = 'B';
if (b[i + 1] == 'W')
b[i + 1] = 'B';
else
b[i + 1] = 'W';
}
}
bool f = 0;
for (long long i = 0; i < n; i++)
if (b[i] == 'W') f = 1;
if (f == 1)
ans.clear();
else {
cout << (long long)ans.size() << '\n';
for (auto k : ans) cout << k << " ";
return 0;
}
b = a;
for (long long i = 0; i < n - 1; i++) {
if (b[i] == 'B') {
ans.push_back(i + 1);
b[i] = 'W';
if (b[i + 1] == 'B')
b[i + 1] = 'W';
else
b[i + 1] = 'B';
}
}
f = 0;
for (long long i = 0; i < n; i++)
if (b[i] == 'B') f = 1;
if (f == 1)
ans.clear();
else {
cout << (long long)ans.size() << '\n';
for (auto k : ans) cout << k << " ";
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;
int main() {
long long n, z = 0, o = 0;
cin >> n;
char x;
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> x;
if (x == 'B') {
v[i] = 0;
z++;
}
if (x == 'W') {
v[i] = 1;
o++;
}
}
if (z == 0 || o == 0) {
cout << 0;
return 0;
}
if (z % 2 == 1 && o % 2 == 1) {
cout << -1;
return 0;
}
long long to, steps = 0;
z % 2 == 0 ? to = 0 : to = 1;
vector<long long> mov;
long long flag = 1;
while (flag) {
flag = 0;
for (long long i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
swap(v[i], v[i - 1]);
steps++;
flag = 1;
mov.push_back(i);
} else {
i++;
}
}
}
for (long long i = 0; i < v.size(); i++) {
if (v[i] == to) {
v[i] == 0 ? v[i] = 1 : v[i] = 0;
v[i + 1] = v[i];
steps++;
mov.push_back(i + 1);
i++;
}
}
cout << steps << endl;
for (auto x : mov) {
cout << x << " ";
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int INF = 1e18;
const long long fact_table = 5000000;
double Pi = 3.1415926535897932384626;
vector<long long> G[550010];
vector<pair<long long, double> > tree[500010];
priority_queue<long long> pql;
priority_queue<pair<long long, long long> > pqp;
priority_queue<long long, vector<long long>, greater<long long> > pqls;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
pqps;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
long long mod = 1000000007;
long long rui(long long number1, long long number2) {
if (number2 == 0) {
return 1;
} else {
long long number3 = rui(number1, number2 / 2);
number3 *= number3;
number3 %= mod;
if (number2 % 2 == 1) {
number3 *= number1;
number3 %= mod;
}
return number3;
}
}
long long gcd(long long number1, long long number2) {
if (number1 > number2) {
swap(number1, number2);
}
if (number1 == 0 || number1 == number2) {
return number2;
} else {
return gcd(number2 % number1, number1);
}
}
void YES(bool condition) {
if (condition) {
cout << "YES"
<< "\n";
;
} else {
cout << "NO"
<< "\n";
;
}
return;
}
void Yes(bool condition) {
if (condition) {
cout << "Yes"
<< "\n";
;
} else {
cout << "No"
<< "\n";
;
}
return;
}
long long n, m, num, sum, a, b, c, d, e, f, g, h, w, i, j, q, r, l;
long long k, ans;
long long x[500005], y[500005], z[500005];
bool used[500005];
char s[500005];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
cin >> s;
long long white = 0, black = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W') {
white++;
x[i] = 2;
} else {
black++;
x[i] = 1;
}
}
if (n % 2 == 0) {
if (white % 2 == 1) {
cout << "-1"
<< "\n";
;
} else {
vector<long long> operation;
for (long long i = 0; i < n - 1; i++) {
if (x[i] == 1) {
operation.push_back(i + 1);
x[i] = 3 - x[i];
x[i + 1] = 3 - x[i + 1];
}
}
cout << operation.size() << "\n";
;
if (operation.size() == 0) {
return 0;
}
for (long long i = 0; i < operation.size(); i++) {
cout << (operation[i]) << " ";
;
}
cout << "\n";
;
return 0;
}
} else {
vector<long long> operation;
if (white % 2 == 1) {
for (long long i = 0; i < n - 1; i++) {
if (x[i] == 1) {
operation.push_back(i + 1);
x[i] = 3 - x[i];
x[i + 1] = 3 - x[i + 1];
}
}
cout << operation.size() << "\n";
;
if (operation.size() == 0) {
return 0;
}
for (long long i = 0; i < operation.size(); i++) {
cout << (operation[i]) << " ";
;
}
cout << "\n";
;
return 0;
} else {
for (long long i = 0; i < n - 1; i++) {
if (x[i] == 2) {
operation.push_back(i + 1);
x[i] = 3 - x[i];
x[i + 1] = 3 - x[i + 1];
}
}
cout << operation.size() << "\n";
;
if (operation.size() == 0) {
return 0;
}
for (long long i = 0; i < operation.size(); i++) {
cout << (operation[i]) << " ";
;
}
cout << "\n";
;
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 | n=int(input())
s=list(input())
i=0; ans=[]; ss=s.copy()
while i<n-1:
if s[i]=='B':
s[i]='W'
s[i+1]='B' if s[i+1]=='W' else 'W'
ans.append(i+1)
i+=1
if s.count('B')==0:
print(len(ans))
print(*ans)
else:
i=0; ans=[]; s=ss
while i<n-1:
if s[i]=='W':
s[i]='B'
s[i+1]='B' if s[i+1]=='W' else 'W'
ans.append(i+1)
i+=1
if s.count('W')==0:
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 | def invert(c):
if c=='W':
return 'B'
return 'W'
def solve():
n = int(input())
s = list(input())
w = s.count('W')
b = s.count('B')
if w%2!=0 and b%2!=0:
print(-1)
return
if b%2==1:
ch = 'B'
else:
ch = 'W'
ans = []
for i in range(len(s)-1):
if s[i]!=ch:
s[i] = invert(s[i])
s[i+1] = invert(s[i+1])
ans.append(i)
print(len(ans))
for x in ans:
print(x+1,end=' ')
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 | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String a = s.next();
StringBuilder answer;
char[] check = new char[]{'W','B'};
for(char ans : check){
int count=0;
boolean temp = true;
answer = new StringBuilder();
for(int i=0;i<n-1;i++){
temp = ((ans==a.charAt(i))==temp);
if(!temp){
count++;
answer.append(i+1);
answer.append(" ");
}
}
temp = ((ans==a.charAt(n-1))==temp);
if(temp==true){
System.out.println(count);
System.out.println(answer);
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 | import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main7{
static class Pair
{
int x;
int y;
public Pair(int x, int y)
{
this.x=x;
this.y=y;
}
@Override
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
// Equal objects must produce the same
// hash code as long as they are equal
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
}
static class Pair1{
int x;
int y;
long z;
Pair1(int x, int y, long z)
{
this.x=x;
this.y=y;
this.z=z;
}
}
public static long pow(long a, long b)
{
long result=1;
while(b>0)
{
if (b % 2 != 0)
{
result=(result*a)%1000000007;
b--;
}
a=(a*a)%1000000007;
b /= 2;
}
return result;
}
public static long fact(long num)
{
long value=1;
long i=1;
for(i=2;i<=num;i++)
{
value=((value)*(long)i);
}
return value;
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
/* public static long lcm(long a,long b)
{
return a * (b / gcd(a, b));
}
*/ public static long sum(int h)
{
return (h*(h+1)/2);
}
/* public static void dfs(int parent,boolean[] visited)
{
ArrayList<Integer> arr=new ArrayList<Integer>();
arr=graph.get(parent);
ts.add(parent);
visited[parent]=true;
for(int i=0;i<arr.size();i++)
{
int num=(int)arr.get(i);
if(visited[num]==false)
{
dfs(num,visited);
}
}
}
static ArrayList<Integer> ts;
static int[] dis;*/
static int mod=1000000007;
static ArrayList<ArrayList<Integer>> graph;
static ArrayList<ArrayList<Integer>> g;
/* public static void bfs(int num,int size)
{
boolean[] visited=new boolean[size+1];
Queue<Integer> q=new LinkedList<>();
q.add(num);
ans[num]=1;
visited[num]=true;
while(!q.isEmpty())
{
int x=q.poll();
ArrayList<Integer> al=graph.get(x);
for(int i=0;i<al.size();i++)
{
int y=al.get(i);
if(visited[y]==false)
{
q.add(y);
ans[y]=ans[x]+1;
visited[y]=true;
}
}
}
}
static int[] ans;*/
static public void main(String args[])throws IOException
{
int t=1;
StringBuilder sb=new StringBuilder();
while(t-->0)
{
int n=i();
String s=s();
char[] ch=s.toCharArray();
ArrayList<Integer> ar=new ArrayList<>();
ArrayList<Integer> ar1=new ArrayList<>();
ArrayList<Integer> ar2=new ArrayList<>();
for(int i=0;i<n;i++)
{
if(ch[i]=='B')
ar.add(i);
else
ar1.add(i);
}
if(ar.size()==0 || ar1.size()==0)
{
sb.append("0\n");
}
else
{
for(int i=0;i<n-1;i++)
{
if(ch[i]=='W')
{
ar2.add(i+1);
ch[i]='B';
if(ch[i+1]=='W')
ch[i+1]='B';
else
ch[i+1]='W';
}
}
int cc=0;
for(int i=0;i<n;i++)
{
if(ch[i]=='B')
cc++;
}
if(cc==n)
{
sb.append(ar2.size()+"\n");
for(int i=0;i<ar2.size();i++)
sb.append(ar2.get(i)+" ");
sb.append("\n");
}
else
{
ar2.clear();
ch=s.toCharArray();
for(int i=0;i<n-1;i++)
{
if(ch[i]=='B')
{
ar2.add(i+1);
ch[i]='W';
if(ch[i+1]=='W')
ch[i+1]='B';
else
ch[i+1]='W';
}
}
cc=0;
for(int i=0;i<n;i++)
{
if(ch[i]=='W')
cc++;
}
if(cc==n)
{
sb.append(ar2.size()+"\n");
for(int i=0;i<ar2.size();i++)
sb.append(ar2.get(i)+" ");
sb.append("\n");
}
else
sb.append("-1\n");
}
}
}
System.out.print(sb.toString());
}
/**/
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
public static long l()
{
String s=in.String();
return Long.parseLong(s);
}
public static void pln(String value)
{
System.out.println(value);
}
public static int i()
{
return in.Int();
}
public static String s()
{
return in.String();
}
}
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 Int() {
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 String() {
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 String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.Int();
return array;
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
BBlocks solver = new BBlocks();
solver.solve(1, in, out);
out.close();
}
}
static class BBlocks {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
int[] s = new int[n];
int[] cnt = new int[2];
for (int i = 0; i < n; i++) {
s[i] = in.readChar() == 'B' ? 0 : 1;
cnt[s[i]]++;
}
IntegerList ans;
if (cnt[0] % 2 == n % 2) {
for (int i = 0; i < n; i++) {
s[i] = 1 - s[i];
}
ans = solve(s);
} else if (cnt[1] % 2 == n % 2) {
ans = solve(s);
} else {
out.println(-1);
return;
}
out.println(ans.size());
for (int i = 0, end = ans.size(); i < end; i++) {
out.append(ans.get(i) + 1).append(' ');
}
}
public IntegerList solve(int[] data) {
IntegerList ans = new IntegerList();
for (int i = 0; i < data.length - 1; i++) {
if (data[i] == 0) {
ans.add(i);
data[i] ^= 1;
data[i + 1] ^= 1;
}
}
return ans;
}
}
static class IntegerList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public IntegerList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerList(IntegerList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerList() {
this(0);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
private void checkRange(int i) {
if (i < 0 || i >= size) {
throw new ArrayIndexOutOfBoundsException();
}
}
public int get(int i) {
checkRange(i);
return data[i];
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerList list) {
addAll(list.data, 0, list.size);
}
public int size() {
return size;
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public String toString() {
return Arrays.toString(toArray());
}
public boolean equals(Object obj) {
if (!(obj instanceof IntegerList)) {
return false;
}
IntegerList other = (IntegerList) obj;
return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
}
public int hashCode() {
int h = 1;
for (int i = 0; i < size; i++) {
h = h * 31 + Integer.hashCode(data[i]);
}
return h;
}
public IntegerList clone() {
IntegerList ans = new IntegerList();
ans.addAll(this);
return ans;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
return this;
}
public FastOutput append(int c) {
cache.append(c);
return this;
}
public FastOutput println(int c) {
return append(c).println();
}
public FastOutput println() {
cache.append(System.lineSeparator());
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class SequenceUtils {
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 20];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public char readChar() {
skipBlank();
char c = (char) next;
next = read();
return c;
}
}
}
| 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 | /*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
****************************************
*/
import java.util.*;
import java.awt.Point;
import java.lang.Math;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.stream.IntStream;
public class Main {
static int oo = (int)1e9;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Musk solver = new Musk();
solver.solve(1, in, out);
out.close();
}
static class Musk {
static int inf = (int) 1e9 + 7;
public void solve(int testNumber, Scanner in, PrintWriter out) {
int N = in.nextInt();
char []S = in.next().toCharArray();
int black=0,white=0;
for(char c:S){
if(c=='B'){
black++;
}else{
white++;
}
}
char target;
if(black%2==0){
target='W';
}else if(white%2==0){
target='B';
}else{
out.println(-1);
return;
}
int count=0;
StringBuilder sb = new StringBuilder();
for(int i=1;i<=N;i++){
if(S[i-1]!=target){
count++;
sb.append(i).append(' ');
S[i-1]=target;
if(S[i]=='W'){
S[i]='B';
}else{
S[i]='W';
}
}
}
out.println(count);
if(count!=0){
out.println(sb);
}
}
}
public int factorial(int n) {
int fact = 1;
int i = 1;
while(i <= n) {
fact *= i;
i++;
}
return fact;
}
public static long gcd(long x,long y)
{
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static int gcd(int x,int y)
{
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static int abs(int a,int b)
{
return (int)Math.abs(a-b);
}
public static long abs(long a,long b)
{
return (long)Math.abs(a-b);
}
public static int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
public static int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
public static long max(long a,long b)
{
if(a>b)
return a;
else
return b;
}
public static long min(long a,long b)
{
if(a>b)
return b;
else
return a;
}
public static long pow(long n,long p,long m)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
public static long pow(long n,long p)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
static long sort(int a[]){
int n=a.length;
int b[]=new int[n];
return mergeSort(a,b,0,n-1);
}
static long mergeSort(int a[],int b[],long left,long right){
long c=0;
if(left<right){
long mid=left+(right-left)/2;
c= mergeSort(a,b,left,mid);
c+=mergeSort(a,b,mid+1,right);
c+=merge(a,b,left,mid+1,right);
}
return c;
}
static long merge(int a[],int b[],long left,long mid,long right){
long c=0;int i=(int)left;int j=(int)mid; int k=(int)left;
while(i<=(int)mid-1&&j<=(int)right){
if(a[i]<=a[j]){
b[k++]=a[i++];
}
else{
b[k++]=a[j++];c+=mid-i;
}
}
while (i <= (int)mid - 1)
b[k++] = a[i++];
while (j <= (int)right)
b[k++] = a[j++];
for (i=(int)left; i <= (int)right; i++)
a[i] = b[i];
return c;
}
} | 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 boost() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
}
int main() {
boost();
int n;
string s;
cin >> n >> s;
vector<int> ans;
for (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';
}
ans.push_back(i + 1);
}
}
if (s[n - 1] == 'W') {
if (n % 2 == 0) {
cout << -1;
return 0;
}
for (int i = 0; i < n / 2; i++) {
ans.push_back(i * 2 + 1);
}
}
cout << ans.size() << endl;
for (auto i : ans) {
cout << i << ' ';
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int check(int* a, int n) {
for (int i = 0; i < n; i++)
if (a[i] == 0) return i;
return -1;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int one = 0, zer = 0;
for (int i = 0; i < n; i++)
if (s[i] == 'W')
one++;
else
zer++;
if (one % 2 && zer % 2) {
printf("-1\n");
return 0;
}
if (one == 0 || zer == 0) {
printf("0");
return 0;
}
int ii = -1, a[n];
if (zer % 2) {
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
a[i] = 0;
if (ii == -1) ii = i;
} else
a[i] = 1;
}
swap(one, zer);
} else {
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
a[i] = 1;
} else {
if (ii == -1) ii = i;
a[i] = 0;
}
}
}
vector<int> v;
while (ii != -1) {
v.push_back(ii);
a[ii] = a[ii] ^ 1;
a[ii + 1] = a[ii + 1] ^ 1;
ii = check(a, n);
}
printf("%ld\n", v.size());
for (int i = 0; i < v.size(); i++) printf("%d ", v[i] + 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.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* @author Mubtasim Shahriar
*/
public class Blocks {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
// int t = sc.nextInt();
int t = 1;
while(t--!=0) {
solver.solve(sc, out);
}
out.close();
}
static class Solver {
public void solve(InputReader sc, PrintWriter out) {
int n = sc.nextInt();
StringBuilder sb = new StringBuilder(sc.next());
ArrayList<Integer> ans = new ArrayList();
int cntw = 0;
int cntb = 0;
for(int i = 0; i < n; i++) {
if(sb.charAt(i)=='W') cntw++;
else cntb++;
}
if(cntw%2==0) {
for(int i = 0; i < sb.length()-1; i++) {
if(sb.charAt(i)=='W') {
if(sb.charAt(i+1)=='W') {
sb.setCharAt(i, 'B');
sb.setCharAt(i+1, 'B');
ans.add(i+1);
i++;
} else {
for(int j = i+1; j < sb.length(); j++) {
if(sb.charAt(j)=='W') {
for(int k = i; k < j-1; k++) {
ans.add(k+1);
sb.setCharAt(k, 'B');
sb.setCharAt(k+1, 'W');
}
sb.setCharAt(j-1, 'B');
sb.setCharAt(j, 'B');
ans.add(j);
i=j;
break;
}
}
}
}
}
// out.println(sb.toString());
out.println(ans.size());
for(Integer i : ans) out.print(i + " ");
if(ans.size()>0) out.println();
} else if(cntb%2==0) {
for(int i = 0; i < sb.length()-1; i++) {
if(sb.charAt(i)=='B') {
// out.println(i + 1 + " " + ans);
if(sb.charAt(i+1)=='B') {
sb.setCharAt(i, 'W');
sb.setCharAt(i+1, 'W');
ans.add(i+1);
i++;
} else {
for(int j = i+1; j < sb.length(); j++) {
if(sb.charAt(j)=='B') {
for(int k = i; k < j-1; k++) {
ans.add(k+1);
sb.setCharAt(k, 'W');
sb.setCharAt(k+1, 'B');
// out.println(sb);
}
sb.setCharAt(j-1, 'W');
sb.setCharAt(j, 'W');
ans.add(j);
i=j;
break;
}
}
}
// out.println(i + 1 + " " + ans);
}
}
// out.println(sb.toString());
out.println(ans.size());
for(Integer i : ans) out.print(i + " ");
if(ans.size()>0) out.println();
} else {
out.println(-1);
}
}
}
static class InputReader {
private boolean finished = false;
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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
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;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n){
int[] array=new int[n];
for(int i=0;i<n;++i)array[i]=nextInt();
return array;
}
public int[] nextSortedIntArray(int n){
int array[]=nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n){
int[] array=new int[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public long[] nextLongArray(int n){
long[] array=new long[n];
for(int i=0;i<n;++i)array[i]=nextLong();
return array;
}
public long[] nextSumLongArray(int n){
long[] array=new long[n];
array[0]=nextInt();
for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
return array;
}
public long[] nextSortedLongArray(int n){
long array[]=nextLongArray(n);
Arrays.sort(array);
return array;
}
}
}
| 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
def Solve():
N = int(sys.stdin.readline())
S = sys.stdin.readline().rstrip()
S = [0 if a == 'B' else 1 for a in S]
ops = []
def Op(i):
S[i + 1] = S[i + 1] ^ 1
S[i] = S[i] ^ 1
ops.append(i)
for i in range(0, N - 1):
if S[i]: Op(i)
if S[-1]:
if sum(1 for i in range(N) if S[i] == 0) % 2 == 1:
print(-1)
return
for i in range(N // 2):
Op(i * 2)
print(len(ops))
if ops:
print(' '.join(str(x + 1) for x in ops))
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 | #include <bits/stdc++.h>
using namespace std;
int countColor(string s, char c) {
int ans = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == c) {
ans++;
}
}
return ans;
}
void change(char& c) {
if (c == 'W') {
c = 'B';
} else {
c = 'W';
}
}
void solve(string s, char c, vector<int>& ans) {
for (int i = 0; i < s.length() - 1; i++) {
if (s[i] == c) {
ans.push_back(i + 1);
change(s[i]);
change(s[i + 1]);
}
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
int black = countColor(s, 'B');
int white = countColor(s, 'W');
if (black == 0 || white == 0) {
cout << 0 << endl;
} else if (black % 2 && white % 2) {
cout << -1 << endl;
} else {
vector<int> ans;
if (black % 2) {
solve(s, 'W', ans);
} else {
solve(s, 'B', ans);
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[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 | n = int(input())
s = input()
s = [0 if ss=='W' else 1 for ss in s]
target = sum(s)%2
count = 0
answer = []
for i in range(len(s)-1):
if s[i] != target:
count += 1
answer.append(i+1)
s[i+1] = 1 - s[i+1]
if s[-1]!=target:
print(-1)
else:
print (count)
print (" ".join(map(str,answer)))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
inf = float("inf")
# sys.setrecursionlimit(10000000)
# abc='abcdefghijklmnopqrstuvwxyz'
# abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod, MOD = 1000000007, 998244353
# words = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'quarter',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',21:'twenty one',22:'twenty two',23:'twenty three',24:'twenty four',25:'twenty five',26:'twenty six',27:'twenty seven',28:'twenty eight',29:'twenty nine',30:'half'}
# vow=['a','e','i','o','u']
# dx,dy=[0,1,0,-1],[1,0,-1,0]
# import random
# from collections import deque, Counter, OrderedDict,defaultdict
# from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
# from math import ceil,floor,log,sqrt,factorial,pi,gcd,degrees,atan2
# from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(str, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
def checker(lst):
value = lst[0]
for i in range(1,n):
if lst[i]!=value:
return False
return True
n = int(input())
string = input()
lst1,lst2 = [],[]
for i in string:
if i=='W':
lst1.append(1)
lst2.append(1)
else:
lst1.append(0)
lst2.append(0)
# if checker(lst):
# print(0)
# exit()
one = [1]*n
zero = [0]*n
ans = []
for i in range(n-1):
if lst1[i]==1:
continue
else:
lst1[i] = 1
lst1[i+1] = lst1[i+1]^1
ans.append(i+1)
# print(lst1)
if checker(lst1):
print(len(ans))
print(*ans)
exit()
ans = []
for i in range(n-1):
if lst2[i]==0:
continue
else:
lst2[i] = 0
lst2[i+1] = lst2[i+1]^1
ans.append(i+1)
if checker(lst2):
print(len(ans))
print(*ans)
exit()
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
arr = list(input())
n1 = 0
n2 = 0
for i in arr:
if i=='W':
n1 += 1
else:
n2 += 1
if (n1*n2)%2==1:
print(-1)
elif arr == [arr[0]]*n:
print(0)
else:
ans = []
i = 0
while arr[i]=='W':
i += 1
while i<n-1:
if arr[i]=='B' and arr[i+1]=='B':
arr[i] = 'W'
arr[i+1] = 'W'
ans.append(i+1)
i += 2
elif arr[i]=='B' and arr[i+1]=='W':
arr[i] = 'W'
arr[i+1] = 'B'
ans.append(i+1)
i += 1
else :
i += 1
if arr[n-1]=='B':
for i in range(0,n-1,2):
ans.append(i+1)
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def main():
n=int(input())
l=list(input())
b=l.count('W')
val=[]
if ('B' not in(l))or('W' not in(l)):
print(0)
elif n%2==0 and b%2==1:
print(-1)
else:
if n%2==0:
for i in range(1,n-1):
if l[i-1]!=l[i]:
l[i]=l[i-1]
if l[i+1]=='W':
l[i+1]='B'
else:
l[i+1]='W'
val.append(i+1)
else:
if b%2==0:
c='B'
else:
c='W'
for i in range(n-1):
if c!=l[i]:
l[i]=c
if l[i+1]=='W':
l[i+1]='B'
else:
l[i+1]='W'
val.append(i+1)
print(len(val))
for i in range(len(val)):
print(val[i],end=' ')
print()
if __name__=="__main__":
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(input())
b = 0
w = 0
for x in s:
if x == 'W':
w += 1
else:
b += 1
if b % 2 and w % 2:
print(-1)
exit()
if b % 2:
c = 'B'
c1 = 'W'
else:
c = 'W'
c1 = 'B'
#c1 is W and c is B
a = []
for i in range(n - 1):
if s[i] == s[i + 1] == c1:
s[i] = c
s[i + 1] = c
a.append(i + 1)
elif s[i] == c1:
s[i], s[i + 1] = s[i + 1], s[i]
a.append(i + 1)
if s[n - 1] == c1:
print(-1)
else:
print(len(a))
print(*a)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
string s;
int a[maxn], tot, n;
int main() {
while (cin >> n) {
memset(a, 0, sizeof a);
tot = 0;
cin >> s;
string temp = s;
if (n == 1) {
puts("0");
continue;
}
for (int i = n - 1; i >= 1; i--) {
if (s[i] == 'B')
s[i] = 'W', s[i - 1] = (s[i - 1] == 'B' ? 'W' : 'B'), a[++tot] = i - 1;
}
if (s[0] == 'B') {
tot = 0;
s = temp;
for (int i = n - 1; i >= 1; i--) {
if (s[i] == 'W')
s[i] = 'B', s[i - 1] = (s[i - 1] == 'B' ? 'W' : 'B'),
a[++tot] = i - 1;
}
if (s[0] == 'W')
puts("-1");
else {
cout << tot << endl;
for (int i = 1; i <= tot; i++) cout << a[i] + 1 << " ";
cout << endl;
}
} else {
cout << tot << endl;
for (int i = 1; i <= tot; i++) cout << a[i] + 1 << " ";
cout << endl;
}
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int debug = 1;
int test = 1;
long double pi = 3.14159265358979323846;
inline long long int min2(long long a, long long b) {
if (a < b) {
return a;
}
return b;
}
inline long long max3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long modinv(long long a) {
long long x, y;
extgcd(a, 1000000007, x, y);
return mod(x, 1000000007);
}
long long gcd(long long u, long long v) {
long long r;
while (0 != v) {
r = u % v;
u = v;
v = r;
}
return u;
}
long long lcm(long long u, long long v) { return u / gcd(u, v) * v; }
void get_all_factors(int a[], long long int n) {
long long int w = sqrt(n);
for (int i = 1; i <= w; i++) {
if (n % i == 0) {
a[i]++;
a[n / i]++;
}
}
long long int x = ceil(sqrt(n));
if (sqrt(n) == ceil(sqrt(n))) a[(int)sqrt(n)]--;
}
long long int memo[100000][110] = {0};
long long C(long long n, long long r) {
if (r == 0 || r == n) return 1;
if (memo[n][r] != -1) return memo[n][r];
return memo[n][r] =
(C(n - 1, r - 1) % 1000000007 + C(n - 1, r) % 1000000007) %
1000000007;
}
int valid(string s, int i) {
if (s == "lol" or i == -1000050000) {
return 0;
}
return 1;
}
void status(string s1 = "lol", long long int i1 = -1000050000,
string s2 = "lol", long long int i2 = -1000050000,
string s3 = "lol", long long int i3 = -1000050000) {
if (debug) {
if (valid(s1, i1)) {
cout << s1 << "=" << i1 << " ";
}
if (valid(s2, i2)) {
cout << s2 << "=" << i2 << " ";
}
if (valid(s3, i3)) {
cout << s3 << "=" << i3 << " ";
}
cout << endl;
}
}
long long int poww(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
long long int powm(long long x, long long y, long long m = 1000000007) {
x = x % m;
long long res = 1;
while (y) {
if (y & 1) res = res * x;
res %= m;
y = y >> 1;
x = x * x;
x %= m;
}
return res % m;
}
vector<vector<long long int> > multiply(vector<vector<long long int> > &a,
vector<vector<long long int> > &b) {
vector<vector<long long int> > c(a.size(),
vector<long long int>(b[0].size()));
assert(a[0].size() == b.size());
long long int mod = 1000000007;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b[0].size(); j++) {
c[i][j] = 0;
for (int k = 0; k < a[0].size(); k++) {
c[i][j] += (a[i][k] * b[k][j]) % mod;
c[i][j] %= mod;
}
}
}
return c;
}
vector<vector<long long int> > fastmatrix(vector<vector<long long int> > &a,
long long int n) {
if (n == 1) {
return a;
}
if (n == 0) {
vector<vector<long long int> > k = a;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
k[i][j] = 1;
}
}
return k;
}
if (n % 2 == 1) {
vector<vector<long long int> > k = fastmatrix(a, n / 2);
vector<vector<long long int> > kk = multiply(k, k);
kk = multiply(kk, a);
return kk;
} else {
vector<vector<long long int> > k = fastmatrix(a, n / 2);
vector<vector<long long int> > kk = multiply(k, k);
return kk;
}
}
vector<long long int> prime(10000000, 1);
void sieve() {
for (int i = 2; i <= 10000; i++) {
if (prime[i]) {
for (int j = i * i; j < 10000000; j += i) {
prime[j] = 0;
}
}
}
}
class comp {
public:
bool operator()(pair<long long int, pair<long long int, long long int> > &a,
pair<long long int, pair<long long int, long long int> > &b) {
if (a.first == b.first) {
return a.second.first > b.second.first;
}
return a.first < b.first;
}
};
long long int getsum(long long int x) { return (x * (x + 1)) / 2; }
void filereader() {}
int comp(int a, int b) { return a > b; }
int main() {
filereader();
debug = 0;
test = 0;
int tt = 1;
if (test) {
cin >> tt;
}
while (tt--) {
int n;
cin >> n;
string second;
cin >> second;
int bcnt = 0, wcnt = 0;
vector<pair<long long int, pair<long long int, long long int> > > pre;
int i = 0;
while (1) {
int s, e;
s = i;
int f = 0;
while (i < n and second[i] == 'B') {
bcnt++;
i++;
}
e = i - 1;
if (e >= s) {
f = 1;
pre.push_back(make_pair('B', make_pair(s, e)));
}
s = i;
while (i < n and second[i] == 'W') {
wcnt++;
i++;
}
e = i - 1;
if (e - s >= 0) {
f = 1;
pre.push_back(make_pair('W', make_pair(s, e)));
}
if (i == n) {
break;
}
}
if (pre.size() == 1) {
cout << 0 << endl;
return 0;
}
vector<long long int> moves;
int rem = 0;
if (bcnt % 2 == 0) {
for (int i = 0; i < pre.size(); i++) {
if (pre[i].first == 'B') {
int s = pre[i].second.first;
int e = pre[i].second.second;
int cnt = e - s + 1;
int j = s;
if (rem == 2) {
j++;
}
while (j <= e) {
moves.push_back(j);
j += 2;
}
if (rem == 2) {
if (cnt % 2 == 0) {
rem = 1;
} else {
rem = 0;
}
} else {
rem = cnt % 2;
}
} else {
if (rem > 0) {
rem++;
int s = pre[i].second.first;
int e = pre[i].second.second;
int cnt = e - s + 1;
int j = s;
while (j <= e) {
moves.push_back(j);
j += 1;
}
}
}
}
} else if (wcnt % 2 == 0) {
for (int i = 0; i < pre.size(); i++) {
if (pre[i].first == 'W') {
int s = pre[i].second.first;
int e = pre[i].second.second;
int cnt = e - s + 1;
int j = s;
if (rem == 2) {
j++;
}
while (j <= e) {
moves.push_back(j);
j += 2;
}
if (rem == 2) {
if (cnt % 2 == 0) {
rem = 1;
} else {
rem = 0;
}
} else {
rem = cnt % 2;
}
} else {
if (rem > 0) {
rem++;
int s = pre[i].second.first;
int e = pre[i].second.second;
int cnt = e - s + 1;
int j = s;
while (j <= e) {
moves.push_back(j);
j += 1;
}
}
}
}
} else {
cout << -1 << endl;
return 0;
}
cout << moves.size() << endl;
for (auto i : moves) {
cout << i + 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;
int main() {
int n;
string s;
cin >> n;
cin >> s;
int c = 0, b = 0, w = 0, i;
vector<int> v;
for (i = 0; i < n; i = i + 1) {
if (s[i] == 'B')
b = b + 1;
else if (s[i] == 'W')
w = w + 1;
}
if (b % 2 != 0 && w % 2 != 0) {
cout << "-1" << endl;
} else if (b == 0 || w == 0) {
cout << "0" << endl;
} else {
if (b % 2 == 0) {
for (i = 0; i < n; i = i + 1) {
if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
c = c + 1;
v.push_back(i + 1);
} else if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
c = c + 1;
v.push_back(i + 1);
}
}
} else if (w % 2 == 0) {
for (i = 0; i < n; i = i + 1) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
c = c + 1;
v.push_back(i + 1);
} else if (s[i] == 'W' && s[i + 1] == 'B') {
swap(s[i], s[i + 1]);
c = c + 1;
v.push_back(i + 1);
}
}
}
cout << c << endl;
for (i = 0; i < v.size(); i = i + 1) {
cout << v[i] << " ";
}
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 | import java.io.BufferedReader;
//import java.io.CharConversionException;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Comparator;
//import java.util.Deque;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import javafx.beans.binding.StringBinding;
//import java.util.LinkedList;
//import java.util.PriorityQueue;
//import java.util.Random;
public class Shawarma_Tent {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String s=br.readLine();
StringBuilder sb=new StringBuilder(s);
StringBuilder answer=new StringBuilder();
int ans=0;
for(int i=0;i<n-1;++i)
{
char ch=sb.charAt(i);
if(ch!='W')
{
++ans;
answer.append((i+1)+" ");
if(sb.charAt(i+1)=='W')
sb.replace(i,i+2,"WB");
else
sb.replace(i,i+2,"WW");
// System.out.println(sb);
}
}
if (sb.charAt(n - 1) == 'B') {
if (n % 2 == 0) {
System.out.println(-1);
}
else
{
System.out.println(ans+n/2);
StringBuilder newindex=new StringBuilder();
for(int i=1;i<n;i+=2)
newindex.append(i+" ");
System.out.println(answer.toString()+newindex);
}
}
else
{
System.out.println(ans);
System.out.println(answer);
}
}
}
| 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 |
# B. Suits
# Link https://codeforces.com/contest/1271/problem/B
n = int(input())
a = list(input())
w = 0
b = 0
for it in a:
if it == 'W':
w+=1
else:
b+=1
x = 'W'
y = 'B'
if(w%2 == 1 and b % 2 == 1):
print(-1)
else:
if(b % 2 == 1):
x = 'B'
y = 'W'
ans = []
for i in range(n-1):
if(a[i] != x):
ans.append(i+1)
a[i+1] = (x if (a[i+1] == y) else y)
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 | 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.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author @Got
*/
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 {
List<Integer> play(char[] s, char c) {
List<Integer> ans1 = new ArrayList<>();
for (int i = 0; i + 1 < s.length; ++i) {
if (s[i] != c) {
s[i] = invert(s[i]);
s[i + 1] = invert(s[i + 1]);
ans1.add(i + 1);
}
}
for (char ci : s) if (ci != c) return null;
return ans1;
}
char invert(char c) {
if (c == 'W') return 'B';
return 'W';
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
char[] s = in.readString().toCharArray();
List<Integer> ans1 = play(s.clone(), 'B');
List<Integer> ans2 = play(s.clone(), 'W');
if (ans1 != null) {
out.printLine(ans1.size());
out.printLine(ans1.toArray());
} else if (ans2 != null) {
out.printLine(ans2.size());
out.printLine(ans2.toArray());
} else {
out.printLine(-1);
}
}
}
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 printLine(int i) {
writer.println(i);
}
}
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);
}
}
}
| 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 = []
n_B = 0
n_W = 0
ans = []
for c in s:
if c == 'W':
n_W += 1
x = 1
else:
n_B += 1
x = 0
L.append(x)
is_possible = True
if n_B % 2 and n_W % 2:
is_possible = False
if n_W == 0 or n_B == 0:
print(0)
elif is_possible:
for i in range(n - 1):
if L[i] == 1:
L[i] = 0
L[i + 1] = (L[i + 1] + 1) % 2
ans.append(i + 1)
if L[-1] == 1:
for i in range(1, (n+1) // 2):
ans.append(2 * i - 1)
print(len(ans))
print(*ans)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b, long long mod) {
if (b == 0 || a == 1) {
if (mod == 1)
return 0;
else
return 1;
}
if (b % 2 == 0) {
long long k = powmod(a, b / 2, mod);
return (k * k) % mod;
} else {
long long k = powmod(a, b / 2, mod);
return ((k * k) % mod * a) % mod;
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
if (a > b)
return gcd(a % b, b);
else
return gcd(b % a, a);
}
long long prime(long long p) {
for (long long i = 2; i * i <= p; i++) {
if (p % i == 0 && i < p) return i;
}
return 1;
}
void solve(long long ppppppppp = 1) {
long long a;
cin >> a;
string s;
cin >> s;
vector<long long> ans;
for (long long i = 1; i < a - 1; i++) {
if (s[i] != s[0]) {
ans.push_back(i + 1);
s[i] = ('B' + 'W' - s[i]);
s[i + 1] = ('B' + 'W' - s[i + 1]);
}
}
long long check = 1;
for (long long i = 0; i < a; i++)
if (s[i] != s[0]) check = 0;
if (check == 1) {
cout << ans.size() << "\n";
for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return;
}
for (long long i = 0; i < a - 2; i++) {
if (s[i] != s[a - 1]) {
ans.push_back(i + 1);
s[i] = ('B' + 'W' - s[i]);
s[i + 1] = ('B' + 'W' - s[i + 1]);
}
}
check = 1;
for (long long i = 0; i < a; i++)
if (s[i] != s[0]) check = 0;
if (check == 1) {
cout << ans.size() << "\n";
for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return;
}
cout << -1;
return;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tututu;
tututu = 1;
for (long long qwerty = 0; qwerty < tututu; qwerty++) 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 | n=int(input())
s=input()
b=[]
for j in s:
b.append(j)
res=[]
j=0
while(j<n-1):
if b[j]=="B" and b[j+1]=="W":
b[j],b[j+1]=b[j+1],b[j]
res.append(j+1)
j+=1
elif b[j]=="B" and b[j+1]=="B":
b[j]="W"
b[j+1]="W"
res.append(j+1)
j+=2
else:
j+=1
if len(set(b))==1:
print(len(res))
print(*res)
else:
if len(b)%2!=0:
j=0
while(j<(n-1)):
res.append(j+1)
j+=2
print(len(res))
print(*res)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | a=int(input())
b=list(input())
n=0
n1=[]
z=b[:]
e=b
for i in range(len(b)-1):
if(b[i]=='W'):
b[i]='B'
if(b[i+1]=='W'):
b[i+1]='B'
else:
b[i+1]='W'
n+=1
n1.append(i+1)
if(b.count('W')!=a and b.count('B')!=a):
e=b[:]
for i in range(len(b)-1):
if(b[i]=='B'):
b[i]='W'
if(b[i+1]=='B'):
b[i+1]='W'
else:
b[i+1]='B'
n+=1
n1.append(i+1)
if(b.count('W')!=a and b.count('B')!=a):
print(-1)
else:
print(n)
if(n!=0):
print(*n1)
else:
print(n)
if(n!=0):
print(*n1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
n = int(input())
s = list(input())
blacks = s.count('B')
whites = n - blacks
operations = []
if( blacks&1== whites&1 ==1):
print(-1)
exit()
elif( blacks&1==1):
i=0
while(i<n):
if(s[i]=='W'):
operations.append(i+1)
s[i]='B'
if(i+1< n):
if(s[i+1]=='W'):
s[i+1]='B'
else: s[i+1]='W'
i+=1
else:
i=0
while(i<n):
if(s[i]=='B'):
operations.append(i+1)
s[i]='W'
if(i+1< n):
if(s[i+1]=='B'):
s[i+1]='W'
else: s[i+1]='B'
i+=1
print(len(operations))
for x in operations:
print(x,end=' ') | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
string s;
bool inc[205];
vector<int> ans;
int main() {
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
if ('B' == s[i]) {
inc[i + 1] = 1;
} else
inc[i + 1] = 0;
}
bool cop[205];
for (int i = 1; i <= n; i++) cop[i] = inc[i];
for (int i = 1; i < n; i++) {
if (cop[i]) {
cop[i] = 0;
cop[i + 1] = !cop[i + 1];
ans.push_back(i);
}
}
if (cop[n]) {
ans.clear();
for (int i = 1; i < n; i++) {
if (!inc[i]) {
inc[i] = 1;
inc[i + 1] = !inc[i + 1];
ans.push_back(i);
}
}
if (!inc[n]) {
cout << -1;
return 0;
}
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.math.BigInteger;
public class asd
{static Scanner s=new Scanner(System.in);
public static void main(String args[]) throws Exception
{
int t=1;
while(t-->0)
{
solve();
}
}
public static void solve() {
int n=s.nextInt();
String str=s.next();ArrayList<Integer> list=new ArrayList<>();
char[] ch=str.toCharArray();
int b=0;int w=0;
for(int i=0;i<str.length();i++)
{
if(ch[i]=='W')
w++;
else
b++;
}
if(w%2==1&&b%2==1)
{
System.out.println("-1");
return;
}
int col=9;
if(w%2==0)
col=2;
else if(b%2==0)
col=3;
if(col==2)
{
//white to black
for(int i=0;i<str.length();i++)
{
if(ch[i]=='W'&&ch[i+1]=='W')
{
list.add(i+1);
ch[i]='B';ch[i+1]='B';
}
else if(ch[i]=='W'&&ch[i+1]=='B')
{
list.add(i+1);
ch[i]='B';ch[i+1]='W';
}
}
}
else
{
//black to white
for(int i=0;i<str.length();i++)
{
if(ch[i]=='B'&&ch[i+1]=='B')
{
list.add(i+1);
ch[i]='W';ch[i+1]='W';
}
else if(ch[i]=='B'&&ch[i+1]=='W')
{
list.add(i+1);
ch[i]='W';ch[i+1]='B';
}
}
}
System.out.println(list.size());
for(int i=0;i<list.size();i++)
System.out.print(list.get(i)+ " ");
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | /**
* BaZ :D
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
static MyScanner scan;
static PrintWriter pw;
static long MOD = 1_000_000_007;
static long INF = 1_000_000_000_000_000_000L;
static long inf = 2_000_000_000;
public static void main(String[] args) {
new Thread(null, null, "BaZ", 1 << 27) {
public void run() {
try {
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static int n,ans;
static char c[];
static ArrayList<Integer> moves;
static void solve() throws IOException
{
//initIo(true);
initIo(false);
StringBuilder sb = new StringBuilder();
n = ni();
c = ne().toCharArray();
solve('W');
if(ans!=-1) {
pl(moves.size());
for(int e: moves) {
p(e);
}
pl();
System.exit(0);
}
solve('B');
if(ans!=-1) {
pl(moves.size());
for(int e: moves) {
p(e);
}
pl();
System.exit(0);
}
pl(-1);
pw.flush();
pw.close();
}
static void solve(char to) {
int arr[] = new int[n];
int cnt = 0;
for(int i=0;i<n;++i) {
if(c[i]!=to) {
arr[i] = 1;
cnt++;
}
}
if(cnt%2!=0) {
ans = -1;
return;
}
ans = 0;
moves = new ArrayList<>();
for(int i=0;i<n;++i) {
if(arr[i] == 1) {
moves.add((i+1));
if(i+1<n) {
arr[i+1]^=1;
}
}
}
}
static void initIo(boolean isFileIO) throws IOException {
scan = new MyScanner(isFileIO);
if(isFileIO) {
pw = new PrintWriter("/Users/amandeep/Desktop/output.txt");
}
else {
pw = new PrintWriter(System.out, true);
}
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static String ne() throws IOException
{
return scan.next();
}
static String nel() throws IOException
{
return scan.nextLine();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static void pa(String arrayName, Object arr[])
{
pl(arrayName+" : ");
for(Object o : arr)
p(o);
pl();
}
static void pa(String arrayName, int arr[])
{
pl(arrayName+" : ");
for(int o : arr)
p(o);
pl();
}
static void pa(String arrayName, long arr[])
{
pl(arrayName+" : ");
for(long o : arr)
p(o);
pl();
}
static void pa(String arrayName, double arr[])
{
pl(arrayName+" : ");
for(double o : arr)
p(o);
pl();
}
static void pa(String arrayName, char arr[])
{
pl(arrayName+" : ");
for(char o : arr)
p(o);
pl();
}
static void pa(String listName, List list)
{
pl(listName+" : ");
for(Object o : list)
p(o);
pl();
}
static void pa(String arrayName, Object[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(Object o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, int[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(int o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, long[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(long o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, char[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(char o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, double[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(double o : arr[i])
p(o);
pl();
}
}
static class MyScanner
{
BufferedReader br;
StringTokenizer st;
MyScanner(boolean readingFromFile) throws IOException
{
if(readingFromFile) {
br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt"));
}
else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String nextLine()throws IOException
{
return br.readLine();
}
String next() throws IOException
{
if(st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
class BBlocks {
public:
void solve(std::istream &in, std::ostream &out) {
string s;
in >> s >> s;
string x = "BW";
for (auto y : x) {
vector<int> answer;
int n = s.length();
string t = s;
for (int i = 0; i + 1 < n; ++i) {
if (t[i] != y) {
answer.push_back(i + 1);
t[i + 1] = x[0] + x[1] - t[i + 1];
}
}
if (t.back() == y) {
out << answer.size() << endl;
for (auto u : answer) {
out << u << " ";
}
out << endl;
return;
}
}
out << -1 << endl;
}
};
int main() {
BBlocks solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=list(input())
c1=s.count('B')
c2=s.count('W')
if c1%2==1 and c2%2==1:
print(-1)
else:
x='W' if c2%2==0 else 'B'
q=0
c=0
a=[]
for i in range(n):
if s[i]==x and q==0 or s[i]!=x and q==1:
q=1
c+=1
a.append(i+1)
else:
q=0
print(c)
print(*a) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
#input = sys.stdin.buffer.readline
n = int(input())
s = list(map(lambda x: 0 if x == 'B' else 1,list(input().strip())))
def flip(index):
if s[index] == 0:s[index] = 1
else:s[index] = 0
out = []
for i in range(1,len(s)-1):
if s[i] == s[0]:continue
flip(i); flip(i+1)
out.append(i + 1)
if s[-1] == s[0]:
print(len(out))
print(*out)
elif (n-1)%2 != 0:
print(-1)
else:
for i in range(n-3,-1,-2):out.append(i+1)
print(len(out))
print(*out)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, w = 0, b = 0;
char s[205];
vector<int> v;
scanf("%d %s", &n, s);
for (int i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (w % 2 == 0) {
for (int i = n - 1; i > 0; i--)
if (s[i] == 'W') {
v.push_back(i);
s[i] = 'B';
if (s[i - 1] == 'W')
s[i - 1] = 'B';
else
s[i - 1] = 'W';
}
} else if (b % 2 == 0) {
for (int i = n - 1; i > 0; i--)
if (s[i] == 'B') {
v.push_back(i);
s[i] = 'W';
if (s[i - 1] == 'W')
s[i - 1] = 'B';
else
s[i - 1] = 'W';
}
} else {
puts("-1");
return 0;
}
printf("%d\n", (int)v.size());
for (int i = 0; i < (int)v.size(); i++) {
if (i) putchar(' ');
printf("%d", v[i]);
}
puts("");
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
import org.omg.CORBA.INTERNAL;
import sun.awt.image.ImageWatched;
import sun.reflect.generics.tree.Tree;
import java.nio.channels.ScatteringByteChannel;
import java.text.SimpleDateFormat;
import java.util.*;
public final class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
String str = s.next();
StringBuilder b = new StringBuilder(str);
int white = 0;
int black = 0;
List<Integer> wPointer = new LinkedList<>();
List<Integer> bPointer = new LinkedList<>();
for (int i = 0; i < N; i++) {
char c = str.charAt(i);
if (c=='W') {
if (white%2 ==0) wPointer.add(i);
if (black % 2 == 1) bPointer.add(i);white++;
} else {
//black
if (white%2 ==1) wPointer.add(i);
if (black % 2 == 0) bPointer.add(i); black++;
}
}
if (black % 2 != 0 && white%2 != 0) {
System.out.print(-1);
return;
}
if (white%2 ==0) {
System.out.println(wPointer.size());
for (int k : wPointer) {
System.out.print(k+1 + " ");
}
} else {
System.out.println(bPointer.size());
for (int k : bPointer) {
System.out.print(k+1 + " ");
}
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
string str;
cin >> str;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (str[i] == 'W') {
w++;
} else {
b++;
}
}
if (w % 2 != 0 && b % 2 != 0) {
cout << "-1";
} else if (w == 0 || b == 0) {
cout << "0";
} else {
if (b % 2 == 0) {
vector<long long> u, v;
for (int i = 0; i < n - 1; i++) {
if (str[i] == 'B' && str[i + 1] == 'B') {
v.push_back(i);
i++;
} else if (str[i] == 'B') {
u.push_back(i);
str[i] = 'W';
str[i + 1] = 'B';
}
}
cout << v.size() + u.size() << "\n";
for (int i = 0; i < u.size(); i++) {
cout << u[i] + 1 << " ";
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] + 1 << " ";
}
} else {
vector<long long> u, v;
for (int i = 0; i < n - 1; i++) {
if (str[i] == 'W' && str[i + 1] == 'W') {
v.push_back(i);
i++;
} else if (str[i] == 'W') {
u.push_back(i);
str[i] = 'B';
str[i + 1] = 'W';
}
}
cout << v.size() + u.size() << "\n";
for (int i = 0; i < u.size(); i++) {
cout << u[i] + 1 << " ";
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] + 1 << " ";
}
}
}
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.io.*;
public class File {
public static 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());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n=sc.nextInt();
char arr[]=sc.next().toCharArray();
char temp[]=new char[n];
for(int i=0;i<n;i++)temp[i]=arr[i];
StringBuilder ans=new StringBuilder();
int c=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]=='W'){
arr[i]='B';
if(arr[i+1]=='W')arr[i+1]='B';
else arr[i+1]='W';
ans.append((i+1)+" ");
c++;
}
}
if(arr[n-1]=='B')
{
out.println(c);
out.println(ans.toString());
}
else
{
ans=new StringBuilder();
c=0;
for(int i=0;i<n-1;i++)
{
if(temp[i]=='B'){
temp[i]='W';
if(temp[i+1]=='W')temp[i+1]='B';
else temp[i+1]='W';
ans.append((i+1)+" ");
c++;
}
}
if(temp[n-1]=='W')
{
out.println(c);
out.println(ans.toString());
}
else out.println(-1);
}
out.close();
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> a;
int n, i, b = 0, w = 0;
scanf("%d", &n);
char c[n];
scanf("%s", c);
for (i = 0; i < n; ++i) {
if (c[i] == 'B') ++b;
if (c[i] == 'W') ++w;
}
if (b == 0 || w == 0) {
printf("0");
return 0;
}
if (b % 2 == 1 && w % 2 == 1) {
printf("-1");
return 0;
}
int count = 0;
for (i = 0; i < n - 1; ++i) {
if (c[i] == 'W') {
c[i] = 'B';
if (c[i + 1] == 'B')
c[i + 1] = 'W';
else
c[i + 1] = 'B';
++count;
a.push_back(i + 1);
}
}
if (c[n - 1] == 'W') {
for (i = 0; i < n - 2; i += 2) {
c[i] = c[i + 1] = 'W';
a.push_back(i + 1);
++count;
}
}
printf("%d\n", count);
for (i = 0; i < a.size(); ++i) printf("%d ", a[i]);
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.Scanner;
public class BAy {
public static void main(String[] args) {
int num; String s;
Scanner scan = new Scanner(System.in);
num=scan.nextInt();
s=scan.next();
char[] s1 = s.toCharArray();
char[] s2 = s.toCharArray();
int dex=0;
int arr[] = new int[num];
for(int i=0;i<num-1;i++){
if(s1[i]=='W'){
s1[i]='B';
arr[dex++]=i+1;
if(s1[i+1]=='B'){
s1[i+1]='W';
}else{
s1[i+1]='B';
}
}
}
if(s1[num-1]=='B'){
System.out.println(dex);
for(int j=0;j<dex;j++){
System.out.print(arr[j]+ " ");
}
return;
}
int dex1=0;
int arr1[] = new int[num];
for(int i=0;i<num-1;i++){
if(s2[i]=='B'){
s2[i]='W';
arr1[dex1++]=i+1;
if(s2[i+1]=='W'){
s2[i+1]='B';
}else{
s2[i+1]='W';
}
}
}
if(s2[num-1]=='W'){
System.out.println(dex1);
for(int j=0;j<dex1;j++){
System.out.print(arr1[j] + " ");
}
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;
string s, t;
vector<int> ans;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
cin >> s;
t = s;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
ans.push_back(i + 1);
}
}
if (s[n - 1] == 'B') {
cout << ans.size() << '\n';
for (auto it : ans) {
cout << it << ' ';
}
cout << '\n';
return 0;
}
ans.clear();
for (int i = 0; i < n - 1; i++) {
if (t[i] == 'B') {
t[i] = 'W';
if (t[i + 1] == 'B')
t[i + 1] = 'W';
else
t[i + 1] = 'B';
ans.push_back(i + 1);
}
}
if (t[n - 1] == 'B') {
cout << -1 << '\n';
return 0;
}
cout << ans.size() << '\n';
for (auto it : ans) {
cout << it << ' ';
}
cout << '\n';
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long powm(long long base, long long exp, long long mod = 1000000007) {
long long ans = 1;
while (exp) {
if (exp & 1) ans = (ans * base) % mod;
exp >>= 1, base = (base * base) % mod;
}
return ans;
}
int32_t main() {
long long n;
cin >> n;
string s;
cin >> s;
long long w = 0, b = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (w == n || b == n)
cout << 0 << endl;
else if (w % 2 != 0 && b % 2 != 0)
cout << "-1" << endl;
else {
string t = s;
vector<long long> ans;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W';
ans.push_back(i + 1);
}
}
long long flag = false;
if (s[n - 1] == 'B') {
cout << ans.size() << endl;
for (auto i : ans) cout << i << endl;
flag = true;
}
if (flag == false) {
ans.clear();
for (long long i = 0; i < n - 1; i++) {
if (t[i] == 'B') {
t[i] = 'W';
t[i + 1] = t[i + 1] == 'B' ? 'W' : 'B';
ans.push_back(i + 1);
}
}
if (t[n - 1] == 'W') {
cout << ans.size() << endl;
for (auto i : ans) cout << i << endl;
}
}
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
n = int(input())
s = [int(i=="B") for i in input()]
b = sum(s)
w = n-b
if w*b == 0:
print(0)
elif w%2 and b%2:
print(-1)
else:
target = 1 if b%2==0 else 0
ans = []
for i in range(n-1):
if s[i] == target:
ans.append(i+1)
s[i+1] ^= 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 | import sys
n=int(sys.stdin.readline())
s=sys.stdin.readline().strip()
a=[]
for x in range(n):
a.append(s[x])
steps=[]
ans="YES"
for i in range(n-1):
if(a[i]=="B"):
a[i]="W"
if(a[i+1]=="B"):
a[i+1]="W"
else:
a[i+1]="B"
steps.append(str(i+1))
cont=0
for p in range(n):
if(a[p]=="B"):
cont=1
break
if(cont==1):
for q in range(n-1):
if(a[q]=="W"):
a[q]="B"
if(a[q+1]=="B"):
a[q+1]="W"
else:
a[q+1]="B"
steps.append(str(q+1))
for r in range(n):
if(a[r]=="W"):
ans="NO"
break
if(ans=="NO"):
print(-1)
else:
print(len(steps))
if(len(steps)>0):
print(" ".join(steps))
| 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.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Blocks {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader f=new FastReader();
int n=f.nextInt();
String str=f.nextLine();
int b=0;
int w=0;
for(int i=0;i<n;i++) {
if(str.charAt(i)=='B') b++;
else if(str.charAt(i)=='W') w++;
}
ArrayList<Integer> index=new ArrayList<Integer>();
if(b%2!=0&&w%2!=0) {
System.out.println(-1);
return;
}
char c[]=str.toCharArray();
int steps=0;
for(int j=0;j<3;j++) {
b=0;
w=0;
boolean allw=true;
boolean allb=true;
for(int i=0;i<n;i++) {
if(c[i]=='B') allw=false;
}
for(int i=0;i<n;i++) {
if(c[i]=='W') allb=false;
}
if(allw==true||allb==true) {
break;
}
for(int i=0;i<n;i++) {
if(c[i]=='B') b++;
else if(c[i]=='W') w++;
}
if(b<=w) {
for(int i=n-1;i>0;i--) {
if(c[i]=='W') {
c[i]='B';
steps++;
index.add(i);
if(c[i-1]=='B') {
c[i-1]='W';
}else if(c[i-1]=='W') {
c[i-1]='B';
}
}
}
}else if(w<b) {
for(int i=n-1;i>0;i--) {
if(c[i]=='B') {
c[i]='W';
steps++;
index.add(i);
if(c[i-1]=='B') {
c[i-1]='W';
}else if(c[i-1]=='W') {
c[i-1]='B';
}
}
}
}
}
boolean allw=true;
boolean allb=true;
for(int i=0;i<n;i++) {
if(c[i]=='B') allw=false;
}
for(int i=0;i<n;i++) {
if(c[i]=='W') allb=false;
}
if(allw==true||allb==true) {
System.out.println(steps);
for(int i=0;i<index.size();i++) {
System.out.print(index.get(i)+" ");
}
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 | n=int(input())
s=input()
arr=[x for x in s]
B=arr.count('B')
W=arr.count('W')
if not B or not W:
print(0)
else:
if B&1 and W&1:
print(-1)
else:
ans=[]
if B%2==0:
for x in range(n-1):
if arr[x]=='B':
if arr[x+1]=='B':
ans.append(x)
arr[x]='W'
arr[x+1]='W'
else:
ans.append(x)
arr[x]='W'
arr[x+1]='B'
else:
for x in range(n-1):
if arr[x]=='W':
if arr[x+1]=='W':
ans.append(x)
arr[x]='B'
arr[x+1]='B'
else:
ans.append(x)
arr[x]='B'
arr[x+1]='W'
print(len(ans))
for x in ans:
print(x+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 | n=int(input())
l=list(input())
z='W'
ans=[]
for i in range(n-1):
if l[i]!=z:
l[i]=z
l[i+1]='W' if l[i+1]=='B' else 'B'
ans.append(i+1)
if l!=['W']*n:
z='B'
for i in range(n-1):
if l[i]!=z:
l[i]=z
l[i+1]='W' if l[i+1]=='B' else 'B'
ans.append(i+1)
if l!=[z]*n:
print(-1)
else:
print(len(ans))
for i in range(len(ans)):
print(ans[i],end=' ')
else:
print(len(ans))
for i in range(len(ans)):
print(ans[i],end=' ') | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s, s1;
int count = 0, count1 = 0;
vector<int> v, v1;
cin.ignore(255, '\n');
getline(cin, s);
s1 = s;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] == 'W' ? s[i + 1] = 'B' : s[i + 1] = 'W';
count++;
v.push_back(i + 1);
}
}
if (s[s.size() - 1] == s[s.size() - 2]) {
cout << count << '\n';
for (int i = 0; i < v.size(); i++) cout << v[i] << ' ';
return 0;
}
for (int i = 0; i < s1.size() - 1; i++) {
if (s1[i] == 'W') {
s1[i] = 'B';
s1[i + 1] == 'B' ? s1[i + 1] = 'W' : s1[i + 1] = 'B';
count1++;
v1.push_back(i + 1);
}
}
if (s1[s1.size() - 1] == s1[s1.size() - 2]) {
cout << count1 << '\n';
for (int i = 0; i < v1.size(); i++) cout << v1[i] << ' ';
return 0;
}
cout << "-1";
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = input()
s = raw_input()
b = s.count('B')
w = n - b
if w == 0 or b == 0:
print 0
else:
ans = []
t = s[0]
flag = 0
for i in range(n - 1):
if (s[i] != t and not flag) or (s[i] == t and flag):
ans.append(i + 1)
flag = 1
else:
flag = 0
if (s[n - 1] != t and not flag) or (s[n - 1] == t and flag):
if n & 1 == 0:
ans = []
else:
for i in range(1, n, 2):
ans.append(i)
if ans:
print len(ans)
print ' '.join(map(str, ans))
else:
print -1
| PYTHON |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, a[1000];
string s;
cin >> n;
cin >> s;
j = 0;
for (i = 0; i < n; ++i) {
if (s[i] == 'B') j++;
}
if (n % 2 == 0) {
if (j % 2 == 1) {
cout << -1;
return 0;
}
}
int ans = 0;
int num = 0;
if (j % 2 == 0) {
for (i = 0; i < n - 1; ++i) {
if (s[i] == 'B' && s[i + 1] == 'B') {
ans++;
a[num] = i;
num++;
s[i] = 'W';
s[i + 1] = 'W';
}
}
for (i = 0; i < n - 1; ++i) {
if (s[i] == 'B') {
for (;; ++i) {
ans++;
a[num] = i;
num++;
if (s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
break;
}
s[i] = 'W';
s[i + 1] = 'B';
}
}
}
cout << ans << endl;
for (i = 0; i < num; ++i) {
cout << a[i] + 1 << " ";
}
} else {
for (i = 0; i < n - 1; ++i) {
if (s[i] == 'W' && s[i + 1] == 'W') {
ans++;
a[num] = i;
num++;
s[i] = 'B';
s[i + 1] = 'B';
}
}
for (i = 0; i < n - 1; ++i) {
if (s[i] == 'W') {
for (;; ++i) {
ans++;
a[num] = i;
num++;
if (s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
break;
}
s[i] = 'B';
s[i + 1] = 'W';
}
}
}
cout << ans << endl;
for (i = 0; i < num; ++i) {
cout << a[i] + 1 << " ";
}
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from collections import Counter
n = int(input())
s = input()
rr = None
for x in "BW":
res = []
toggle = False
for i, v in enumerate(s):
if v == x:
toggle = not toggle
if toggle:
res.append(i+1)
if not toggle and (rr is None or len(res) < len(rr)):
rr = res
if rr is None:
print(-1)
else:
print(len(rr))
print(*rr) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
long long x = 0;
vector<long long> ans;
for (auto &ch : s) {
if (ch == 'B') x++;
}
if (x == 0 || x == n) {
cout << 0;
return;
} else if ((x & 1) && ((n - x) & 1)) {
cout << -1;
return;
} else {
if (x & 1) {
for (long long i = 0; i < n - 1; ++i) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
ans.push_back(i);
} else if (s[i] == 'W' && s[i + 1] == 'B') {
s[i] = 'B';
s[i + 1] = 'W';
ans.push_back(i);
}
}
} else if ((n - x) & 1) {
for (long long i = 0; i < n - 1; ++i) {
if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
ans.push_back(i);
} else if (s[i] == 'B' && s[i + 1] == 'W') {
s[i] = 'W';
s[i + 1] = 'B';
ans.push_back(i);
}
}
} else {
for (long long i = 0; i < n - 1; ++i) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
ans.push_back(i);
} else if (s[i] == 'W' && s[i + 1] == 'B') {
s[i] = 'B';
s[i + 1] = 'W';
ans.push_back(i);
}
}
}
}
cout << (long long)((ans).size()) << "\n";
for (auto mov : ans) cout << mov + 1 << " ";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long TC = 1;
for (long long T = 1; T <= TC; ++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 | n = int(input())
s = list(input())
k = sum([1 for i in s if i == "B"])
if(n % 2 == 0 and k % 2 == 1):
print(-1)
else:
color = s[0] if n % 2 == 0 else ("B" if k % 2 == 1 else "W")
moves = []
for i in range(n):
if(s[i] == color):
continue
else:
moves.append(str(i + 1))
s[i] = "W" if s[i] == "B" else "B"
s[i + 1] = "W" if s[i + 1] == "B" else "B"
print(len(moves))
print(" ".join(moves))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import os, sys, math
import collections
if os.path.exists('testing'):
name = os.path.basename(__file__)
if name.endswith('.py'):
name = name[:-3]
src = open(name + '.txt', encoding='utf8')
input = src.readline
def solve():
result = []
data = [ 1 if q == 'W' else 0 for q in n ]
count = 0
for x in range(len(data)):
v = data[x]
if v == 0:
count += 1
if x + 1 < len(data) and data[x + 1] == 0:
count -= 2
if count % 2:
if len(data) % 2 == 0:
return None
data = [ 1 - q for q in data ]
for x in range(len(data)):
if data[x] == 0:
result.append(x)
data[x] = 1
if x + 1 == len(data):
assert False
return None
data[x + 1] = 1 - data[x + 1]
return result
s = int(input().strip())
n = input().strip()
res = solve()
if res is None:
print(-1)
else:
print(len(res))
print(' '.join(map(str, (q + 1 for q in res))))
# 0 1 0
# 1 0 0
# 1 1 1
# 0 1 0 1 0
# 1 0 0 1 0
# 1 1 1 1 0
# 0 0 1 1 0
# 0 0 0 0 0
# 1 0 1 0 1
# 1 1 0 0 1
# 1 1 1 1 1 | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
L=input()
c1="W"
c2="B"
WC=L.count(c1)
BC=L.count(c2)
s=[x for x in L]
if WC%2==1 and BC%2==1:
print(-1)
exit()
if WC==n or BC==n:
print(0)
exit()
count1=[]
if WC%2==1 and BC%2==0:
for i in range(len(s)-1):
if s[i]==c2:
count1.append(i+1)
s[i]=c1
if s[i+1]==c1:
s[i+1]=c2
else:
s[i+1]=c1
count1=set(count1)
print(len(count1))
for x in count1:
print(x, end=' ')
print()
elif WC%2==0:
for i in range(len(s)-1):
if s[i]==c1:
count1.append(i+1)
s[i]=c2
if s[i+1]==c1:
s[i+1]=c2
else:
s[i+1]=c1
count1=set(count1)
print(len(count1))
for x in count1:
print(x, end=' ')
print()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Q2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
MScanner s = new MScanner(System.in);
int n = s.nextInt();
String str = s.next();
char[] arr = str.toCharArray();
int count_b = 0;
int count_w = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'B') {
count_b++;
} else {
count_w++;
}
}
if (count_b % 2 != 0 && count_w % 2 != 0) {
System.out.println(-1);
return;
}
if (count_b == 0 || count_w == 0) {
System.out.println(0);
return;
}
if (blackandwhite(arr, 'B', 'W', count_w, count_b)) {
return;
} else {
System.out.println(-1);
}
}
private static boolean blackandwhite(char[] arr, char c, char d, int w, int b) {
// TODO Auto-generated method stub
ArrayList<Integer> ans = new ArrayList<Integer>();
if (b % 2 != 0) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == d) {
if (arr[i + 1] == d) {
arr[i] = c;
arr[i + 1] = c;
} else {
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
ans.add(i + 1);
}
}
} else {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == c) {
if (arr[i + 1] == c) {
arr[i] = d;
arr[i + 1] = d;
} else {
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
ans.add(i + 1);
}
}
}
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] != arr[i + 1]) {
return false;
}
}
System.out.println(ans.size());
for (Integer x : ans) {
System.out.print(x + " ");
}
return true;
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[] in = new int[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[] in = new long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[] in = new int[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[] in = new long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[] in = new Integer[n];
for (int i = 0; i < n; i++)
in[i] = nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[] in = new Long[n];
for (int i = 0; i < n; i++)
in[i] = nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static void shuffle(long[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
long tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
} | 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())
out = []
if s.count("B") == n or s.count("W") == n:
print (0)
exit()
for i in range(n-1):
#Bγ«γγ
if s[i] == "W":
s[i] = "B"
out.append(i+1)
if s[i+1] == "B":
s[i+1] = "W"
else:
s[i+1] = "B"
if s.count("B") == n:
print (len(out))
print (" ".join(map(str,out)))
exit()
for i in range(n-1):
#Wγ«γγ
if s[i] == "B":
s[i] = "W"
out.append(i+1)
if s[i+1] == "W":
s[i+1] = "B"
else:
s[i+1] = "W"
if s.count("W") == n:
print (len(out))
print (" ".join(map(str,out)))
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 | /*
_.!._
/O*@*O\
<\@(_)@/>
,;, .--;` `;--. ,
O@O_ / |d b| \ _hnn
| `/ \ | | / \` |
&&&& :##;\ /;##; &&&&
| \ / `##/| |##' \ / |
\ %%%%`</| |#'`%%%% /
'._|_ \ | |' / _|_.'
_/ / \ \ \ \
/ (\( '. '-._&&&&
( ()##, o'--.._`\-)
'-():`##########'()()()
/:::::/()`Y`()\:::::\
\::::( () | () )::::/
`"""`\().'.()/'"""`
+''''`--''-..`--..__
.\ _,/:i--._`:-:+._`.``-._
/`.._,,' \ `-.``--:.b....=.
|`..__,,..`. '`.__::i--.-::_
)- .....--i'\.. --+`'''-'
,' .'.._,.-'|._-b\
/,'<' V `i| \\
|/ -|,--.." ,'-. ||\\\ |||||||||
''/ | O ' | O '\ \\\\ _/ _\
|,','| , . | \\\\\\|| ||||| ||
._,:'/ /-\ '^' -Y"\\ || ||||| ||
.|/,'| |/':: ':=:' ,'| | || ||||| ||
|+,';' /|_/ \ _/ \b':.\ \'| ,'
,,:-i''_i' | ``-.Y',. ,|`: | \;- | |_,'
__ |'| |i:'._ ,' ,' ,; | |-)-' __--:b__
.P| | |/,'|\ - ._ / / _,Y- ,:/' `. `'".._
,'|| -','' | ._i._ `':| ,..,' ,Y;' \ `- ._
|||||,.. | \ '-.._ _,' / _,b-' `. '-.
||||P..i, .| '....,-' _,'''''-''' ' _,.. `\
+'` <'/ |`-.....---' ._ ,._
| | ,'``,:-''''/,--`.
Y|.b_,,: | || ,;,Y' / |.
,' /'----' .'| .. | | '" .`Y' .,-b_....;;,.
|+|,' | | \., ' ,' `:. _ ,/__` _=: _,'``-
/ +,' | /\_........:.' '"----:::::'Y .'.| |||
|' ' .'/- \\ /'|| || | |||
||| /| \L /'|| ||/ | |||
`.| ,'/ .| / ,'||/o;/ |||
`..._,, | |/| ' |||
``-' | |, |||
| ,. | |||
,=--------.... | "" | |||
,/,'. i=..+._ ,.. '..;---:::''- | |
'/| __....b `-''`---....../.,Y'''''j:.,.._ | `._
.' _.Y.-' `.. ii:,'--------' | :-+. .| | b\
| .=_,.---'''''--...:..--:' / _..-----..:= | | '|\
| '-''`'--- ---'_,,,--'' `,.. | | \.
\ . ,' _,--'' :: _,/ ||| | \
`::b\` _,-i,-' ,..---' ,|:| | _|
`'--.:-._ ____,,,;.,'' `--._ '''''''' |'|' .' '
``'--....Y''-' `''--..._..____._____...,' | 'o-'
`''''`'''i==_+=_=i__
||'''- ' `.
`-.......-''
*/
//package cf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Three{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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 String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
// Select a particular column and the entire rows will be shifted and the selected column will be sorted while other columns will be
// rearranged accordingly
public static void sortbyColumn(long arr[][], int col)
{
Arrays.sort(arr, new Comparator<long[]>() {
@Override
public int compare(final long[] entry1,
final long[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
// Max contigious subArray sum
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
// Combination
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
// Factorial
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
{
li.add(i);
}
}
}
static void primeFactorSieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
li.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[][];
static int visited[],isit[];
static ArrayList<Integer> adj[],li;
//static int dp[][][];
static int MOD=1000000007,max=0;
static char ch[];
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static int calcdist(int r1,int c1,int r2,int c2){
if(r2-r1==c2-c1){
return isRight(r1,c1)?0:r2-r1;
}
r2-=r1-1;
c2-=c1-1;
if(isLeft(r1,c1)){
return (r2-c2)/2;
}else{
return (r2-c2+1)/2;
}
}
static boolean isLeft(int r,int c){
if((r+c)%2==0){
return true;
}
return false;
}
static boolean isRight(int r,int c){
if((r+c)%2!=0){
return true;
}
return false;
}
static int binarySearch(List<Long> ll , long x)
{
int l = 0, r = ll.size() - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (ll.get(m) == x)
return m;
// If x greater, ignore left half
if (ll.get(m) < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return l-1;
}
public static char toCh(char c)
{
int ch = Integer.parseInt(Character.toString(c));
ch = (ch%9)+1;
return Integer.toString(ch).charAt(0);
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int n = sc.nextInt();
String st = sc.next();
char s[] = new char[n];
s = st.toCharArray();
li = new ArrayList<>();
for(int i = 0;i<n-1;i++)
{
if(s[i] == 'B')
{
if(s[i+1] == 'B')
{
li.add(i+1);
s[i] = 'W';
s[i+1] = 'W';
i++;
}
else
{
li.add(i+1);
s[i] = 'W';
s[i+1] = 'B';
}
}
}
if(s[n-1] == 'B')
{
if(n%2 == 0)
w.println(-1);
else
{
for(int i = 0;i<n-1;i+=2)
li.add(i+1);
w.println(li.size());
for(int i : li)
{
w.print(i+" ");
}
}
}
else
{
w.println(li.size());
for(int i : li)
{
w.print(i+" ");
}
}
w.close();
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
int n;
cin >> n;
cin >> s;
for (auto &c : s) c = (c == 'B') ? 1 : 0;
for (int col = 0; col < 2; col++) {
string t = s;
vector<int> ans;
for (int i = 0; i + 1 < n; i++) {
if (t[i] != col) {
t[i] = col;
t[i + 1] ^= 1;
ans.push_back(i);
}
}
if (t.back() == col) {
cout << ans.size() << endl;
for (auto x : ans) cout << x + 1 << ' ';
return 0;
}
}
cout << -1 << endl;
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(input())
cnt_b = 0
cnt_w = 0
for i in range(n):
if s[i] == "B":
cnt_b += 1
if s[i] == "W":
cnt_w += 1
if cnt_b % 2 == 1 and cnt_w % 2 == 1:
print(-1)
exit()
if cnt_b % 2 == 0:
num = 0
ans = []
for i in range(n):
if s[i] == "B":
num += 1
ans.append(i + 1)
s[i] = "W"
if s[i+1] == "B":
s[i+1] = "W"
else:
s[i+1] = "B"
else:
num = 0
ans = []
for i in range(n):
if s[i] == "W":
num += 1
ans.append(i + 1)
s[i] = "B"
if s[i+1] == "B":
s[i+1] = "W"
else:
s[i+1] = "B"
print(num)
if num != 0:
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Short.parseShort;
import static java.lang.Byte.parseByte;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
public class Contest {
static int nextInt() throws IOException {
return parseInt(next());
}
static long nextLong() throws IOException {
return parseLong(next());
}
static short nextShort() throws IOException {
return parseShort(next());
}
static byte nextByte() throws IOException {
return parseByte(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens())
tok = new StringTokenizer(in.readLine());
return tok.nextToken();
}
static String nextLine() throws IOException {
return in.readLine();
}
private static BufferedReader in;
private static StringTokenizer tok;
private static PrintWriter out = new PrintWriter(System.out);
private static Utilities doThis = new Utilities();
public static void main(String[] args) throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
/*--------------------SolutionStarted-------------------*/
solve();
/*--------------------SolutionEnded--------------------*/
in.close();
out.close();
}
static int n, m,k,
count =0, length=0, sum =0,
q, t,
idx, temp;
static char[] s;
static int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
static long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE;
static int[] nums;
static void solve() throws IOException {
next();
s = next().toCharArray();
ArrayList<Integer> arr = new ArrayList<>(600);
int b= count(s,'B');
int w = count(s, 'W');
if (odd(b)&&odd(w))
bad();
char ch, temp, op;
int even;
if (even(b)) {
ch = 'B';
op = 'W';
even = b;
}
else {
ch ='W';
op = 'B';
even =w;
}
for (int i = 0; i <s.length ; i++) {
for (int j = 0; j <s.length-1 ; j++) {
if (s[i]==ch){
if (s[i]==s[i+1]) {
s[i] = s[i + 1] = op;
even-=2;
}else {
temp = s[i];
s[i] = s[i + 1];
s[i + 1] = temp;
}
arr.add(i+1);
}
if (even==0)
break;
}
}
out.println(arr.size());
for (int i = 0; i <arr.size() ; i++) {
out.print(arr.get(i)+" ");
}
}
static void binary(int num){
out.println(Integer.toBinaryString(num));
}
static boolean equals(char a, char b){
return a==b;
}
static int log2(int x){
return (int) (Math.log(x)/Math.log(2));
}
static int count(char[] s, char c){
int count = 0;
for (int i = 0; i < s.length; i++) {
if (s[i]==c)
count++;
}
return count;
}
static void swap(char[] s, int a,int b){
char temp =s[b];
s[b]= s[a];
s[a] = temp;
}
static boolean odd(int a){
return a%2==1;
}
static boolean even(int a){
return a%2==0;
}
static void yes(){
out.println("YES");
out.close();
System.exit(0);
}
static void no() {
out.println("NO");
out.close();
System.exit(0);
}
static void bad(){
out.println(-1);
out.close();
System.exit(0);
}
}
class CharPair{
char ch;
int value;
CharPair(char c, int v){
ch =c; value=v;
}
char get(){
return ch;
}
}
class IntPair implements Comparable<IntPair> {
int teamID, points;
IntPair(int teamID, int points) {
this.teamID = teamID;
this.points = points;
}
@Override
public int compareTo(IntPair o) {
if (teamID == o.teamID)
return 0;
if (points == o.points)
return o.teamID - teamID;
return points - o.points;
}
public String toString() {
return teamID + " " + points;
}
}
class BooleanPair {
boolean key, value;
BooleanPair() {
}
BooleanPair(boolean key, boolean value) {
this.key = key;
this.value = value;
}
}
class Utilities {
//int arrays start
int lowerBound(int[] arr, int low, int high, int element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (element > arr[middle])
low = middle + 1;
else
high = middle;
}
if (arr[low] < element)
return -1;
return low;
}
int upperBound(int[] arr, int low, int high, int element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (arr[middle] > element)
high = middle;
else
low = middle + 1;
}
if (arr[low] <= element)
return -1;
return low;
}
//int arrays end
//long arrays start
int lowerBound(long[] arr, int low, int high, long element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (element > arr[middle])
low = middle + 1;
else
high = middle;
}
if (arr[low] < element)
return -1;
return low;
}
int upperBound(long[] arr, int low, int high, long element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (arr[middle] > element)
high = middle;
else
low = middle + 1;
}
if (arr[low] <= element)
return -1;
return low;
}
//long arrays end
//double arrays start
int lowerBound(double[] arr, int low, int high, double element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (element > arr[middle])
low = middle + 1;
else
high = middle;
}
if (arr[low] < element)
return -1;
return low;
}
int upperBound(double[] arr, int low, int high, double element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (arr[middle] > element)
high = middle;
else
low = middle + 1;
}
if (arr[low] <= element)
return -1;
return low;
}
//double arrays end
}
class Bitmasks{
static byte andBitmask(byte state, byte... masks){
byte temp=0;
for (int i = 0; i < masks.length; i++) {
temp|=masks[i];
}
return (byte) (state&temp);
}
static byte toggleBitmask(byte state ,byte... masks){
return state;
}
static byte removeBitmask(byte state, byte... masks){
return state;
}
static byte addBitmask(byte state ,byte... masks){
return state;
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static int check(char[] ch) {
int B = 0, W = 0;
for (int i = 0; i < ch.length; i++)
if (ch[i] == 'B') B++;
else W++;
if (B == 0 || W == 0) return 1;
return 0;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()); ;
String s = br.readLine();
char[] ch = s.toCharArray();
int l = s.length();
int B = 0, W = 0;
for (int i = 0; i < l; i++) {
if (s.charAt(i) == 'B') B++;
else W++;
}
if (W == 0 || B == 0) System.out.println(0);
else if (B % 2 != 0 && W % 2 != 0) System.out.println(-1);
else {
int cnt1 = 0;
List<Integer> list1 = new ArrayList<>();
int z = 0;
while (check(ch) != 1) {
z++;
if (z > 50) break;
for (int i = 0; i < l; i++) {
if (ch[i] == 'W' && i != l - 1) {
ch[i] = 'B';
if (ch[i + 1] == 'B') ch[i + 1] = 'W';
else ch[i + 1] = 'B';
cnt1++;
list1.add(i + 1);
}
}
}
while (check(ch) != 1) {
for (int i = 0; i < l; i++) {
if (ch[i] == 'B' && i != l - 1) {
ch[i] = 'W';
if (ch[i + 1] == 'B') ch[i + 1] = 'W';
else ch[i + 1] = 'B';
cnt1++;
list1.add(i + 1);
}
}
}
System.out.println(cnt1);
for (int i = 0; i < cnt1; i++) {
if (i != 0) System.out.print(" ");
System.out.print(list1.get(i));
}
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class CF1271B {
static class Pair{
int a;
int b;
Pair(int a,int b){
this.a = a;
this.b = b;
}
}
public static void main(String[] args) {
FastReader input = new FastReader();
int n = input.nextInt();
String s = input.nextLine();
char[] arr = s.toCharArray();
ArrayList<Pair> list = new ArrayList<Pair>();
for(int i = 0;i < arr.length-2;i++){
if(arr[i+1] != arr[i]){
arr[i+1] = arr[i];
if(arr[i+2] == 'W'){
arr[i+2] = 'B';
}
else{
arr[i+2] = 'W';
}
list.add(new Pair(i+2,i+3));
}
}
for(int i = arr.length-1;i > 1;i--){
if(arr[i-1] != arr[i]){
arr[i-1] = arr[i];
if(arr[i-2] == 'W'){
arr[i-2] = 'B';
}
else{
arr[i-2] = 'W';
}
list.add(new Pair(i-2+1,i-1+1));
}
}
for(int i = 0;i < arr.length-1;i++){
if(arr[i] != arr[i+1]){
System.out.println(-1);
return;
}
}
System.out.println(list.size());
for(Pair p : list){
System.out.print(p.a + " ");
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
scan in = new scan(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BBlocks solver = new BBlocks();
solver.solve(1, in, out);
out.close();
}
static class BBlocks {
public void solve(int testNumber, scan in, PrintWriter out) {
int t = in.scanInt();
StringBuilder wstr = new StringBuilder(in.scanString());
StringBuilder bstr = new StringBuilder(wstr);
boolean flag = true;
int step = 0;
ArrayList<Integer> answer = new ArrayList<>();
//white
int i = 0;
while (flag) {
if (step > 3 * t) {
flag = false;
} else {
if (check(wstr)) {
print(answer, out);
return;
} else {
while (wstr.charAt(i) == 'W') {
i++;
}
if (i < wstr.length() - 1) {
wstr.setCharAt(i, 'W');
answer.add(i + 1);
if (wstr.charAt(i + 1) == 'B') {
wstr.setCharAt(i + 1, 'W');
} else if (wstr.charAt(i + 1) == 'W') {
wstr.setCharAt(i + 1, 'B');
}
i = (i + 1) % wstr.length();
step++;
} else {
step++;
}
}
}
}
flag = true;
step = 0;
answer = new ArrayList<>();
//black
i = 0;
while (flag) {
if (step > 3 * t) {
flag = false;
} else {
if (check(bstr)) {
print(answer, out);
return;
} else {
while (bstr.charAt(i) == 'B') {
i++;
}
if (i < bstr.length() - 1) {
bstr.setCharAt(i, 'B');
answer.add(i + 1);
if (bstr.charAt(i + 1) == 'W') {
bstr.setCharAt(i + 1, 'B');
} else if (bstr.charAt(i + 1) == 'B') {
bstr.setCharAt(i + 1, 'W');
}
i = (i + 1) % bstr.length();
step++;
} else {
step++;
}
}
}
}
out.print("-1");
}
void print(ArrayList<Integer> ans, PrintWriter out) {
out.println(ans.size());
for (int i = 0; i < ans.size(); i++) {
out.print(ans.get(i) + " ");
}
}
boolean check(StringBuilder t) {
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) != t.charAt(0)) {
return false;
}
}
return true;
}
}
static class scan {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public scan(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0)
return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
// else throw new InputMismatchException();
}
return neg * integer;
}
public String scanString() {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n)) {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
}
| 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, i;
cin >> n;
string s;
cin >> s;
vector<int> ans;
for (i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
ans.push_back(i);
s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W');
}
}
if (s[n - 1] == 'B') {
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " ";
} else if (n % 2 == 1) {
for (i = 0; i < n - 1; i += 2) ans.push_back(i);
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++) cout << ans[i] + 1 << " ";
} else {
cout << "-1";
}
ans.clear();
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 itertools import product
n=int(input())
s=list(input())
d={'B':0,'W':1}
c1,c2=0,0
for i in range(len(s)):
if s[i]=='B':c1+=1
else:c2+=1
s[i]=d[s[i]]
ans=[]
if c1&1 and c2&1:
print(-1)
else:
for i in reversed(range(1,len(s)-1)):
if s[i]!=s[i+1]:
s[i]^=1
s[i-1]^=1
ans.append(i)
if s[0]!=s[1]:
for i in range(2,len(s),2):
ans.append(i)
if not ans:
print(0)
else:
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | N = int(input())
S_raw = input()
S = [0] * N
for i in range(N):
if S_raw[i] == 'B':
S[i] = 1
S_ori = S[:]
ans = []
for i in range(N-1):
if S[i] == 1:
S[i] = 0
S[i+1] = 1 - abs(S[i+1])
ans.append(i+1)
if S[-1] == 0:
print(len(ans))
print(*ans)
exit()
S = S_ori[:]
ans = []
for i in range(N-1):
if S[i] == 0:
S[i] = 1
S[i+1] = 1 - abs(S[i+1])
ans.append(i+1)
if S[-1] == 1:
print(len(ans))
print(*ans)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
k = list(input())
b = k.count('B')
t = n-b
count = 0
a=[]
if(b%2 != 0 and t%2!=0):
print(-1)
elif(b==0 or t==0):
print(0)
else:
c=""
q = 1
if(b%(1+1)==0):
while q<n:
if(k[q] == 'B' and k[q-1]=='B'):
k[q] = k[q-1] = 'W'
c+=str(q)+" "
q+=1
count+=1
elif(k[q-1] == 'B' and k[q] == "W"):
k[q-1] = "W"
k[q] = 'B'
c+=str(q)+" "
q+=1
count+=1
elif(k[q] == 'W' and k[q-1] == "W"):
q+=1
else:
q+=1
else:
while q<n:
if(k[q] == 'W' and k[q-1]=='W'):
k[q] = k[q-1] = 'B'
c+=str(q)+" "
q+=1
count+=1
elif(k[q] == 'B' and k[q-1] == "W"):
k[q] = "W"
k[q-1] = 'B'
c+=str(q)+" "
q+=1
count+=1
elif(k[q] == 'B' and k[q-1] == "B"):
q+=1
else:
q+=1
print(count)
print(c) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
public class Hello {
public static long primeFactors(int n)
{
long c = 0;
// Print the number of 2s that divide n
while (n % 2 == 0) {
c++;
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
c++;
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
c++;
return c;
}
static long A = 1000000000+7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i] = (s.charAt(i)=='B')?0:1;
}
int c1 = 0;
String res = "";
for(int i=0;i<n-1;i++) {
if(arr[i]==1 && arr[i+1]==1) {
arr[i]=0;
arr[i+1]=0;
c1++;
res+=(i+1)+" ";
}else if(arr[i]==1 && arr[i+1]==0) {
arr[i] =0;
arr[i+1] = 1;
res+=(i+1)+" ";
c1++;
}
}
if(arr[arr.length-1]==0) {
System.out.println(c1);
System.out.println(res);
return;
}
for(int i=0;i<n;i++) {
arr[i] = (s.charAt(i)=='W')?0:1;
}
int c2 = 0;
String res2 = "";
for(int i=0;i<n-1;i++) {
if(arr[i]==1 && arr[i+1]==1) {
arr[i]=0;
arr[i+1]=0;
c2++;
res2+=(i+1)+" ";
}else if(arr[i]==1 && arr[i+1]==0) {
arr[i] =0;
arr[i+1] = 1;
res2+=(i+1)+" ";
c2++;
}
}
if(arr[arr.length-1]==0) {
System.out.println(c2);
System.out.println(res2);
}else {
System.out.println(-1);
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
char[] arr=sc.next().toCharArray();
int w=0,b=0;
for(int i=0;i<n;i++)
{
if(arr[i]=='B')
b++;
else
w++;
}
if(w%2==1&&b%2==1)
System.out.println("-1");
else
{
StringBuilder str=new StringBuilder();
int k=0;
if(w%2==0)
{
for(int i=0;i<n;)
{
if(arr[i]=='W')
{
for(int j=i+1;j<n;j++)
{
k++;
str.append(j+" ");
if(arr[j]=='W')
{
i=j+1;
break;
}
}
}
else
i++;
}
}
else if(b%2==0)
{
for(int i=0;i<n;)
{
if(arr[i]=='B')
{
for(int j=i+1;j<n;j++)
{
k++;
str.append(j+" ");
if(arr[j]=='B')
{
i=j+1;
break;
}
}
}
else
i++;
}
}
System.out.println(k+"\n"+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 = input()
s = [l[i:i+1] for i in range(len(l))]
path = []
temp = 'B'
for i in range(len(s)):
if i<n-1:
if s[i]!=temp:
path.append(i)
if s[i+1]!=temp:
s[i+1] = temp
else:
s[i+1] = 'W'
s[i] = temp
flag = 0
for i in range(len(s)):
if i<n-1:
if s[i]!=s[i+1]:
flag = 1
break
if(flag==0):
print(len(path))
for i in path:
print(i+1,end=" ")
else:
temp = 'W'
for i in range(len(s)):
if i<n-1:
if s[i]!=temp:
path.append(i)
if s[i+1]!=temp:
s[i+1] = temp
else:
s[i+1] = 'B'
s[i] = temp
flag = 0
for i in range(len(s)):
if i<n-1:
if s[i]!=s[i+1]:
flag = 1
break
if(flag==0):
print(len(path))
for i in path:
print(i+1,end=" ")
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
string s, t;
cin >> s;
t = s;
vector<long long int> ans;
for (__typeof(n - 1) i = (0) - ((0) > (n - 1));
i != (n - 1) - ((0) > (n - 1)); i += 1 - 2 * ((0) > (n - 1))) {
if (t[i] != 'B') {
t[i] = 'B';
if (t[i + 1] == 'B')
t[i + 1] = 'W';
else
t[i + 1] = 'B';
ans.push_back(i + 1);
}
}
if (t[n - 1] == 'W') {
ans.clear();
for (__typeof(n - 1) i = (0) - ((0) > (n - 1));
i != (n - 1) - ((0) > (n - 1)); i += 1 - 2 * ((0) > (n - 1))) {
if (s[i] != 'W') {
s[i] = 'W';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
ans.push_back(i + 1);
}
}
if (s[n - 1] == 'B') {
cout << -1;
return;
}
}
cout << ans.size() << '\n';
for (auto it : ans) cout << it << " ";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int T;
T = 1;
while (T--) {
solve();
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
vector<long long> stone;
long long pow_of_10(long long x) {
long long result = 1;
for (long long i = 1; i < x + 1; i++) {
result *= 10;
}
return result;
}
long long c;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool ispalin(string second) {
string t = second;
reverse(second.begin(), second.end());
if (second == t) {
return true;
}
return false;
}
long long yess(long long n, long long m, long long k, long long i,
long long j) {
while (i <= n) {
cout << m - j + 1 << " ";
while (j <= m) {
cout << i << " " << j << " ";
j++;
}
cout << "\n";
j = 1;
i++;
}
return 0;
}
long long power(long long base, long long exp, long long b) {
long long ans = 1;
while (exp > 0) {
if (exp % 2) {
ans *= base;
ans %= b;
}
base *= base;
base %= b;
exp /= 2;
}
return ans;
}
const int MAX = 1e6 + 1;
bool prime[MAX + 1];
void sieve1() {
for (int i = 0; i <= MAX; i++) prime[i] = 1;
prime[0] = prime[1] = 0;
for (int i = 4; i <= MAX; i += 2) prime[i] = 0;
for (int p = 3; p * p <= MAX; p += 2) {
if (prime[p] == 1)
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = 0;
}
}
}
long long findSum(string str) {
long long n = str.length();
long long x = 0;
for (long long i = n - 1; i >= 0; i--) {
x += (str[i] - '0');
}
return x;
}
long long power(long long base, long long exp) {
long long ans = 1;
while (exp > 0) {
if (exp % 2) {
ans *= base;
ans %= 1000000007;
}
base *= base;
base %= 1000000007;
exp /= 2;
}
return ans % 1000000007;
}
long long fact[1000006];
const auto MX = 300001;
long long nCr(long long n, long long r) {
long long nn = fact[n] % 1000000007;
long long rr = power(fact[r], 1000000007 - 2) % 1000000007;
long long rn = power(fact[n - r], 1000000007 - 2) % 1000000007;
return (((nn * rr) % 1000000007) * rn) % 1000000007;
}
long long divi(long long a, long long b) { return (a + b - 1) / b; }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long solve(long long p) {
long long n;
cin >> n;
string second;
cin >> second;
string t = second;
vector<long long> v;
for (long long i = 0; i < n - 1; i++) {
if (second[i] == 'W') {
second[i] = 'B';
v.push_back(i);
if (second[i + 1] == 'W') {
second[i + 1] = 'B';
} else {
second[i + 1] = 'W';
}
}
}
if (second[n - 1] == 'B') {
cout << v.size() << "\n";
for (auto x : v) {
cout << x + 1 << " ";
}
return 0;
}
v.clear();
second = t;
for (long long i = 0; i < n - 1; i++) {
if (second[i] == 'B') {
second[i] = 'W';
v.push_back(i);
if (second[i + 1] == 'B') {
second[i + 1] = 'W';
} else {
second[i + 1] = 'B';
}
}
}
if (second[n - 1] == 'W') {
cout << v.size() << "\n";
for (auto x : v) {
cout << x + 1 << " ";
}
return 0;
}
cout << "-1\n";
return 0;
return 0;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long flag = 0;
if (flag) {
long long t;
cin >> t;
long long i = 1;
while (t--) {
solve(i);
i++;
}
} else {
solve(1);
}
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
public class Codeforces {
public static void main(String[] args) {
Scanner s = new Scanner(System. in);
// int t = 1;
// while(t>0) {
int n=s.nextInt();
String st=s.next();
char a[]=st.toCharArray();
ArrayList<Integer> a1=new ArrayList<>();
for(int i=0;i<n-1;i++){
if(a[i]=='B'){
a[i]='W';a[i+1]=(a[i+1]=='B')?'W':'B';
a1.add(i+1);
}
}
if(a[n-2]==a[n-1]){
System.out.println(a1.size());
for(int i=0;i<a1.size();i++)
System.out.print(a1.get(i)+" ");
System.out.println();return;
}
a=st.toCharArray();
a1=new ArrayList<>();
for(int i=0;i<n-1;i++){
if(a[i]=='W'){
a[i]='B';a[i+1]=(a[i+1]=='W')?'B':'W';
a1.add(i+1);
}
}
if(a[n-2]==a[n-1]){
System.out.println(a1.size());
for(int i=0;i<a1.size();i++)
System.out.print(a1.get(i)+" ");
System.out.println();return;
}
System.out.println(-1);
// }
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
def main():
nos = int(input())
p = input()
pattern = list(p)
bcount = pattern.count('B')
wcount = pattern.count('W')
if bcount%2 != 0 and wcount%2 != 0:
print(-1)
return 0
count = 0
steps = []
if bcount%2 == 0:
i = 0
while i< len(pattern)-1 :
if (pattern[i] == 'B' and pattern[i+1] == 'W') :
count += 1
steps.append(i+1)
temp = pattern[i]
pattern[i] = pattern[i+1]
pattern[i+1] = temp
i += 1
elif pattern[i] == pattern[i+1]:
i+=2
else:
i +=1
i = 0
for i in range(len(pattern)-1):
if (pattern[i] == 'B' and pattern[i+1] == 'B'):
count += 1
pattern[i] = 'W'
pattern[i+1] = 'W'
steps.append(i+1)
else:
i = 0
while i< len(pattern)-1 :
if (pattern[i] == 'W' and pattern[i+1] == 'B'):
count += 1
steps.append(i+1)
temp = pattern[i]
pattern[i] = pattern[i+1]
pattern[i+1] = temp
i += 1
elif pattern[i] == pattern[i+1]:
i+=2
else:
i +=1
for i in range(len(pattern)-1):
if (pattern[i] == 'W' and pattern[i+1] == 'W'):
count += 1
pattern[i] = 'B'
pattern[i+1] = 'B'
steps.append(i+1)
print(count)
if count>0:
for i in steps:
print(i, end = " ")
print("")
#print(pattern)
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 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int i, n, cnt = 0;
cin >> n;
string s;
cin >> s;
vector<int> ans;
for (i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] == 'W' ? (s[i + 1] = 'B') : (s[i + 1] = 'W');
cnt++;
ans.push_back(i + 1);
} else {
continue;
}
}
if (s[i] == 'W') {
cout << cnt << endl;
for (int u : ans) cout << u << " ";
} else {
for (i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] == 'B' ? (s[i + 1] = 'W') : (s[i + 1] = 'B');
cnt++;
ans.push_back(i + 1);
}
}
if (s[i] == 'B') {
cout << cnt << endl;
for (int u : ans) cout << u << " ";
} else {
cout << -1;
}
}
}
int main() {
int t = 1;
while (t--) {
solve();
cout << endl;
}
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
blocks = str(input())
count_b = sum([1 for i in blocks if i == 'B'])
count_w = sum([1 for i in blocks if i == 'W'])
if count_b % 2 != 0 and count_w % 2 != 0:
print(-1)
elif count_b == 0 or count_w == 0:
print(0)
else:
ans = []
blocks = list(blocks)
blocks += '1'
while count_w > 1:
for idx, each in enumerate(blocks):
next_each = idx + 1
if each == 'W' and blocks[next_each] != '1':
blocks[idx] = 'B'
blocks[next_each] = 'W' if blocks[next_each] == 'B' else 'B'
ans.append(str(next_each))
break
# print(blocks)
count_w = sum([1 for i in blocks if i == 'W'])
if count_w >= 1:
while count_b > 1:
for idx, each in enumerate(blocks):
next_each = idx + 1
if each == 'B' and blocks[next_each] != '1':
blocks[idx] = 'W'
blocks[next_each] = 'B' if blocks[next_each] == 'W' else 'W'
ans.append(str(next_each))
break
# print(blocks)
count_b = sum([1 for i in blocks if i == 'B'])
print(len(ans))
print(" ".join(ans))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | # ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
def main():
n = int(input())
sa = input()
s = list(sa)
# print(s)
if len(set(s)) == 1:
print(0)
return
a = []
for i in range(n - 1):
if s[i] == 'W' and s[i + 1] == 'B':
s[i] = 'B'
s[i + 1] = 'W'
a.append(i + 1)
elif s[i] == 'W' and s[i + 1] == 'W':
a.append(i + 1)
s[i] = 'B'
s[i + 1] = 'B'
if len(set(s)) == 1:
print(len(a))
print(*a)
return
s = list(sa)
a = []
for i in range(n - 1):
if s[i] == 'B' and s[i + 1] == 'W':
s[i] = 'W'
s[i + 1] = 'B'
a.append(i + 1)
elif s[i] == 'B' and s[i + 1] == 'B':
a.append(i + 1)
s[i] = 'W'
s[i + 1] = 'W'
if len(set(s)) == 1:
print(len(a))
print(*a)
return
print(-1)
return
if __name__ == "__main__":
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | t=int(input())
l=list(input())
B=l.count('B')
W=l.count('W')
ll=[]
c=l.copy()
if t%2==0:
if B%2!=0 or W%2!=0:
print("-1")
else:
if W==t or B==t:print(0)
else:
for i in range(t-1):
if l[i]!='W':
ll.append(i+1)
if l[i+1]=='B':l[i+1]='W'
else:l[i+1]='B'
if l[-1]=='B':
ll=[]
for i in range(t-1):
if c[i]!='B':
ll.append(i+1)
if c[i+1]=='W':l[i+1]='B'
else:c[i+1]='W'
print(len(ll))
print(*ll)
else:
if W==t or B==t:print(0)
else:
for i in range(t-1):
if l[i]!='W':
ll.append(i+1)
if l[i+1]=='B':l[i+1]='W'
else:l[i+1]='B'
if l[t-1]=='B':
ll=[]
for j in range(t-1):
if c[j]!='B':
ll.append(j+1)
if c[j+1]=='B':c[j+1]='W'
else:c[j+1]='B'
print(len(ll))
print(*ll)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a;
cin >> a;
string s;
cin >> s;
int len = s.size();
vector<int> v;
int countb = 0;
for (int i = 0; i < len; i++) {
if (s[i] == 'B') {
countb++;
}
}
int countw = len - countb;
char flag;
if (countb == 0 || countw == 0) {
cout << 0;
return 0;
}
if (countb % 2 != 0 && countw % 2 != 0) {
cout << -1;
return 0;
} else if (countb % 2 == 0) {
flag = 'W';
} else {
flag = 'B';
}
int countt = 0;
for (int i = 0; i < len - 1; i++) {
if (s[i] != flag) {
countt++;
v.push_back(i);
if (s[i + 1] != flag) {
i++;
} else {
s[i + 1] = s[i];
}
}
}
cout << countt << endl;
auto itr = v.begin();
while (itr != v.end()) {
cout << *itr + 1 << " ";
itr++;
}
}
| 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 block;
cin >> block;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
b += block[i] == 'B';
w += block[i] == 'W';
}
if (b % 2 && w % 2) {
cout << -1;
} else {
char change = 'B', to = 'W';
if (b % 2) {
change = 'W';
to = 'B';
}
vector<int> op;
for (int i = 0; i + 1 < n; i++) {
if (block[i] == change) {
op.push_back(i + 1);
if (block[i + 1] == change)
block[i + 1] = to;
else
swap(block[i], block[i + 1]);
}
}
int sz = op.size();
cout << sz << "\n";
for (int i = 0; i < sz; i++) cout << op[i] << " ";
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
string choices = "BW";
for (int c = 0; c < 2; c++) {
string s2 = s;
vector<int> res;
for (int c2 = 1; c2 < s2.size(); c2++) {
if (s2[c2 - 1] != choices[c]) {
s2[c2 - 1] = choices[c];
if (s2[c2] == 'B')
s2[c2] = 'W';
else
s2[c2] = 'B';
res.push_back(c2);
}
}
bool valid = true;
for (int c2 = 0; c2 < s2.size(); c2++) {
if (s2[c2] != choices[c]) {
valid = false;
break;
}
}
if (valid) {
cout << res.size() << endl;
for (int c = 0; c < res.size(); c++) cout << res[c] << " ";
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 | n = int(input())
string = input()
sequence = [0] * n
for i in range(n):
if string[i] == 'B':
sequence[i] = -1
else:
sequence[i] = 1
def Try(char, seq):
commands = []
count = 0
for i in range(n-1):
if seq[i] != char:
seq[i] = char
seq[i+1] *= -1
commands.append(i+1)
count += 1
if seq[-1] != char:
if n % 2 == 0:
return None
else:
for j in range(1, n-1, 2):
count += 1
commands.append(j)
return [count, commands]
else:
return [count, commands]
s2 = sequence.copy()
r = Try(1, sequence)
if not r:
r = Try(-1, s2)
if not r:
print(-1)
else:
print(r[0])
for rr in r[1]:
print(rr, end=' ')
else:
if r[0] > n:
r2 = Try(-1, s2)
if r2:
r = r2
print(r[0])
for rr in r[1]:
print(rr, 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.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[] a1 = sc.next().substring(0, n).toCharArray();
char[] c = {'B', 'W'};
char[] a2;
int k;
int[] s = new int[n];
for (int i = 0; i < c.length; i++) {
k = 0;
a2 = Arrays.copyOf(a1, n);
for (int j = 0; j < n - 1; j++) {
if (a2[j] != c[i]) {
a2[j] = a2[j] == 'B' ? 'W' : 'B';
a2[j + 1] = a2[j + 1] == 'B' ? 'W' : 'B';
s[k] = j + 1;
k++;
}
}
if (a2[n - 1] == c[i]) {
System.out.println(k);
for (int j = 0; j < k; j++) {
System.out.printf("%d ", s[j]);
}
break;
} else if (i == c.length - 1) {
System.out.println(-1);
}
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int w = 0, b = 0, k = 0, i, j, d = 0;
vector<int> v;
char a;
for (i = 0; i < s.size(); i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (w % 2 != 0 && b % 2 != 0)
cout << -1 << endl;
else if (w == s.size() || b == s.size())
cout << 0 << endl;
else {
for (i = 1; i + 1 < s.size(); i++) {
if (s[i] != s[i - 1]) {
if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = s[i + 1] = 'B';
k++;
} else if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = s[i + 1] = 'W';
k++;
} else {
swap(s[i], s[i + 1]);
k++;
}
v.push_back(i);
}
}
if (s[i] != s[i - 1]) {
k = k + i / 2;
j = 0;
while (j < i) {
v.push_back(j);
j = j + 2;
}
}
cout << k << endl;
for (i = 0; i < k; i++) cout << v[i] + 1 << " ";
}
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.