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 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class experiment
{
static int M = 1_000_000_007;
static int INF = 2_000_000_000;
static final FastScanner fs = new FastScanner();
//variable
public static void main(String[] args) throws IOException {
int n = fs.nextInt();
char[] s = fs.next().toCharArray();
int nw = 0, nb = 0;
ArrayList<Pairs> even = new ArrayList<>();
ArrayList<Pairs> odd = new ArrayList<>();
ArrayList<Integer> ans = new ArrayList<>();
for(int i=0;i<n;i++){
if(s[i] == 'W') nw++;
else nb++;
}
boolean w = false;
if(nw % 2 ==0) w = true;
else if(nb % 2 == 1) {
System.out.println("-1");
return;
}
char find = 'B';
if(w) find = 'W';
for(int i=0;i<n;i++) {
int j = i;
while(j<n && s[j] == find) {
j++;
}
if(j!=i){
j--;
if((j-i+1)%2 == 0) even.add(new Pairs(i,j));
else odd.add(new Pairs(i,j));
}
i = j;
}
for(int i=0;i<even.size();i++) {
int i1 = even.get(i).value;
int j1 = even.get(i).index;
while(i1<j1) {
ans.add(i1);
i1+=2;
}
}
even.clear();
for(int i=0;i<odd.size();i+=2) {
int j1 = odd.get(i).index;
int i2 = odd.get(i+1).value;
while(j1<i2-1) {
ans.add(j1);
j1++;
}
int i1 = odd.get(i).value;
int j2 = odd.get(i+1).index;
if(i1 != odd.get(i).index) even.add(new Pairs(i1,odd.get(i).index-1));
even.add(new Pairs(i2-1,j2));
}
for(int i=0;i<even.size();i++) {
int i1 = even.get(i).value;
int j1 = even.get(i).index;
while(i1<j1) {
ans.add(i1);
i1+=2;
}
}
System.out.println(ans.size());
for(int i:ans) System.out.print((i+1)+" ");
}
//class
//function
// Template
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
if(a==0) return b;
return gcd(b%a,a);
}
static void premutation(int n, ArrayList<Integer> arr,boolean[] chosen) {
if(arr.size() == n) {
}else {
for(int i=1; i<=n; i++) {
if(chosen[i]) continue;
arr.add(i);
chosen[i] = true;
premutation(n,arr,chosen);
arr.remove(i);
chosen[i] = false;
}
}
}
static boolean isPalindrome(char[] c) {
int n = c.length;
for(int i=0; i<n/2; i++) {
if(c[i] != c[n-i-1]) return false;
}
return true;
}
static long nCk(int n, int k) {
return (modMult(fact(n),fastexp(modMult(fact(n-k),fact(k)),M-2)));
}
static long fact (long n) {
long fact =1;
for(int i=1; i<=n; i++) {
fact = modMult(fact,i);
}
return fact%M;
}
static int modMult(long a,long b) {
return (int) (a*b%M);
}
static int negMult(long a,long b) {
return (int)((a*b)%M + M)%M;
}
static long fastexp(long x, int y){
if(y==1) return x;
if(y == 0) return 1;
long ans = fastexp(x,y/2);
if(y%2 == 0) return modMult(ans,ans);
else return modMult(ans,modMult(ans,x));
}
static final Random random = new Random();
static void ruffleSort(int[] arr)
{
int n = arr.length;
for(int i=0; i<n; i++)
{
int j = random.nextInt(n),temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
public static class Pairs implements Comparable<Pairs>
{
int value,index;
Pairs(int value, int index)
{
this.value = value;
this.index = index;
}
public int compareTo(Pairs p)
{
return Integer.compare(this.value,p.value);
}
}
}
class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer("");
String next() throws IOException
{
while(!str.hasMoreTokens())
str = new StringTokenizer(br.readLine());
return str.nextToken();
}
char nextChar() throws IOException {
return next().charAt(0);
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
float nextfloat() throws IOException
{
return Float.parseFloat(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
byte nextByte() throws IOException
{
return Byte.parseByte(next());
}
int [] arrayIn(int n) throws IOException
{
int[] arr = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = nextInt();
}
return arr;
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.io.*;
import java.text.*;
public class Main{
//SOLUTION BEGIN
//Into the Hardware Mode
void pre() throws Exception{}
void solve(int TC)throws Exception{
int n = ni();
char[] ch = n().toCharArray();
int c = 0;
for(int i = 0; i< n; i++)if(ch[i] == 'W')c++;
if(c%2 == 1 && (n-c)%2 == 1)pn(-1);
else{
ArrayList<Integer> ans = new ArrayList<>();
if(c%2 == 0){
//all B
for(int i = 0; i< n-1; i++){
if(ch[i] == 'W'){
ans.add(1+i);
ch[i] = 'B';
ch[i+1] = (char)(-ch[i+1]+'W'+'B');
}
}
hold(ch[n-1] == 'B');
}else{
//all W
for(int i = 0; i< n-1; i++){
if(ch[i] == 'B'){
ans.add(1+i);
ch[i] = 'W';
ch[i+1] = (char)(-ch[i+1]+'W'+'B');
}
}
hold(ch[n-1] == 'W');
}
pn(ans.size());
for(int i:ans)p(i+" ");pn("");
}
}
//SOLUTION END
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
void exit(boolean b){if(!b)System.exit(0);}
long IINF = (long)1e18;
final int INF = (int)1e9+2, MX = (int)1e5+5;
DecimalFormat df = new DecimalFormat("0.00000000000");
double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-7;
static boolean multipleTC = false, memory = false, fileIO = false;
FastReader in;PrintWriter out;
void run() throws Exception{
if(fileIO){
in = new FastReader("C:/users/user/desktop/inp.in");
out = new PrintWriter("C:/users/user/desktop/out.out");
}else {
in = new FastReader();
out = new PrintWriter(System.out);
}
//Solution Credits: Taranpreet Singh
int T = (multipleTC)?ni():1;
pre();
for(int t = 1; t<= T; t++)solve(t);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
else new Main().run();
}
int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str = "";
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const ll mod = 1e9 + 7;
const ll N = 1e5 + 8;
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
string s;
cin >> s;
for (auto &c : s) {
if (c == 'W')
c = '1';
else
c = '0';
}
for (int color = 0; color < 2; color++) {
string t = s;
vector<int> ans;
for (int i = 0; i < n - 1; i++) {
if (t[i] != color + '0') {
if (t[i] == '0')
t[i] = '1';
else
t[i] = '0';
if (t[i + 1] == '0')
t[i + 1] = '1';
else
t[i + 1] = '0';
ans.push_back(i + 1);
}
}
if (t.back() - '0' == color) {
cout << ans.size() << endl;
for (auto i : ans) {
cout << i << ' ';
}
cout << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math,datetime,functools
from collections import deque,defaultdict,OrderedDict
import collections
def allblack(s,n,w):
i=0
ans=[]
while i<n-1:
if s[i]=='W':
if s[i+1]=='W':
ans.append(i+1)
w-=2
i+=2
else:
ans.append(i+1)
s[i],s[i+1]=s[i+1],s[i]
i+=1
else:
if s[i+1]=='B':
i+=2
else:
i+=1
if w==0:
return ans
else:
return [-1]
def allwhite(s,n,b):
i=0
ans=[]
while i<n-1:
if s[i]=='B':
if s[i+1]=='B':
ans.append(i+1)
b-=2
i+=2
else:
ans.append(i+1)
s[i],s[i+1]=s[i+1],s[i]
i+=1
else:
if s[i+1]=='W':
i+=2
else:
i+=1
# print(b,i)
if b==0:
return ans
else:
return [-1]
def main():
starttime=datetime.datetime.now()
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
#Solving Area Starts-->
zz=0
for _ in range(1):
n=ri()
s=rs()
b,w=0,0
a=['.']*n
for i in range(n):
if s[i]=='B':
b+=1
a[i]='B'
else:
w+=1
a[i]='W'
if b==n or w==n:
print(0)
else:
aa=a.copy()
ab=a.copy()
aw=allwhite(aa,n,b)
ab=allblack(ab,n,w)
# print(aw)
# print(ab)
if aw[0]!=-1:
print(len(aw))
print(*aw)
elif ab[0]!=-1:
print(len(ab))
print(*ab)
else:
print(-1)
#<--Solving Area Ends
endtime=datetime.datetime.now()
time=(endtime-starttime).total_seconds()*1000
if(os.path.exists('input.txt')):
print("Time:",time,"ms")
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024 * 8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
import java.util.*;
public class cagon {
public static void main(String[] args) {
ArrayList<Integer> caca = new ArrayList<Integer>();
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
StringBuilder m = new StringBuilder(scn.next());
if(n==1) {System.out.println(0);System.exit(0);}
for(int i = 1;i<m.length()-1;i++) {
if(m.charAt(i)!=m.charAt(i-1)) {
caca.add(i+1);
m.setCharAt(i, m.charAt(i-1));
if(m.charAt(i+1)=='W') m.setCharAt(i+1, 'B');
else m.setCharAt(i+1, 'W');
}
}
if(caca.size()==0&m.charAt(m.length()-1)==m.charAt(m.length()-2))System.out.println(0);
else if(m.length()%2==0&m.charAt(m.length()-1)!=m.charAt(m.length()-2)) {
System.out.println(-1);
} else if(m.length()%2==0) {
System.out.println(caca.size());
for(int i = 0;i<caca.size();i++) {
if(i==0)System.out.print(caca.get(i));
else System.out.print(" "+caca.get(i));
if(i==caca.size()-1)System.out.println();
}
} else if(m.length()%2==1&m.charAt(m.length()-1)!=m.charAt(m.length()-2)) {
for(int i = 1;i<n;i++) {
if(i%2==1)caca.add(i);
}
System.out.println(caca.size());
for(int i = 0;i<caca.size();i++) {
if(i==0)System.out.print(caca.get(i));
else System.out.print(" "+caca.get(i));
if(i==caca.size()-1)System.out.println();
}
} else {
System.out.println(caca.size());
for(int i = 0;i<caca.size();i++) {
if(i==0)System.out.print(caca.get(i));
else System.out.print(" "+caca.get(i));
if(i==caca.size()-1)System.out.println();
}
}
scn.close();
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=input()
w=s.count("W")
b=n-w
# s=list(s)
if b%2==w%2==1:
print(-1)
else:
c=0
p=0
ans=[]
x="W" if w%2==0 else "B"
for i in range(n):
if (s[i]==x and p==0) or (s[i]!=x and p==1):
p=1
c+=1
ans.append(i+1)
else:
p=0
print(c)
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Cf205 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public 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);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Cf205(),"Main",1<<27).start();
}
public void run()
{
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int l = in.nextInt();
String s = new String();
s = in.next();
StringBuilder str = new StringBuilder(s);
int i,j,k;
int cb=0,cw=0;
for(i=0;i<l;i++)
{
if(s.charAt(i)=='B')
{
cb++;
}
else
{
cw++;
}
}
int count = 0;
ArrayList<Integer> list = new ArrayList<>();
if(cb%2!=0 && cw%2!=0)
{
w.println(-1);
w.flush();
w.close();
return;
}
if(cb==l || cw==l)
{
w.println(0);
w.flush();
w.close();
return;
}
else
{
if(cb%2!=0)
{
while(cb!=l)
{
for(i=0;i<l-1;i++)
{
if(str.charAt(i)=='W')
{
if(str.charAt(i+1)=='W')
{
str.replace(i,i+2,"BB");
list.add(i+1);
cb=cb+2;
}
else
{
str.replace(i,i+2,"BW");
list.add(i+1);
}
count++;
}
}
}
}
if(cw%2!=0)
{
while(cw!=l)
{
for(i=0;i<l-1;i++)
{
if(str.charAt(i)=='B')
{
if(str.charAt(i+1)=='B')
{
str.replace(i,i+2,"WW");
list.add(i+1);
cw=cw+2;
}
else
{
str.replace(i,i+2,"WB");
list.add(i+1);
}
count++;
}
}
}
}
if(cb%2==0 && cw%2==0)
{
while(cb!=l)
{
for(i=0;i<l-1;i++)
{
if(str.charAt(i)=='W')
{
if(str.charAt(i+1)=='W')
{
str.replace(i,i+2,"BB");
list.add(i+1);
cb=cb+2;
}
else
{
str.replace(i,i+2,"BW");
list.add(i+1);
}
count++;
}
}
}
if(cb>=3*l)
{
count=0;
list.clear();
while(cw!=l)
{
for(i=0;i<l-1;i++)
{
if(str.charAt(i)=='B')
{
if(str.charAt(i+1)=='B')
{
str.replace(i,i+2,"WW");
list.add(i+1);
cw=cw+2;
}
else
{
str.replace(i,i+2,"WB");
list.add(i+1);
}
count++;
}
}
}
}
}
}
w.println(count);
Iterator<Integer> it = list.listIterator();
while(it.hasNext())
{
w.print(it.next() + " ");
}
w.println();
w.flush();
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 | b=[];w=[];v=[]
n=int(input())
x=input()
for i in range(n):
if x[i]=='B':b+=i+1,
else:w+=i+1,
lb,lw=len(b),len(w)
if lb%2+lw%2==2:print(-1)
else:
if lb%2:b,w,lb,lw=w,b,lw,lb
for i in range(1,lb,2):
v.extend([i for i in range(b[i]-1,b[i-1],-1)])
v+=b[i-1],
print(len(v))
print(*v) | 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, copy
n = int(sys.stdin.readline())
arr = list(sys.stdin.readline().rstrip())
btemp = copy.deepcopy(arr)
wtemp = copy.deepcopy(arr)
b2w = []
w2b = []
for i in range(n - 1):
if btemp[i] == 'B':
btemp[i] = 'W'
if btemp[i + 1] == 'B':
btemp[i + 1] = 'W'
else:
btemp[i + 1] = 'B'
b2w.append(i + 1)
for i in range(n - 1):
if wtemp[i] == 'W':
wtemp[i] = 'B'
if wtemp[i + 1] == 'B':
wtemp[i + 1] = 'W'
else:
wtemp[i + 1] = 'B'
w2b.append(i + 1)
bcan = False
if len(set(btemp)) == 1:
bcan = True
wcan = False
if len(set(wtemp)) == 1:
wcan = True
if bcan:
print(len(b2w))
print(' '.join(map(str, b2w)))
elif wcan:
print(len(w2b))
print(' '.join(map(str, w2b)))
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())
x=input()
numW=0
outlist=[]
num=0
for letter in x:
if letter=='W':
numW+=1
if numW%2==1 and len(x)%2==1:
t=0
while t<len(x):
if x[t]=='B':
outlist.append(t+1)
num+=1
if x[t+1]=='W':
x=x[:t]+'WB'+x[t+2:]
t+=1
else:
x=x[:t]+'WW'+x[t+2:]
t+=2
else:
t+=1
elif numW%2==1 and len(x)%2==0:
print('-1')
quit()
else:
t=0
while t<len(x):
if x[t]=='W':
outlist.append(t+1)
num+=1
if x[t+1]=='W':
x=x[:t]+'BB'+x[t+2:]
t+=2
else:
x=x[:t]+'BW'+x[t+2:]
t+=1
else:
t+=1
print(str(num))
for j in range(len(outlist)):
outlist[j]=str(outlist[j])
print(' '.join(outlist)) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | # import sys
# import collections
# MAX = 20
# sys.setrecursionlimit(10**6)
def inpu():
return input().split(' ')
def inti(a):
for i in range(len(a)):
a[i] = int(a[i])
return a
def inp():
a = inpu()
a = inti(a)
return a
n = int(input())
a = input()
b = 0
w = 0
for i in range(n):
if a[i] == 'W':
w += 1
else:
b += 1
if b == 0 or w == 0:
print(0)
exit(0)
if b % 2 == 0:
ans = []
i = 0
while i < n:
firstblack = -1
while i < n-1:
if a[i] == 'B':
firstblack = i
break
i += 1
if i >= n-1:
break
secondblack = -1
ans.append(i)
i += 1
while i < n:
if a[i] == 'B':
secondblack = i
break
ans.append(i)
i += 1
i += 1
print(len(ans))
for i in ans:
print(i+1, end=' ')
elif w % 2 == 0:
ans = []
i = 0
while i < n:
firstblack = -1
while i < n-1:
if a[i] == 'W':
firstblack = i
break
i += 1
if i >= n-1:
break
secondblack = -1
ans.append(i)
i += 1
while i < n:
if a[i] == 'W':
secondblack = i
break
ans.append(i)
i += 1
i += 1
print(len(ans))
for i in ans:
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 | n=int(input())
s=input()
x=s.count('B')
y=s.count('W')
if(x==0 or y==0):
print(0)
else:
B=list(s)
A=[]
for i in range(n-1):
if(B[i]=='B'):
B[i]='W'
A.append(i+1)
if(B[i+1]=='B'):
B[i+1]='W'
else:
B[i+1]='B'
if(B.count('B')==0):
print(len(A))
print(*A)
else:
for i in range(n-1):
if(B[i]=='W'):
B[i]='B'
A.append(i+1)
if(B[i+1]=='W'):
B[i+1]='B'
else:
B[i+1]='W'
if(B.count('W')==0):
print(len(A))
print(*A)
else:
for i in range(n-1):
if(B[i]=='B'):
B[i]='W'
A.append(i+1)
if(B[i+1]=='B'):
B[i+1]='W'
else:
B[i+1]='B'
if(B.count('B')==0):
print(len(A))
print(*A)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
noB,noW = 0,0
for i in s :
if i =="B":
noB += 1
else :
noW += 1
if noW%2== 1 and noB%2== 1:
print(-1)
elif noB==0 or noW==0 :
print(0)
elif noW%2== 0 and noB%2== 1 :
li = []
ans = []
count = 0
for i in range(len(s)):
if s[i] == "W" :
li.append(i)
for i in range(int(len(li)/2)):
a= li[2*i]
b = li[2*i+1]
for j in range(a+1,b+1):
ans.append(str(j))
count += 1
print(count)
u = ' '.join(ans)
print(u)
elif (noW%2== 1 and noB%2== 0) or(noW%2== 0 and noB%2== 0) :
li = []
ans = []
count = 0
for i in range(len(s)):
if s[i] == "B" :
li.append(i)
for i in range(int(len(li)/2)):
a= li[2*i]
b = li[2*i+1]
for j in range(a+1,b+1):
ans.append(str(j))
count += 1
print(count)
u = ' '.join(ans)
print(u)
| 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 | # -*- coding: utf-8 -*-
import sys
W = 'W'
B = 'B'
def try_to_color(cubes, color):
# if color == W:
# print('whitening...')
# else:
# print('blackening...')
r = 0
swaps = []
cubes = list(cubes)
i = 0
opposite_color = B
if color == B:
opposite_color = W
while i < len(cubes):
# print(cubes, i)
if cubes[i] == color:
i += 1
continue
else:
# BW or BB for W and WB or WW for B
if i + 1 >= len(cubes):
r = -1
swaps = []
break
if cubes[i] == opposite_color and cubes[i+1] == opposite_color:
# BB for W or WW for B
r += 1
cubes[i], cubes[i+1] = color, color
swaps.append(i+1)
i += 2
continue
else:
# BW for W or WB for B
r += 1
cubes[i], cubes[i+1] = color, opposite_color
swaps.append(i+1)
i += 1
continue
# print('cubes after try', cubes)
return r, swaps
def solve():
n = int(sys.stdin.readline())
cubes = sys.stdin.readline().strip()
# print('cubes', cubes)
white_r, white_swaps = try_to_color(cubes, W)
black_r, black_swaps = try_to_color(cubes, B)
# print('white', white_r, white_swaps)
# print('black', black_r, black_swaps)
vars = filter(lambda x: x[0] != -1, [
[white_r, white_swaps],
[black_r, black_swaps]
])
if not vars:
sys.stdout.write('-1\n')
return
d = sorted(vars, key=lambda x: x[0])[0]
d[1] = map(str, d[1])
if d[0] > 3 * n:
sys.stdout.write('-1\n')
return
sys.stdout.write('%s\n' % d[0])
if d[0] != 0:
sys.stdout.write(' '.join(d[1]) + '\n')
solve()
| 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 = 0, cnt = 0, w = 0, b = 0, sam = 1;
cin >> n;
int ans[100000];
string s;
cin >> s;
for (i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) sam++;
}
for (i = 0; i < n - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'W') {
swap(s[i], s[i + 1]);
ans[j++] = i + 1;
} else if (s[i] == 'B' && s[i + 1] == 'B') {
s[i] = 'W';
s[i + 1] = 'W';
ans[j++] = i + 1;
}
}
if (s[n - 1] == 'B') {
for (i = 0; i < n - 1; i++) {
if (s[i] == 'W' && s[i + 1] == 'B') {
swap(s[i], s[i + 1]);
ans[j++] = i + 1;
} else if (s[i] == 'W' && s[i + 1] == 'W') {
s[i] = 'B';
s[i + 1] = 'B';
ans[j++] = i + 1;
}
}
}
if (sam == n)
cout << 0 << endl;
else if (s[0] == s[n - 1]) {
cout << j << endl;
for (i = 0; i < j; i++) cout << ans[i] << ' ';
cout << endl;
} else
cout << -1 << endl;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
String str=in.next();
char[] arr=str.toCharArray();
String ans="";
int cnt=0;
for(int i=0;i<n;i++){
if((i==n) || (i+1)==n){
break;
}
if(arr[i]=='B'){
invert(arr,i);
invert(arr,i+1);
ans+=(i+1)+" ";
cnt++;
}
}
if(arr[n-1]=='W'){
System.out.println(cnt+"\n"+ans);
}
else{
ans="";
cnt=0;
arr=str.toCharArray();
for(int i=0;i<n;i++){
if((i==n) || (i+1)==n){
break;
}
if(arr[i]=='W'){
invert(arr,i);
invert(arr,i+1);
ans+=(i+1)+" ";
cnt++;
}
}
if(arr[n-1]=='B'){
System.out.println(cnt+"\n"+ans);
}
else
System.out.println(-1);
}
}
static void invert(char arr[],int i){
if(arr[i]=='B')
arr[i]='W';
else
arr[i]='B';
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=(int)(input())
s=input()
t=list(s)
blm=0
bll=[]
blp=1
for i in range(len(t)):
if t[i]=='W':
if i!=len(t)-1:
t[i]="B"
if t[i+1]=='W':
t[i+1]="B"
else:
t[i+1]="W"
blm=blm+1
bll.append(i+1)
else:
blp=0
t=list(s)
wlm=0
wll=[]
wlp=1
for i in range(len(t)):
if t[i]=='B':
if i!=len(t)-1:
t[i]="W"
if t[i+1]=='B':
t[i+1]="W"
else:
t[i+1]="B"
wlm=wlm+1
wll.append(i+1)
else:
wlp=0
if wlp==0 and blp==0:
print(-1)
else:
if blm<wlm and blp==1:
print(blm)
x=''
for i in bll:
x=x+str(i)+" "
#print("b")
elif wlp==1:
print(wlm)
x=''
for i in wll:
x=x+str(i)+" "
# print("w")
else:
print(blm)
x=''
for i in bll:
x=x+str(i)+" "
print(x) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
string s;
cin >> s;
long long int w = 0, b = 0;
for (long long int i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
vector<long long int> v;
if (b == 0 or w == 0)
cout << 0 << endl;
else if (w % 2 == 0) {
for (long long int i = 0; i < n; i++) {
if (s[i] == 'W') {
s[i] = 'B';
v.push_back(i + 1);
if (s[i + 1] == 'W') {
s[i + 1] = 'B';
} else
s[i + 1] = 'W';
}
}
cout << v.size() << '\n';
for (long long int x : v) cout << x << " ";
cout << endl;
} else if (b % 2 == 0) {
for (long long int i = 0; i < n; i++) {
if (s[i] == 'B') {
s[i] = 'W';
v.push_back(i + 1);
if (s[i + 1] == 'B') {
s[i + 1] = 'W';
} else
s[i + 1] = 'B';
}
}
cout << v.size() << endl;
for (long long int x : v) cout << x << " ";
cout << '\n';
} else
cout << "-1\n";
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException
{
FastScanner f = new FastScanner();
int ttt=1;
// ttt=f.nextInt();
PrintWriter out=new PrintWriter(System.out);
for(int tt=0;tt<ttt;tt++) {
int n=f.nextInt();
char[] l=f.next().toCharArray();
ArrayList<Integer> ans=new ArrayList<>();
int[] count=new int[n];
for(int i=0;i<n;i++) {
if(l[i]=='B') count[0]++;
else count[1]++;
}
if(count[0]*count[1]==0) {
System.out.println(0);
}
else if(count[0]%2==0){
for(int i=0;i<n-1;i++) {
if(l[i]=='B') {
ans.add(i+1);
l[i]='W';
if(l[i+1]=='W') l[i+1]='B';
else l[i+1]='W';
}
}
if(l[n-1]=='B') {
if(n%2==0) {
System.out.println(-1);
}
else {
for(int i=1;i<n;i+=2) {
ans.add(i);
}
for(int i:ans) System.out.print(i+" ");
System.out.println();
}
}
else {
System.out.println(ans.size());
for(int i:ans) System.out.print(i+" ");
System.out.println();
}
}
else {
for(int i=0;i<n-1;i++) {
if(l[i]=='W') {
ans.add(i+1);
l[i]='B';
if(l[i+1]=='B') l[i+1]='W';
else l[i+1]='B';
}
}
if(l[n-1]=='W') {
if(n%2==0) {
System.out.println(-1);
}
else {
for(int i=1;i<n;i+=2) {
ans.add(i);
}
for(int i:ans) System.out.print(i+" ");
System.out.println();
}
}
else {
System.out.println(ans.size());
for(int i:ans) System.out.print(i+" ");
System.out.println();
}
}
}
out.close();
}
static void sort(int[] p) {
ArrayList<Integer> q = new ArrayList<>();
for (int i: p) q.add( i);
Collections.sort(q);
for (int i = 0; i < p.length; i++) p[i] = q.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
a = []
symbol = 'B'
anti = 'W'
if s.count('B') % 2:
if s.count('W') % 2:
print('-1')
exit()
else:
symbol = 'W'
anti = 'B'
s = list(s)
for i in range(len(s) - 1):
if s[i] == symbol:
s[i + 1] = symbol if s[i+1] != symbol else anti
a.append(i + 1)
print(len(a), '\n', *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 | def f(a , c):
#print a
d = []
for i in xrange(len(a) - 1):
if a[i] == c:
if a[i] == 'B':
a[i] = 'W'
else:
a[i] = 'B'
if a[i + 1] == 'B':
a[i + 1] = 'W'
else:
a[i + 1] = 'B'
d.append(str(i + 1))
print len(d)
print " ".join(d)
n = int(raw_input())
a = list(raw_input())
b = 0
w = 0
for i in xrange(len(a)):
if a[i] == 'B':
b += 1
else:
w += 1
if b % 2 == 0:
f(a , 'B')
elif w % 2 == 0:
f(a , 'W')
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 | //package CodeforcesProject;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.function.BiFunction;
public class Main extends IO {
public static void main(String[] args) throws Exception {
int quantity = readInt();
String[] base = readArrayString("");
long answer = 0;
List<Integer> position = new ArrayList<>();
for (int i = 0; i < quantity; i++) {
if (i + 1 < quantity) {
if (base[i].equals("B")) {
base[i] = "W";
base[i + 1] = base[i + 1].equals("W") ? "B" : "W";
answer++;
position.add(i + 1);
}
}
}
if (base[quantity - 1].equals("B")) {
for (int i = 0; i < quantity; i++) {
if (i + 1 < quantity) {
if (base[i].equals("W")) {
base[i] = "B";
base[i + 1] = base[i + 1].equals("B") ? "W" : "B";
answer++;
position.add(i + 1);
}
}
}
if (base[quantity - 1].equals("W")) {
System.out.println(-1);
} else {
writeLong(answer, "\n");
writeArray(position.toArray(Integer[]::new), " ", false);
print();
}
} else {
writeLong(answer, "\n");
writeArray(position.toArray(Integer[]::new), " ", false);
print();
}
}
}
class math {
protected static int remains = 0x989687;
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static float gcd(float a, float b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static double gcd(double a, double b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static double lcm(double a, double b) { // NOK
return a / gcd(a, b) * b;
}
protected static float lcm(float a, float b) { // NOK
return a / gcd(a, b) * b;
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static double log(double value, int base) {
return Math.log(value) / Math.log(base);
}
protected static long factorial(int number) {
if (number < 0) {
return 0;
}
return solveFactorial(number);
}
private static long solveFactorial(int number) {
if (number > 0) {
return solveFactorial(number - 1) * number;
}
return 1;
}
}
class Int implements Comparable<Integer> {
protected int value;
Int(int value) {
this.value = value;
}
@Override
public int compareTo(Integer o) {
return (this.value < o) ? -1 : ((this.value == o) ? 0 : 1);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == (Integer) obj;
}
return false;
}
@Override
public int hashCode() {
return value;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
class Fraction<T extends Number> extends Pair {
private Fraction(T dividend, T divider) {
super(dividend, divider);
reduce();
}
protected static <T extends Number> Fraction<T> createFraction(T dividend, T divider) {
return new Fraction<>(dividend, divider);
}
protected void reduce() {
if (getFirstElement() instanceof Integer) {
Integer Dividend = (Integer) getFirstElement();
Integer Divider = (Integer) getSecondElement();
int gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Long) {
Long Dividend = (Long) getFirstElement();
Long Divider = (Long) getSecondElement();
long gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Float) {
Float Dividend = (Float) getFirstElement();
Float Divider = (Float) getSecondElement();
float gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
} else if (getFirstElement() instanceof Double) {
Double Dividend = (Double) getFirstElement();
Double Divider = (Double) getSecondElement();
double gcd = math.gcd(Dividend, Divider);
setFirst(Dividend / gcd);
setSecond(Divider / gcd);
}
}
protected void addWithoutReturn(Fraction number) throws UnsupportedOperationException {
add(number, 0);
}
private Fraction add(Fraction number, int function) throws UnsupportedOperationException {
if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) {
Integer Dividend = (Integer) getFirstElement();
Integer Divider = (Integer) getSecondElement();
Integer Dividend1 = (Integer) number.getFirstElement();
Integer Divider1 = (Integer) number.getSecondElement();
Integer lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) {
Long Dividend = (Long) getFirstElement();
Long Divider = (Long) getSecondElement();
Long Dividend1 = (Long) number.getFirstElement();
Long Divider1 = (Long) number.getSecondElement();
Long lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) {
Float Dividend = (Float) getFirstElement();
Float Divider = (Float) getSecondElement();
Float Dividend1 = (Float) number.getFirstElement();
Float Divider1 = (Float) number.getSecondElement();
Float lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) {
Double Dividend = (Double) getFirstElement();
Double Divider = (Double) getSecondElement();
Double Dividend1 = (Double) number.getFirstElement();
Double Divider1 = (Double) number.getSecondElement();
Double lcm = math.lcm(Divider, Divider1);
if (function == 0) {
setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1);
setSecond(lcm);
reduce();
return null;
}
Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm);
result.reduce();
return result;
} else {
throw new UnsupportedOperationException();
}
}
protected Fraction addWithReturn(Fraction number) {
return add(number, 1);
}
protected void multiplyWithoutReturn(Fraction number) throws UnsupportedOperationException {
multiply(number, 0);
}
protected Fraction multiplyWithReturn(Fraction number) throws UnsupportedOperationException {
return multiply(number, 1);
}
private Fraction multiply(Fraction number, int function) throws UnsupportedOperationException {
if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) {
Integer first = (Integer) getFirstElement() * (Integer) number.getFirstElement();
Integer second = (Integer) getSecondElement() * (Integer) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) {
Long first = (Long) getFirstElement() * (Long) number.getFirstElement();
Long second = (Long) getSecondElement() * (Long) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) {
Float first = (Float) getFirstElement() * (Float) number.getFirstElement();
Float second = (Float) getSecondElement() * (Float) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) {
Double first = (Double) getFirstElement() * (Double) number.getFirstElement();
Double second = (Double) getSecondElement() * (Double) number.getSecondElement();
if (function == 0) {
setFirst(first);
setSecond(second);
reduce();
return null;
}
Fraction answer = Fraction.createFraction(first, second);
answer.reduce();
return answer;
} else {
throw new UnsupportedOperationException();
}
}
}
class Pair<T, T1> implements Cloneable {
private T first;
private T1 second;
Pair(T obj, T1 obj1) {
first = obj;
second = obj1;
}
protected static <T, T1> Pair<T, T1> createPair(T element, T1 element1) {
return new Pair<>(element, element1);
}
protected T getFirstElement() {
return first;
}
protected T1 getSecondElement() {
return second;
}
protected void setFirst(T element) {
first = element;
}
protected void setSecond(T1 element) {
second = element;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Pair)) {
return false;
}
Pair pair = (Pair) obj;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + (first == null ? 0 : first.hashCode());
return 31 * hashCode + (second == null ? 0 : second.hashCode());
}
@Override
public Object clone() {
return Pair.createPair(first, second);
}
}
class Graph {
private int[][] base;
private boolean[] used;
private int quantity;
private Integer[] ancestor;
public int[][] getBase() {
return base.clone();
}
public boolean[] getUsed() {
return used.clone();
}
public int getQuantity() {
return quantity;
}
public Integer[] getAncestor() {
return ancestor.clone();
}
public void setBase(int[][] base) {
this.base = base;
}
protected void start(int length) {
used = new boolean[length];
ancestor = new Integer[length];
Arrays.fill(ancestor, -1);
quantity = 0;
}
protected void edgesMatrixToDefault(int length, int quantity, boolean readConsole, int[][] value) throws Exception {
base = new int[length][];
List<ArrayList<Integer>> inputBase = new ArrayList<>();
for (int i = 0; i < length; i++) {
inputBase.add(new ArrayList<>());
}
for (int i = 0; i < quantity; i++) {
int[] input = readConsole ? IO.readArrayInt(" ") : value[i];
inputBase.get(input[0] - 1).add(input[1] - 1);
//inputBase.get(input[0] - 1).add(input[2]); // price
inputBase.get(input[1] - 1).add(input[0] - 1);
//inputBase.get(input[1] - 1).add(input[2]); // price
}
for (int i = 0; i < length; i++) {
base[i] = inputBase.get(i).stream().mapToInt(Integer::intValue).toArray();
}
start(length);
}
protected void adjacencyMatrixToDefault(int length, int not, boolean readConsole, int[][] value) throws Exception {
this.base = new int[length][];
List<Integer> buffer = new ArrayList<>();
for (int i = 0; i < length; i++) {
int[] InputArray = readConsole ? IO.readArrayInt(" ") : value[i];
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != not) {
buffer.add(index);
// buffer.add(InputArray[index]); // price
}
}
this.base[i] = buffer.stream().mapToInt(Integer::intValue).toArray();
buffer.clear();
}
start(length);
}
protected void dfs(int position) throws Exception {
used[position] = true;
quantity++;
int next;
for (int index = 0; index < base[position].length; index++) {
next = base[position][index];
if (!used[next]) {
ancestor[next] = position;
dfs(next);
} /*else {
if (next != ancestor[position]) { // if cycle
throw new Exception();
}
}*/
}
}
protected int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
ancestor[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt((int[] ints) -> ints[1]));
queue.add(new int[]{start, 0});
int position;
int[] getQueue;
while (queue.size() != 0) {
getQueue = queue.poll();
position = getQueue[0];
if (getQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < this.base[position].length; index += 2) {
if (dist[position] + this.base[position][index + 1] < dist[this.base[position][index]] && !this.used[this.base[position][index]]) {
dist[this.base[position][index]] = dist[position] + this.base[position][index + 1];
this.ancestor[this.base[position][index]] = position;
queue.add(new int[]{this.base[position][index], dist[this.base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean solveFloydWarshall(int[][] base, int length, int not) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == not || base[k][j] == not) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != not) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
protected static Pair<Long, int[][]> solveKruskal(int[][] edgesMatrix, final int countVertex, final int indexSort) {
int[][] answer = new int[countVertex - 1][2];
long sum = 0;
Arrays.sort(edgesMatrix, Comparator.comparingInt(value -> value[indexSort]));
SystemOfDisjointSets dsu = new SystemOfDisjointSets(countVertex);
for (int i = 0; i < countVertex; i++) {
dsu.makeSet(i);
}
int index = 0;
for (int[] value : edgesMatrix) {
if (dsu.mergeSets(value[0], value[1])) {
sum += value[indexSort];
answer[index] = new int[]{value[0], value[1]};
index++;
}
}
if (index < countVertex - 1) {
return Pair.createPair(null, null);
}
return Pair.createPair(sum, answer);
}
static class SegmentTree {
private int[] segmentArray;
private BiFunction<Integer, Integer, Integer> function;
protected void setSegmentArray(int[] segmentArray) {
this.segmentArray = segmentArray;
}
protected int[] getSegmentArray() {
return segmentArray.clone();
}
protected void setFunction(BiFunction<Integer, Integer, Integer> function) {
this.function = function;
}
protected BiFunction<Integer, Integer, Integer> getFunction() {
return function;
}
SegmentTree() {
}
SegmentTree(int[] startBase, int neutral, BiFunction<Integer, Integer, Integer> function) {
this.function = function;
int length = startBase.length;
int[] base;
if ((length & (length - 1)) != 0) {
int pow = 0;
while (length > 0) {
length >>= 1;
pow++;
}
pow--;
base = new int[2 << pow];
System.arraycopy(startBase, 0, base, 0, startBase.length);
Arrays.fill(base, startBase.length, base.length, neutral);
} else {
base = startBase;
}
segmentArray = new int[base.length << 1]; // maybe * 4
Arrays.fill(segmentArray, neutral);
inDepth(base, 1, 0, base.length - 1);
}
private void inDepth(int[] base, int position, int low, int high) {
if (low == high) {
segmentArray[position] = base[low];
} else {
int mid = (low + high) >> 1;
inDepth(base, position << 1, low, mid);
inDepth(base, (position << 1) + 1, mid + 1, high);
segmentArray[position] = function.apply(segmentArray[position << 1], segmentArray[(position << 1) + 1]);
}
}
protected int getValue(int left, int right, int neutral) {
return findValue(1, 0, ((segmentArray.length) >> 1) - 1, left, right, neutral);
}
private int findValue(int position, int low, int high, int left, int right, int neutral) {
if (left > right) {
return neutral;
}
if (left == low && right == high) {
return segmentArray[position];
}
int mid = (low + high) >> 1;
return function.apply(findValue(position << 1, low, mid, left, Math.min(right, mid), neutral),
findValue((position << 1) + 1, mid + 1, high, Math.max(left, mid + 1), right, neutral));
}
protected void replaceValue(int index, int value) {
update(1, 0, (segmentArray.length >> 1) - 1, index, value);
}
private void update(int position, int low, int high, int index, int value) {
if (low == high) {
segmentArray[position] = value;
} else {
int mid = (low + high) >> 1;
if (index <= mid) {
update(position << 1, low, mid, index, value);
} else {
update((position << 1) + 1, mid + 1, high, index, value);
}
segmentArray[position] = function.apply(segmentArray[position << 1], segmentArray[(position << 1) + 1]);
}
}
}
static class SegmentTreeGeneric<T> {
private Object[] segmentArray;
private BiFunction<T, T, T> function;
protected void setSegmentArray(T[] segmentArray) {
this.segmentArray = segmentArray;
}
protected Object getSegmentArray() {
return segmentArray.clone();
}
protected void setFunction(BiFunction<T, T, T> function) {
this.function = function;
}
protected BiFunction<T, T, T> getFunction() {
return function;
}
SegmentTreeGeneric() {
}
SegmentTreeGeneric(T[] startBase, T neutral, BiFunction<T, T, T> function) {
this.function = function;
int length = startBase.length;
Object[] base;
if ((length & (length - 1)) != 0) {
int pow = 0;
while (length > 0) {
length >>= 1;
pow++;
}
pow--;
base = new Object[2 << pow];
System.arraycopy(startBase, 0, base, 0, startBase.length);
Arrays.fill(base, startBase.length, base.length, neutral);
} else {
base = startBase;
}
segmentArray = new Object[base.length << 1]; // maybe * 4
Arrays.fill(segmentArray, neutral);
inDepth(base, 1, 0, base.length - 1);
}
private void inDepth(Object[] base, int position, int low, int high) {
if (low == high) {
segmentArray[position] = base[low];
} else {
int mid = (low + high) >> 1;
inDepth(base, position << 1, low, mid);
inDepth(base, (position << 1) + 1, mid + 1, high);
segmentArray[position] = function.apply((T) segmentArray[position << 1], (T) segmentArray[(position << 1) + 1]);
}
}
protected T getValue(int left, int right, T neutral) {
return findValue(1, 0, ((segmentArray.length) >> 1) - 1, left, right, neutral);
}
private T findValue(int position, int low, int high, int left, int right, T neutral) {
if (left > right) {
return neutral;
}
if (left == low && right == high) {
return (T) segmentArray[position];
}
int mid = (low + high) >> 1;
return function.apply(findValue(position << 1, low, mid, left, Math.min(right, mid), neutral),
findValue((position << 1) + 1, mid + 1, high, Math.max(left, mid + 1), right, neutral));
}
protected void replaceValue(int index, T value) {
update(1, 0, (segmentArray.length >> 1) - 1, index, value);
}
private void update(int position, int low, int high, int index, T value) {
if (low == high) {
segmentArray[position] = value;
} else {
int mid = (low + high) >> 1;
if (index <= mid) {
update(position << 1, low, mid, index, value);
} else {
update((position << 1) + 1, mid + 1, high, index, value);
}
segmentArray[position] = function.apply((T) segmentArray[position << 1], (T) segmentArray[(position << 1) + 1]);
}
}
}
}
class SystemOfDisjointSets {
private int[] rank;
private int[] ancestor;
SystemOfDisjointSets(int size) {
this.rank = new int[size];
this.ancestor = new int[size];
}
protected void makeSet(int value) {
ancestor[value] = value;
rank[value] = 0;
}
protected int findSet(int value) {
if (value == ancestor[value]) {
return value;
}
return ancestor[value] = findSet(ancestor[value]);
}
protected boolean mergeSets(int first, int second) {
first = findSet(first);
second = findSet(second);
if (first != second) {
if (rank[first] < rank[second]) {
int number = first;
first = second;
second = number;
}
ancestor[second] = first;
if (rank[first] == rank[second]) {
rank[first]++;
}
return true;
}
return false;
}
}
interface Array {
void useArray(int[] a);
}
interface Method {
void use();
}
class FastSort {
protected static int[] sort(int[] array, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, array.length);
return array;
}
protected static int[] sortClone(int[] array, int ShellHeapMergeMyInsertionSort) {
int[] base = array.clone();
sort(base, ShellHeapMergeMyInsertionSort, array.length);
return base;
}
private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) {
if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) {
Random random = new Random();
ShellHeapMergeMyInsertionSort = random.nextInt(4);
}
if (ShellHeapMergeMyInsertionSort == 0) {
ShellSort(array);
} else if (ShellHeapMergeMyInsertionSort == 1) {
HeapSort(array);
} else if (ShellHeapMergeMyInsertionSort == 2) {
MergeSort(array, 0, length - 1);
} else if (ShellHeapMergeMyInsertionSort == 3) {
straightMergeSort(array, length);
} else if (ShellHeapMergeMyInsertionSort == 4) {
insertionSort(array);
}
}
private static void straightMergeSort(int[] array, int size) {
if (size == 0) {
return;
}
int length = (size >> 1) + ((size % 2) == 0 ? 0 : 1);
Integer[][] ZeroBuffer = new Integer[length + length % 2][2];
Integer[][] FirstBuffer = new Integer[0][0];
for (int index = 0; index < length; index++) {
int ArrayIndex = index << 1;
int NextArrayIndex = (index << 1) + 1;
if (NextArrayIndex < size) {
if (array[ArrayIndex] > array[NextArrayIndex]) {
ZeroBuffer[index][0] = array[NextArrayIndex];
ZeroBuffer[index][1] = array[ArrayIndex];
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
ZeroBuffer[index][1] = array[NextArrayIndex];
}
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
}
}
boolean position = false;
int pointer0, pointer, pointer1, number = 4, NewPointer, count;
Integer[][] NewBuffer;
Integer[][] OldBuffer;
length = (size >> 2) + ((size % 4) == 0 ? 0 : 1);
while (true) {
pointer0 = 0;
count = (number >> 1) - 1;
if (!position) {
FirstBuffer = new Integer[length + length % 2][number];
NewBuffer = FirstBuffer;
OldBuffer = ZeroBuffer;
} else {
ZeroBuffer = new Integer[length + length % 2][number];
NewBuffer = ZeroBuffer;
OldBuffer = FirstBuffer;
}
for (int i = 0; i < length; i++) {
pointer = 0;
pointer1 = 0;
NewPointer = pointer0 + 1;
if (length == 1) {
for (int g = 0; g < size; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
array[g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
array[g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
return;
}
for (int g = 0; g < number; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
if (OldBuffer[NewPointer][pointer1] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
pointer0 += 2;
}
position = !position;
length = (length >> 1) + length % 2;
number <<= 1;
}
}
private static void ShellSort(int[] array) {
int j;
for (int gap = (array.length >> 1); gap > 0; gap >>= 1) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
private static void HeapSort(int[] array) {
for (int i = (array.length >> 1) - 1; i >= 0; i--)
shiftDown(array, i, array.length);
for (int i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
shiftDown(array, 0, i);
}
}
private static void shiftDown(int[] array, int i, int n) {
int child;
int tmp;
for (tmp = array[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (array[child] < array[child + 1]))
child++;
if (tmp < array[child])
array[i] = array[child];
else
break;
}
array[i] = tmp;
}
private static int leftChild(int i) {
return (i << 1) + 1;
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) >> 1;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
int n = high - low + 1;
int[] Temp = new int[n];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid || j <= high) {
if (i > mid)
Temp[k++] = array[j++];
else if (j > high)
Temp[k++] = array[i++];
else if (array[i] < array[j])
Temp[k++] = array[i++];
else
Temp[k++] = array[j++];
}
for (j = 0; j < n; j++)
array[low + j] = Temp[j];
}
private static void insertionSort(int[] elements) {
for (int i = 1; i < elements.length; i++) {
int key = elements[i];
int j = i - 1;
while (j >= 0 && key < elements[j]) {
elements[j + 1] = elements[j];
j--;
}
elements[j + 1] = key;
}
}
}
class IO {
private static BufferedReader read;
private static boolean fileInput = false;
private static BufferedWriter write;
private static boolean fileOutput = false;
public static void setFileInput(boolean fileInput) {
IO.fileInput = fileInput;
}
public static void setFileOutput(boolean fileOutput) {
IO.fileOutput = fileOutput;
}
private static void startInput() {
try {
read = new BufferedReader(fileInput ? new FileReader("input.txt") : new InputStreamReader(System.in));
} catch (Exception error) {
}
}
private static void startOutput() {
try {
write = new BufferedWriter(fileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out));
} catch (Exception error) {
}
}
protected static int readInt() throws Exception {
if (read == null) {
startInput();
}
return Integer.parseInt(read.readLine());
}
protected static long readLong() throws Exception {
if (read == null) {
startInput();
}
return Long.parseLong(read.readLine());
}
protected static String readString() throws Exception {
if (read == null) {
startInput();
}
return read.readLine();
}
protected static int[] readArrayInt(String split) throws Exception {
if (read == null) {
startInput();
}
return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray();
}
protected static long[] readArrayLong(String split) throws Exception {
if (read == null) {
startInput();
}
return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray();
}
protected static String[] readArrayString(String split) throws Exception {
if (read == null) {
startInput();
}
return read.readLine().split(split);
}
protected static void writeArray(int[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception error) {
}
}
protected static void writeArray(Integer[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception error) {
}
}
protected static void writeArray(Int[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index].value));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception error) {
}
}
protected static void writeArray(long[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception error) {
}
}
protected static void writeArray(Long[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception error) {
}
}
public static void writeArray(String[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(array[index]);
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception ignored) {
}
}
protected static void writeArray(boolean[] array, String split, boolean enter) {
if (write == null) {
startOutput();
}
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Boolean.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
if (enter) {
writeEnter();
}
} catch (Exception ignored) {
}
}
protected static void writeInt(int number, String end) {
if (write == null) {
startOutput();
}
try {
write.write(Integer.toString(number));
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeInt(Integer number, String end) {
if (write == null) {
startOutput();
}
try {
write.write(Integer.toString(number));
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeLong(long number, String end) {
if (write == null) {
startOutput();
}
try {
write.write(Long.toString(number));
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeLong(Long number, String end) {
if (write == null) {
startOutput();
}
try {
write.write(Long.toString(number));
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeString(String word, String end) {
if (write == null) {
startOutput();
}
try {
write.write(word);
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeBoolean(boolean value, String end) {
if (write == null) {
startOutput();
}
try {
write.write(value ? "true" : "false");
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeBoolean(Boolean value, String end) {
if (write == null) {
startOutput();
}
try {
write.write(value ? "true" : "false");
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeChar(char word, String end) {
if (write == null) {
startOutput();
}
try {
write.write(word);
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeChar(Character word, String end) {
if (write == null) {
startOutput();
}
try {
write.write(word);
write.write(end);
} catch (Exception ignored) {
}
}
protected static void writeEnter() {
if (write == null) {
startOutput();
}
try {
write.newLine();
} catch (Exception ignored) {
}
}
protected static void print(boolean exit) throws Exception {
if (exit) {
print();
} else {
write.flush();
}
}
protected static void print() throws Exception {
if (write == null) {
return;
}
write.flush();
if (read != null) {
read.close();
}
write.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() {
long long int n;
cin >> n;
string st, st1;
cin >> st;
st1 = st;
vector<long long int> ve, ve1;
for (int i = 1; i < n; i++) {
if (st[i] == 'W') {
if (st[i - 1] != st[i]) {
ve.push_back(i);
swap(st[i], st[i - 1]);
}
} else {
if (st[i] == st[i - 1]) {
ve.push_back(i);
st[i] = 'W';
st[i - 1] = 'W';
}
}
}
for (int i = 1; i < n; i++) {
if (st1[i] == 'B') {
if (st1[i - 1] != st1[i]) {
ve1.push_back(i);
swap(st1[i], st1[i - 1]);
}
} else {
if (st1[i] == st1[i - 1]) {
ve1.push_back(i);
st1[i] = 'B';
st1[i - 1] = 'B';
}
}
}
long long int ck = 1;
for (int i = 1; i < n; i++) {
if (st[i] != st[i - 1]) {
ck = 2;
for (int j = 1; j < n; j++) {
if (st1[j] != st1[j - 1]) {
cout << -1;
return 0;
}
}
}
}
if (ck == 1) {
cout << ve.size() << endl;
for (int i = 0; i < ve.size(); i++) cout << ve[i] << " ";
} else {
cout << ve1.size() << endl;
for (int i = 0; i < ve1.size(); i++) cout << ve1[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 | def check(arr):
ans=[]
for i in range(len(arr)-1):
if arr[i]=="W":
continue
else:
ans.append(i+1)
arr[i]="W"
if arr[i+1]=="W":
arr[i+1]="B"
else:
arr[i+1]="W"
if len(set(arr))==1:
return ans
else:
return -1
def check1(arr1):
ans1=[]
for i in range(len(arr1)-1):
if arr1[i]=="B":
continue
else:
ans1.append(i+1)
arr1[i]="B"
if arr1[i+1]=="B":
arr1[i+1]="W"
else:
arr1[i+1]="B"
if len(set(arr1))==1:
return ans1
else:
return -1
n=int(input())
s=input()
arr=list(s)
arr1=list(s)
a=check(arr)
b=check1(arr1)
if a!=-1:
print(len(a))
print(*a,sep=" ")
else:
if b!=-1:
print(len(b))
print(*b,sep=" ")
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
p=list(1 if i=="B" else 0for i in input())
q=p[:]
c=[]
d=[]
for i in range(n-1):
if p[i]==0:
p[i]=1
p[i+1]=1-p[i+1]
c.append(i+1)
if q[i]==1:
q[i]=0
q[i+1]=1-q[i+1]
d.append(i+1)
if sum(p)==n:
print(len(c))
print(*c)
elif sum(q)==0:
print(len(d))
print(*d)
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
num = int(input())
string = input()
def count(string):
Black = 0
White = 0
for char in string :
if char == "B":
Black += 1
else :
White += 1
return (Black,White)
def Make(mylist,color):
retlist = []
if color == "B" :
for index in range(0,len(mylist)-1):
if not mylist[index] == "B":
mylist[index] = "B"
retlist.append(index)
if mylist[index+1] == "B":
mylist[index+1] = "W"
else:
mylist[index+1] = "B"
else:
for index in range(0,len(mylist)-1):
if not mylist[index] == "W":
mylist[index] = "W"
retlist.append(index)
if mylist[index+1] == "B":
mylist[index+1] = "W"
else:
mylist[index+1] = "B"
return retlist
mylist = list(string)
mytuple = count(mylist)
if mytuple[0]%2 != 0 and mytuple[1]%2 != 0 :
print(-1)
elif mytuple[0]%2 == 0 :
retlist = Make(mylist,"W")
print(len(retlist))
for char in retlist:
print(char+1,end=" ")
else:
retlist = Make(mylist,"B")
print(len(retlist))
for char in retlist:
print(char+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())
st=input()
w,b=[],[]
ans=[]
for i in range(n):
if st[i]=='W':
w.append(i)
else:
b.append(i)
if len(w)==0 or len(b)==0:
print(0)
elif len(w)%2!=0 and len(b)%2!=0:
print(-1)
elif len(w)%2==0:
ans=0
for i in range(0,len(w),2):
ans+=w[i+1]-w[i]
print(ans)
for i in range(0,len(w),2):
for j in range(w[i+1],w[i],-1):
print(j,end=' ')
elif len(b)%2==0:
ans=0
for i in range(0,len(b),2):
ans+=b[i+1]-b[i]
print(ans)
for i in range(0,len(b),2):
for j in range(b[i+1],b[i],-1):
print(j,end=' ')
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
blocks = list(input())
b = blocks.count('B')
w = blocks.count('W')
if b * w == 0:
print(0)
else:
if b * w % 2 == 1:
print(-1)
else:
l1 = []
if b % 2 == 0:
for i in range(n - 1):
if blocks[i] == 'B':
blocks[i] = 'W'
if blocks[i + 1] == 'B':
blocks[i + 1] = 'W'
else:
blocks[i + 1] = 'B'
l1.append(str(i + 1))
else:
for i in range(n - 1):
if blocks[i] == 'W':
blocks[i] = 'B'
if blocks[i + 1] == 'B':
blocks[i + 1] = 'W'
else:
blocks[i + 1] = 'B'
l1.append(str(i + 1))
print(len(l1))
print(' '.join(l1))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from sys import stdin
def main():
input = lambda: stdin.readline()[:-1]
N = int(input())
S = input()
bn, wn = S.count('B'), S.count('W')
if bn % 2 and wn % 2:
print(-1)
return
if not bn % 2:
color = ('B', 'W')
if not wn % 2:
color = ('W', 'B')
s = list(S)
ans = []
for i in range(N - 1):
if s[i] == color[0]:
ans.append(i + 1)
s[i] = 'B' if s[i] == 'W' else 'W'
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
print(len(ans))
if len(ans):
print(*ans)
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input(''))
a = input('')
p = []
for i in range(n):
if(a[i] == 'B'):
p.append(0)
else:
p.append(1)
fin = []
i = 0
while(i<n-1):
if(p[i] != p[i+1] and p[i] == 1):
fin.append(i)
p[i] = 0
p[i+1] = 1
if(p[i] == p[i+1] and p[i] == 1):
p[i] = 0
p[i+1] = 0
fin.append(i)
i += 1
i += 1
o = 0
z = 0
for i in range(n):
if(p[i] == 0):
z += 1
else:
o += 1
res = True
if(o == 1 and z%2 != 0):
print(-1)
res = False
elif(o == 1 and z%2 == 0):
for i in range(z//2):
fin.append(2*i)
if(res):
print(len(fin))
for h in fin:
print(h+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 | n = int(input())
s = list(input())
count = 0
ans = []
while count <= n*3:
for i in range(n - 1):
if s[i] != "B":
ans.append(i + 1)
count += 1
s[i] = "B"
if s[i + 1] == "B":
s[i + 1] = "W"
else:
s[i + 1] = "B"
if "W" not in s:
print(count)
if ans:
print(*ans)
exit()
elif count > n*3:
print(-1)
exit()
for i in range(n - 1):
if s[i] != "W":
ans.append(i + 1)
count += 1
s[i] = "W"
if s[i + 1] == "W":
s[i + 1] = "B"
else:
s[i + 1] = "W"
if "B" not in s:
print(count)
if ans:
print(*ans)
exit()
elif count > n*3:
print(-1)
exit()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=input()
l=[]
for i in s:
l.append(i)
ci=[]
if l.count('B')%2==0:
for i in range(n-1):
if l[i]=='B':
ci.append(i)
l[i]='W'
if l[i+1]=='W':
l[i+1]='B'
else:
l[i+1]='W'
#print(l)
print(len(ci))
for i in ci:
print(i+1,end=" ")
print()
elif l.count('W')%2==0:
for i in range(n-1):
if l[i]=='W':
ci.append(i)
l[i]='B'
if l[i+1]=='B':
l[i+1]='W'
else:
l[i+1]='B'
#print(l)
print(len(ci))
for i in ci:
print(i+1,end=" ")
print()
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | fl=[]
def minKBitFlips(A, K):
global fl
N = len(A)
hint = [0] * N
ans = flip = 0
# When we flip a subarray like A[i], A[i+1], ..., A[i+K-1]
# we can instead flip our current writing state, and put a hint at
# position i+K to flip back our writing state.
for i, x in enumerate(A):
flip ^= hint[i]
if x ^ flip == 0: # If we must flip the subarray starting here...
ans += 1 # We're flipping the subarray from A[i] to A[i+K-1]
fl.append(i+1)
if i+K > N: return -1 # If we can't flip the entire subarray, its impossible
flip ^= 1
if i+K < N: hint[i + K] ^= 1
return ans
n=int(input())
s=input()
a=[]
for i in range(0,n):
if s[i]=='B':
a.append(1)
else:
a.append(0)
# a=[1,0,0,0,0,0,0,1]
ans=minKBitFlips(a,2)
# print(ans)
# print(fl)
if (ans==-1):
fl=[]
a=[1-i for i in a]
ans2=minKBitFlips(a,2)
if (ans2==-1):
print(-1)
else:
print(ans2)
print(*fl)
else:
print(ans)
print(*fl)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import math
import sys
input = sys.stdin.readline
R = lambda : list(map(int,input().split()))
n=int(input())
s=input()
w=s.count("W")
b=n-w
# s=list(s)
if b%2==w%2==1:
print(-1)
else:
c=0
p=0
ans=[]
x="W" if w%2==0 else "B"
for i in range(n):
if (s[i]==x and p==0) or (s[i]!=x and p==1):
p=1
c+=1
ans.append(i+1)
else:
p=0
print(c)
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
def f(n, S, b):
s = [b] + [i == 'W' for i in S]
ops = []
for i in range(1, n):
if s[i] != s[i - 1]:
ops.append(i)
s[i] = not s[i]
s[i + 1] = not s[i + 1]
if (not s[0]) in s :
return False
else:
print(len(ops))
for i in ops:
print(i, end=' ')
return True
if not (f(n, s, True) or f(n, s, False)):
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int N;
string s;
vector<int> res;
bool Solve() {
res.clear();
string t = s;
for (int i = 0; i < (int)t.size() - 1; i++) {
if (t[i] == 'W') {
res.push_back(i + 1);
t[i] = 'B';
if (t[i + 1] == 'W')
t[i + 1] = 'B';
else
t[i + 1] = 'W';
}
}
for (int i = 0; i < t.size(); i++)
if (t[i] == 'W') return false;
return true;
}
int main() {
cin >> N >> s;
if (Solve()) {
cout << res.size() << "\n";
for (int i = 0; i < res.size(); i++) cout << res[i] << " ";
return 0;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'W')
s[i] = 'B';
else
s[i] = 'W';
}
if (Solve()) {
cout << res.size() << "\n";
for (int i = 0; i < res.size(); i++) cout << res[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 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Blocks {
public static void main(String[] args) throws IOException {
int n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
String s = br.readLine();
int b = 0, w =0;
for (int i =0; i<n; i++) {
if (s.charAt(i) == 'B') b++;
else w++;
}
if (w % 2 != 0 && b %2 != 0) {
System.out.println("-1");
} else {
int f = 0;
char[] str = s.toCharArray();
StringBuilder steps = new StringBuilder();
if (b % 2 == 0 && w%2 != 0) {
for (int i = 0; i < n; i++) {
// flip bw
if (str[i] == 'B' && str[i+1] == 'W') {
str[i] = 'W';
str[i+1] = 'B';
steps.append((i+1)+" ");
f++;
} else if (str[i] == 'B' && str[i+1] == 'B') {
str[i] = 'W';
str[i+1] = 'W';
steps.append((i+1)+" ");
f++;
}
}
} else if (w % 2 == 0 && b%2 != 0) {
for (int i = 0; i < n; i++) {
// flip wb
if (str[i] == 'W' && str[i+1] == 'B') {
str[i] = 'B';
str[i+1] = 'W';
steps.append((i+1)+" ");
f++;
} else if (str[i] == 'W' && str[i+1] == 'W') {
str[i] = 'B';
str[i+1] = 'B';
steps.append((i+1)+" ");
f++;
}
}
} else {
if (w > b) {
// prop w
for (int i=0; i<n; i++) {
// flip wb
if (str[i] == 'W' && str[i+1] == 'B') {
str[i] = 'B';
str[i+1] = 'W';
steps.append((i+1)+" ");
f++;
} else if (str[i] == 'W' && str[i+1] == 'W') {
str[i] = 'B';
str[i+1] = 'B';
steps.append((i+1)+" ");
f++;
}
}
} else {
for (int i=0; i<n; i++) {
// flip bw
if (str[i] == 'B' && str[i + 1] == 'W') {
str[i] = 'W';
str[i + 1] = 'B';
steps.append((i+1)+" ");
f++;
} else if (str[i] == 'B' && str[i + 1] == 'B') {
str[i] = 'W';
str[i + 1] = 'W';
steps.append((i+1)+" ");
f++;
}
}
}
}
System.out.println(f);
System.out.println(steps);
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
string=input()
s=list(string)
counts=0
arr=[]
ans=''
W_count=s.count('W')
B_count=s.count('B')
Ws=W_count%2
Bs=B_count%2
while True:
if Ws==1 and Bs==1:
print(-1)
break
if s.count('W')==0 or s.count('B')==0:
print(counts)
for x in arr:
ans+=str(x)+' '
print(ans)
break
else:
if Ws>=Bs:
for i,x in enumerate(s):
if x=='B':
s[i]='W'
if i!=len(s)-1 :
s[i+1]= "W" if s[i+1]=="B" else "B"
counts+=1
arr.append(i+1)
else:
for i,x in enumerate(s):
if x=='W':
s[i]='B'
if i!=len(s)-1:
s[i+1]="W" if s[i+1]=="B" else "B"
counts+=1
arr.append(i+1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> q;
string s;
cin >> s;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'W') {
s[i] = 'B';
s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W';
q.push_back(i);
}
}
if (s[n - 1] == 'B') {
cout << q.size() << endl;
for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' ';
cout << endl;
return 0;
}
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = s[i + 1] == 'W' ? 'B' : 'W';
q.push_back(i);
}
}
if (s[n - 1] == 'W') {
cout << q.size() << endl;
for (int i = 0; i < q.size(); i++) cout << q[i] + 1 << ' ';
cout << endl;
return 0;
}
cout << "-1" << endl;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
import os
from io import BytesIO, IOBase
#########################
# imgur.com/Pkt7iIf.png #
#########################
# returns the list of prime numbers less than or equal to n:
'''def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r'''
# returns all the divisors of a number n(takes an additional parameter start):
'''def divs(n, start=1):
divisors = []
for i in range(start, int(math.sqrt(n) + 1)):
if n % i == 0:
if n / i == i:
divisors.append(i)
else:
divisors.extend([i, n // i])
return divisors'''
# returns the number of factors of a given number if a primes list is given:
'''def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
return divs_number'''
# returns the leftmost and rightmost positions of x in a given list d(if x isnot present then returns (-1,-1)):
'''def flin(d, x, default=-1):
left = right = -1
for i in range(len(d)):
if d[i] == x:
if left == -1: left = i
right = i
if left == -1:
return (default, default)
else:
return (left, right)'''
# returns (b**p)%m
'''def modpow(b,p,m):
res=1
b=b%m
while(p):
if p&1:
res=(res*b)%m
b=(b*b)%m
p>>=1
return res'''
# if m is a prime this can be used to determine (1//a)%m or modular multiplicative inverse of a number a
'''def mod_inv(a,m):
return modpow(a,m-2,m)'''
# returns the ncr%m for (if m is a prime number) for very large n and r
'''def ncr(n,r,m):
res=1
if r==0:
return 1
if n-r<r:
r=n-r
p,k=1,1
while(r):
res=((res%m)*(((n%m)*mod_inv(r,m))%m))%m
n-=1
r-=1
return res'''
# returns ncr%m (if m is a prime number and there should be a list fact which stores the factorial values upto n):
'''def ncrlis(n,r,m):
return (fact[n]*(mod_inv(fact[r],m)*mod_inv(fact[n-r],m))%m)%m'''
#factorial list which stores factorial of values upto n:
'''mx_lmt=10**5+10
fact=[1 for i in range(mx_lmt)]
for i in range(1,mx_lmt):
fact[i]=(i*fact[i-1])%mod'''
#count xor of numbers from 1 to n:
'''def xor1_n(n):
d={0:n,1:1,2:n+1,3:0}
return d[n&3]'''
def cel(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return defaultdict(int)
def ddl(): return defaultdict(list)
def ddd(): return defaultdict(defaultdict(int))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict
#from collections import deque
#from collections import OrderedDict
#from math import gcd
#import time
#import itertools
#import timeit
#import random
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#from bisect import insort_left as il
#from bisect import insort_right as ir
#from heapq import *
#mod=998244353
#mod=10**9+7
d={'W':'B','B':'W'}
for _ in range(1):
n=ii()
a=input()
s=list(a)
f=0
res=[]
for i in range(n-1):
if s[i]=='B':
s[i],s[i+1]=d[s[i]],d[s[i+1]]
res.append(i+1)
i+=2
if s[-1]=='B':
s=list(a)
res=[]
for i in range(n-1):
if s[i]=='W':
s[i],s[i+1]=d[s[i]],d[s[i+1]]
res.append(i+1)
if s[-1]=='W':
f=1
if f==0:
if res:
print(len(res))
print(*res)
else:
print(0)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import copy
a=int(input())
s=list(input())
n=len(s)
s2=copy.deepcopy(s)
ans1=[]
for i in range(n-1):
if s[i]=='W':
None
else:
s[i]='B'
if s[i+1]=='B':
s[i+1]='W'
else:
s[i+1]='B'
ans1.append(i+1)
if s[n-1]=='W':
print(len(ans1))
print(*ans1,end=" ")
print()
else:
ans2=[]
for i in range(n-1):
if s2[i]=='B':
None
else:
s2[i]='B'
if s2[i+1]=='B':
s2[i+1]='W'
else:
s2[i+1]='B'
ans2.append(i+1)
if s2[n-1]=='B':
print(len(ans2))
print(*ans2,end=" ")
print()
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n;
cin >> n;
vector<int> v;
string a;
cin >> a;
int w = 0, b = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 'W')
w++;
else
b++;
}
if (b % 2 == 1 && w % 2 == 1) {
cout << -1;
exit(0);
} else if (b == 0 || w == 0) {
cout << 0;
exit(0);
}
char c;
if (b % 2 == 1)
c = 'W';
else
c = 'B';
for (int i = 0; i < n - 1; ++i) {
if (a[i] == c)
if (a[i + 1] == a[i])
i++;
else {
v.push_back(i + 1);
swap(a[i], a[i + 1]);
}
}
for (int i = 0; i < n; ++i) {
if (a[i] == c) {
v.push_back(i + 1);
i += 1;
}
}
cout << v.size() << '\n';
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
cout << '\n';
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def solve(n,a):
w=a.count('W');b=a.count('B')
if w==0 or b==0:return 0
if w%2!=0 and b%2!=0:return -1
ind=[];ans=[];indw=[]
for i in range(n):
if a[i]=='B':ind.append(i)
if a[i]=='W':indw.append(i)
i=0
if b%2==0:
while i<b:ans+=(int(x) for x in range(ind[i]+1,ind[i+1]+1));i+=2
return ans
while i<w:ans+=(int(x) for x in range(indw[i]+1,indw[i+1]+1));i+=2
return ans
n=int(input());a=list(input());p=solve(n,a)
if p==-1 or p==0:print(p)
else:print(len(p));print(*p) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e5 + 6;
const long long int MOD = 1e9 + 7;
vector<int> prim(1000005, 1);
int fact[maxn];
long long int binomialCoeff(long long int n, long long int k) {
long long int res = 1;
if (k > n - k) k = n - k;
for (long long int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
bool isVowel(char ch) {
if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'u' || ch == 'o') {
return true;
}
return false;
}
long long int power(long long int x, long long int i, long long int mod) {
long long int ans = 1;
while (i > 0) {
if (i & 1) ans = (ans * x) % mod;
i >>= 1;
x = (x * x) % mod;
}
return ans;
}
long long int modInverse(long long int x, long long int mod) {
return power(x, mod - 2, mod);
}
int nCr(int n, int r) {
if (n < r) {
return 0;
}
return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) *
modInverse(fact[r], MOD)) %
MOD;
}
long long int power(int x, unsigned int y) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
void erath(int n) {
prim[1] = 0;
prim[0] = 0;
prim[2] = 1;
for (int i = 2; i * i <= n; i++) {
if (prim[i]) {
for (int j = i * i; j <= n; j += i) {
prim[j] = 0;
}
}
}
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a / gcd(a, b)) * b;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
long long int n, m, k;
int getmid(int l, int r) { return (l + (r - l) / 2); }
struct segtree {
int siz;
vector<pair<long long int, long long int> > sums;
void init(int n) {
siz = 1;
while (siz < n) siz *= 2;
sums.assign(2 * siz, {LONG_MAX, 0});
}
void set(int i, int v, int cur, int l, int r) {
if (r - l == 1) {
sums[cur].first = v;
sums[cur].second = 1;
return;
}
int m = getmid(l, r);
if (i < m) {
set(i, v, 2 * cur + 1, l, m);
} else
set(i, v, 2 * cur + 2, m, r);
sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first);
if (sums[2 * cur + 1].first == sums[cur].first &&
sums[2 * cur + 2].first == sums[cur].first) {
sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second;
} else if (sums[2 * cur + 1].first == sums[cur].first) {
sums[cur].second = sums[cur * 2 + 1].second;
} else {
sums[cur].second = sums[cur * 2 + 2].second;
}
}
void set(int i, int v) { set(i, v, 0, 0, siz); }
pair<long long int, long long int> sum(int l, int r, int cur, int lx,
int rx) {
if (lx >= r || l >= rx) return {LONG_MAX, 0};
if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second};
int mid = getmid(lx, rx);
pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid),
s2 = sum(l, r, 2 * cur + 2, mid, rx);
if (s1.first < s2.first) {
return s1;
} else if (s1.first > s2.first)
return s2;
else
return {s1.first, s1.second + s2.second};
}
pair<long long int, long long int> sum(int l, int r) {
return sum(l, r, 0, 0, siz);
}
};
vector<int> gr[100000];
void ans() {
cin >> n;
string s;
cin >> s;
int bl = 0, wh = 0;
for (auto it : s) {
if (it == 'W') wh++;
}
bl = n - wh;
if (bl == 0 || bl == n) {
cout << 0 << endl;
} else if (bl % 2 == 1 && wh % 2 == 1) {
cout << -1 << endl;
} else {
int pos;
char c;
int i = 0, j = n - 1;
if (bl % 2 == 0) {
c = 'B';
pos = bl;
} else {
c = 'W';
pos = wh;
}
vector<long long int> ans;
while (j > 0) {
if (s[j] == c) {
if (s[j - 1] != c) {
ans.push_back(j);
swap(s[j - 1], s[j]);
} else {
ans.push_back(j);
j--;
}
}
j--;
}
cout << ans.size() << endl;
for (auto it : ans) cout << it << " ";
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--) {
ans();
}
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())
string = list(input().strip())
def make_white(s):
inverts = []
for i in range(len(s) - 1):
if s[i] == 'B':
s[i] = 'W'
if s[i+1] == 'W':
s[i+1] = 'B'
else:
s[i+1] = 'W'
inverts.append(i+1)
if s[-1] == 'W':
return True, inverts
else:
return False, inverts
def make_black(s):
inverts = []
for i in range(len(s) - 1):
if s[i] == 'W':
s[i] = 'B'
if s[i+1] == 'W':
s[i+1] = 'B'
else:
s[i+1] = 'W'
inverts.append(i+1)
if s[-1] == 'B':
return True, inverts
else:
return False, inverts
can, inv = make_white(string[:])
if can:
print(len(inv))
print(" ".join(map(str, inv)))
else:
can2, inv2 = make_black(string[:])
if can2:
print(len(inv2))
print(" ".join(map(str, inv2)))
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Codeforces{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String color=sc.nextLine();
int B=0;
int W=0;
for(int i=0;i<n;i++) {
if(String.valueOf(color.charAt(i)).equals("B")) B+=1;
else W+=1;
}
if(Math.max(B,W)%2!=0 && Math.min(B,W)%2!=0) System.out.println("-1");
else if(Math.min(B,W)%2==0 && Math.max(B,W)%2==0){
int count=0;
List<Integer> index=new ArrayList<>();
if(B<W) {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WB"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WW"+color.substring(i+2);
}
}
}
else {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BW"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BB"+color.substring(i+2);
}
}
}
System.out.println(count);
for(int i=0;i<index.size();i++) {
System.out.print(String.valueOf(index.get(i))+" ");
}
}
else if(Math.min(B,W)%2!=0 && Math.max(B,W)%2==0) {
int count=0;
List<Integer> index=new ArrayList<>();
if(B<W) {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BB"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BW"+color.substring(i+2);
}
}
}
else {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WW"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WB"+color.substring(i+2);
}
}
}
System.out.println(count);
for(int i=0;i<index.size();i++) {
System.out.print(String.valueOf(index.get(i))+" ");
}
}
else {
int count=0;
List<Integer> index=new ArrayList<>();
if(B>W) {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WB"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WW"+color.substring(i+2);
}
}
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BB"+color.substring(i+2);
}
}
}
else {
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BW"+color.substring(i+2);
}
else if(String.valueOf(color.charAt(i)).equals("W") && String.valueOf(color.charAt(i+1)).equals("W")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"BB"+color.substring(i+2);
}
}
for(int i=0;i<n-1;i++) {
if(String.valueOf(color.charAt(i)).equals("B") && String.valueOf(color.charAt(i+1)).equals("B")) {
count+=1;
index.add(i+1);
color=color.substring(0,i)+"WW"+color.substring(i+2);
}
}
}
System.out.println(count);
for(int i=0;i<index.size();i++) {
System.out.print(String.valueOf(index.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.util.*;
public class coloring {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
String s=sc.nextLine();
//System.out.println(s);
char car[]=s.toCharArray();
char ca[]=new char[car.length];
int B=0;
int W=0;
for(int i=0;i<s.length();i++){
if(car[i]=='W'){
ca[i]='W';
W++;
}
if(car[i]=='B'){
ca[i]='B';
B++;
}
}
if(B%2==1 && W%2==1){
System.out.println(-1);
return;
}
ArrayList<Integer> ar=new ArrayList<Integer>();
for(int i=0;i<n;i++){
if(car[i]=='B'){
ar.add(i+1);
if(i==n-1){
continue;
}
if(car[i+1]=='W'){
car[i+1]='B';
}
else{
car[i+1]='W';
}
}
}
if(car[n-1]!='W'){
ar.clear();
for(int i=0;i<n;i++){
if(ca[i]=='W'){
ar.add(i+1);
if(i==n-1){
continue;
}
if(ca[i+1]=='W'){
ca[i+1]='B';
}
else{
ca[i+1]='W';
}
}
}
}
System.out.println(ar.size());
for(int i=0;i<ar.size();i++){
System.out.println(ar.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 atexit
import io
import sys
@atexit.register
def flush():
sys.__stdout__.write(sys.stdout.getvalue())
sys.stdout = io.BytesIO()
input = sys.stdin.readline
sys.stdout = io.BytesIO()
n = int(input())
b = list(input().strip())
w = b[:]
moves = []
for i in xrange(n - 1):
if b[i] == "W":
moves.append(i + 1)
b[i] = "B" if b[i] == "W" else "W"
b[i + 1] = "B" if b[i + 1] == "W" else "W"
if b[-1] == "B":
print(len(moves))
print(" ".join(map(str, moves)))
else:
moves = []
for i in xrange(n - 1):
if w[i] == "B":
moves.append(i + 1)
w[i] = "B" if w[i] == "W" else "W"
w[i + 1] = "B" if w[i + 1] == "W" else "W"
if w[-1] == "W":
print(len(moves))
print(" ".join(map(str, moves)))
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;
cin >> n;
string s, s1;
cin >> s;
s1 = s;
vector<int> v;
int coun = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'W') {
coun++;
continue;
} else if (i < n - 1) {
v.push_back(i + 1);
s[i] = 'W';
coun++;
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
if (coun == n) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
} else {
v.clear();
int coun = 0;
for (int i = 0; i < n; i++) {
if (s1[i] == 'B') {
coun++;
continue;
} else if (i < n - 1) {
v.push_back(i + 1);
s1[i] = 'B';
coun++;
if (s1[i + 1] == 'W')
s1[i + 1] = 'B';
else
s1[i + 1] = 'W';
}
}
if (coun == n) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
} else
cout << -1 << endl;
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | /*
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
*******************************
*/
import java.util.*;
import java.io.*;
public class x1271B
{
public static void main(String omkar[]) throws Exception
{
//BufferedReader infile = new BufferedReader(new FileReader("cowdate.in"));
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
String input = infile.readLine();
StringBuilder sb = new StringBuilder();
int b = 0;
for(char c: input.toCharArray())
if(c == 'B')
b++;
if(b%2 == 1 && (N-b)%2 == 1)
{
System.out.println(-1);
return;
}
if(b == 0 || b == N)
{
System.out.println(0);
return;
}
if(b%2 == 1)
{
int w = N-b;
char[] arr = input.toCharArray();
ArrayList<Integer> ls = new ArrayList<Integer>();
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';
ls.add(i+1);
}
for(int i=0; i < N-1; i+=2)
ls.add(i+1);
System.out.println(ls.size());
for(int x: ls)
System.out.print(x+" ");
}
else
{
int w = b;
char[] arr = input.toCharArray();
ArrayList<Integer> ls = new ArrayList<Integer>();
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';
ls.add(i+1);
}
for(int i=0; i < N-1; i+=2)
ls.add(i+1);
System.out.println(ls.size());
for(int x: ls)
System.out.print(x+" ");
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.Scanner;
public class Codeforces1271B_Real {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int length = input.nextInt();
input.nextLine();
String given = input.nextLine();
int countW = 0;
int i;
for (i = 0; i < given.length(); i++) {
if (given.charAt(i) == 'W')
countW += 1;
}
if (length % 2 == 0 && countW % 2 != 0) {
System.out.println("-1");
System.exit(0);
}
int[] todo = new int[3 * length];
int place = 0;
for (i = 0; i < given.length() - 1; i++) {
if (given.substring(i, i + 2).equals("WB")) {
given = given.substring(0, i) + "BW" + given.substring(i + 2);
todo[place] = i + 1;
place += 1;
}
if (given.substring(i, i + 2).equals("WW")) {
given = given.substring(0, i) + "BB" + given.substring(i + 2);
todo[place] = i + 1;
place += 1;
}
}
if (given.charAt(length - 1) == 'W') {
for (i = 0; i < length / 2; i++) {
todo[place] = 2 * i + 1;
place += 1;
}
}
int count = 0;
for (i = 0; i < 3 * length; i++) {
if (todo[i] != 0)
count += 1;
}
System.out.println(count);
for (i = 0; i < count; i++) {
System.out.print(todo[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 main():
n = int(input())
bb = list(input())
res = []
for i in range(len(bb) -1):
if bb[i] == 'B':
res.append(i+1)
bb[i] = 'W'
bb[i+1] = 'W' if bb[i+1] == 'B' else 'B'
if bb[-1] == 'W':
print(len(res))
if len(res) > 0:
print(' '.join(map(str, res)))
return
for i in range(len(bb) -1):
if bb[i] == 'W':
res.append(i+1)
bb[i] = 'B'
bb[i+1] = 'W' if bb[i+1] == 'B' else 'B'
if bb[-1] == 'B':
print(len(res))
if len(res) > 0:
print(' '.join(map(str, res)))
return
print(-1)
if __name__ == "__main__":
main() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
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 double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
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;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int n=input.scanInt();
StringBuilder str=new StringBuilder(input.scanString());
StringBuilder str1=new StringBuilder(""+str);
StringBuilder ans=new StringBuilder("");
int indx=n-1;
int cnt=0;
for(int i=0;i<n-1;i++) {
if(str.charAt(i)=='B') {
continue;
}
if(str.charAt(i)=='W' && str.charAt(i+1)=='B') {
str.setCharAt(i,'B');
str.setCharAt(i+1,'W');
cnt++;
ans.append((i+1)+" ");
}
else {
str.setCharAt(i,'B');
str.setCharAt(i+1,'B');
cnt++;
ans.append((i+1)+" ");
}
}
boolean is_pos=true;
for(int i=0;i<n;i++) {
if(str.charAt(i)!='B') {
is_pos=false;
break;
}
}
if(is_pos) {
System.out.println(cnt+"\n"+ans);
return;
}
ans=new StringBuilder("");
indx=n-1;
cnt=0;
for(int i=0;i<n-1;i++) {
if(str1.charAt(i)=='W') {
continue;
}
if(str1.charAt(i)=='B' && str1.charAt(i+1)=='W') {
str1.setCharAt(i,'W');
str1.setCharAt(i+1,'B');
cnt++;
ans.append((i+1)+" ");
}
else {
str1.setCharAt(i,'W');
str1.setCharAt(i+1,'W');
cnt++;
ans.append((i+1)+" ");
}
}
is_pos=true;
for(int i=0;i<n;i++) {
if(str1.charAt(i)!='W') {
is_pos=false;
break;
}
}
if(is_pos) {
System.out.println(cnt+"\n"+ans);
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 | # n, s_x, s_y = map(int, input().split())
# east = west = north = south = 0
# for i in range(n):
# x, y = map(int, input().split())
# if x < s_x:
# west += 1
# else:
# east += 1
# if y < s_y:
# # south-west
# south += 1
# else:
# north += 1
# ans = max([east, west, north, south])
# print(ans)
# if east == ans:
# print(s_x + 1, s_y)
# elif west == ans:
# print(s_x - 1, s_y)
# elif north == ans:
# print(s_x, s_y + 1)
# else:
# print(s_x, s_y-1)
n = int(input())
s = list(input())
b = s.count('B')
w = n - b
if w % 2 == 1 and b % 2 == 1:
print(-1)
else:
ans = []
if w % 2 == 1:
odd = 'W'
even = 'B'
else:
odd = 'B'
even = 'W'
for i in range(n-1):
if s[i] != odd:
ans.append(i + 1)
s[i] = odd
s[i+1] = even if s[i+1] == odd else odd
print(len(ans))
print(*ans) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
s = s.lower()
l = []
o = []
c = 0
c2 = 0
o2 = []
for i in s:
l.append(i)
b = s.count("b")
w = s.count("w")
flag = 1
l2 = l[:]
if(b % 2 != 0 and w % 2 != 0):
print(-1)
flag = 0
else:
if(b % 2 == 0):
for i in range(len(l) - 1):
if(l[i] == 'b' and l[i + 1] == 'b'):
c += 1
l[i] , l[i + 1] = 'w' , 'w'
o.append(i)
elif(l[i] == 'b' and l[i + 1] == 'w'):
c += 1
l[i] , l[i + 1] = 'w' , 'b'
o.append(i)
else:
c = 99999999
if(w % 2 == 0):
for i in range(len(l2) - 1):
if(l2[i] == 'w' and l2[i + 1] == 'w'):
c2 += 1
l2[i] , l2[i + 1] = 'b' , 'b'
o2.append(i)
elif(l2[i] == 'w' and l2[i + 1] == 'b'):
c2 += 1
l2[i] , l2[i + 1] = 'b' , 'w'
o2.append(i)
else:
c2 = 99999999
if(flag != 0):
if(c < c2):
print(c)
for i in o:
print(i + 1 , end = ' ')
else:
print(c2)
for i in o2:
print(i + 1 , end = ' ')
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def main():
n = int(input())
line = input()
b = 0
w = 0
pos = dict()
pos["B"] = []
pos["W"] = []
for i in range(len(line)):
if line[i] == "B":
b += 1
pos["B"].append(i)
else:
pos["W"].append(i)
w += 1
if b % 2 == 1 and w % 2 == 1:
print(-1)
return 0
else:
if b % 2 == 0:
key = "B"
else:
key = "W"
line = list(line)
to_print = ""
lngth = 0
for i in range(0, len(pos[key]), 2):
for k in range(pos[key][i], pos[key][i + 1]):
to_print += str(k + 1) + " "
lngth += 1
print(lngth)
print(to_print)
if __name__ == '__main__':
t = 1
for i in range(t):
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;
using ll = long long;
ll fast_pow(ll a, ll b, ll m) {
ll res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % m;
a = (a * a) % m;
b = b / 2;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator-(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first - r.first, l.second - r.second};
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) {
return {l.first + r.first, l.second + r.second};
}
const ll maxn = 1e5 + 10LL;
const ll inf = 1e9 + 10LL;
vector<int> prefix_function(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j]) j = pi[j - 1];
if (s[i] == s[j]) j++;
pi[i] = j;
}
return pi;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> ans;
int ctr = 0;
bool a = true;
while (ctr < n - 1) {
if (s[ctr] == 'W' && s[ctr + 1] == 'W') {
ans.push_back(ctr + 1);
s[ctr] = 'B';
s[ctr + 1] = 'B';
ctr += 2;
} else if (s[ctr] == 'W' && s[ctr + 1] == 'B') {
s[ctr] = 'B';
s[ctr + 1] = 'W';
ans.push_back(ctr + 1);
ctr++;
} else {
ctr++;
}
}
if (s[n - 1] == 'W') {
ctr = n - 3;
while (ctr >= 0) {
if (s[ctr] == 'B' && s[ctr + 1] == 'B') {
s[ctr] = 'B';
s[ctr + 1] = 'B';
ans.push_back(ctr + 1);
}
ctr -= 2;
}
}
if (n % 2 == 0 && s[n - 1] == 'W') a = false;
if (a) {
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
} else {
cout << -1 << endl;
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
b = 0
w = 0
for i in s:
if i == 'W':w+=1
else:b+=1
if w%2 and b%2:
print(-1)
elif w%2:
a = [i for i in s]
ans = []
for i in range(n):
if a[i] != 'W':
ans.append(i+1)
if(a[i+1] == 'W'): a[i+1] = 'B'
else: a[i+1] = 'W'
print(len(ans))
for i in ans: print(i,end=' ')
print()
else:
a = [i for i in s]
ans = []
for i in range(n):
if a[i] != 'B':
ans.append(i+1)
if(a[i+1] == 'W'): a[i+1] = 'B'
else: a[i+1] = 'W'
print(len(ans))
for i in ans: print(i,end=' ')
print() | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from math import ceil
from math import factorial
from collections import Counter
from operator import itemgetter
ii = lambda: int(input())
iia = lambda: list(map(int,input().split()))
isa = lambda: list(input().split())
n = ii()
s = input()
s = list(s)
if(s.count('B')%2==1 and s.count('W')%2==1):
print(-1)
else:
i = 0
ans = []
while(i<n-2):
if(s[i]!=s[i+1]):
s[i+1] = s[i]
if(s[i+2]=='B'):
s[i+2]='W'
else:
s[i+2]='B'
ans.append(i+2)
i+=1
if(s.count('W')!=n and s.count('B')!=n):
for i in range(0,n-2,2):
ans.append(i+1)
if s[i]=='W':
s[i]='B'
s[i+1]='B'
else:
s[i]='W'
s[i+1]='W'
print(len(ans))
print(*ans)
'''''' | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | from collections import deque
def solve():
n = int(input())
t = list(input())
res = []
if t.count(t[0]) == len(t):
return "0\n"
for i in range(len(t)-1):
if t[i] == 'B':
t[i] = 'W'
t[i+1] = 'W' if t[i+1] == 'B' else "B"
res.append(i+1)
if t[-1] == 'B':
if len(t) % 2 == 0:
return "-1\n"
for i in range(0, len(t)-2, 2):
t[i] = t[i+1] = 'B'
res.append(i+1)
return f'{len(res)}\n{" ".join(str(a) for a in res)}'
# n = int(input())
# for i in range(n):
print(solve())
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = input()
s = list(s)
new = s.copy()
ans = ''
lst = []
def swap(a,b):
if a=='W':a = 'B';
else : a='W';
if b=='W': b = 'B';
else: b = 'W';
return a,b
#print(swap('B','B'))
#let's make all white
for i in range(len(s)-1):
if s[i]=='B':
s[i],s[i+1]=swap(s[i],s[i+1])
lst.append(i)
else:
pass
if 'B' in s:
s = new
lst = []
for i in range(len(s)-1):
if s[i]=='W':
s[i],s[i+1]= swap(s[i],s[i+1])
lst.append(i)
else:
pass
if 'W' in s:
print(-1)
else:
print(len(lst))
for a in lst:
print(a+1,end=" ")
else:
print(len(lst))
for a in lst:
print(a+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())
s = input()
b = 0
w = 0
for i in s:
if i == 'B': b += 1
else: w += 1
if b % 2 and w % 2: print(-1)
else:
ch = 'W'
if w % 2:
ch = 'B'
ans = []
for i in range(n):
if s[i] == ch:
s = list(s)
if s[i] == 'B': s[i] = 'W'
else: s[i] = 'B'
if s[i + 1] == 'B': s[i + 1] = 'W'
else: s[i + 1] = 'B'
s = ''.join(s)
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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
//_________________________________________________________________
public class Main {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
// Scanner sc= new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// int t=sc.nextInt();
// while (t-->=1){
int n=sc.nextInt();
char ch[]=sc.next().toCharArray();
ArrayList<Integer> list = new ArrayList<>();
Map<Character,Integer> map = new LinkedHashMap<>();
for (int i=0;i<ch.length;i++){
map.put(ch[i],map.getOrDefault(ch[i],0)+1);
}
if ((map.get('B')==null&&map.get('W')==n)||(map.get('B')==n&&map.get('W')==null)){
System.out.println(0);
return;
}
// System.out.println(map.get('B')+" "+map.get('W'));
if (map.get('B')%2==0||map.get('W')%2==0){
if (map.get('B')%2==0){
for (int i=0;i<n-1;i++){
if (ch[i]=='B'&&ch[i+1]=='B'){
list.add(i+1);
ch[i+1]='W';
}
else if (ch[i]=='B'&&ch[i+1]!='B'){
list.add(i+1);
ch[i+1]='B';
}
}
}
else{
for (int i=0;i<n-1;i++){
if (ch[i]=='W'&&ch[i+1]=='W'){
list.add(i+1);
ch[i+1]='B';
}
else if (ch[i]=='W'&&ch[i+1]!='W'){
list.add(i+1);
ch[i+1]='W';
}
}
}
System.out.println(list.size());
for (int i:list){
System.out.print(i+" ");
}
// System.out.println();
}
else{
System.out.println(-1);
}
out.flush();
}
//------------------------------------if-------------------------------------------------------------------------------------------------------------------------------------------------
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static String sortString(String s) {
char temp[] = s.toCharArray();
Arrays.sort(temp);
return new String(temp);
}
static class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair other) {
if (this.a == other.a) return other.b > this.b ? -1 : 1;
else if (this.a > other.a) return 1;
else return -1;
}
}
static int[] frequency(String s){
int fre[]= new int[26];
for (int i=0;i<s.length();i++){
fre[s.charAt(i)-'a']++;
}
return fre;
}
static int mod =(int)1e9;
static long mod(long x) {
return ((x % mod + mod) % mod);
}
static long add(long x, long y) {
return mod(mod(x) + mod(y));
}
static long mul(long x, long y) {
return mod(mod(x) * mod(y));
}
static int[] find(int n, int start, int diff) {
int a[] = new int[n];
a[0] = start;
for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff;
return a;
}
static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
static void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
static boolean sorted(int a[]) {
int n = a.length;
boolean flag = true;
for (int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) flag = false;
}
if (flag) return true;
else return false;
}
public static int findlog(long n) {
if (n == 0)
return 0;
if (n == 1)
return 0;
if (n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if (den == 0)
return 0;
return (int) (num / den);
}
public static long gcd(long a, long b) {
if (b % a == 0)
return a;
return gcd(b % a, a);
}
public static int gcdInt(int a, int b) {
if (b % a == 0)
return a;
return gcdInt(b % a, a);
}
static void sortReverse(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
// Collections.sort.(l);
Collections.sort(l, Collections.reverseOrder());
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(long n) {
long[] a = new long[(int) n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
#for _ in range(int(input())):
n=int(input())
s=input()
t=s
s=list(s)
ans=[]
for i in range(n-1):
if s[i]=="W":
continue
else:
ans.append(i+1)
s[i+1]="W" if s[i+1]=="B" else "B"
if s[-1]=="W":
print(len(ans))
print(*ans)
else:
ans=[]
t = list(t)
for i in range(n - 1):
if t[i] == "B":
pass
else:
ans.append(i + 1)
t[i + 1] = "W" if t[i + 1] == "B" else "B"
if t[-1]=="B":
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 | from math import sqrt
n=int(input())
s=[i for i in input()]
if (s.count('W'))%2 and (s.count('B'))%2:print(-1);exit()
else:
i=0;ans=[]
if s.count('B')%2==0:
while i<n-1:
if s[i]=='B':
ans.append(i+1)
if s[i+1]=='W':s[i+1]='B'
else:s[i+1]='W'
i+=1
# print(s)
else:
i=0;ans=[]
while i<n-1:
if s[i]=='W':
ans.append(i+1)
if s[i+1]=='W':s[i+1]='B'
else:s[i+1]='W'
i+=1
# print(ans)
print(len(ans))
if len(ans)!=0:print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | def func(s,l,c):
if c=="W":p="B"
else:p="W"
for i in range(n-1):
if s[i]==c:s[i]=p;s[i+1]=c if s[i+1]==p else p;l.append(i+1)
if len(set(s))==1:return l
else:return []
n,s=int(input()),list(input())
a,b=func(list(s),[],"B"),func(list(s),[],"W")
if len(set(s))==1:print(0)
elif not a and not b:print(-1)
else:print(*[len(a)]+a if not b else[len(b)]+b) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<int> ans;
char s[221];
int main() {
int n;
scanf("%d %s", &n, s);
for (int 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);
}
}
if (s[n - 1] == 'B') {
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i)
printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " ");
return 0;
}
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'B') {
s[i] = 'W';
s[i + 1] = (s[i + 1] == 'W') ? 'B' : 'W';
ans.push_back(i + 1);
}
}
if (s[n - 1] == 'W') {
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i)
printf("%d%s", ans[i], (i == ans.size() - 1) ? "\n" : " ");
return 0;
}
printf("-1\n");
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<int> v;
bool slove(string s, char co, char nc) {
v.clear();
for (int i = 0; i < s.length() - 1; ++i) {
if (s[i] != co) {
s[i] = co;
if (s[i + 1] != co)
s[i + 1] = co;
else
s[i + 1] = nc;
v.push_back(i + 1);
}
}
for (int i = 0; i < s.length(); ++i) {
if (s[i] != co) return false;
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
return true;
}
int main() {
int n;
cin >> n;
getchar();
string s;
cin >> s;
if (slove(s, 'B', 'W'))
return 0;
else if (slove(s, 'W', 'B'))
return 0;
else
cout << "-1";
return 0;
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(raw_input())
s = raw_input()
b = s.count('B')
w = s.count('W')
if b % 2 == 1 and w % 2 == 1:
print '-1'
elif b % 2 == 0:
s = list(s)
a = []
for i in xrange(n):
if s[i] == 'B':
s[i] = 'W'
s[i+1] = 'W' if s[i+1] == 'B' else 'B'
a.append(i+1)
print len(a)
print ' '.join(map(str, a))
else:
s = list(s)
a = []
for i in xrange(n):
if s[i] == 'W':
s[i] = 'B'
s[i+1] = 'B' if s[i+1] == 'W' else 'W'
a.append(i+1)
print len(a)
print ' '.join(map(str, a))
| PYTHON |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
a = list(input())
ops = list()
def opp(a):
return 'B' if a=='W' else 'W'
for i in range(n-1):
if a[i] == "W":
a[i] == "B"
a[i+1] = opp(a[i+1])
ops.append(i+1)
else:
continue
if a[n-1] == 'B':
print(len(ops))
print(' '.join(map(str, ops)))
else:
if n%2 == 0:
print(-1)
else:
ops.extend(list(range(1, n, 2)))
print(len(ops))
print(' '.join(map(str, ops))) | 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 c = 0;
vector<int> v;
int n;
cin >> n;
string s;
cin >> s;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b == 0 && w == n)
cout << "0";
else if (w == 0 && b == n)
cout << "0";
else {
char ch;
if (s[0] == 'B')
ch = 'W';
else
ch = 'B';
for (int i = 0; i < n - 1; i++) {
if (s[i] == ch) {
if (s[i] == 'B')
s[i] = 'W';
else
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
v.push_back(i);
c++;
}
}
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b == 0 && w == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else if (w == 0 && b == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else {
char ch;
if (s[n - 1] == 'B')
ch = 'W';
else
ch = 'B';
for (int i = n - 1; i > 0; i--) {
if (s[i] == ch) {
if (s[i] == 'B')
s[i] = 'W';
else
s[i] = 'B';
if (s[i - 1] == 'B')
s[i - 1] = 'W';
else
s[i - 1] = 'B';
v.push_back(i - 1);
c++;
}
}
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b == 0 && w == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else if (w == 0 && b == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else {
char ch;
if (s[0] == 'B')
ch = 'W';
else
ch = 'B';
for (int i = 0; i < n - 1; i++) {
if (s[i] == ch) {
if (s[i] == 'B')
s[i] = 'W';
else
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
v.push_back(i);
c++;
}
}
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
if (b == 0 && w == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else if (w == 0 && b == n) {
cout << c << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << " ";
} else
cout << "-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 | n = int(input())
s = input()
if s.count("W")%2 == 1 and s.count("B")%2 == 1:
print(-1)
else:
if s.count("B")%2==0:
odp = []
cur = list(s)
for i in range(n-1):
if cur[i] != "W":
odp.append(i+1)
if cur[i+1] == "W":
cur[i+1] = "B"
else:
cur[i+1] = "W"
print(len(odp))
print(*odp)
else:
odp = []
cur = list(s)
for i in range(n-1):
if cur[i] != "B":
odp.append(i+1)
if cur[i+1] == "B":
cur[i+1] = "W"
else:
cur[i+1] = "B"
print(len(odp))
print(*odp) | 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 do(s):
if s == "B":
return 1
return 0
n = input()
arr = map(do,raw_input())
def f(a):
return (a+1)%2
last = arr[0]
ret = []
for i in range(1,n-1):
if arr[i] != last:
ret.append(i+1)
arr[i] = f(arr[i])
arr[i+1] = f(arr[i+1])
last = arr[i]
if arr[-1] == arr[0]:
print len(ret)
print " ".join(map(str,ret))
else:
if n%2 == 0:
print -1
else:
for i in range(0,n-1,2):
ret.append(i+1)
print len(ret)
print " ".join(map(str,ret))
| 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;
using ll = long long;
const ll mod = 1e9 + 7;
const int q = 2e5 + 5;
const int INF = 1e9;
void done() {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> pos;
int ok = 0;
for (int i = 2; i < n; i++) {
if (a[i] > a[i - 1] && a[i] > a[i + 1]) pos.push_back(i);
}
int cnt = 0, d = 1, ans = 1;
for (int i = 0, j = 0; i < pos.size(); i++) {
int q = pos[i];
while ((pos[j] - q) + 2 <= k && j < pos.size()) {
j++;
}
if (j - i > d) {
ans = pos[i] - 1;
cnt = j - i + 1;
d = j - i;
ok = 1;
}
}
cout << cnt << " " << ans << '\n';
}
void solve() {}
void another() {
int n;
int b, w, cnt = 0;
string first;
int a[205];
int t[205];
cin >> n;
cin >> first;
for (int i = 0; i < first.size(); i++) {
if (first[i] == 'B')
t[i] = 0, b++;
else
t[i] = 1, w++;
}
if (b % 2 & w % 2) {
cout << -1;
return;
}
b = b % 2 ? 0 : 1;
for (int i = 0; i < n - 1; i++) {
if (t[i] ^ b) {
t[i] ^= 1;
t[i + 1] ^= 1;
a[cnt++] = i + 1;
}
}
cout << cnt << endl;
for (int i = 0; i < cnt; i++) cout << a[i] << ' ';
return;
}
void test_case() {
int t;
cin >> t;
while (t--) another();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
another();
}
| 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()
x=s.count('B')
y=s.count('W')
l= []
for i in s:
if(i=='W'):
l.append(1)
else:
l.append(0)
if(x&1 and y&1):
print(-1)
exit()
if(x==0 or y==0):
print(0)
exit()
ans = []
for i in range(1,len(s)-1):
if(l[i]!=l[i-1]):
l[i] = not(l[i])
l[i+1] = not(l[i+1])
ans.append(i+1)
x = l.count(0)
y = l.count(1)
for i in range(0,len(l),2):
if(l[i] == x&1):
ans.append(i+1)
print(len(ans))
print(*ans)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n, s, l = int(input()), list(input()), []
w, b, default = s.count('W'), s.count('B'), 'B'
if w % 2 and b % 2:
exit(print(-1))
elif w % 2:
default = 'W'
for i in range(n-1):
if s[i] != s[i + 1] and s[i] != default:
s[i], s[i + 1] = s[i + 1], s[i]
l.append(i+1)
else:
if s[i] == s[i + 1] and s[i] != default:
s[i], s[i + 1] = default, default
l.append(i+1)
# print(s)
print(len(l))
print(*l)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
input = sys.stdin.readline
from collections import *
n = int(input())
S = list(input()[:-1])
if S in [['B']*n, ['W']*n]:
print(0)
exit()
B, W = 0, 0
for Si in S:
if Si=='B':
B += 1
else:
W += 1
if B%2==1 and W%2==1:
print(-1)
exit()
ans = []
if B%2==0:
l = []
for i in range(n):
if S[i]=='B':
l.append(i)
for i in range(0, len(l), 2):
s = l[i]
t = l[i+1]
for i in range(s, t):
ans.append(i)
else:
l = []
for i in range(n):
if S[i]=='W':
l.append(i)
for i in range(0, len(l), 2):
s = l[i]
t = l[i+1]
for i in range(s, t):
ans.append(i)
print(len(ans))
print(*list(map(lambda x: x+1, ans))) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n = int(input())
s = list(input())
dup_s = s[::]
i = 0
a1, a2 = list(), list()
while i < n:
if s[i] == 'W':
i += 1
continue
if i < n-1:
s[i] = 'W'
if s[i+1] == 'B':
s[i+1] = 'W'
else:
s[i+1] = 'B'
a1.append(i+1)
i += 1
i = 0
while i < n:
if dup_s[i] == 'B':
i += 1
continue
if i < n-1:
dup_s[i] = 'B'
if dup_s[i+1] == 'W':
dup_s[i+1] = 'B'
else:
dup_s[i+1] = 'W'
a2.append(i+1)
i += 1
if dup_s.count('B') == 0 or dup_s.count('B') == n:
print(len(a2))
print(*a2)
elif s.count('B') == 0 or s.count('B') == n:
print(len(a1))
print(*a1)
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())
inp = input()
blacks = inp.count("B")
whites = inp.count("W")
ansCount_ = 0
ansList = []
if blacks % 2 == 1 and whites % 2 == 1:
print(-1)
else:
if blacks % 2 == 1 and whites % 2 == 0:
changeTo = "W"
elif blacks % 2 == 0 and whites % 2 == 1:
changeTo = "B"
else:
if blacks > whites:
changeTo = "B"
else:
changeTo = "W"
for i in range(len(inp) - 1):
if inp[i] == changeTo:
inp = list(inp)
if changeTo == "B":
inp[i] = "W"
else:
inp[i] = "B"
if inp[i + 1] == "B":
inp[i + 1] = "W"
else:
inp[i + 1] = "B"
inp = "".join(inp)
ansCount_ += 1
ansList.append(i+1)
print(ansCount_)
print(" ".join(str(i) for i in ansList))
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | /* package whatever; // 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 final class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String str = s.next();
char[] strarr = str.toCharArray();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2= new ArrayList<>();
for(int i=0;i<str.length()-1;i++){
if(strarr[i] == 'W'){
strarr[i] = 'B';
strarr[i+1] = strarr[i+1] == 'W'?'B':'W';
list1.add(i);
}
}
boolean flag = true;
for(int i=0;i<str.length();i++){
if(strarr[i]!='B') {
flag= false;
break;
}
}
if(flag){
System.out.println(list1.size());
for(int i: list1){
System.out.print(i+1+" ");
}
return;
}
strarr = str.toCharArray();
for(int i=0;i<str.length()-1;i++){
if(strarr[i] == 'B'){
strarr[i] = 'W';
strarr[i+1] = strarr[i+1] == 'W'?'B':'W';
list2.add(i);
}
}
flag = true;
for(int i=0;i<str.length();i++){
if(strarr[i]!='W') {
flag= false;
break;
}
}
if(flag){
System.out.println(list2.size());
for(int i: list2){
System.out.print(i+1+" ");
}
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 main() {
vector<long long int> ans;
long long int n, cw, i;
char re;
string s;
cin >> n;
cin >> s;
cw = count(s.begin(), s.end(), 'W');
if (cw == 0 || cw == n)
cout << "0";
else {
re = s[n - 1];
for (i = n - 2; i > 0; i--) {
if (s[i] != re) {
s[i] = re;
s[i - 1] = s[i - 1] == 'W' ? 'B' : 'W';
ans.push_back(i - 1);
}
}
if (s[0] == s[n - 1]) {
cout << ans.size() << endl;
for (auto x : ans) cout << (x + 1) << " ";
} else {
if (n % 2 == 1) {
for (i = 1; i < n; i += 2) ans.push_back(i);
cout << ans.size() << endl;
for (auto x : ans) cout << (x + 1) << " ";
} else
cout << "-1";
}
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 |
s=int(input())
s=input()
arr=[]
ans=[]
for i in range(len(s)):
arr.append(s[i])
black=s.count('B')
white=s.count('W')
if(black%2==white%2==1):
print('-1')
else:
i=0
count=0
while(i<len(arr)-2):
if(arr[i]!=arr[i+1]):
count=count+1
arr[i+1]=arr[i]
if(arr[i+2]=='B'):
arr[i+2]='W'
else:
arr[i+2]='B'
ans.append(i+2)
i=i+1
if(arr.count('B')==0 or arr.count('W')==0):
print(count)
print(' '.join(map(str,ans)))
else:
count=count+len(s)//2
for i in range(len(s)//2):
ans.append((2*i)+1)
print(count)
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 | from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def tinput():
return input().split()
def rinput():
return map(int, tinput())
def rlinput():
return list(rinput())
mod = int(1e9)+7
# for _ in range(iinput()):
n = iinput()
s = input()
cntb = s.count('B')
cntw = n-cntb
if cntb % 2 and cntw % 2:
print(-1)
else:
if cntb % 2:
whites = []
for i in range(n):
if s[i] == 'W':
whites.append(i)
ans = []
for i in range(0, len(whites), 2):
for j in range(whites[i], whites[i+1]):
ans.append(str(j+1))
print(len(ans))
print(' '.join(ans))
elif cntw % 2:
blacks = []
for i in range(n):
if s[i] == 'B':
blacks.append(i)
ans = []
for i in range(0, len(blacks), 2):
for j in range(blacks[i], blacks[i+1]):
ans.append(str(j+1))
print(len(ans))
print(' '.join(ans))
else:
whites = []
for i in range(n):
if s[i] == 'W':
whites.append(i)
ans = []
for i in range(0, len(whites), 2):
for j in range(whites[i], whites[i+1]):
ans.append(str(j+1))
print(len(ans))
if len(ans) != 0:
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 | 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.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int N = in.nextInt();
char a[] = in.next().toCharArray();
ArrayList<Integer> operations = new ArrayList<>();
for (int i = 0; i < N - 1; i++) {
if (a[i] != a[0]) {
operations.add(i + 1);
if (a[i] == 'B') {
a[i] = 'W';
} else {
a[i] = 'B';
}
if (a[i + 1] == 'B') {
a[i + 1] = 'W';
} else {
a[i + 1] = 'B';
}
}
}
// see if first few is even
int c1 = 0, c2 = 0;
for (int i = 0; i < N; i++) {
if (a[i] == a[0]) {
c1++;
} else {
break;
}
}
if (c1 == N) {
out.printLine(operations.size());
for (int x : operations) {
out.print(x + " ");
}
out.printLine();
return;
}
for (int i = N - 1; i >= 0; i--) {
if (a[i] == a[N - 1]) {
c2++;
} else {
break;
}
}
if (c1 % 2 == 0) {
for (int i = 0; i < c1; i += 2) {
operations.add(i + 1);
if (a[i] == 'W') {
a[i] = a[i + 1] = 'B';
} else {
a[i] = a[i + 1] = 'W';
}
}
} else if (c2 % 2 == 0) {
for (int i = N - 1; i >= 0 && c2 > 0; i -= 2) {
operations.add(i + 1);
if (a[i] == 'W') {
a[i] = a[i - 1] = 'B';
} else {
a[i] = a[i - 1] = 'W';
}
c2 -= 2;
}
} else {
out.printLine(-1);
return;
}
out.printLine(operations.size());
for (int x : operations) {
out.print(x + " ");
}
out.printLine();
}
}
static class OutputWriter {
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();
}
}
static class InputReader {
BufferedReader in;
StringTokenizer tokenizer = null;
public InputReader(InputStream inputStream) {
in = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (IOException e) {
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
st=input()
s=list(st)
f=0
if 'B' not in s or 'W' not in s:
f=1
li=[]
for i in range(n-1):
if s[i]=='B' and s[i+1]=='B':
s[i]='W'
s[i+1]='W'
li.append(i+1)
elif s[i] == 'B' and s[i+1]=='W':
s[i]='W'
s[i+1]='B'
li.append(i+1)
if s[n-1]=='B':
for i in range(n-1):
if s[i]=='W' and s[i+1]=='W':
s[i]='B'
s[i+1]='B'
li.append(i+1)
elif s[i] == 'W' and s[i+1]=='B':
s[i]='B'
s[i+1]='W'
li.append(i+1)
if s[n-1]=='W' and not f:
print(-1)
elif not f:
print(len(li))
print(*li)
elif f:
print(0)
else:
if not f:
print(len(li))
print(*li)
elif f:
print(0)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys,math,bisect
from random import randint
inf = float('inf')
mod = (10**9)+7
"========================================"
def lcm(a,b):
return int((a/math.gcd(a,b))*b)
def gcd(a,b):
return int(math.gcd(a,b))
def tobinary(n):
return bin(n)[2:]
def binarySearch(a,x):
i = bisect.bisect_left(a,x)
if i!=len(a) and a[i]==x:
return i
else:
return -1
def lowerBound(a, x):
i = bisect.bisect_left(a, x)
if i:
return (i-1)
else:
return -1
def upperBound(a,x):
i = bisect.bisect_right(a,x)
if i!= len(a)+1 and a[i-1]==x:
return (i-1)
else:
return -1
def primesInRange(n):
ans = []
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
ans.append(p)
return ans
def primeFactors(n):
factors = []
while n % 2 == 0:
factors.append(2)
n = n // 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
factors.append(i)
n = n // i
if n > 2:
factors.append(n)
return factors
def isPrime(n,k=5):
if (n <2):
return True
for i in range(0,k):
a = randint(1,n-1)
if(pow(a,n-1,n)!=1):
return False
return True
"========================================="
"""
n = int(input())
n,k = map(int,input().split())
arr = list(map(int,input().split()))
"""
from collections import deque,defaultdict,Counter
import heapq,string
for _ in range(1):
n=int(input())
f=list(input())
s=f[::]
first= [ ]
second = []
mp = {'B':'W','W':'B'}
for i in range(n-1):
if f[i]=='W':
pass
else:
first.append(i+1)
f[i]='W'
f[i+1]=mp[f[i+1]]
if f[-1]=='W':
print(len(first))
print(*first)
else:
for i in range(n-1):
if s[i]=='B':
pass
else:
second.append(i+1)
s[i]='B'
s[i+1]=mp[s[i+1]]
if s[-1]=='B':
print(len(second))
print(*second)
else:
print(-1)
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
public class JavaApplication20 {
public static void main(String[] args) {
FastReader read = new FastReader();
int n = read.nextInt();
String s = read.nextLine();
StringBuilder str = new StringBuilder(s);
int w = 0; int b = 0;
for(int i = 0; i<n; i++){
if(s.charAt(i) == 'W'){
w++;
}
else{
b++;
}
}
if(b%2 != 0 && w%2 != 0){
System.out.println(-1);
return;
}
ArrayList<Integer> ans = new ArrayList<Integer>();
if(b%2 != 0){
for(int i = 0; i<n-1; i++){
if(str.charAt(i) == 'W'){
ans.add(i+1);
str.replace(i, i+1, "B");
if(str.charAt(i+1) == 'W'){
str.replace(i+1, i+2, "B");
}
else{
str.replace(i+1, i+2, "W");
}
}
}
}
else{
for(int i = 0; i<n-1; i++){
if(str.charAt(i) == 'B'){
ans.add(i+1);
str.replace(i, i+1, "W");
if(str.charAt(i+1) == 'B'){
str.replace(i+1, i+2, "W");
}
else{
str.replace(i+1, i+2, "B");
}
}
}
}
System.out.println(ans.size());
for(int i = 0; i<ans.size(); i++){
System.out.print(ans.get(i) + " ");
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.util.*;
import java.io.*;
public class B1271{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = 1;
br.readLine();
StringBuilder sb = new StringBuilder();
while((t--)>0){
char in[] = br.readLine().toCharArray();
int b = 0;
int a =0;
for(int i=0;i<in.length;i++){
if(in[i] == 'B')
b++;
else
a++;
}
if(a%2==1&&b%2==1){
System.out.println("-1");
return;
}
if(a==0 || b==0)
{
System.out.println("0");
return;
}
if(a%2==0){
find(in,'B');
}
else if(b%2==0){
find(in,'W');
}
}
}
private static void find(char[] in, char type) {
ArrayList<Integer> moves = new ArrayList<Integer>();
for(int i=0;i<in.length-1;i++){
if(in[i]!=type){
swap(in,i);
swap(in,i+1);
moves.add(i);
}
}
System.out.println(moves.size());
for(int i=0;i<moves.size();i++){
System.out.print(moves.get(i)+1+" ");
}
}
private static void swap(char[] in, int i) {
if(in[i] == 'W')
in[i] = 'B';
else if(in[i] == 'B')
in[i] = 'W';
}
static int PI(String s) {
return Integer.parseInt(s);
}
static int[] PA(String temp[]){
int arr[] =new int[temp.length];
for(int i=0;i<arr.length;i++){
arr[i] = PI(temp[i]);
}
return arr;
}
} | 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 invert_c(c):
if c == 'B':
return 'W'
return 'B'
def invert_s(s, i):
return s[:i] + invert_c(s[i]) + invert_c(s[i+1]) + s[i+2:]
# init
p = []
# input
n = int(raw_input())
s = raw_input()
# solve
for i in range(1, n-1):
if s[i] != s[i-1]:
p.append(i+1)
s = invert_s(s, i)
for i in range(n-2, 0, -1):
if s[i] != s[i+1]:
p.append(i)
s = invert_s(s, i-1)
# output
for i in range(0, n-1):
if s[i] != s[i+1]:
print -1
exit(0)
print len(p)
if len(p) > 0:
print " ".join(map(str, p))
| 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() {
long long int n, i = 0, b = 0, w = 0;
vector<long long int> v;
cin >> n;
char s[230];
cin >> s;
for (i = 0; i < n; i++) {
if (s[i] == 'W')
w++;
else
b++;
}
if (b % 2 == 1 && w % 2 == 1)
cout << "-1" << endl;
else {
if (w % 2 == 0) {
for (i = 0; i < n - 1;) {
if (s[i] == 'W') {
if (s[i + 1] == 'W') {
v.push_back(i + 1);
i += 2;
} else {
s[i + 1] = 'W';
v.push_back(i + 1);
i++;
}
} else
i++;
}
} else {
for (i = 0; i < n - 1;) {
if (s[i] == 'B') {
if (s[i + 1] == 'B') {
v.push_back(i + 1);
i += 2;
} else {
s[i + 1] = 'B';
v.push_back(i + 1);
i++;
}
} else
i++;
}
}
long long int x = v.size();
cout << x << endl;
for (i = 0; i < x; i++) 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 os
import sys
from io import BytesIO, IOBase
def solution(s, n):
if 'B' not in s or 'W' not in s:
write(0)
return
ss, res = list(s), []
for i in range(n - 1):
if ss[i] == 'B':
ss[i] = 'W'
ss[i + 1] = 'W' if s[i + 1] == 'B' else 'B'
res.append(i + 1)
if 'B' not in ss:
write(len(res))
write(*res)
return
ss, res = list(s), []
for i in range(n - 1):
if ss[i] == 'W':
ss[i] = 'B'
ss[i + 1] = 'B' if s[i + 1] == 'W' else 'W'
res.append(i + 1)
if 'W' not in ss:
write(len(res))
write(*res)
return
write(-1)
def main():
n = r_int()
s = input()
solution(s, n)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input():
return sys.stdin.readline().rstrip("\r\n")
def write(*args, end='\n'):
for x in args:
sys.stdout.write(str(x) + ' ')
sys.stdout.write(end)
def r_array():
return [int(x) for x in input().split()]
def r_int():
return int(input())
if __name__ == "__main__":
main()
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | n=int(input())
s=input()
A=[]
x=0
for i in range(n-1):
if(s[i]==s[i+1]):
x+=1
if(x==n-1):
print(0)
else:
for i in range(n-1):
if(s[i]=="B"):
if(s[i+1]=="B"):
s=s[:i]+"WW"+s[i+2:]
else:
s=s[:i]+"WB"+s[i+2:]
A.append(i+1)
if(s[-1]=="B"):
for i in range(n-1):
if(s[i]=="W"):
if(s[i+1]=="W"):
s=s[:i]+"BB"+s[i+2:]
else:
s=s[:i]+"BW"+s[i+2:]
A.append(i+1)
y=0
for i in range(n-1):
if(s[i]!=s[i+1]):
print(-1)
y+=1
break
if(y==0):
print(len(A))
for i in range(len(A)):
print(A[i], end=" ")
| PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import sys
# inf = open('input.txt', 'r')
# reader = (line.rstrip() for line in inf)
reader = (line.rstrip() for line in sys.stdin)
def inversions(s, n):
k = s.count('B')
if k % 2 == 0:
todel = 'B'
elif n % 2 == 1:
todel = 'W'
else:
return None
a = [c == todel for c in s]
ans = []
for i in range(n - 1):
if a[i]:
a[i] = not(a[i])
a[i+1] = not(a[i+1])
ans.append(i + 1)
return ans
n = int(next(reader))
s = next(reader)
ans = inversions(s, n)
if ans is None:
print(-1)
else:
print(len(ans))
print(*ans)
# inf.close()
| 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 i in range(n):
if s[i]=="B":
b+=1
else:
w+=1
if b%2 and w%2:
print(-1)
elif b==0 or w==0:
print(0)
else:
if b%2:
x="W"
y="B"
else:
x="B"
y="W"
ans=[]
for i in range(1,n):
if s[i]==x and s[i-1]==x:
s[i]=y
s[i-1]=y
ans.append(i)
if s[i]==y and s[i-1]==x:
for j in range(i,0,-1):
if s[i]==y and s[i-1]==x:
ans.append(i)
s[i]=x
s[i-1]=y
if len(ans)<=3*n:
print(len(ans))
print(*ans)
else:
print(-1) | PYTHON3 |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Main
{
public static void ans(int []arr,int n)
{
int[] warr=new int[n];
for(int i=0;i<n;i++){
warr[i]=arr[i];
}
int times=0;
ArrayList<Integer> c=new ArrayList<>();
for(int i=0;i<n-1;i++){
if(arr[i]==0)
{
arr[i]=1;
if(arr[i+1]==0)
arr[i+1]=1;
else
arr[i+1]=0;
times++;
c.add(i+1);
}
}
if(arr[n-1]==1)
{
System.out.println(times);
for(int i=0;i<c.size();i++)
System.out.print(c.get(i)+" ");
return;
}
int wtimes=0;
ArrayList<Integer> cw=new ArrayList<>();
for(int i=0;i<n-1;i++){
if(warr[i]==1)
{
warr[i]=0;
if(warr[i+1]==0)
warr[i+1]=1;
else
warr[i+1]=0;
wtimes++;
cw.add(i+1);
}
}
if(warr[n-1]==0)
{
System.out.println(wtimes);
for(int i=0;i<cw.size();i++)
System.out.print(cw.get(i)+" ");
return;
}
System.out.println(-1);
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int arr[]=new int[n];
String s=br.readLine();
int black=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='B'){
arr[i]=1;
black++;
}
else
arr[i]=0;
}
if(black==0 || black==s.length()){
System.out.println(0);
return;
}
ans(arr,n);
}} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 2e5 + 7;
int a[N];
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int b = 0, w = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B')
b++;
else
w++;
}
vector<int> v;
if (w % 2 && b % 2) {
cout << "-1" << '\n';
return;
} else if (w % 2) {
for (int i = n - 1; i >= 0; i--) {
while ((i - 1) >= 0 && s[i] == 'B') {
v.push_back(i - 1);
s[i] = 'W';
w++;
b--;
if (s[i - 1] == 'B')
s[i - 1] = 'W', w++, b--;
else
s[i - 1] = 'B', b++, w--;
}
if (!b) break;
}
} else {
for (int i = n - 1; i >= 0; i--) {
while ((i - 1) >= 0 && s[i] == 'W') {
v.push_back(i - 1);
s[i] = 'B';
w--;
b++;
if (s[i - 1] == 'B')
s[i - 1] = 'W', w++, b--;
else
s[i - 1] = 'B', b++, w--;
}
if (!w) break;
}
}
cout << v.size() << '\n';
for (int i = 0; i < v.size(); i++) {
cout << v[i] + 1 << " ";
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
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>
using namespace std;
signed main() {
long long n;
cin >> n;
string s;
cin >> s;
string t = s;
long long o = 0;
vector<long long> v;
for (long long i = 0; i < s.size() - 1; i++) {
if (s[i] == 'B') {
o++;
v.push_back(i);
s[i] = 'W';
if (s[i + 1] == 'W')
s[i + 1] = 'B';
else
s[i + 1] = 'W';
}
}
if (s[n - 1] == 'W') {
cout << o << endl;
for (auto i : v) cout << i + 1 << " ";
} else {
s = t;
vector<long long> v1;
long long o1 = 0;
for (long long i = 0; i < s.size() - 1; i++) {
if (s[i] == 'W') {
o1++;
v1.push_back(i);
s[i] = 'B';
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
if (s[n - 1] == 'B') {
cout << o1 << endl;
for (auto i : v1) cout << i + 1 << " ";
} else
cout << "-1";
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Created by flk on 2019/12/8.
*/
public class B {
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
Problem solver = new Problem();
solver.solve(in, out);
out.flush();
}
static class Problem {
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
int[] w = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; ++i) {
char ch = s.charAt(i);
if (ch == 'B') {
w[i] = 1;
b[i] = 2;
} else {
w[i] = 2;
b[i] = 1;
}
}
List<Integer> res = new ArrayList<>();
boolean flag = true;
for (int i = 0; i < n; ++i) {
if (w[i] == 0 || w[i] == 2) {
continue;
}
if (i == n - 1) {
if (w[i] == 1) {
flag = false;
break;
} else if (w[i] == 2) {
continue;
}
}
if (w[i] == 1) {
w[i]--;
w[i+1]--;
res.add(i);
}
if (w[i] > 0 && w[i+1] == 0) {
flag = false;
break;
}
if (w[i] > 0 && w[i+1] > 0) {
w[i]--;
w[i+1]--;
res.add(i);
}
}
if (flag) {
out.println(res.size());
for (int i = 0; i < res.size(); ++i) {
if (i != 0) {
out.print(' ');
}
out.print(res.get(i) + 1);
}
out.println();
} else {
res.clear();
flag = true;
for (int i = 0; i < n; ++i) {
if (b[i] == 0 || b[i] == 2) {
continue;
}
if (i == n - 1) {
if (b[i] == 1) {
flag = false;
break;
} else if (b[i] == 2) {
continue;
}
}
if (b[i] == 1) {
b[i]--;
b[i+1]--;
res.add(i);
}
if (b[i] > 0 && b[i+1] == 0) {
flag = false;
break;
}
if (b[i] > 0 && b[i+1] > 0) {
b[i]--;
b[i+1]--;
res.add(i);
}
}
if (flag) {
out.println(res.size());
for (int i = 0; i < res.size(); ++i) {
if (i != 0) {
out.print(' ');
}
out.print(res.get(i) + 1);
}
out.println();
} else {
out.println(-1);
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | JAVA |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, c = 0, c1 = 0;
cin >> n;
string s, s1;
cin >> s;
s1 = s;
vector<long long> v, v1;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'B') {
s[i] = 'W';
v.push_back(i + 1);
if (s[i + 1] == 'B')
s[i + 1] = 'W';
else
s[i + 1] = 'B';
}
}
for (long long i = 0; i < n - 1; i++) {
if (s1[i] == 'W') {
s1[i] = 'B';
v1.push_back(i + 1);
if (s1[i + 1] == 'B')
s1[i + 1] = 'W';
else
s1[i + 1] = 'B';
}
}
for (long long i = 0; i < n; i++) {
if (s[i] == 'W') c++;
}
for (long long i = 0; i < n; i++) {
if (s1[i] == 'B') c1++;
}
if (c != n && c1 != n)
cout << "-1";
else {
if (c == n) {
cout << v.size() << endl;
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
if (c1 == n) {
cout << v1.size() << endl;
for (long long i = 0; i < v1.size(); i++) cout << v1[i] << " ";
return 0;
}
}
}
| CPP |
1271_B. Blocks | There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white.
You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa).
You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β
n. If it is impossible to find such a sequence of operations, you need to report it.
Input
The first line contains one integer n (2 β€ n β€ 200) β the number of blocks.
The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black.
Output
If it is impossible to make all the blocks having the same color, print -1.
Otherwise, print an integer k (0 β€ k β€ 3 β
n) β the number of operations. Then print k integers p_1, p_2, ..., p_k (1 β€ p_j β€ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation.
If there are multiple answers, print any of them.
Examples
Input
8
BWWWWWWB
Output
3
6 2 4
Input
4
BWBB
Output
-1
Input
5
WWWWW
Output
0
Input
3
BWB
Output
2
2 1
Note
In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black.
It is impossible to make all colors equal in the second example.
All blocks are already white in the third example.
In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white). | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int countb = 0;
int countw = 0;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == 'B')
countb++;
else if (s[i] == 'W')
countw++;
}
if (countb == n or countw == n) {
cout << 0 << endl;
return 0;
}
int count = 0;
if (countw % 2 != 0 && countb % 2 != 0) {
cout << -1 << endl;
return 0;
} else {
if (countb % 2 == 0) {
vector<int> v;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'B') {
count++;
s[i] = (s[i] == 'B') ? 'W' : 'B';
s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B';
v.push_back(i + 1);
}
}
cout << count << endl;
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
} else if (countw % 2 == 0) {
vector<int> v;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'W') {
count++;
s[i] = (s[i] == 'B') ? 'W' : 'B';
s[i + 1] = (s[i + 1] == 'B') ? 'W' : 'B';
v.push_back(i + 1);
}
}
cout << count << endl;
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
}
}
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.