Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n=int(int(input())) s=input() a1=b1=0 k1=0 l=list(s) l1=[] for i in s: if i=='W': a1+=1 else: b1+=1 if a1%2==0: k1='W' k2='B' elif b1%2==0: k1='B' k2='W' if k1==0: print(-1) else: for i in range(n-1): if l[i]==k1: l[i]=k2 l1.append(i+1) if l[i+1]==k1: l[i+1]=k2 else: l[i+1]=k1 print(len(l1)) if len(l): print(*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
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; vector<int> nums; int a = 0; for (int i = (0); i < (n); i++) { if (s[i] == 'B') a++; } if (a & 1 and !(n & 1)) { cout << -1 << endl; return 0; } if (a & 1) { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } else { for (int i = (0); i < (n - 1); i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = 'B' + 'W' - s[i + 1]; nums.push_back(i); } } } cout << nums.size() << endl; for (int i = (0); i < (nums.size()); i++) cout << nums[i] + 1 << " \n"[i == nums.size() - 1]; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
x=int(input()) y=input() c=0 s=[] lis=y[0] t=True for i in range (x-1): if y[i]=="W": c+=1 else: c-=1 if y[i]!=lis and t==True: s=s+[str(i+1)] t=False elif y[i]==lis and t==False: s=s+[str(i+1)] t=False else: t=True if y[x-1]=="W" : c+=1 else: c-=1 if (y[x-1]==lis and t==False)or (y[x-1]!=lis and t==True): for j in range (x-2): if j%2==0: s=s+[str(j+1)] if c%4==x%4 or c%4==4-x%4: print(len(s)) print(" ".join(s)) 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 collections import Counter,deque,defaultdict,OrderedDict,namedtuple from bisect import bisect_left,bisect_right,insort from math import sqrt,ceil,floor,factorial,gcd from sys import stdin a=int(input()) s=input() c=Counter(s) if c['B']==0 or c['W']==0: print(0) elif c['B']&1 and c['W']&1: print(-1) else: n=len(s) t=s[:] s=list(s) cnt=float('inf') cnt2=float('inf') l1,l2=[],[] if c['B']&1==0: ans=c['B']//2 + ceil(c['W']/2) cnt=0 for i in range(n-1): if s[i]=='B': s[i]='W' if s[i+1]=='B': s[i+1]='W' else: s[i+1]='B' cnt+=1 l1.append(i+1) s=list(t) if c['W']&1==0: ans2=c['W']//2 + ceil(c['B']/2) cnt2=0 for i in range(n-1): if s[i]=='W': s[i]='B' if s[i+1]=='B': s[i+1]='W' else: s[i+1]='B' cnt2+=1 l2.append(i+1) if cnt<cnt2: print(cnt) print(*l1) else: print(cnt2) print(*l2)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import org.w3c.dom.ls.LSOutput; import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); scan.nextLine(); String s = scan.nextLine(); int counterW = 0; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'W') counterW++; } int counterB = n - counterW; char oldC; char newC; if (counterW % 2 == 0) { oldC = 'W'; newC = 'B'; } else { if (counterB % 2 == 0) { oldC = 'B'; newC = 'W'; } else { System.out.println(-1); return; } } StringBuilder str = new StringBuilder(s); StringBuilder res = new StringBuilder(); int counter = 0; for (int i = 0; i < n - 1; i++) { if (str.charAt(i) == oldC) { res.append(i + 1).append(" "); str.setCharAt(i + 1, str.charAt(i + 1) == 'W' ? 'B' : 'W'); counter++; } } System.out.println(counter); System.out.println(res); } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class compete608b { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String str = br.readLine(); int Ws = 0; char[] charArr = str.toCharArray(); for(char ch : charArr) { Ws += ch == 'W' ? 1 : 0; } if(n % 2 == 0 && Ws % 2 == 1) { System.out.println(-1); } else if (Ws == n || Ws == 0) { System.out.println(0); } else { StringBuilder sb = new StringBuilder(); char base = Ws % 2 == 1 ? 'W' : 'B'; int res = 0; for(int i = 0; i < charArr.length - 1; i++) { if(base != charArr[i]) { sb.append(i + 1 + " "); charArr[i] = base; charArr[i + 1] = charArr[i + 1] == 'W' ? 'B' : 'W'; res++; } } System.out.println(res + "\n" + sb); } } }
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 = s.count("B") r = [] if b%2!=0 and w%2!=0: exit(print(-1)) elif b%2!=0: s = [1 if x == "B" else 0 for x in s] elif w%2!=0: s = [1 if x =="W" else 0 for x in s] else: if w<b: s = [1 if x == "B" else 0 for x in s] else: s = [1 if x == "W" else 0 for x in s] for i in range(n//2): if s[i] == 0 and s[i+1] == 0: r.append(i+1) s[i] = s[i+1] = 1 elif s[i] == 0: r.append(i+1) s[i] = 1 s[i+1] = 0 for i in range(n-1,(n//2)-1,-1): if s[i] == 0 and s[i-1] == 0: r.append(i) s[i] = s[i-1] = 1 elif s[i] == 0: r.append(i) s[i-1] = 0 print(len(r)) if r != []: print(*r)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
a=int(input()) b=list(input()) n=0 n1=[] z=b[:] while b.count('W')!=a and b.count('B')!=a: e=b for i in range(len(b)-1): if(b[i]=='W'): b[i]='B' if(b[i+1]=='W'): b[i+1]='B' else: b[i+1]='W' n+=1 n1.append(i+1) if(e==b): while b.count('W')!=a and b.count('B')!=a: e=b[:] for i in range(len(b)-1): if(b[i]=='B'): b[i]='W' if(b[i+1]=='B'): b[i+1]='W' else: b[i+1]='B' n+=1 n1.append(i+1) if(e==b): print(-1) break else: print(n) if(n!=0): print(*n1) break break else: print(n) if(n!=0): print(*n1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int n; string s; vector<int> v[2]; bool f(char c, int ind) { int last = -1; for (int i = 1; i <= n; i++) { if (s[i - 1] == c) { if (last == -1) { last = i; v[ind].push_back(i); } else { last = -1; } } else if (last != -1) { v[ind].push_back(i); } } if (last != -1) { return 0; } else { return 1; } } int main() { cin >> n; cin >> s; bool bb = f('B', 0); bool ww = f('W', 1); if (bb && ww) { if (v[0].size() < v[1].size()) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } } else if (bb) { cout << v[0].size() << endl; for (int i : v[0]) cout << i << " "; } else if (ww) { cout << v[1].size() << endl; for (int i : v[1]) cout << i << " "; } else cout << -1; return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n=input() x=raw_input() cb=0 cw=0 for i in x: if i=='W': cw+=1 else: cb+=1 a=[i for i in x] if cw%2!=0 and cb%2!=0: print -1 else: c=0 arr=[] if cw%2!=0: for i in range(n-1): if a[i]=='B': c+=1 if a[i+1]=='B': a[i+1]='W' else: a[i+1]='B' arr.append(i) else: for i in range(n-1): if a[i]=='W': c+=1 if a[i+1]=='W': a[i+1]='B' else: a[i+1]='W' arr.append(i) print c for i in arr: print i+1, print
PYTHON
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.*; import java.lang.*; import java.io.*; public class Main { /* HashMap<> map=new HashMap<>(); TreeMap<> map=new TreeMap<>(); map.put(p,map.getOrDefault(p,0)+1); for(Map.Entry<> mx:map.entrySet()){ int v=mx.getValue(),k=mx.getKey(); }for (int i = 1; i <= 1000; i++) fac[i] = fac[i - 1] * i % mod; ArrayList<Pair<Character,Integer>> l=new ArrayList<>(); ArrayList<> l=new ArrayList<>(); HashSet<> has=new HashSet<>(); PriorityQueue<Integer> pq=new PriorityQueue<>(new Comparator<Integer>(){ public int compare(Integer a,Integer b){ return Integer.compare(b,a); } }); Arrays.sort(ar,new Comparator<int[]>() { public int compare(int[] a, int[] b) { return Double.compare(a[0], b[0]); } });*/ PrintWriter out; FastReader sc; long mod=(long)(1e9+7); long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; /****************************************************************************************** *****************************************************************************************/ public void sol(){ int n=ni(); StringBuilder sb=new StringBuilder(); char[] ar=rl(); int a=0,b=0; for(char ch:ar){ if(ch=='W')a++; else b++; }if(a%2!=0&&b%2!=0)pl("-1"); else{ int p=0; if(a%2==0){ for(int i=0;i<n-1;i++){ if(ar[i]=='W'&&ar[i+1]=='W'){ sb.append(i+1+" "); ar[i+1]='B'; p++; }else if(ar[i]=='W'){ ar[i+1]='W'; sb.append(i+1+" "); p++; } } }else{ for(int i=0;i<n-1;i++){ if(ar[i]=='B'&&ar[i+1]=='B'){ ar[i+1]='W'; sb.append(i+1+" "); p++; }else if(ar[i]=='B'){ ar[i+1]='B'; sb.append(i+1+" "); p++; } } }pl(p); pl(sb); } } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); int t=1; // t=g.ni(); while(t-->0) g.sol(); g.out.flush(); } /**************************************************************************************** *****************************************************************************************/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); } public long pow(long x,long y){ return (long)Math.pow(x,y); } public int pow(int x,int y){ return (int)Math.pow(x,y); } public double pow(double x,double y){ return Math.pow(x,y); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); }public long lcm(long a, long b) { return (a / gcd(a, b)) * b; } void sort1(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } 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); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } int[] sieve(int n) { boolean prime[] = new boolean[n+1]; int[] k=new int[n+1]; for(int i=0;i<=n;i++) { prime[i] = true; k[i]=i; } for(int p = 2; p <=n; p++) { if(prime[p] == true) { // sieve[p]=p; for(int i = p*2; i <= n; i += p) { prime[i] = false; // sieve[i]=p; while(k[i]%(p*p)==0){ k[i]/=(p*p); } } } }return k; } int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r, long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } public static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + "," + y; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(Pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.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
n = int(input()) s = str(input()) b = [] rz = [] for i in range(n): if s[i] == 'W': b += [0] else: b += [1] for i in range(n - 2): if b[i] != b[i + 1]: b[i + 1] = (b[i + 1] + 1) % 2 b[i + 2] = (b[i + 2] + 1) % 2 rz += [i + 1] if b[n - 1] != b[n - 2]: if n % 2 == 0: print(-1) else: print(len(rz) + n // 2) for i in range(len(rz)): print(rz[i] + 1, '', end='') for i in range(1, n, 2): print(i, '', end='') else: if rz == []: print(0) else: print(len(rz)) for i in range(len(rz)): print(rz[i] + 1, '', end='')
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> const long long MOD = 1000 * 1000 * 1000 + 7; const long long MOD1 = 998244353; using namespace std; long long power(unsigned long long x, unsigned long long y) { unsigned long long res = 1; while (y > 0) { if (y & 1) res = (unsigned long long)(res * x); y = y >> 1; if (x <= 100000000) x = (unsigned long long)(x * x); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long N; string S; cin >> N; cin >> S; vector<long long> pos; for (int i = 0; i < S.length() - 1; i++) { if (S[i] == 'B') continue; if (S[i] == 'W' && S[i + 1] == 'B') { S[i] = 'B'; S[i + 1] = 'W'; pos.push_back(i + 1); } if (S[i] == 'W' && S[i + 1] == 'W') { S[i] = 'B'; S[i + 1] = 'B'; pos.push_back(i + 1); } } if (S[N - 1] == 'B') { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) cout << pos[i] << ' '; } else { for (int i = N - 1; i >= 1; i--) { if (S[i] == 'W') continue; if (S[i] == 'B' && S[i - 1] == 'W') { S[i] = 'W'; S[i - 1] = 'B'; pos.push_back(i); } if (S[i] == 'B' && S[i - 1] == 'B') { S[i] = 'W'; S[i - 1] = 'W'; pos.push_back(i); } } if (S[0] == 'B') cout << -1; else { cout << pos.size() << '\n'; for (int(i) = (0); (i) < (pos.size()); ++(i)) { cout << pos[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
from sys import stdin from collections import deque mod = 10**9 + 7 # def rl(): # return [int(w) for w in stdin.readline().split()] from bisect import bisect_right from bisect import bisect_left from collections import defaultdict from math import sqrt,factorial,gcd,log2,inf,ceil # map(int,input().split()) # # l = list(map(int,input().split())) # from itertools import permutations # a = int(input()) # b = int(input()) # c = int(input()) # d = int(input()) # e = int(input()) # f = int(input()) # ans = 0 # # for i in range(d+1): # c = e*(min(i,a)) + f*(min(d-i,c,b)) # ans = max(c,ans) # print(c,i,a) # # print(ans) n = int(input()) s = list(input()) i = 0 ans = [] if len(set(s)) == 1: print(0) exit() while i<n: j = i while s[j]=='W': if j+1<n: if s[j+1] == 'B': s[j],s[j+1] = s[j+1],s[j] ans.append(j+1) else: s[j] = 'B' s[j+1] = 'B' ans.append(j+1) j+=1 if j == n: break i = j+1 if j == n: break if len(set(s)) == 1: print(len(ans)) print(*ans) exit() else: if s.count('B')%2 == 0: i = 0 while s[i] == 'B': ans.append(i+1) i+=2 if i>=n: break print(len(ans)) print(*ans) exit() else: print(-1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; string s; cin >> n >> s; vector<int64_t> ans, ans2; string s2 = s; bool ok = false, ok2 = false; map<char, char> m = {{'B', 'W'}, {'W', 'B'}}; for (int64_t i = 0; i < n - 1; ++i) { if (s[i] == 'B' or ok) { ok = true; if (s[i + 1] == 'B') ok = false; s[i] = m[s[i]]; s[i + 1] = m[s[i + 1]]; ans.emplace_back(i); } if (s2[i] == 'W' or ok2) { ok2 = true; if (s2[i + 1] == 'W') ok2 = false; s2[i] = m[s2[i]]; s2[i + 1] = m[s2[i + 1]]; ans2.emplace_back(i); } } if (!ok and s[n - 1] != 'B') { cout << ans.size() << '\n'; for (auto i : ans) cout << i + 1 << ' '; } else if (!ok2 and s2[n - 1] != 'W') { cout << ans2.size() << '\n'; for (auto i : ans2) cout << i + 1 << ' '; } else cout << -1; return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Main { static void solve() { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String s = scan.next(); boolean[] arr = new boolean[s.length()]; for(int i=0;i<arr.length;i++) { char ch = s.charAt(i); if (ch == 'B') { arr[i] = true; }else { arr[i] = false; } } boolean[] ones = Arrays.copyOf(arr, arr.length); List<Integer> onesRes = new ArrayList<>(); int i=0; while(i+1<ones.length) { if(!ones[i]) { ones[i] = true; ones[i+1] = !ones[i+1]; onesRes.add(i+1); } i++; } if(ones[ones.length-1]) { System.out.println(onesRes.size()); for (Integer val:onesRes) { System.out.print(val + " "); } return; } boolean[] zeroes = Arrays.copyOf(arr, arr.length); List<Integer> zeroesRes = new ArrayList<>(); i=0; while(i+1<zeroes.length) { if(zeroes[i]) { zeroes[i] = false; zeroes[i+1] = !zeroes[i+1]; zeroesRes.add(i+1); } i++; } if(!zeroes[zeroes.length-1]) { System.out.println(zeroesRes.size()); for (Integer val:zeroesRes) { System.out.print(val + " "); } return; } System.out.println(-1); // System.out.println(res); } public static void main(String[] args) { solve(); // Main m = new Main(); // Scanner scan = new Scanner(System.in); // long n = scan.nextLong(); // long r = scan.nextLong(); // List<Long> list = new ArrayList<>(); //// int[] arr = new int[n]; // for (long i=0;i<n;i++){ // list.add(scan.nextLong()); // } // System.out.println(ch); // solve(n, arr); // scan.close(); } static int[] countSort() { int[] input = new int[]{1, 4, 1, 2, 7, 5, 2}; int[] output = new int[input.length]; int[] count = new int[8]; for (int i = 0; i < input.length; i++) { count[input[i]]++; } //forgot to sum those values, iterate over count int sum = 0; for (int i = 0; i < count.length; i++) { sum += count[i]; count[i] = sum; } for (int i = 0; i < input.length; i++) { int pos = count[input[i]]; output[pos - 1] = input[i]; count[input[i]]--; } return output; } static class LRUCache { Map<Integer, Node> map; Node front; Node tail; int capacity; public LRUCache(int capacity) { map = new HashMap<>(capacity); this.capacity = capacity; } public int get(int key) { if (!map.containsKey(key)) { return -1; } Node node = map.get(key); if (node == front) { return front.data; } if (node == tail) { tail = node.prev; } front.prev = node; node.prev.next = node.next; if (node.next != null) { node.next.prev = node.prev; } node.next = front; node.prev = null; front = node; return front.data; } public void put(int key, int value) { Node node = new Node(key, value); ; if (front == null) { front = node; tail = node; map.put(key, node); return; } if (map.containsKey(key)) { node = map.get(key); node.data = value; if (node == front) { map.put(key, node); return; } if (node == tail) { tail = node.prev; } front.prev = node; node.prev.next = node.next; if (node.next != null) { node.next.prev = node.prev; } node.next = front; node.prev = null; front = node; map.put(key, node); } else if (map.size() < capacity) { front.prev = node; node.next = front; node.prev = null; front = node; map.put(key, node); } else { //remove a tail, insert to front map.remove(tail.key); if (tail.prev != null) { tail.prev.next = null; } tail = tail.prev; front.prev = node; node.next = front; node.prev = null; front = node; map.put(key, node); } } } static class Node { int key; int data; Node next; Node prev; Node(int key, int data) { this.key = key; this.data = data; } } static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } static class Trie { void add(String word) { } boolean search(String w) { return false; } } static class Node1 { public int val; public List<Node1> children; public Node1() { } public Node1(int _val, List<Node1> _children) { val = _val; children = _children; } } ; static class TrieNode { } static class KvNode implements Comparable<KvNode> { String key; int freq; public KvNode(String key, int freq) { this.key = key; this.freq = freq; } @Override public int compareTo(KvNode o) { if (this.key.equals(o.key)) { return 0; } return Integer.compare(o.freq, this.freq); } } static class ListNode { int val; ListNode next; ListNode(int x) { val = 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
// javac b.java && java _ import java.io.*; import java.util.*; public class b { public static void main(String[] args) { new b(); } FS in = new FS(); PrintWriter out = new PrintWriter(System.out); int n, pdn, pdb; char[] bloc; b() { n = in.nextInt(); bloc = in.next().toCharArray(); pdn = (n & 1); int cnt = 0; for (int i = 0; i < n; i++) if (bloc[i] == 'B') cnt++; pdb = (cnt & 1); if (pdn == 0 && pdb == 1) out.println(-1); else { if (cnt == 0) out.println(0); else { int cntB = cnt, cntW = n - cnt; char match = '_', other = '_'; if ((cntB & 1) == 0) { match = 'B'; other = 'W'; } else { match = 'W'; other = 'B'; } // send cnt(match) --> 0 cnt = 0; List<Integer> idx = new ArrayList<Integer>(); for (int i = 0; i < n - 1; i++) if (bloc[i] == match) { bloc[i] = other; if (bloc[i + 1] == match) bloc[i + 1] = other; else bloc[i + 1] = match; cnt++; idx.add(i + 1); } out.println(cnt); if (cnt != 0) { for (int i : idx) out.print(i + " "); out.println(); } } } out.close(); } int min(int a, int b) { if (a < b) return a; return b; } int max(int a, int b) { if (a > b) return a; return b; } long min(long a, long b) { if (a < b) return a; return b; } long max(long a, long b) { if (a > b) return a; return b; } boolean z(int x) { if (x == 0) return true; return false; } boolean z(long x) { if (x == 0) return true; return false; } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
def main(): n = int(input()) s = list(input()) ans = [] for i in range(1, n): if s[i-1] == 'W': ans.append(i) s[i-1] = 'B' if s[i-1] == 'W' else 'W' s[i] = 'B' if s[i] == 'W' else 'W' if 'W' in s: for i in range(1, n): if s[i-1] == 'B': ans.append(i) s[i-1] = 'B' if s[i-1] == 'W' else 'W' s[i] = 'B' if s[i] == 'W' else 'W' if 'B' in s: print("-1") return print(len(ans)) print(" ".join(str(i) for i in 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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int cnt[2] = {0}; for (auto &v : s) { if (v == 'B') cnt[0]++; else cnt[1]++; } if (cnt[0] % 2 && cnt[1] % 2) { cout << -1 << '\n'; } else { if (cnt[0] % 2 == 0) { for (auto &v : s) { if (v == 'W') v = 'B'; else v = 'W'; } } vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { ans.push_back(i + 1); if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; s[i] = 'B'; } } cout << ans.size() << '\n'; for (auto &v : ans) cout << v << ' '; cout << '\n'; } }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* * @author Rohini Chandra */ import java.util.*; import java.io.*; public class rough{ public static void main(String args[]) throws IOException{ /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out);*/ //List<Integer> list = new ArrayList<>(); //Map<Integer,Integer> map = new HashMap<>(); //Set<Integer> set = new HashSet<>(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(),b=0,w=0; char str[] = sc.next().toCharArray(); List<Integer> list = new ArrayList<>(); for(int i=0; i<n; i++){ if(str[i]=='B') b++; else w++; } if(b==0 || w==0) prt(0); else if(b%2!=0 && w%2!=0) prt(-1); else{ if(b%2==0){ for(int i=0; i<n-1; i++){ if(str[i]=='B' && str[i+1]=='B'){ list.add(i); i++; } else if(str[i]=='B' && str[i+1]=='W'){ str[i]='W'; str[i+1]='B'; list.add(i); } //System.out.println(" i= "+i+" "+list); } } else{ for(int i=0; i<n-1; i++){ if(str[i]=='W' && str[i+1]=='W'){ list.add(i); i++; } else if(str[i]=='W' && str[i+1]=='B'){ str[i]='B'; str[i+1]='W'; list.add(i); } } } System.out.println(list.size()); for(int x : list) System.out.print(x+1+" "); } } //******** PRINT ********// static void prt(long a){ System.out.print(a); } //******** PRINTLN ********// static void prtl(List<Integer> b){ System.out.println(b); } //******** FACTORIAL ********// static int fact(int n){ if(n==1) return 1; else return n*fact(n-1); } //******** OCCURRENCE ********// static Map<String,Integer> ocr(String str[]){ Map<String,Integer> map = new HashMap<>(); for(int i=0; i<str.length; i++){ if(!map.containsKey(str[i])) map.put(str[i], 1); else map.put(str[i], map.get(str[i])+1); } return map; } //******** PRIME OR NOT ********// static boolean isprime(long num){ if(num<=1) return false; else if(num<=3) return true; else if(num%2==0 || num%3==0) return false; else{ for(int i=5; i*i<=num; i+=6){ if(num%i==0 || num%(i+2)==0) return false; } return true; } } //******** GCD ************// static long gcd(long a, long b){ if(b==0) return a; else if(a==0) return b; else if(a>b) gcd(a%b,b); else if(a<b) gcd(a,b%a); else if(a==b) return a; return 0; } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import sys import collections from collections import Counter import itertools import math import timeit #input = sys.stdin.readline ######################### # imgur.com/Pkt7iIf.png # ######################### 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 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 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 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) def ceil(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 collections.defaultdict(int) def ddl(): return collections.defaultdict(list) n = ii() s = input() d = {'B':'W', 'W':'B'} rb = [] rw = [] white = [x for x in s] black = [x for x in s] for i in range(n - 1): if white[i] == 'B': rw.append(i + 1) white[i] = 'W' white[i + 1] = d[white[i + 1]] if black[i] == 'W': rb.append(i + 1) black[i] = 'B' black[i + 1] = d[black[i + 1]] if ''.join(white) == 'W'*n: print(len(rw)) prr(rw, ' ') elif ''.join(black) == 'B'*n: print(len(rb)) prr(rb, ' ') else: print(-1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; long long int xpow(long long int a, long long int b) { if (b == 0) return 1; if (b % 2 == 0) { long long int k = xpow(a, b / 2); return k * k; } if (b % 2 != 0) return a * xpow(a, b - 1); } void display(vector<int> v) { for (auto k : v) cout << k << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n; cin >> n; string s; cin >> s; string temp = s; vector<int> ans; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; ans.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } vector<int> ans1; for (int i = 0; i < n - 1; i++) { if (temp[i] == 'W') { temp[i] = 'B'; ans1.push_back(i + 1); if (temp[i + 1] == 'W') temp[i + 1] = 'B'; else temp[i + 1] = 'W'; } } set<char> se1, se2; for (auto c : s) se1.insert(c); for (auto c : temp) se2.insert(c); if (se1.size() == 1) { cout << ans.size() << endl; display(ans); } else if (se2.size() == 1) { cout << ans1.size() << endl; display(ans1); } else cout << "-1" << endl; return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
x=int(input()) y=input() c=0 s=[] q=y[0] t=True for i in range (x-1): if y[i]=="W": c+=1 else: c-=1 if y[i]!=q and t==True: s=s+[str(i+1)] t=False elif y[i]==q and t==False: s=s+[str(i+1)] t=False else: t=True if y[x-1]=="W" : c+=1 else: c-=1 if (y[x-1]==q and t==False)or (y[x-1]!=q and t==True): for j in range (x-2): if j%2==0: s=s+[str(j+1)] if c%4==x%4 or c%4==4-x%4: print(len(s)) print(" ".join(s)) 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() z = [i for i in s] if len(set(z)) == 1: print(0) exit() ans = [] i = 0 while i < n-1: if z[i] == 'B': i+=1 else: if z[i+1] == 'W': z[i] = 'B' z[i+1] = 'B' ans.append(i+1) i+=2 else: z[i] = 'B' z[i+1] = 'W' ans.append(i+1) i+=1 if len(set(z)) == 1: print(len(ans)) print(*ans) exit() z = [i for i in s] ans = [] i = 0 while i < n-1: if z[i] == 'W': i+=1 else: if z[i+1] == 'B': z[i] = 'W' z[i+1] = 'W' ans.append(i+1) i+=2 else: z[i] = 'W' z[i+1] = 'B' ans.append(i+1) i+=1 if len(set(z)) == 1: print(len(ans)) print(*ans) else: print(-1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
"""609C""" # import math # import sys def main(): # n ,m= map(int,input().split()) # a = list(map(int,input().split())) n = int(input()) string = str(input()) list1 = list(string) list2 = list(string) # flag = True arr = [] for i in range(n-1): if list1[i]=='B': list1[i]='W' arr.append(i+1) if list1[i+1]=='W': list1[i+1]='B' else: list1[i+1]='W' if list1[n-1]=='W': print(len(arr)) for x in arr: print(x,end=' ') print() return else: arr = [] for i in range(n-1): if list2[i]=='W': list2[i]='B' arr.append(i+1) if list2[i+1]=='W': list2[i+1]='B' else: list2[i+1]='W' if list2[n-1]=="B": print(len(arr)) for x in arr: print(x,end=' ') print() return print(-1) return main() # def test(): # t = int(input()) # while t: # main() # t-=1 # test()
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
z = int(input()) a = list(input()) l = [] for i in range(z-1): if a[i]=="B": if a[i+1]=="B": a[i]=a[i+1]="W" else: a[i]="W" a[i+1]="B" l.append(i+1) if len(set(a))==2: for i in range(z - 1): if a[i] == "W": if a[i + 1] == "W": a[i] = a[i + 1] = "B" else: a[i] = "B" a[i + 1] = "W" l.append(i + 1) if len(set(a))==2: print(-1) else: print(len(l)) print(*l) else: print(len(l)) print(*l)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Cf131 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 Cf131(),"Main",1<<27).start(); } static class Pair{ int nod; int ucn; Pair(int nod,int ucn){ this.nod=nod; this.ucn=ucn; } public static Comparator<Pair> wc = new Comparator<Pair>(){ public int compare(Pair e1,Pair e2){ //reverse order if(e1.ucn < e2.ucn) return 1; // 1 for swaping. else if (e1.ucn > e2.ucn) return -1; else{ // if(e1.siz>e2.siz) // return 1; // else // return -1; return 0; } } }; } public static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } ////recursive BFS public static int bfsr(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,int[] pre){ b[s]=true; int p = 0; int t = a[s].size(); for(int i=0;i<t;i++){ int x = a[s].get(i); if(!b[x]){ dist[x] = dist[s] + 1; p+= (bfsr(x,a,dist,b,pre)+1); } } pre[s] = p; return p; } //// iterative BFS public static int bfs(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,PrintWriter w){ b[s]=true; int siz = 0; Queue<Integer> q = new LinkedList<>(); q.add(s); while(q.size()!=0){ int i=q.poll(); Iterator<Integer> it = a[i].listIterator(); int z=0; while(it.hasNext()){ z=it.next(); if(!b[z]){ b[z]=true; dist[z] = dist[i] + 1; siz++; q.add(z); } } } return siz; } public static char alter(char x){ if(x=='B')return 'W'; else return 'B'; } ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int defaultValue=0; int n = sc.nextInt(); String s = sc.next(); //s = sc.next(); //w.println(s); char[] a = s.toCharArray(); int black = 0; int white = 0; int count = 0; ArrayList<Integer> ans = new ArrayList<Integer>(); boolean poss = true; for(int i=0;i<n;i++){ if(a[i]== 'B')black++; else white++; } if(white%2==1 && black%2==1){ w.print("-1"); } else { if(white%2==1){ //while(black!=0){ for(int i=0;i<n-1;i++){ if(a[i]=='W')continue; else{ count++; ans.add(i+1); a[i] = alter(a[i]); black--; a[i+1] = alter(a[i+1]); if(a[i+1]=='B')black++; } if(black == 0){ break; } } //} } else{ //while(white!=0){ for(int i=0;i<n-1;i++){ if(a[i]=='B')continue; else{ count++; ans.add(i+1); a[i] = alter(a[i]); black--; a[i+1] = alter(a[i+1]); if(a[i+1]=='W')white++; } if(white == 0){ break; } } //} } w.println(count); for(int i=0;i<count;i++){ w.print(ans.get(i)+" "); } w.println(""); } //int t = sc.nextInt(); // while(t-->0){ // } 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
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; const int inf = (int)1e9 + 99999, mod = (int)1e9 + 7; const long long linf = (long long)2e18 + 99999; int kob(int x, int y) { return ((long long)x * y) % mod; } int aza(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int kos(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } const int maxn = (int)1e5 + 777, maxn2 = (int)1e6 + 55; static long holdrand = 1L; void srand(unsigned int seed) { holdrand = (long)seed; } int rand() { return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff); } void solve() { int n; cin >> n; string s; cin >> s; int b = 0, w = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') b++; else w++; } if (b % 2 == w % 2 && w % 2 == 1) { cout << "-1"; return; } vector<int> res, v; if (b % 2 == 0) { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'B') { v.push_back(i); } } } else { for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'W') { v.push_back(i); } } } for (int i = 0; i < (int)v.size(); i += 2) { for (int j = v[i]; j < v[i + 1] - 1; ++j) res.push_back(j); res.push_back(v[i + 1] - 1); } cout << (int)res.size() << '\n'; for (int i = 0; i < (int)res.size(); i++) cout << res[i] + 1 << ' '; } int main() { int t = 1; while (t--) { solve(); } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
# n, m, r = (int(x) for x in input().split()) # buy = min(int(x) for x in input().split()) # sell = max(int(x) for x in input().split()) # if sell <= buy: # print(r) # else: # shares_nr = r // buy # print(shares_nr * (sell - buy) + r) def solvea(): ties_nr = int(input()) scarfs_nr = int(input()) gilets_nr = int(input()) jackets_nr = int(input()) tie_jacket_price = int(input()) sc_gl_jck_price = int(input()) if tie_jacket_price > sc_gl_jck_price: ans = tie_jacket_price * min(ties_nr, jackets_nr) jackets_nr -= min(ties_nr, jackets_nr) ans += sc_gl_jck_price * min((scarfs_nr, gilets_nr, jackets_nr)) else: ans = sc_gl_jck_price * min((scarfs_nr, gilets_nr, jackets_nr)) jackets_nr -= min((scarfs_nr, gilets_nr, jackets_nr)) ans += tie_jacket_price * min(ties_nr, jackets_nr) print(ans) def solveb(): n = int(input()) s = list(input()) if s.count('W') == 0: print(0) return if s.count('B') == 0: print(0) return operations = [] if s.count('W') % 2 == 0: for i in range(n - 1): x = s[i] if x == 'B': continue operations.append(i + 1) s[i] = 'B' if s[i + 1] == 'W': s[i + 1] = 'B' else: s[i + 1] = 'W' elif s.count('B') % 2 == 0: for i in range(n - 1): x = s[i] if x == 'W': continue operations.append(i + 1) s[i] = 'W' if s[i + 1] == 'B': s[i + 1] = 'W' else: s[i + 1] = 'B' else: print(-1) return print(len(operations)) print(*operations) if __name__ == "__main__": solveb()
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.*; public class Main { static char[] change(char[] c, int i) { if (c[i]=='W') c[i]='B'; else if (c[i]=='B') c[i]='W'; return c; } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); String s = in.next(); int cnt_w = 0; int cnt_b = 0; for (int i=0;i<n;i++) { if (s.charAt(i)=='W') cnt_w++; if (s.charAt(i)=='B') cnt_b++; } if (cnt_w%2==1 && cnt_b%2==1) { out.println(-1); out.close(); return; } if (cnt_w==0 || cnt_b==0) { out.println(0); out.close(); return; } ArrayList<Integer> ans = new ArrayList<Integer>(); // StringBuilder sb = new StringBuilder(s); char[] c = s.toCharArray(); if (cnt_w%2==0) { // white to black for (int i=0;i<n-1;i++) { if (c[i]=='W') { c = change(c, i); c = change(c, i+1); ans.add(i); } } } else { // black to white for (int i=0;i<n-1;i++) { if (c[i]=='B') { c = change(c, i); c = change(c, i+1); ans.add(i); } } } out.println(ans.size()); for (int i=0;i<ans.size();i++) { if (i!=ans.size()-1) { out.print((ans.get(i)+1)+" "); } else { out.println(ans.get(i)+1); } } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public 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
fast=lambda:stdin.readline().strip() zzz=lambda:[int(i) for i in fast().split()] z,zz=input,lambda:list(map(int,z().split())) szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz()) from re import * from sys import * from math import * from heapq import * from queue import * from bisect import * from string import * from itertools import * from collections import * from math import factorial as f from bisect import bisect as bs from bisect import bisect_left as bsl from collections import Counter as cc from itertools import accumulate as ac def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2)) def output(answer):stdout.write(str(answer)) ###########################---Test-Case---################################# """ If you Know me , Then you probably don't know me ! """ ###########################---START-CODING---############################## n=int(z()) arr=list(fast()) ans=0 lst=[] for i in range(n-1): x=arr[i] y=arr[i+1] if x=='B': arr[i]='W' ans+=1 lst.append(i+1) if y=='B': arr[i+1]='W' else: arr[i+1]='B' if arr[-1]=='B': for i in range(n-1): x=arr[i] y=arr[i+1] if x=='W': arr[i]='B' ans+=1 if ans>3*n: print(-1) exit() lst.append(i+1) if y=='W': arr[i+1]='B' else: arr[i+1]='W' if arr[-1]=='W': for i in range(n-1): x=arr[i] y=arr[i+1] if x=='B': arr[i]='W' ans+=1 if ans>3*n: print(-1) exit() lst.append(i+1) if y=='B': arr[i+1]='W' else: arr[i+1]='B' if arr[-1]=='W': print(ans) print(*lst) exit() else: print(-1) exit() else: print(ans) print(*lst) exit() else: print(ans) print(*lst)
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 namespace std::chrono; char change(char c) { if (c == 'B') return 'W'; else return 'B'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; vector<long long> op; for (long long i = 1; i < n - 1; i++) { if (s[i] == s[i - 1]) continue; s[i] = change(s[i]); s[i + 1] = change(s[i + 1]); op.push_back(i + 1); } if (s[n - 1] == s[n - 2]) { cout << op.size() << '\n'; for (long long x : op) cout << x << ' '; cout << '\n'; return 0; } for (long long i = n - 2; i > 0; i--) { if (s[i] == s[i + 1]) continue; s[i] = change(s[i]); s[i - 1] = change(s[i - 1]); op.push_back(i); } if (s[0] != s[1]) { cout << "-1\n"; return 0; } cout << op.size() << "\n"; for (long long x : op) cout << x << ' '; cout << '\n'; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) cubs = input() if cubs.count('W') % 2 + cubs.count('B') % 2 != n % 2: print(-1) else: count, s = 0, [] cubs = list(cubs) for i in range(n - 1): if cubs[i] == 'W': cubs[i] = 'B' if cubs[i + 1] == 'W': cubs[i + 1] = 'B' else: cubs[i + 1] = 'W' count += 1 s.append(str(i + 1)) if cubs[-1] == 'W': count += n // 2 for i in range(n // 2): s.append(str(i * 2 + 1)) print(count) print(' '.join(s))
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; void solve() { long long n; string s, t; cin >> n >> t; s = t; long long f = 1; std::vector<long long> v; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') continue; else { v.push_back(i + 1); if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } if (s[n - 1] == 'B') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else { v.clear(); for (long long i = 0; i < n - 1; i++) { if (t[i] == 'W') continue; else { v.push_back(i + 1); if (t[i + 1] == 'B') t[i + 1] = 'W'; else t[i + 1] = 'B'; } } if (t[n - 1] == 'W') { cout << v.size() << '\n'; for (auto i = v.begin(); i != v.end(); i++) cout << *i << ' '; } else cout << -1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T = 1; while (T--) solve(); return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static int check(char[] ch) { int B = 0, W = 0; for (int i = 0; i < ch.length; i++) if (ch[i] == 'B') B++; else W++; if (B == 0 || W == 0) return 1; return 0; } public static void main(String[] args) throws IOException { FastReader fs=new FastReader(); int l = fs.nextInt(); String s = fs.next(); char[] ch=s.toCharArray(); int B = 0, W = 0; for (int i = 0; i < l; i++) { if (s.charAt(i) == 'B') B++; else W++; } if (W == 0 || B == 0) System.out.println(0); else if (B % 2 != 0 && W % 2 != 0) System.out.println(-1); else { int cnt1 = 0; List<Integer> list1 = new ArrayList<>(); for (int i = 0; i < l; i++) { if (ch[i] == 'W' && i != l - 1) { ch[i] = 'B'; if (ch[i + 1] == 'B') ch[i + 1] = 'W'; else ch[i + 1] = 'B'; cnt1++; list1.add(i + 1); } } if(check(ch)==0){ for(int i=0;i<l-1;i+=2){ cnt1++; list1.add(i+1); } } System.out.println(cnt1); for (int i = 0; i < cnt1; i++) { if (i != 0) System.out.print(" "); System.out.print(list1.get(i)); } } } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m, flag = 0; string s, ss; vector<long long> res; long long a[1000009], b[1000009]; void check(long long num) { res.clear(); for (i = 2; i <= n; i++) { if (a[i - 1] != num) { a[i - 1] ^= 1; a[i] ^= 1; res.push_back(i - 1); } } if (a[n] == num) { cout << res.size() << '\n'; for (auto to : res) cout << to << ' '; cout << '\n'; exit(0); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; for (i = 1; i <= n; i++) { if (s[i - 1] == 'W') b[i] = 1, k++; } if (k == 0 || k == n) return cout << 0 << '\n', 0; for (i = 1; i <= n; i++) a[i] = b[i]; check(0); for (i = 1; i <= n; i++) a[i] = b[i]; check(1); cout << -1 << '\n'; exit(0); }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = nextInt(); int a[] = new int[n]; String s = next(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'B') a[i] = 0; else a[i] = 1; } ArrayDeque<Integer> ans = new ArrayDeque<>(); boolean change = false; for (int i = 1; i < n; i++) { if (change) { change = false; a[i] = (a[i] + 1) % 2; } if (i != n - 1 && a[i - 1] != a[i]) { a[i] = (a[i] + 1) % 2; ans.addLast(i); change = true; } } if (n % 2 == 1 && a[0] != a[n - 1]) { for (int i = 0; i < n - 1; i += 2) { a[i] = (a[i] + 1) % 2; a[i + 1] = (a[i + 1] + 1) % 2; ans.addLast(i); } } if (a[0] != a[n - 1]) { out.println(-1); } else { out.println(ans.size()); while (!ans.isEmpty()) out.print(ans.pollFirst() + 1 + " "); } out.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { 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
from collections import deque n=int(input()) s=input() d=deque() if((n&1)==0): w=0 b=0 for j,i in enumerate(s): if i=='W': d.append(j) w+=1 else: b+=1 if(w&1)==0 and (b&1)==0: c=0 ans=[] while(len(d)!=0): c+=1 t=d.popleft() if s[t+1]=='W': d.popleft() else: d.appendleft(t+1) ans.append(t+1) print(c) print(*ans) else: print(-1) else: w=0 b=0 for j,i in enumerate(s): if i=='W': w+=1 else: b+=1 c='' if(w==0 or b==0): print(0) else: if((w&1))==0: c='W' for j,i in enumerate(s): if i=='W': d.append(j) else: c='B' for j,i in enumerate(s): if i=='B': d.append(j) ans=[] while(len(d)!=0): t=d.popleft() if s[t+1]==c: d.popleft() else: d.appendleft(t+1) ans.append(t+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
t = int(input()) s = input();w =s.count('W');b=s.count('B') if w % 2 == 1 and b % 2 == 1:print(-1);exit() else: c = 0 p = 0 ans =[] x = 'W' if w % 2 ==0 else 'B' for i in range(t): 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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; string str2; str2 = str; int ans = 0; vector<int> a; int ans2 = 0; vector<int> a2; for (int i = 0; i < n - 1; i++) { if (str[i] == 'B') { ans++; a.push_back(i + 1); str[i] = 'W'; if (str[i + 1] == 'B') str[i + 1] = 'W'; else str[i + 1] = 'B'; } } for (int i = 0; i < n - 1; i++) { if (str2[i] == 'W') { ans2++; a2.push_back(i + 1); str2[i] = 'B'; if (str2[i + 1] == 'W') str2[i + 1] = 'B'; else str2[i + 1] = 'W'; } } for (int i = 0; i < n; i++) { if (str[i] == 'B') { for (int j = 0; j < n; j++) { if (str2[i] == 'W') { cout << "-1"; return 0; } } cout << ans2 << endl; for (int i = 0; i < a2.size(); i++) { cout << a2[i] << " "; } return 0; } } cout << ans << endl; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.ArrayList; import java.util.Scanner; public class B608 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner obj=new Scanner(System.in); int n=obj.nextInt(); char[] ch=obj.next().toCharArray(); int k=0,m=0; ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<n-1;i++) { if(ch[i]=='B') { ch[i]='W'; if(ch[i+1]=='B') ch[i+1]='W'; else ch[i+1]='B'; arr.add(i+1); } } if(ch[n-1]!='W') { for(int i=0;i<n-1;i++) { if(ch[i]=='W') { ch[i]='B'; if(ch[i+1]=='B') ch[i+1]='W'; else ch[i+1]='B'; arr.add(i+1); } } if(ch[n-1]!='B') { System.out.println("-1"); } else { System.out.println(arr.size()+" "); for(int i:arr) { System.out.print(i+" "); } System.out.println(); } } else { System.out.println(arr.size()+" "); for(int i:arr) { System.out.print(i+" "); } System.out.println(); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n=int(input()) s1=str(input()) x1=s1.count('W') x2=s1.count('B') if(x1==0 or x2==0): print(0) elif(x1%2!=0 and x2%2!=0): print(-1) else: k=0 l=[] s=list(s1) if(True): i=0 if(x1%2!=0): ch='W' else: ch='B' if(True): while(i<n-1): if(s[i]==s[i+1] and s[i]!=ch): k+=1 l.append(i+1) i+=2 elif(s[i]!=s[i+1] and s[i]!=ch): k+=1 l.append(i+1) if(s[i+1]=='B'): s[i+1]='W' else: s[i+1]='B' i+=1 elif(s[i]==s[i+1]): i+=2 else: i+=1 print(k) for i in range(len(l)): print(l[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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0; vector<int> a; for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'W') { s.at(i) = 'W'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } int flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != 'W') { flag = 0; break; } } if (flag == 0) { for (int i = 0; i < n - 1; i++) { if (s.at(i) != 'B') { s.at(i) = 'B'; a.push_back(i + 1); c++; if (s.at(i + 1) == 'W') { s.at(i + 1) = 'B'; } else { s.at(i + 1) = 'W'; } } } } flag = 1; for (int i = 0; i < n; i++) { if (s.at(i) != s.at(0)) { flag = 0; break; } } if (flag) { cout << c << '\n'; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } } else { cout << -1; } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n=int(input()) a=input() w=a.count('W') b=n-w if w%2==1 and b%2==1: print(-1) else: x='W' if w%2==0 else 'B' c=0 p=0 ans=[] for i in range(n): if (a[i]==x and p==0) or (a[i]!=x and p==1): c+=1 p=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
k = int(input()) s = input() if s.count('W') == k or s.count('B') == k: print(0) elif s.count('W') % 2 == 1 and s.count('B') % 2 == 1: print(-1) else: ans1 = 9999999999999 l1 = [] if s.count('W') % 2 == 0: t = 0 s1 = list(s) if s[0] == 'W': prev = 1 else: prev = -1 for i in range(1, k): if prev != -1 and s[i] == 'W': # s1[i] = 'B' # s1[i - 1] = 'B' l1.append(i) t += 1 prev = -1 elif prev != -1: l1.append(i) t += 1 elif s[i] == 'W': prev = 1 ans1 = t ans2 = 9999999999999 l2 = [] if s.count('B') % 2 == 0: t = 0 s1 = list(s) if s[0] == 'B': prev = 1 else: prev = -1 for i in range(1, k): if prev != -1 and s[i] == 'B': # s1[i] = 'B' # s1[i - 1] = 'B' l2.append(i) t += 1 prev = -1 elif prev != -1: l2.append(i) t += 1 elif s[i] == 'B': prev = 1 ans2 = t if ans1 < ans2: print(ans1) print(*l1) else: print(ans2) print(*l2)
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
'''input 3 BWB ''' import sys debug = 1 readln = sys.stdin.readline #sys.setrecursionlimit(1000000) def write(s): sys.stdout.write(str(s)) def writeln(s): sys.stdout.write(str(s)) sys.stdout.write('\n') def readint(): return int(readln().strip()) def readints(): return map(int, readln().split()) def readstr(): return readln().strip() def readstrs(): return readln().split() def dprint(*args): if debug: print(' '.join(map(str, args))) def solve(s, tar): n = 0 idxs = [] last = s[0] for i in xrange(1, len(s)): if last != tar: n += 1 idxs.append(i) last = 'B' if s[i] == 'W' else 'W' else: last = s[i] if last != tar: return -1, [] else: return n,idxs n = readints() s = readstr() n, idxs = solve(s, 'W') if n < 0: n, idxs = solve(s, 'B') writeln(n) if n > 0: writeln(' '.join(map(str, idxs)))
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
number = int(input()) words = input() word = [] for i in range(0,number): if(words[i]=='B'): word.append(0) elif(words[i]=='W'): word.append(1) arr = [] kim = word[0] for i in range(1,number-1): if(word[i]!=kim): word[i]=1-word[i] word[i+1]=1-word[i+1] arr.append(i+1) #print(word) if(word[number-1]==kim): print(len(arr)) for i in arr: print(i,end = ' ') elif(number%2==0): print("-1") else: for i in range(0,number-1): if(i %2 == 0): word[i]=1-word[i] word[i+1]=1-word[i+1] arr.append(i+1) #print(word) print(len(arr)) for i in arr: print(i,end = ' ')
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) s = input() Num_B = s.count('B') Num_W = s.count('W') Num_index = [] test = 0 if Num_B % 2 == 1 and Num_W % 2 == 1: print(-1) test = -1 elif Num_B % 2 == 0 and Num_W % 2 == 0: if Num_B >= Num_W: for x in range(len(s)): if s[x] == 'W': Num_index.append(x) else: for x in range(len(s)): if s[x] == 'B': Num_index.append(x) elif Num_B % 2 == 1: for x in range(len(s)): if s[x] == 'W': Num_index.append(x) elif Num_W % 2 == 1: for x in range(len(s)): if s[x] == 'B': Num_index.append(x) if test != -1: k = 0 move = [] for x in range(0,len(Num_index),2): k += (Num_index[x+1] - Num_index[x]) move += [(i+1) for i in range(Num_index[x],Num_index[x+1])] if k > 3 * n: print(-1) else: print(k) print(*move)
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; signed main() { long long n; cin >> n; long long flag = 0; string str; cin >> str; string s = str; vector<long long> vec; for (long long i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; vec.push_back(i + 1); } } if (s[n - 1] != 'W') { for (long long i = n - 1; i > 0; i--) { if (s[i] == 'W') { s[i] = 'B'; if (s[i - 1] == 'W') s[i - 1] = 'B'; else s[i - 1] = 'W'; vec.push_back(i); } } if (s[0] == 'W') flag = 1; } if (flag == 1) cout << "-1"; else { cout << vec.size() << endl; for (long long i = 0; i < vec.size(); i++) cout << vec[i] << " "; } }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.util.*; public class Question2 { public static void solve(List<Character> s, int n) { List<Integer> ret = new ArrayList<>(); List<Character> s1 = new ArrayList<>(s); for(int i=0; i<n-1; i++) { if (s1.get(i) == 'W') { s1.set(i, 'B'); s1.set(i+1, s1.get(i+1) == 'B' ? 'W' : 'B'); ret.add(i+1); } } if (s1.get(s1.size()-1) == 'B') { out.println(ret.size()); for(int x : ret) { out.print(x + " "); } return; } ret = new ArrayList<>(); List<Character> s2 = new ArrayList<>(s); for(int i=0; i<n-1; i++) { if (s2.get(i) == 'B') { s2.set(i, 'W'); s2.set(i+1, s2.get(i+1) == 'B' ? 'W' : 'B'); ret.add(i+1); } } if (s2.get(s2.size()-1) == 'W') { out.println(ret.size()); for(int x : ret) { out.print(x + " "); } return; } out.println(-1); } public static void main(String[] args){ MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); List<Character> s = new ArrayList<>(); for (char ch : sc.nextLine().toCharArray()) { s.add(ch); } solve(s, n); out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.*; public class KosyaReshaetZadachi { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String str = scan.next(); int[] arr = new int[n]; int Bcount = 0; int Wcount = 0; for(int i = 0; i < n; i++){ if(str.charAt(i) == 'W') { arr[i] = 0; Wcount++; } else { arr[i] = 1; Bcount++; } } if(n % 2 == 0 && (Bcount == 1 || Wcount == 1)){ System.out.println(-1); return; } if(Wcount > Bcount) { for(int i = 0; i < n; i++){ arr[i ] = (arr[i] + 1) % 2; } int temp = Wcount; Wcount = Bcount; Bcount = temp; } int hCount = 0; ArrayList<Integer> hod = new ArrayList<>(); // System.out.println("B " + Bcount); // System.out.println("W " + Wcount); for(int i = 0; i < n - 1; i++){ if(arr[i] == 0) { hCount++; hod.add(i + 1); arr[i] = 1; Wcount--; Bcount++; arr[i + 1] = (arr[i + 1] + 1) % 2; if(arr[i + 1] == 1) { Wcount--; Bcount++; } else { Wcount++; Bcount--; } } } // System.out.println("B " + Bcount); // System.out.println("W " + Wcount); if(Wcount == 0){ System.out.println(hCount); for(int h: hod){ System.out.print(h + " "); } } else { for(int i = n - 1; i > 0; i--){ if(arr[i] == 1) { hCount++; hod.add(i); arr[i] = 0; Bcount--; Wcount++; arr[i - 1] = (arr[i - 1] + 1) % 2; if(arr[i - 1] == 0) { Bcount--; Wcount++; } else { Bcount++; Wcount--; } } } if(Bcount == 0){ System.out.println(hCount); for(int h: hod){ System.out.print(h + " "); } } else { System.out.println(-1); } } // System.out.println("B " + Bcount); // System.out.println("W " + Wcount); // System.out.println(); // for(int i = 0; i < n; i++){ // System.out.print(arr[i] + " "); // } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string second; cin >> n >> second; string t = second; bool ok = 1; vector<int> vec; for (int i = 0; i < n; i++) { if (second[i] == 'W') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'W'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'W'; } second[to] = 'W'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; cout << endl; return 0; } ok = 1; vec.clear(); second = t; for (int i = 0; i < n; i++) { if (second[i] == 'B') continue; if (i + 1 < n && second[i + 1] == second[i]) { vec.push_back(i); second[i] = second[i + 1] = 'B'; continue; } bool foo = 1; int to = -1; for (int j = i + 1; j < n; j++) if (second[j] == second[i]) { to = j; break; } for (int j = i + 1; j < to; j++) if (second[j] == second[i]) foo = 0; if (to == -1) foo = 0; if (foo) { for (int j = i; j < to; j++) { vec.push_back(j); second[j] = 'B'; } second[to] = 'B'; continue; } ok = 0; break; } if (ok) { cout << (int)vec.size() << endl; for (auto i : vec) cout << i + 1 << " "; 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 N = int(input()) s = [x == 'W' for x in input()] ans = [] flip = False for i in range(N-1): if not s[i]: ans.append(i+1) s[i] = not s[i] s[i+1] = not s[i+1] if not s[-1]: if N % 2 == 0: print(-1) sys.exit(0) for i in range(0, N-1, 2): ans.append(i+1) print(len(ans)) for x in ans: print(x, end=' ')
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Problem1271B { public static void main(String[] args) { Problem1271B instance = new Problem1271B(); BufferedReader bfr = null; try { bfr = new BufferedReader(new InputStreamReader(System.in)); String line = ""; int n = 0; if ((line = bfr.readLine()) != null) { n = Integer.parseInt(line); } if ((line = bfr.readLine()) != null) { instance.process(n, line.trim()); } } catch (Throwable t) { System.err.println(t); } finally { if (bfr != null) { try { bfr.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void process(int n, String s) { char ch[] = s.toCharArray(); char target = ch[n - 1]; int i = n - 2; List<Integer> result = new ArrayList<Integer>(); while (i >= 0) { if (ch[i + 1] != target) { if (ch[i] == 'B') { ch[i] = 'W'; } else { ch[i] = 'B'; } ch[i + 1] = target; result.add(i); } i--; } if (ch[0] == target) { printResult(result); } else { i = n - 2; ch = s.toCharArray(); if (target == 'B') { target = 'W'; } else { target = 'B'; } result = new ArrayList<Integer>(); while (i >= 0) { if (ch[i + 1] != target) { if (ch[i] == 'B') { ch[i] = 'W'; } else { ch[i] = 'B'; } ch[i + 1] = target; result.add(i); } i--; } if (ch[0] == target) { printResult(result); } else { System.out.println("-1"); } } } private void printResult(List<Integer> result) { System.out.println(result.size()); for (int j = 0; j < result.size(); j++) { if (j > 0) { System.out.print(" "); } System.out.print((1 + result.get(j))); } System.out.println(); } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.*; import java.io.*; import java.math.*; public class B608{ public static void main(String args[]){ FastReader sc = new FastReader(); int n,i; n=sc.nextInt(); char s[]=sc.next().toCharArray(); int b=0,w=0; for(i=0;i<n;i++){ if(s[i]=='B') b++; else w++; } if(b==0||w==0){ System.out.println(0); System.exit(0); } if(n%2==0&&((b%2==1)||w%2==1)){ System.out.println(-1); System.exit(0); } ArrayList<Integer> ans=new ArrayList<Integer>(); int count=0; for(i=n-1;i>=1;i--){ if(s[i]=='W'){ ans.add(i-1); s[i]='B'; s[i-1]=(s[i-1]=='B')?'W':'B'; count++; //out.println(new String(s)); } } if(s[0]!=s[1]){ for(i=1;i<n;i+=2){ count++; ans.add(i); } } out.println(count); for(i=0;i<ans.size();i++) out.print((ans.get(i)+1)+" "); out.println(); out.flush(); } static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } static class pair{ int first,second; pair(int f,int s){ first=f; second=s; } } public static boolean isPrime(int n) { if(n<2) return false; for(int i=2;i<=(int)Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static void print(int a[],int l,int r){ int i; for(i=l;i<=r;i++) out.print(a[i]+" "); out.println(); } public static long fastexpo(long x, long y, long p){ long res=1; while(y > 0){ if((y & 1)==1) res= ((res%p)*(x%p))%p; y= y >> 1; x = ((x%p)*(x%p))%p; } return res; } public static boolean[] sieve (int n) { boolean primes[]=new boolean[n+1]; Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]){ for(int j=i*i;j<=n;j+=i) primes[j]=false; } } return primes; } public static long gcd(long a,long b){ return (BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))).longValue(); } public static void merge(int a[],int l,int m,int r){ int n1,n2,i,j,k; n1=m-l+1; n2=r-m; int L[]=new int[n1]; int R[]=new int[n2]; for(i=0;i<n1;i++) L[i]=a[l+i]; for(j=0;j<n2;j++) R[j]=a[m+1+j]; i=0;j=0; k=l; while(i<n1&&j<n2){ if(L[i]<=R[j]){ a[k]=L[i]; i++; } else{ a[k]=R[j]; j++; } k++; } while(i<n1){ a[k]=L[i]; i++; k++; } while(j<n2){ a[k]=R[j]; j++; k++; } } public static void sort(int a[],int l,int r){ int m; if(l<r){ m=(l+r)/2; sort(a,l,m); sort(a,m+1,r); merge(a,l,m,r); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.util.*; public class Main { static FastReader f = new FastReader(); public static void main(String[] args) { int n = f.nextInt(); char[] s = f.next().toCharArray(); int b = 0; for(int i=0;i<n;i++) { if(s[i] == 'B') { b++; } } if((n & 1) == 0 && (b & 1) == 1) { System.out.println(-1); return; } if(n == b || b==0) { System.out.println(0); return; } ArrayList<Integer> ansList = new ArrayList(); for(int i=0;i<n-1;i++) { if(s[i] == 'W') { s[i] = 'B'; s[i+1] = s[i+1] == 'B' ? 'W' : 'B'; ansList.add(i+1); } } if(s[n-1] == 'W') { for(int i=0;i<n-1;i+=2) { ansList.add(i+1); } } System.out.println(ansList.size()); for(int i : ansList) { System.out.print(i+ " "); } } //fast input reader 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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, w = 0, b = 0; cin >> n; string s; cin >> s; queue<long long> q; for (long long i = 0; i < n; i++) { if (s[i] == 'W') w++; else b++; } if (w % 2 == 1 && b % 2 == 1) { cout << -1; return 0; } else if (w % 2 == 1 && b % 2 == 0) { for (long long i = 0; i < n; i++) { if (s[i] == 'B') { q.push(i); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } } else { for (long long i = 0; i < n; i++) { if (s[i] == 'W') { q.push(i); s[i] = 'B'; if (s[i + 1] == 'W') s[i + 1] = 'B'; else s[i + 1] = 'W'; } } } cout << q.size() << endl; while (!q.empty()) { cout << q.front() + 1 << " "; q.pop(); } }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; const int N = (int)3e5 + 5; const int mod = (int)1e9 + 7; int n, b, w; char a[300]; vector<int> v; inline void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 'B') b++; else w++; } if (b == 0 || w == 0) { cout << 0; return; } if (b % 2 == 1 && w % 2 == 1) { cout << -1; return; } if (w % 2 == 0) { for (int i = 1; i < n; i++) { if (a[i] == 'W') { if (a[i + 1] == 'W') { v.push_back(i); a[i] = 'B'; a[i + 1] = 'B'; } else { v.push_back(i); a[i] = 'B'; a[i + 1] = 'W'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; return; } for (int i = 1; i < n; i++) { if (a[i] == 'B') { if (a[i + 1] == 'B') { v.push_back(i); a[i] = 'W'; a[i + 1] = 'W'; } else { v.push_back(i); a[i] = 'W'; a[i + 1] = 'B'; } } } cout << (int)v.size() << endl; for (auto u : v) cout << u << " "; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tt = 1; while (tt--) { solve(); } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) num = list(input()) tnum = num.copy() ans = 0 anss = [] for i in range(n-1): if (tnum[i] == 'W'): tnum[i] = 'B' if (tnum[i+1] == 'B'): tnum[i+1] = 'W' else: tnum[i+1] = 'B' ans += 1 anss.append(i+1) if (tnum[n-1] == 'B'): print(ans) if (ans != 0): print(*anss) exit(0) tnum = num.copy() ans = 0 anss = [] for i in range(n-1): if (tnum[i] == 'B'): tnum[i] = 'W' if (tnum[i+1] == 'B'): tnum[i+1] = 'W' else: tnum[i+1] = 'B' ans += 1 anss.append(i+1) if (tnum[n-1] == 'W'): print(ans) if (ans != 0): print(*anss) exit(0) print(-1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import javafx.util.Pair; public class CodeChef { public static boolean inside(int a, ArrayList<Pair<Integer, Integer>> pairs) { for (int i = 0; i < pairs.size(); i++) { if (a >= pairs.get(i).getKey() && a <= pairs.get(i).getValue()) return true; } return false; } static boolean flag = false; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int n=Integer.parseInt(br.readLine()); String ss=br.readLine(); int[] arr=new int[n]; int[] arr1=new int[n]; for(int i=0;i<n;i++) {if(ss.charAt(i)=='W') {arr[i]=0;arr1[i]=0;} else { arr[i]=1;arr1[i]=1;}} ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0;i<n-1;i++) {if(arr1[i]==1) {arr1[i]=0;arr1[i+1]=1-arr1[i+1]; list.add(i+1);}} if(arr1[n-1]==1) { ArrayList<Integer> list1=new ArrayList<Integer>(); for(int i=0;i<n-1;i++) {if(arr[i]==0) {arr[i]=1;arr[i+1]=1-arr[i+1]; list1.add(i+1);} } if(arr[n-1]==0) System.out.println("-1"); else {System.out.println(list1.size());for(int j=0;j<list1.size();j++) System.out.print(list1.get(j)+" "); } } else {System.out.println(list.size());for(int j=0;j<list.size();j++) System.out.print(list.get(j)+" "); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; long double pi = 3.14159265358979323846; vector<long long> al[500005]; long long vis[500005], color[500005], I[101][101]; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long bpow(long long n, long long po) { long long res = 1; while (po > 0) { if (po % 2) { res *= n; po--; } else { n *= n; po /= 2; } } return res; } long long binpow(long long n, long long po, long long mod) { long long res = 1; while (po > 0) { if (po % 2) { res = (res * n) % mod; po--; } else { n = (n * n) % mod; po /= 2; } } return res; } long long swap(long long *a, long long *b) { long long temp = *a; *a = *b; *b = temp; } long long fact(long long n) { if ((n == 0) || (n == 1)) return 1; else return n * fact(n - 1); } long long C(long long n, long long r) { if (r > n - r) r = n - r; long long ans = 1; long long i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; ans = ans % 1000000007; } return ans; } void mmul(long long A[][101], long long B[][101], long long dim) { long long res[dim][dim]; for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { res[i][j] = 0; for (long long k = 0; k < dim; k++) res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % 1000000007; } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = res[i][j]; } void mpow(long long A[][101], long long dim, long long po) { for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) { if (i == j) I[i][j] = 1; else I[i][j] = 0; } while (po > 0) { if (po % 2 == 1) { mmul(I, A, dim); po--; } else { mmul(A, A, dim); po /= 2; } } for (long long i = 0; i < dim; i++) for (long long j = 0; j < dim; j++) A[i][j] = I[i][j]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long testcases = 1; while (testcases--) { long long n; cin >> n; string s; cin >> s; long long white = 0, black = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') white++; else black++; } vector<long long> ans; if (black == 0 || white == 0) cout << 0; else if (n % 2 == 0 && white % 2 == 1) cout << -1; else if (black % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'B' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'W' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'B' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } else if (white % 2 == 0) { long long flag = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W' && flag == 0) { ans.push_back(i + 1); flag = 1; } else if (s[i] == 'B' && flag == 1) ans.push_back(i + 1); else if (s[i] == 'W' && flag == 1) flag = 0; } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; } } }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Solution implements Runnable { public static void main(String args[]) throws Exception{new Thread(null, new Solution(),"Main",1<<27).start();} public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=in.nextInt(); String s=in.next(); int wi=0; int b=0; for(int i=0;i<n;i++){ if(s.charAt(i)=='B') b++; else wi++; } if(wi==0 || b==0) w.println("0"); else if(wi%2!=0 && b%2!=0) w.println("-1"); else if(wi%2==0){ ArrayList<Integer> arr=new ArrayList<>(); char[] c=s.toCharArray(); for(int i=0;i<n-1;i++){ if(c[i]=='W'){ c[i]='B'; c[i+1]=c[i+1]=='B'?'W':'B'; arr.add(i+1); } } w.println(arr.size()); for(int i=0;i<arr.size();i++) w.print(arr.get(i)+" "); w.println(); } else{ ArrayList<Integer> arr=new ArrayList<>(); char[] c=s.toCharArray(); for(int i=0;i<n-1;i++){ if(c[i]=='B'){ c[i]='W'; c[i+1]=c[i+1]=='B'?'W':'B'; arr.add(i+1); } } w.println(arr.size()); for(int i=0;i<arr.size();i++) w.print(arr.get(i)+" "); w.println(); } w.flush(); w.close(); } 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 long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a);} public static long findGCD(long arr[], int n) { long result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result);return result; } static void sortbycolomn(long arr[][], int col) { Arrays.sort(arr, new Comparator<long[]>() { @Override public int compare(final long[] entry1,final long[] entry2) { if (entry1[col] > entry2[col])return 1; else return -1; } }); } }
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
#q = int(input()) #x, y = map(int,input().split(' ')) #print (' '.join(list(map(str, s)))) n = int(input()) s = list(input()) cnt = 0 lst = [] for i in range(1,n-1): if s[i] != s[i-1]: cnt = cnt + 1 lst.append(i+1) if s[i] == 'W': s[i] = 'B' else: s[i] = 'W' if s[i+1] == 'W': s[i+1] = 'B' else: s[i+1] = 'W' if s[-1] != s[-2]: if n % 2 == 0: print(-1) else: cnt = cnt + n // 2 for i in range(1,n,2): lst.append(i) print(cnt) print(*lst) else: print(cnt) print(*lst)
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.math.*; import java.io.*; import java.lang.*; public class Main { static class Pair { int x; int y; public Pair(int x, int y) { this.x=x; this.y=y; } } static class Comp implements Comparator<Pair> { public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x && p1.y!=p2.y) { return p1.x-p2.x; } else { return 0; } } } public static void main(String[] args) { FastReader sc =new FastReader(); int n = sc.nextInt(); char ch[] = sc.next().toCharArray(); char ch2[] = ch.clone(); boolean pos1=true; List<Integer> list=new ArrayList<>(); //Can I make it W; for(int i=0;i<n;i++) { if(ch[i]=='W') { continue; } else { if(i==n-1) { pos1=false; break; } else { ch[i+1]=(ch[i+1] == 'W') ? 'B' : 'W'; list.add(i); } } } if(pos1) { System.out.println(list.size()); for(int i: list) System.out.print((i+1)+" "); System.out.println(); return; } boolean pos2=true; List<Integer> list2=new ArrayList<>(); for(int i=0;i<n;i++) { if(ch2[i]=='B') { continue; } else { if(i==n-1) { pos2=false; break; } else { ch2[i+1]=(ch2[i+1] == 'B') ? 'W' : 'B'; list2.add(i); } } } if(pos2) { System.out.println(list2.size()); for(int i: list2) System.out.print((i+1)+" "); System.out.println(); return; } System.out.println(-1); } public static void swap(char ch[][], int i, int j, int l, int m) { char temp = ch[i][j]; ch[i][j]=ch[l][m]; ch[l][m]=temp; } public static int bs(int num[], int x) { int low=0; int high=num.length-1; int ans=-1; while(low<=high) { int mid = low + (high-low)/2; if(num[mid]==x) { ans = mid; high=mid-1; } else if(num[mid]<x) { // ans=mid; low=mid+1; } else { ans=mid; high=mid-1; } } return ans; } public static int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low-1); for (int j=low; j<high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; return i+1; } public static void sort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); sort(arr, low, pi-1); sort(arr, pi+1, high); } } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } 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 void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.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 (final 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 (final IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(final int n) { final int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } } } //private ArrayList<String[]> action = new ArrayList<String[]>(); //Templates for Comparator and Classes - @jagrit_07 /* Arrays.sort(newEmployees, new Comparator<Employee>() { @Override public int compare(Employee emp1, Employee emp2) { return emp1.getName().compareTo(emp2.getName()); } }); class Pair { long i; //index; long l; //left; long c; //cost; public Pair(long x,long y,long z) { this.i=x; this.l=y; this.c=z; } public String toString() { return this.i+" "+this.l+" "+this.c; } } class Comp implements Comparator<Pair> { public int compare(Pair p1, Pair p2) { if(p1.c!=p2.c) { return (int)(p1.c-p2.c); //sort acc to cost; } else{ return (int)(p1.i-p2.i); //sort acc to index; } } } */ /* HashMap - Put template - d.put(a1,d.getOrDefault(a1,0)+1); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } Deque<String> deque = new LinkedList<String>(); List<Integer> c[] = new ArrayList[3]; */
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.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.*; import java.io.*; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public class Contest1 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); char[]s=sc.nextLine().toCharArray(); int[]a=new int[n]; for (int i =0;i<n;i++){ if (s[i]=='W')a[i]=1; } boolean can=true; int[]a1=a.clone(); StringBuilder sol = new StringBuilder(); int c =0; for (int i =0;i<n-1;i++){ if (a1[i]==0)continue; else { c++; a1[i]=0; a1[i+1]=1-a1[i+1]; sol.append(i+1); sol.append(" "); } } for (int i =0;i<n;i++){ can&=a1[i]==0; } if (can){ pw.println(c); pw.println(sol); } else { can=true; a1=a.clone(); sol = new StringBuilder(); c =0; for (int i =0;i<n-1;i++){ if (a1[i]==1)continue; else { c++; a1[i]=1; a1[i+1]=1-a1[i+1]; sol.append(i+1); sol.append(" "); } } for (int i =0;i<n;i++){ can&=a1[i]==1; } if (can){ pw.println(c); pw.println(sol); } else pw.println(-1); } pw.flush(); } static class Edge{ int v,c; Edge(int a,int b){ v=a;c=b; } } static class UnionFind { int[] p, rank, setSize; int numSets; HashSet<Integer>[]ele; public UnionFind(int N) { ele=new HashSet[N]; p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { ele[i]=new HashSet<>(); p[i] = i; setSize[i] = 1; ele[i].add(i); } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if(rank[x] > rank[y]) { for (int zz:ele[y])ele[x].add(zz); p[y] = x; setSize[x] += setSize[y]; } else { for (int zz:ele[x])ele[y].add(zz); p[x] = y; setSize[y] += setSize[x]; if(rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
def flip(x): if x == 'B': return 'W' return 'B' def B(): input() S = input() for ocz in 'BW': cop = [x for x in S] uz = 0 roz = [] for i in range(len(cop) - 1): if cop[i] != ocz: uz += 1 roz.append(i+1) cop[i] = ocz cop[i+1] = flip(cop[i+1]) if cop[-1] == ocz: print(uz) for op in roz: print(op, end = ' ') print() return print(-1) 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; int main() { int n; vector<int> v; string s; cin >> n >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') goto out; for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } if (s[n - 1] == 'W') { cout << -1; return 0; } out: cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) c = list(input()) a = c.count('W') b = n - a if a%2 == 0: f = 'B' elif b%2 == 0: f = 'W' else: f = -1 if f == -1: print(-1) else: m = [] for i in range(n-1): if c[i] != f: c[i] = 'W' if c[i] == 'B' else 'B' c[i+1] = 'W' if c[i+1] == 'B' else 'B' m.append(i+1) #print(c, f) print(len(m)) for j in m: print(j, end=' ') print("")
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<long long int> v[3]; long long int n, i, r = 0; cin >> n; string s, ss; cin >> s; char rev = ('B' ^ 'W'); ss = s; for (i = 0; i < n - 1; ++i) { if (s[i] == 'B') { v[1].push_back(i); s[i] = 'W'; s[i + 1] = rev ^ s[i + 1]; } } if (s[n - 1] == 'B') { r = 1; for (i = 0; i < n - 1; ++i) { if (ss[i] == 'W') { v[2].push_back(i); ss[i] = 'B'; ss[i + 1] = rev ^ ss[i + 1]; } } } if (r == 0 && s[n - 1] == 'W') { cout << v[1].size() << "\n"; for (auto it : v[1]) cout << it + 1 << " "; return 0; } if (r == 1 && ss[n - 1] == 'B') { cout << v[2].size() << "\n"; for (auto it : v[2]) cout << it + 1 << " "; 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
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<long long, long long>; template <class T> void cmax(T& x, T y) { (x < y) && (x = y); } template <class T> void cmin(T& x, T y) { (x > y) && (x = y); } string s; long long n, b; vector<long long> ans; signed main() { ios ::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; for (long long i = 1; i <= n; i++) { if (s[i - 1] == 'B') b++; } if (s.size() % 2 == 0 && b % 2) return puts("-1"), 0; for (long long i = 1; i < n - 1; i++) { if (s[i - 1] != s[i]) { ans.push_back(i + 1); s[i] = (s[i] == 'B' ? 'W' : 'B'); s[i + 1] = (s[i + 1] == 'B' ? 'W' : 'B'); } } for (long long i = 0; i < n; i++) { if (s.size() % 2 == 0 && s[i] == 'W') { ans.push_back(i + 1); i++; } else if (s.size() % 2 && b % 2 && s[i] == 'W') ans.push_back(i + 1), ++i; else if (s.size() % 2 && b % 2 == 0 && s[i] == 'B') ans.push_back(i + 1), ++i; } cout << ans.size() << '\n'; for (long long x : ans) cout << x << ' '; 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() { long long int n, i; cin >> n; string s; cin >> s; long long int c = 0; vector<long long int> ans; string s1 = s; for (i = 0; i < n - 1; i++) { if (s1[i] != 'W') { s1[i] = 'W'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'W') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } c = 0; s1 = s; ans.clear(); for (i = 0; i < n - 1; i++) { if (s1[i] != 'B') { s1[i] = 'B'; if (s1[i + 1] == 'W') { s1[i + 1] = 'B'; } else { s1[i + 1] = 'W'; } c++; ans.push_back(i + 1); } } if (s1[n - 1] == 'B') { cout << c << endl; for (i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } 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 java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); String s=input.next(); char ch[]=s.toCharArray(); ArrayList<Integer> list=new ArrayList<>(); int i=0; while(i<s.length()-1) { if(ch[i]=='W') { i++; } else { list.add(i); ch[i]='W'; if(ch[i+1]=='B') { ch[i+1]='W'; } else { ch[i+1]='B'; } i++; } } if(ch[n-1]=='W') { out.println(list.size()); for(int j=0;j<list.size();j++) { out.print((list.get(j)+1)+" "); } out.println(); } else { if((n-1)%2==0) { for(int j=0;j<n-1;j+=2) { list.add(j); } out.println(list.size()); for(int j=0;j<list.size();j++) { out.print((list.get(j)+1)+" "); } out.println(); } else { out.println(-1); } } } out.close(); } 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.*; import java.math.BigInteger; public class tr0 { static PrintWriter out; static StringBuilder sb; static final double EPS = 1e-9; static long mod = (int) 998244353; static int inf = (int) 1e9 + 2; static long[] fac; static int[] a; static int[] si; static ArrayList<Integer> primes; static ArrayList<Integer>[] ad; static ArrayList<pair>[] d; static edge[] ed; static int[][] vis; static int[] l, ch; static int[] occ; static Queue<Integer>[] can; static String s; static long[][][] memo; static int n, m; /* * 0 -> 0 0 1-> 0 1 2 -> 1 1 3-> 1 0 */ public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int n=sc.nextInt(); char []a=sc.nextLine().toCharArray(); int w=0; int b=0; for(int i=0;i<n;i++) if(a[i]=='W') w++; else b++; if(n%2==0 && w%2!=0) { System.out.println(-1); return; } int c=0; sb=new StringBuilder(); if(w%2==0) { for(int i=0;i<n;i++) { if(i<n-1 && a[i]=='W' && a[i+1]=='W') { c++; sb.append((i+1)+" "); i++; } else if(i<n-1 && a[i]=='W' && a[i+1]=='B') { c++; sb.append((i+1)+" "); a[i]='B'; a[i+1]='W'; } } } else { for(int i=0;i<n;i++) { if(i<n-1 && a[i]=='B' && a[i+1]=='B') { c++; sb.append((i+1)+" "); i++; } else if(i<n-1 && a[i]=='B' && a[i+1]=='W') { c++; sb.append((i+1)+" "); a[i]='W'; a[i+1]='B'; } } } out.println(c); out.println(sb); out.flush(); } static class qu implements Comparable<qu> { int st, ed, id; qu(int i, int v, int x) { st = i; ed = v; id = x; } public String toString() { return st + " " + ed + " " + id; } public int compareTo(qu o) { return id - o.id; } } static class seg implements Comparable<seg> { int a; int b; seg(int s, int e) { a = s; b = e; } public String toString() { return a + " " + b; } public int compareTo(seg o) { // if(a==o.a) return o.b - b; // return } } static class pair { int to; int number; pair(int t, int n) { number = n; to = t; } public String toString() { return to + " " + number; } } static int modPow(int a, int e) { int res = 1; while (e > 0) { if ((e & 1) == 1) res = (res * a); a = (a * a); e >>= 1; } return res; } static long inver(long x) { int a = (int) x; long e = (mod - 2); long res = 1; while (e > 0) { if ((e & 1) == 1) { res = (int) ((1l * res * a) % mod); } a = (int) ((1l * a * a) % mod); e >>= 1; } return res % mod; } static class edge implements Comparable<edge> { int from; int to; int number; edge(int f, int t, int n) { from = f; to = t; number = n; } public String toString() { return from + " " + to + " " + number; } public int compareTo(edge f) { return f.number - number; } } static void seive(int N) { si = new int[N]; primes = new ArrayList<>(); si[1] = 1; for (int i = 2; i < N; i++) { if (si[i] == 0) { si[i] = i; primes.add(i); } for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++) si[primes.get(j) * i] = primes.get(j); } } static long fac(int n) { if (n == 0) return fac[n] = 1; if (n == 1) return fac[n] = 1; long ans = 1; for (int i = 1; i <= n; i++) fac[i] = ans = (i % mod * ans % mod) % mod; return ans % mod; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class unionfind { int[] p; int[] size; int[] max; int num; unionfind(int n) { p = new int[n]; size = new int[n]; max = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; max[i] = i; } Arrays.fill(size, 1); num = 0; } int findSet(int v) { if (v == p[v]) return v; max[v] = Math.max(max[v], max[p[v]]); p[v] = findSet(p[v]); max[v] = Math.max(max[v], max[p[v]]); return p[v]; } boolean sameSet(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return true; return false; } int max() { int max = 0; for (int i = 0; i < size.length; i++) if (size[i] > max) max = size[i]; return max; } boolean combine(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return true; if (size[a] > size[b]) { p[b] = a; max[a] = Math.max(max[a], max[b]); size[a] += size[b]; } else { p[a] = b; max[b] = Math.max(max[a], max[b]); size[b] += size[a]; } return false; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static int check(char[] ch) { int B = 0, W = 0; for (int i = 0; i < ch.length; i++) if (ch[i] == 'B') B++; else W++; if (B == 0 || W == 0) return 1; return 0; } public static void main(String[] args) throws IOException { FastReader fs=new FastReader(); int l = fs.nextInt(); String s = fs.next(); char []ch=s.toCharArray(); StringBuffer sf=new StringBuffer(s); int B = 0, W = 0; for (int i = 0; i < l; i++) { if (s.charAt(i) == 'B') B++; else W++; } if (W == 0 || B == 0) System.out.println(0); else if (B % 2 != 0 && W % 2 != 0) System.out.println(-1); else { int cnt1 = 0; List<Integer> list1 = new ArrayList<>(); for (int i = 0; i < l; i++) { if (ch[i] == 'W' && i != l - 1) { ch[i] = 'B'; if (ch[i + 1] == 'B') ch[i + 1] = 'W'; else ch[i + 1] = 'B'; cnt1++; list1.add(i + 1); } } if(check(ch)==0){ for(int i=0;i<l-1;i+=2){ cnt1++; list1.add(i+1); } } System.out.println(cnt1); for (int i = 0; i < cnt1; i++) { if (i != 0) System.out.print(" "); System.out.print(list1.get(i)); } } } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.*; import java.io.*; import java.lang.Math.*; public class KickStart2020{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }} static boolean equal(String a) { boolean hachu = true; char ab = a.charAt(0); for(int i = 1; i < a.length(); i++) { if(ab != a.charAt(i)) { hachu = false; break; } } return hachu; } static boolean compare(String a) { int z = 0; for(int i = 0; i < a.length(); i++) { if(a.charAt(i) == '1') { z = i; break; } } String c = a.substring(0, z + 1); String d = a.substring(z + 1, a.length()); if(equal(c) && equal(d)) return true; else return false; } public static void main(String[] args) throws Exception{ FastReader sc = new FastReader(); int t = sc.nextInt(); String a = sc.next(); char arr[] = a.toCharArray(); char brr[] = a.toCharArray(); ArrayList<Integer> ss = new ArrayList<Integer>(); ArrayList<Integer> sg = new ArrayList<Integer>(); for(int i = 0; i < t - 1; i++) { if(arr[i] == 'W') { arr[i] = 'B'; if(arr[i + 1] == 'W') arr[i+1] = 'B'; else arr[i+1] = 'W'; ss.add(i + 1);} } if(arr[t - 1] == 'B') { System.out.println(ss.size()); for(int e: ss) System.out.print(e + " "); } else { for(int i = 0; i < t - 1; i++) { if(brr[i] == 'B') { brr[i] = 'W'; if(brr[i + 1] == 'B') brr[i+1] = 'W'; else brr[i+1] = 'B'; sg.add(i + 1); } } if(brr[t - 1] == 'W') { System.out.println(sg.size()); for(int e: sg) System.out.print(e + " "); } else { System.out.println(-1); } } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
//When I wrote this code, only God & I understood what it did. Now only God knows !! import java.util.*; import java.io.*; import java.math.*; public class Main { static class FastReader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public FastReader() { this(System.in); }public FastReader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String next(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){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 int i(){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 double d() throws IOException {return Double.parseDouble(next()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void scanIntArr(int [] arr){ for(int li=0;li<arr.length;++li){ arr[li]=i();}} public void scanLongArr(long [] arr){for (int i=0;i<arr.length;++i){arr[i]=l();}} public void shuffle(int [] arr){ for(int i=arr.length;i>0;--i) { int r=(int)(Math.random()*i); int temp=arr[i-1]; arr[i-1]=arr[r]; arr[r]=temp; } } } public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); PrintWriter pw = new PrintWriter(System.out); /* inputCopy 8 BWWWWWWB outputCopy 3 6 2 4 inputCopy 4 BWBB outputCopy -1 inputCopy 5 WWWWW outputCopy 0 inputCopy 3 BWB outputCopy 2 2 1 */ int n=fr.i(); char [] str=fr.nextLine().toCharArray(); StringBuilder ans=new StringBuilder(""); int k=0; for(int ni=0;ni<n-1;++ni) { if(str[ni]=='W' && str[ni+1]=='W') { str[ni]='B'; str[ni+1]='B'; ans.append((ni+1)+" "); ++ni; ++k; } else if(str[ni]=='W' && str[ni+1]=='B') { str[ni]='B'; str[ni+1]='W'; ans.append((ni+1)+" "); ++k; } } if(str[n-1]=='B') { pw.println(k); pw.println(ans); pw.flush(); return; } for(int ni=n-2;ni>0;--ni) { if(str[ni]=='B' && str[ni-1]=='B') { str[ni]='W'; str[ni-1]='W'; ans.append((ni)+" "); ++ni; ++k; } else if(str[ni]=='B' && str[ni-1]=='W') { str[ni]='W'; str[ni-1]='B'; ans.append((ni)+" "); ++k; } } if(str[0]=='W') { pw.println(k); pw.println(ans); pw.flush(); return; } pw.println(-1); pw.flush(); pw.close(); } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n, s, b, w = int(input()), list(input()), 0, 0 for c in s: if c == 'B': b += 1 else: w += 1 if w % 2 and b % 2: print(-1) else: if w % 2 == 0 and b % 2 == 0: c = 'B' if b < w else 'W' else: c = 'B' if b % 2 else 'W' a = [] for i in range(n - 1): if s[i] != c: a.append(i + 1) s[i] = c s[i + 1] = 'B' if s[i + 1] == 'W' else 'W' print(len(a)) print(*a)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
def sol(): n = int(input()) s = list(input()) b = s.count('B') w = s.count('W') if w % 2 == 1 and b % 2 == 1: print(-1) return ans = [] s = list(s) if w % 2 == 1: odd = 'W' even = 'B' else: odd = 'B' even = 'W' for i in range(n-1): if s[i] != odd: ans.append(i + 1) s[i] = odd s[i+1] = even if s[i+1] == odd else odd print(len(ans)) print(' '.join(map(str, ans))) sol()
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) s = list(input()) prev=s[0] i=1 ans=[] while i<n and s[i]==prev: i+=1 j=i while i<n-1: if s[i]==prev: i+=1 continue else: ans.append(i+1) if s[i]!=s[i+1]: s[i],s[i+1]=s[i+1],s[i] i+=1 else: s[i]=s[i+1]=prev i+=2 #prev=s[i] # print(s) #print(s) if s[-1]!=s[-2] and n%2==0: print(-1) elif s[-1]!=s[-2] and n%2==1: for i in range(n-2,0,-2): ans.append(i) print(len(ans)) print(*ans) else: print(len(ans)) print(*ans)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.*; import java.io.*; import java.lang.*; public class Codeforces{ BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); PrintWriter out =new PrintWriter(System.out); StringTokenizer st =new StringTokenizer(""); String next(){ if(!st.hasMoreTokens()){ try{ st=new StringTokenizer(br.readLine()); } catch(Exception e){ } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } public static void main(String[] args) { new Codeforces().solve(); } boolean ok(char arr[],char tar,List<Integer> res){ for(int i=0;i<arr.length;i++){ if(arr[i]!=tar){ if(i==arr.length-1) return false; arr[i]=tar; if(arr[i+1]=='W')arr[i+1]='B'; else{ arr[i+1]='W';} res.add(i+1); } } return true; } void solve(){ int n = nextInt(); char arr[] =next().toCharArray(); char a[]=new char[n]; for(int i=0;i<n;i++) a[i]=arr[i]; List<Integer> res=new ArrayList<>(); res.clear(); boolean flag=false; if(ok(arr,'W',res)==true){ out.println(res.size()); for(int i:res)out.print(i+" " ); // out.println(Arrays.toString(arr)); flag=true; } res.clear(); if(ok(a,'B',res)==true && !flag){ out.println(res.size()); for(int i:res)out.print(i+" " ); flag=true; } if(!flag)out.println(-1); out.close(); } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; inline bool valid(int x, int n) { return 0 <= x && x < n; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; template <typename T> inline T pop(queue<T>& q) { T front = q.front(); q.pop(); return front; } template <typename T> inline T gcd(T a, T b) { for (; b; a %= b, swap(a, b)) ; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; vector<int> rst; int w = 0, b = 0; for (char c : s) c == 'W' ? w++ : b++; if (w % 2 && b % 2) { cout << -1; return 0; } if (!w || !b) { cout << 0; return 0; } if (w % 2) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { rst.push_back(i + 1); s[i] = 'W'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } else { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { rst.push_back(i + 1); s[i] = 'B'; s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W'); } } } cout << rst.size() << "\n"; for (int i : rst) cout << i << " "; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) a = [0 if c=='W' else 1 for c in input()] if a.count(0) == 0 or a.count(1) == 0: print(0) exit() t = 0 if a.count(1) % 2 == 0: t = 1 ans = [] for i in range(n-1): if a[i] == t: ans.append(i+1) a[i] ^= 1 a[i+1] ^= 1 if a.count(t) > 0: print(-1) else: print(len(ans)) if 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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; /** * * @author is2ac */ public class B_CF { public static void main(String[] args) { FastScanner56 fs = new FastScanner56(); PrintWriter pw = new PrintWriter(System.out); //int t = fs.ni(); int t = 1; //Boolean[][][] dp = new Boolean[101][101][10001]; for (int tc = 0; tc < t; tc++) { int n = fs.ni(); String s = fs.next(); int[] a = new int[n]; for (int i = 0; i < n; i++) { if (s.charAt(i)=='B') a[i]++; } int ct = 0; int[] b = a.clone(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (b[i]==0) { if (i==n-1) { ct=-1; break; } else { b[i+1] = (b[i+1]+1)%2; ct++; sb.append((i+1) + " "); } } } if (ct!=-1) { pw.println(ct); pw.println(sb); continue; } b = a.clone(); ct = 0; sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (b[i]==1) { if (i==n-1) { ct=-1; break; } else { b[i+1] = (b[i+1]+1)%2; ct++; sb.append((i+1) + " "); } } } if (ct==-1) { pw.println(-1); } else { pw.println(ct); pw.println(sb); continue; } } pw.close(); } public static boolean good(int[] a, int[] b, int d, int m) { for (int i = 0; i < a.length; i++) { if ((a[i]+d)%m!=b[i]) return false; } return true; } static boolean isp(int x) { int y = (int) Math.sqrt(x); if (Math.pow(y, 2) == x) { return true; } return false; } } class FastScanner56 { BufferedReader br; StringTokenizer st; public FastScanner56() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) { ret[i] = ni(); } return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) { ret[i] = nl(); } return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; const int N = 5e6 + 5; char a[205]; const int mod = 1e9 + 7; vector<int> ans; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; cin >> (a + 1); int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] == 'B') cnt++; if (cnt % 2 == 1 && (n - cnt) % 2 == 1) cout << -1 << endl; else { if (cnt % 2 == 0) { for (int i = 1; i <= n; i++) { if (a[i] == 'B' && a[i + 1] == 'B') { ans.push_back(i); a[i] = 'W', a[i + 1] = 'W'; } } for (int i = 1; i <= n; i++) { if (a[i] == 'B') { int p = -1; for (int j = i + 2; j <= n; j++) { if (a[j] == 'B') { p = j; break; } } for (int j = p - 1; j >= i; j--) { ans.push_back(j); } a[i] = 'W', a[p] = 'W'; } } cout << ans.size() << endl; for (int t : ans) cout << t << " "; } else { for (int i = 1; i <= n; i++) { if (a[i] == 'W' && a[i + 1] == 'W') { ans.push_back(i); a[i] = 'B', a[i + 1] = 'B'; } } for (int i = 1; i <= n; i++) { if (a[i] == 'W') { int p = -1; for (int j = i + 2; j <= n; j++) { if (a[j] == 'W') { p = j; break; } } for (int j = p - 1; j >= i; j--) { ans.push_back(j); } a[i] = 'B', a[p] = 'B'; } } cout << ans.size() << endl; for (int t : ans) cout << t << " "; } } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) s = input() t1, t2 = s.count('B'), s.count('W') a = list(s) if t1 % 2 == 1 and t2 % 2 == 1: print(-1) else: count, result = 0, list() b = "#" b = "W" if t2 % 2 == 0 else 'B' for i in range(n - 1): if a[i] == b: count += 1 a[i + 1] = 'B' if a[i + 1] == 'W' else 'W' result.append(i + 1) print(count) print(*result)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; vector<long long> divisor(long long n) { vector<long long> v; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) v.push_back(i); else { v.push_back(i); v.push_back(n / i); } } } return v; } bool isprime(int n) { int i; for (i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; } vector<long long> sieve() { vector<long long> v(1000001); int i, j; for (i = 0; i < 1000001; i++) v[i] = 1; for (i = 2; i <= sqrt(1000001); i++) if (v[i] == 1) for (j = i * i; j <= 1000000; j += i) v[j] = 0; v[1] = 0; return v; } string dectobi(long long n) { string s; while (n > 0) { if (n & 1) s.push_back('1'); else s.push_back('0'); n >>= 1; } reverse(s.begin(), s.end()); return s; } long long power(long long a, long long b, long long m) { long long res = 1; while (b > 0) { if (b % 2) res = (res * a) % m; a = (a * a) % m; b /= 2; } return res % m; } int main() { ios_base::sync_with_stdio(false); long long a, b = 31, c, d, e, sum = 0, count = 0, flag = 0, j = 0, i = 1; long long x, y; set<char> se; long long n, m, t, k, l, r; string s; vector<long long> v; cin >> n >> s; string second = s; for (i = 0; i < n - 1; i++) { if (s[i] == 'W') { v.push_back(i + 1); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; } } for (i = 0; i < n; i++) se.insert(s[i]); if (se.size() == 1) { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } v.clear(); se.clear(); s = second; for (i = 0; i < n - 1; i++) { if (s[i] == 'B') { v.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else if (s[i + 1] == 'W') s[i + 1] = 'B'; } } for (i = 0; i < n; i++) se.insert(s[i]); if (se.size() == 1) { cout << v.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } cout << "-1"; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) s = input() w=s.count("W") b = n-w s=list(s) x=0 if b==0 or w==0: print(0) elif w%2==1 and w%2 == b%2: print(-1) else: t=s[0] an=[] i=0 while i<n-1: p = s[i]+s[i+1] if p[0]==p[1] and p[0]!=t: x+=1 an.append(i+1) i+=1 s[i-1],s[i]=t,t else: if p[0]!=t: x+=1 an.append(i+1) s[i],s[i+1]=s[i+1],s[i] i+=1 if s[n-1]!=t: i=0 while i<n-1: x+=1 an.append(i+1) i+=2 print(x) print(*an)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); String str = in.next(); int Ws = 0; char[] charArr = str.toCharArray(); for (char ch : charArr) { Ws += ch == 'W' ? 1 : 0; } if (n % 2 == 0 && Ws % 2 == 1) { out.println(-1); } else if (Ws == n || Ws == 0) { out.println(0); } else { StringBuilder sb = new StringBuilder(); char base = Ws % 2 == 1 ? 'W' : 'B'; int res = 0; for (int i = 0; i < charArr.length - 1; i++) { if (base != charArr[i]) { sb.append(i + 1).append(" "); charArr[i] = base; charArr[i + 1] = charArr[i + 1] == 'W' ? 'B' : 'W'; res++; } } out.println(res + "\n" + sb); } } } }
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 CodeforcesA { public static void main(String args[]) { Scanner s= new Scanner(System.in); int n=s.nextInt(); String str=s.next(); int countb=0; int countw=0; int ans[]= new int [str.length()]; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='B') { countb++; ans[i]=0; }else { countw++; ans[i]=1; } } if(countb%2!=0 && countw%2!=0) { System.out.println("-1"); }else { int k=0; ArrayList<Integer> l = new ArrayList<>(); if(countb>countw && countw%2!=0) { // making all white for(int i=0;i<ans.length-1;i++) { if(ans[i]==1) { continue; }else { ans[i]=ans[i]>0?0:1; ans[i+1]=ans[i+1]>0?0:1; l.add(i+1); } } }else { if(countb%2!=0) { for(int i=0;i<ans.length-1;i++) { if(ans[i]==0) { continue; }else { ans[i]=ans[i]>0?0:1; ans[i+1]=ans[i+1]>0?0:1; l.add(i+1); } } }else { for(int i=0;i<ans.length-1;i++) { if(ans[i]==1) { continue; }else { ans[i]=ans[i]>0?0:1; ans[i+1]=ans[i+1]>0?0:1; l.add(i+1); } } } } System.out.println(l.size()); for(int i=0;i<l.size();i++) { System.out.print(l.get(i)+" "); } } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = int(input()) a = list(input()) ans = [] if 'W' not in a or 'B' not in a: print(len(ans)) print(*ans) else: for i in range(n - 1): if a[i] == 'W': a[i] = 'B' ans.append(i + 1) if a[i + 1] == 'B': a[i + 1] = 'W' else: a[i + 1] = 'B' if 'W' not in a or 'B' not in a: print(len(ans)) print(*ans) else: for i in range(n - 1, 0, -1): if a[i] == 'B': ans.append(i) a[i] = 'W' if a[i - 1] == 'W': a[i - 1] = 'B' else: a[i - 1] = 'W' if 'W' not in a or 'B' not in a: print(len(ans)) print(*ans) else: for i in range(n - 1): if a[i] == 'W': a[i] = 'B' ans.append(i + 1) if a[i + 1] == 'B': a[i + 1] = 'W' else: a[i + 1] = 'B' if 'W' in a and 'B' in a: print(-1) else: print(len(ans)) print(*ans)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.util.ArrayList; import java.util.Scanner; public class Blocks { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int n=s.nextInt(); String str=s.next(); int[] arr1=new int[n]; int[] arr2=new int[n]; int count1=0; int count2=0; ArrayList<Integer> ans1=new ArrayList<Integer>(); ArrayList<Integer> ans2=new ArrayList<Integer>(); for(int i=0;i<arr1.length;i++) { if(str.charAt(i)=='W') { arr1[i]=1; arr2[i]=1; } } for(int i=0;i<str.length()-1;i++) { if(arr1[i]==0) { arr1[i]=arr1[i]^1; arr1[i+1]=arr1[i+1]^1; ans1.add(i+1); count1++; } if(arr2[i]==1) { arr2[i]=arr2[i]^1; arr2[i+1]=arr2[i+1]^1; ans2.add(i+1); count2++; } } for(int i=0;i<n;i++) { if(arr1[i]==0) { count1=Integer.MAX_VALUE; break; } } for(int i=0;i<n;i++) { if(arr2[i]==1) { count2=Integer.MAX_VALUE; break; } } if(count1==Integer.MAX_VALUE&&count2==Integer.MAX_VALUE) { System.out.println("-1"); }else { if(count1==Integer.MAX_VALUE&&count2!=Integer.MAX_VALUE) { System.out.println(count2); for(int i=0;i<ans2.size();i++) { System.out.print(ans2.get(i)+" "); } }else if(count2==Integer.MAX_VALUE&&count1!=Integer.MAX_VALUE) { System.out.println(count1); for(int i=0;i<ans1.size();i++) { System.out.print(ans1.get(i)+" "); } }else { if(count1>count2) { System.out.println(count2); for(int i=0;i<ans2.size();i++) { System.out.print(ans2.get(i)+" "); } }else { System.out.println(count1); for(int i=0;i<ans1.size();i++) { System.out.print(ans1.get(i)+" "); } } } } } }
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using i64 = int_fast64_t; void solve() { int n; std::cin >> n; std::string s; std::cin >> s; std::vector<int> ans; for (int i = (0); (i) < (n - 1); ++(i)) { if (s[i] == 'W' and s[i + 1] == 'W') { ans.emplace_back(i); s[i] = 'B', s[i + 1] = 'B'; } else if (s[i] == 'W' and s[i + 1] == 'B') { ans.emplace_back(i); s[i] = 'B', s[i + 1] = 'W'; } } if (s.back() == 'B') { std::cout << ans.size() << std::endl; for (int i = (0); (i) < (ans.size()); ++(i)) std::cout << ans[i] + 1 << " \n"[i + 1 == ans.size()]; } else { if (s.size() % 2 == 0) { std::cout << "-1" << std::endl; return; } else { for (int i = (0); (i) < (s.size() / 2); ++(i)) { s[2 * i] = 'W', s[2 * i + 1] = 'W'; ans.push_back(2 * i); } } std::cout << ans.size() << std::endl; for (int i = (0); (i) < (ans.size()); ++(i)) std::cout << ans[i] + 1 << " \n"[i + 1 == ans.size()]; } } int main() { std::cin.tie(nullptr); solve(); return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int n; string s; vector<int> v; int main() { cin >> n >> s; string t = s; int x = count(s.begin(), s.end(), 'B'); int y = count(s.begin(), s.end(), 'W'); if (x % 2 == y % 2 && x % 2 == 1) { return cout << -1, 0; } if (x == n || y == n) { cout << "0"; return 0; } x = n * 10 + 1; while (v.size() <= n * 3 && x--) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } int cnt = count(s.begin(), s.end(), 'B'); if (cnt == n) { cout << v.size() << endl; for (auto i : v) cout << i << " "; return 0; } } v.clear(); s = t; x = n * 10 + 1; while (v.size() <= n * 3 && x--) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else s[i + 1] = 'B'; v.push_back(i + 1); } } int cnt = count(s.begin(), s.end(), 'W'); if (cnt == n) { cout << v.size() << endl; for (auto i : v) cout << i << " "; return 0; } } cout << -1; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, sc, out); out.close(); } static class Task { public void solve(int testNumber, InputReader sc, PrintWriter out) throws IOException { int n=sc.nextInt(); String t=sc.next(); int[] s=new int[t.length()]; int ans=0; ArrayList<Integer> buf=new ArrayList<>(); for(int i=0;i<t.length();i++) s[i]=(t.charAt(i)=='W')?1:0; for(int i=1;i<s.length;i++) { if(s[i]!=s[i-1]) { if(i+1>=s.length) { break; } ans++; buf.add(i); s[i]^=1; s[i+1]^=1; } } if(s[s.length-1]==s[s.length-2]) { out.println(ans); for(int v:buf) out.print((v+1)+" "); return ; } if(s.length%2==0) { out.println("-1"); return ; } ans+=s.length/2; out.println(ans); for(int v:buf) out.print((v+1)+" "); for(int i=0;i+1<t.length();i+=2) out.print((i+1)+" "); } } static class InputReader{ StreamTokenizer tokenizer; public InputReader(InputStream stream){ tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); tokenizer.ordinaryChars(33,126); tokenizer.wordChars(33,126); } public String next() throws IOException { tokenizer.nextToken(); return tokenizer.sval; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean hasNext() throws IOException { int res=tokenizer.nextToken(); tokenizer.pushBack(); return res!=tokenizer.TT_EOF; } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(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
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): n = int(input()) s = input() a = [] t = [c == 'B' for c in s] for i in range(n - 1): if t[i]: a.append(i + 1) t[i] = False t[i + 1] = not t[i + 1] if not t[-1]: print(len(a)) print(*a) return a = [] t = [c == 'W' for c in s] for i in range(n - 1): if t[i]: a.append(i + 1) t[i] = False t[i + 1] = not t[i + 1] if not t[-1]: print(len(a)) print(*a) return print(-1) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
PYTHON
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import java.awt.Desktop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URI; import java.net.URISyntaxException; import java.sql.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.Vector; public class codechef3 { static class comp implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if(Math.abs(o1)>Math.abs(o2)) return -1; else return 1; } } //======================================================= //sorting Pair static class comp1 implements Comparator<Pair<Integer,Integer>> { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { if(o1.k>o2.k) return 1; else return -1; } } //======================================================= //Creating Pair class //---------------------------------------------------------------------- static class Pair<Integer,Intetger> { int k=0; int v=0; public Pair(int a,int b) { k=a; v=b; } } //-------------------------------------------------------------------------- static class FastReader {BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //gcd of two number public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } //-------------------------------------------------------------------------------------------- //lcm of two number static int x; public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } //------------------------------------------------------------------------------------------- public static void main(String[] args) { FastReader s=new FastReader(); { int n=s.nextInt(); String s1=s.next(); char[] c=s1.toCharArray(); int w=0,b=0; for(int i=0;i<n;i++) { if(c[i]=='W') w++; else b++; } if(b%2!=0&&w%2!=0) System.out.println("-1"); else if(b==0||w==0) System.out.println("0"); else { ArrayList<Integer> l=new ArrayList<>(); if(b%2==0&&w%2!=0) { for(int i=0;i<n-1;i++) { if(c[i]=='B') { l.add(i+1); c[i]='W'; if(c[i+1]=='B') c[i+1]='W'; else c[i+1]='B'; } } }else { for(int i=0;i<n-1;i++) { if(c[i]=='W') { l.add(i+1); c[i]='B'; if(c[i+1]=='B') c[i+1]='W'; else c[i+1]='B'; } } } System.out.println(l.size()); for(int i=0;i<l.size();i++) System.out.print(l.get(i)+" "); System.out.println(); } } }}
JAVA
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
import sys input = sys.stdin.readline n = int(input()) s = list(input())[:n] res = [] #sonomama flag = True for i in range(1,n-1): if flag: if s[i] == s[0]: flag = True else: res.append(i+1) flag = False else: if s[i] != s[0]: flag = True else: res.append(i+1) flag = False if flag and s[-1] == s[0]: print(len(res)) print(*res) exit() if (not flag) and s[-1] != s[0]: print(len(res)) print(*res) exit() res = [1] #sonomama flag = False for i in range(1,n-1): if flag: if s[i] != s[0]: flag = True else: res.append(i+1) flag = False else: if s[i] == s[0]: flag = True else: res.append(i+1) flag = False if flag and s[-1] != s[0]: print(len(res)) print(*res) exit() if (not flag) and s[-1] == s[0]: print(len(res)) print(*res) exit() print(-1)
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; string s; cin >> s; vector<long long int> a1(n, 0); vector<long long int> a2(n, 0); for (long long int i = 0; i < n; i++) { if (s[i] == 'B') a1[i] = -1; else a1[i] = 1; } a2 = a1; vector<long long int> v1; vector<long long int> v2; for (long long int i = 0; i < n - 1; i++) { if (a1[i] == -1) { a1[i + 1] *= -1; v1.push_back(i); } if (a2[i] == 1) { a2[i + 1] *= -1; v2.push_back(i); } } if (a1[n - 1] == -1 && a2[n - 1] == 1) cout << -1; else { if (a1[n - 1] != -1) { cout << v1.size() << endl; for (long long int i = 0; i < v1.size(); i++) cout << v1[i] + 1 << " "; } else { cout << v2.size() << endl; for (long long int i = 0; i < v2.size(); i++) cout << v2[i] + 1 << " "; } } return 0; }
CPP
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n = input() s = list(input()) if s.count("B")%2 == 1 and s.count("W")%2 == 1: print(-1) else: ans = [] for j in (0,1): for i in range(len(s)-1): if s[i] != 'WB'[j]: ans.append(i) s[i] = 'W' if s[i] == 'B' else 'B' s[i+1] = 'W' if s[i+1] == 'B' else 'B' # print(i, s) if s.count('W') == 0 or s.count('B') == 0: break print(len(ans)) print(' '.join(str(x+1) for x in ans))
PYTHON3
1271_B. Blocks
There are n blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed 3 β‹… n. If it is impossible to find such a sequence of operations, you need to report it. Input The first line contains one integer n (2 ≀ n ≀ 200) β€” the number of blocks. The second line contains one string s consisting of n characters, each character is either "W" or "B". If the i-th character is "W", then the i-th block is white. If the i-th character is "B", then the i-th block is black. Output If it is impossible to make all the blocks having the same color, print -1. Otherwise, print an integer k (0 ≀ k ≀ 3 β‹… n) β€” the number of operations. Then print k integers p_1, p_2, ..., p_k (1 ≀ p_j ≀ n - 1), where p_j is the position of the left block in the pair of blocks that should be affected by the j-th operation. If there are multiple answers, print any of them. Examples Input 8 BWWWWWWB Output 3 6 2 4 Input 4 BWBB Output -1 Input 5 WWWWW Output 0 Input 3 BWB Output 2 2 1 Note In the first example, it is possible to make all blocks black in 3 operations. Start with changing blocks 6 and 7, so the sequence is "BWWWWBBB". Then change blocks 2 and 3, so the sequence is "BBBWWBB". And finally, change blocks 4 and 5, so all blocks are black. It is impossible to make all colors equal in the second example. All blocks are already white in the third example. In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks 2 and 3 (so the sequence is "BBW"), and then change blocks 1 and 2 (so all blocks are white).
2
8
n=int(input()) s=input() q=list(s) b,w=s.count("B"),s.count("W") l=[] if b==n or w==n: print(0) elif b%2 and w%2: print(-1) else: if b%2==0: for i in range(len(q)-1): if q[i]=="B" and q[i+1]=="W": q[i],q[i+1]=q[i+1],q[i] l.append(i+1) elif q[i]=="B" and q[i+1]=="B": q[i],q[i+1]="W","W" l.append(i+1) else: for i in range(len(q)-1): if q[i]=="W" and q[i+1]=="B": q[i],q[i+1]=q[i+1],q[i] l.append(i+1) elif q[i]=="W" and q[i+1]=="W": q[i],q[i+1]="B","B" l.append(i+1) print(len(l)) print(*l)
PYTHON3