Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
#define mod1 (long long)998244353
#define mod2 (long long)1000000007
#define first_bit(x) (x&(-x))
#define last_bit(x) (x&~(-x))
#define ff first
#define int long long
#define ss second
#define pb push_back
#define w(x) long long x;cin>>x;while(x--)
#define vi vector<long long>
#define mii map<long long,long long>
#define pii pair<long long,long long>
#define set_bits __builtin_popcountll
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz(x) ((long long)x.size())
#define all(x) begin(x), end(x)
#define memo(x,y) memset((x),(y),sizeof(x));
long long fastexpo(long long a,long long b ,long long MOD = mod1){
long long ans=1;
while(b){
if(b&1)
ans=(ans*1ll*a);
a=(a*1ll*a);
b=b/2;
}
return ans;
}
int solve(string s,int val,int type){
if(type>1)type=1;
vector<int> p,np;
for(int i=0;i<s.size();i++){
if(i%2==val&&s[i]!='B')np.push_back(i);
if(i%2!=val&&s[i]=='B')p.push_back(i);
}
if(np.size()!=p.size())return INT64_MAX;
int ans=0;
for(int i=0;i<p.size();i++){
ans+=fastexpo(abs((p[i]-np[i])),type);
}
return ans;
}
signed main(){
w(t){
int n,k;
cin>>k;
string s;
cin>>s;
n=s.size();
int ans=min(solve(s,1,k),solve(s,0,k));
if(ans==INT64_MAX)cout<<-1<<endl;
else cout<<ans<<endl;
}
}
# cook your dish here
def outOfIndex(boys,girls,COST):
if COST == 0:
return len(boys)
else:
total_cost = [ abs(x-y) for x,y in zip(boys,girls)]
total_cost = sum(total_cost)
return total_cost
for _ in range(int(input())):
COST = int(input())
queue = input()
B = queue.count('B')
G = queue.count('G')
boys=[]
girls = []
if (abs(B-G)>1):
print(-1)
else:
if B > G:
for c in range(len(queue)):
if c%2!=0 and queue[c]=='B':
boys.append(c)
if c%2==0 and queue[c] =='G':
girls.append(c)
print(outOfIndex(boys,girls,COST))
boys.clear()
girls.clear()
elif B < G:
for c in range(len(queue)):
if c%2!=0 and queue[c]=='G':
girls.append(c)
if c%2==0 and queue[c] =='B':
boys.append(c)
print(outOfIndex(boys,girls,COST))
boys.clear()
girls.clear()
else:
for c in range(len(queue)):
if c%2!=0 and queue[c]=='B':
boys.append(c)
if c%2==0 and queue[c] =='G':
girls.append(c)
attempt1 = outOfIndex(boys,girls,COST)
boys.clear()
girls.clear()
for c in range(len(queue)):
if c%2!=0 and queue[c]=='G':
girls.append(c)
if c%2==0 and queue[c] =='B':
boys.append(c)
attempt2 = outOfIndex(boys,girls,COST)
print(min(attempt1,attempt2))
boys.clear()
girls.clear()
#include<stdio.h>
#include<string.h>
#include<math.h>
int abs(int);
int main()
{
int i,j,ans,type,l,g,b,e,t;
char s[100000];
scanf("%d",&t);
while(t--)
{scanf("%d",&type);
scanf("%s",&s);
l=strlen(s);
g=0;b=0;ans=0;j=0;
for(i=0;i<l;i++)
{
if(s[i]=='G')
g++;
else b++;
}
if(type==0)
{if(g==b+1)
{
for(i=0;i<l;i=i+2)
if(s[i]=='B')
ans++;
}
else if(b==g+1)
{
for(i=0;i<l;i=i+2)
if(s[i]=='G')
ans++;
}
else if(b==g)
{
e=0;
for(i=0;i<l;i=i+2)
{
if(s[i]=='B')
ans++;
else
e++;
}
if(e<ans)
ans=e;
}
else ans=-1;
}
else
{
if(g==b+1)
{
for(i=0;i<l;i=i+2)
{
while(s[j]!='G')
j++;
ans+=abs(i-j);
j++;
}
}
else if(b==g+1)
{
for(i=0;i<l;i=i+2)
{
while(s[j]!='B')
j++;
ans+=abs(i-j);
j++;
}
}
else if(b==g)
{
e=0;
for(i=0;i<l;i=i+2)
{
while(s[j]!='B')
j++;
ans+=abs(i-j);
j++;
}j=0;
for(i=0;i<l;i=i+2)
{
while(s[j]!='G')
j++;
e+=abs(i-j);
j++;
}
if(e<ans)
ans=e;
}
else ans=-1;
}
printf("%d\n",ans);
}
return 0;
}
int abs(int i)
{
if(i>0)
return i;
else
return -i;
}
/* package codechef; // don't place package name! */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
class Main
{
static class Point {
public double x, y;
private static final int MAX = (int) 1e6 + 1;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (int)x * MAX + (int)y;
}
public boolean equals(Object ob) {
if(ob instanceof Point) {
Point other = (Point) ob;
return other.x == x && other.y == y;
}
return false;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
/* static int days4=0;
static int all;
static long phi[]=new long[1000001];
static long ans[]=new long[1000001];
//static int tree[],sum[];
//public static long mod = 1000000007; public static long [][] tempMatrix;
public static long max = (long) Math.pow(10, 9) + 7;
static StringBuilder res = new StringBuilder();
//static Node tree[];
//static int a[];
static long mod = 998244353;
public static int rootColor=0;
static int MX = (1<<18);
static boolean primes[]=new boolean[10000001];
*/
private static final int MAXN = 5000;
private static final int MOD = 1000_000_007;
// private static Modular modular_nCr = new Modular(MOD);
// private static Modular modular = new Modular(MOD);
private static final long[][] nCr = new long[MAXN+5][MAXN+5];
// static int[] maxval = new int[1000000];
// static int[] minval = new int[1000000];
// private static long bruteAns = 1;
// static double eps = 1e-7;
static {
nCr[0][0]=1;
for(int i=0;i<=MAXN;i++)
nCr[i][0]=1;
for(int i=1;i<=MAXN;i++){
for(int j=1;j<=i;j++){
nCr[i][j]=(nCr[i-1][j-1]+nCr[i-1][j])%MOD;
}
}
}
/*
static { nCr[0][0] = 1;
for (int i = 1; i < MAXN; i++) {
nCr[i][0] = 1;
for (int j = 1; j <= i; j++) {
nCr[i][j] = modular_nCr.add(nCr[i - 1][j - 1], nCr[i - 1][j]);
} }
}
*/
public static void main (String[] args) throws java.lang.Exception
{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
FastReader in = new FastReader(System.in);
// FastScanner in=new FastScanner();
// Scanner sc = new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
// TreeMap<Integer, Integer> h1 = new TreeMap<Integer, Integer>();
HashMap<Integer, Integer> h2 = new HashMap<Integer, Integer>();
// HashSet<Point> s = new HashSet<Point>();
// HashSet<Double> s2 = new HashSet<Double>();
// HashSet<Double> s3 = new HashSet<Double>();
// HashSet<Character> h2 = new HashSet<Character>();
//long t= in.nextLong();
// long t = in.nextLong();
//DecimalFormat df = new DecimalFormat("#,###,##0.000000");
/* boolean prime[]=new boolean[10000000];
int p[]=new int[10000000];
int k=1;
// Arrays.fill(prime, true);
prime[0]=false;
prime[1]=false;for(int i=2;i<10000000;i++)
{
if(!prime[i])
{
p[i]=p[i-1];
continue;
}
p[i]=k; k++;
for(long j=(long)i*i;j<10000000;j+=i)
prime[(int)j]=false;
}
long mod = 1000000007;
StringBuilder sb = new StringBuilder();
// DecimalFormat df = new DecimalFormat("######0.00000000000");
// int[] dp = new int[101];*/
int t = in.nextInt();
while(t-->0)
{
int type = in.nextInt();
if(type>1)
type=1;
String s = in.next();
int n = s.length();
int b=0,g=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='B')
b++;
else
g++;
}
if(Math.abs(b-g)>1)
{
System.out.println(-1);
}
else if(b==g)
{
long cost=Long.MAX_VALUE;
cost = Math.min(cost , search('B','G',n, s, type));
cost = Math.min(cost , search('G','B',n, s, type));
System.out.println(cost);
}
else
{
long cost=Long.MAX_VALUE;
if(b>g)
{
cost = Math.min(cost , search('B','G',n, s, type));
}
else
{
cost = Math.min(cost , search('G','B',n, s, type));
}
System.out.println(cost);
}
}
/*
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(0)+" "+list1.get(0));
}*/
}
static long search(char fir,char sec,int n, String s,int type)
{
long cost = 0;
List<Integer> list=new ArrayList<>();
List<Integer> list1=new ArrayList<>();
for(int i=0;i<n;i++)
{
char ch=s.charAt(i);
if(i%2==0)
{
if(ch!=fir)
list.add(i);
}
else
{
if(ch!=sec)
list1.add(i);
}
}
Collections.sort(list);
Collections.sort(list1);
for(int i=0;i<list.size();i++)
{
long t=Math.abs(list.get(i)-list1.get(i));
if(type==0)
cost = cost + (long)Math.pow(t,type);
else
cost = cost + t;
}
return cost;
}
static long gcd(long a, long b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
static long nextPower_2 ( long x, long y )
{
long count = 0 ;
while ( y < x )
{count ++ ;
y = y << 1 ;
}
return count ;
}
static long power(long a, long b, long p)
{ long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x >= p) x %= p;
}
y = (y * y); if (y >= p) y %= p;
b /= 2;}
return x;
}
public static class Modular {
private int MOD;
Modular(int MOD) {
this.MOD = MOD;
}
public long add(long a, long b) {
return (a + b) % MOD;
}
public long sub(long a, long b) {
return (a - b + MOD) % MOD;
}
public long mul(long a, long b) {
return (a * b) % MOD;
}
public long div(long a, long b) {
return mul(a, inverseEuler(b));
} public long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);if (x >= MOD) x %= MOD;
}
y = (y * y);
if (y >= MOD) y %= MOD;
b /= 2;}
return x;
}
public long inverseEuler(long n) {
return power(n, MOD - 2);
}
}
static class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;FastReader(InputStream is) {
in = is;
} int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) { return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) {
sb.append((char) c);}
return sb.toString();
}String nextLine() throws IOException {
int c;for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
} return sb.toString();
}char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
return (char) c;
} int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan(); }
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}long nextLong() throws IOException {
int c;long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
/*
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(){br=new BufferedReader(new InputStreamReader(System.in));}
String nextToken(){
while(st==null||!st.hasMoreElements())
try{st=new StringTokenizer(br.readLine());}catch(Exception e){}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(nextToken());}
long nextLong(){return Long.parseLong(nextToken());}
double nextDouble(){return Double.parseDouble(nextToken());}
}*/
}
# cook your code here
def foo(typ,k,bg):
count=0
if typ==0:
for i in range(len(k)):
if k[i]!=bg[i]:
count+=1
return count/2
if typ==1 or typ==2:
b=[]
g=[]
for i in range(len(k)):
if k[i]!=bg[i]:
if k[i]=='B':
b.append(i)
else:
g.append(i)
for i in range(len(b)):
count=count+abs(b[i]-g[i])
return count
for _ in xrange(input()):
typ=input()
k=raw_input()
b= k.count("B")
g= k.count("G")
bg=''
gb=''
for _ in xrange(len(k)/2):
bg=bg+'BG'
gb=gb+'GB'
if len(k)%2==1:
bg=bg+'B'
gb=gb+'G'
if b==g:
v1=foo(typ,k,bg)
v2=foo(typ,k,gb)
print min(v1,v2)
elif b==g+1:
v1=foo(typ,k,bg)
print v1
elif g==b+1:
v2=foo(typ,k,gb)
print v2
else:
print -1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace chef4
{
class Program
{
static void Main(string[] args)
{
int type, b, g, i, j, len, m, f;
ulong ans;
string str;
int[] p = new int[100000];
int[] q = new int[100000];
string[] tokens = Console.ReadLine().Split();
int t = int.Parse(tokens[0]);
while (t-- != 0)
{
ans = 0;
tokens = Console.ReadLine().Split();
type = int.Parse(tokens[0]);
str = Console.ReadLine();
len = str.Length;
char[] s = str.ToCharArray();
char[] l = str.ToCharArray();
b = 0; g = 0; i = 0;
foreach (char c in s)
{
if (c == 'B')
{
p[b] = i;
b++;
}
else
{
q[g] = i;
g++;
}
++i;
}
if (type == 0)
{
if (b == g)
{
j = 0;
ulong temp;
m = 0; f = 0;
//starting with b
foreach (char c in s)
{
if (j % 2 == 0 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1)
{
while (q[f] < j || q[f] % 2 == 1)
f++;
s[j] = 'G';
s[q[f]] = 'B';
f++;
ans++;
}
else
{
while (p[m] < j || p[m] % 2 == 0)
m++;
s[j] = 'B';
s[p[m]] = 'G';
m++;
ans++;
}
j++;
}
temp = ans;
ans = 0;
j = 0;
m = 0; f = 0;
foreach (char c in l)
{
if (j % 2 == 0 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 0)
{
while (q[f] < j || q[f] % 2 == 0)
f++;
l[j] = 'G';
l[q[f]] = 'B';
f++;
ans++;
}
else
{
while (p[m] < j || p[m] % 2 == 1)
m++;
l[j] = 'B';
l[p[m]] = 'G';
m++;
ans++;
}
j++;
}
Console.WriteLine(ans < temp ? ans : temp);
}
else if (b == g + 1)
{
j = 0;
m = 0; f = 0;
ans = 0;
//starting with b
foreach (char c in s)
{
if (j % 2 == 0 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1)
{
while (q[f] < j || q[f] % 2 == 1)
f++;
s[j] = 'G';
s[q[f]] = 'B';
f++;
ans++;
}
else
{
while (p[m] < j || p[m] % 2 == 0)
m++;
s[j] = 'B';
s[p[m]] = 'G';
m++;
ans++;
}
j++;
}
Console.WriteLine(ans);
}
else if (g == b + 1)
{
j = 0;
m = 0; f = 0;
ans = 0;
//Starting with g
foreach (char c in s)
{
if (j % 2 == 0 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 0)
{
while (q[f] < j || q[f] % 2 == 0)
f++;
s[j] = 'G';
s[q[f]] = 'B';
f++;
ans++;
}
else
{
while (p[m] < j || p[m] % 2 == 1)
m++;
s[j] = 'B';
s[p[m]] = 'G';
m++;
ans++;
}
j++;
}
Console.WriteLine(ans);
}
else
Console.WriteLine(-1);
}
else
{
if (b == g)
{
j = 0;
ulong temp;
m = 0; f = 0;
//starting with b
foreach (char c in s)
{
if (j % 2 == 0 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1)
{
while (f < j || s[f] == 'B')
f++;
s[j]='G';
s[f]='B';
ans += (ulong)Math.Pow(f - j, 1);
}
else
{
while (m < j || s[m] == 'G')
m++;
s[j]='B';
s[m] = 'G';
ans += (ulong)Math.Pow(m - j, 1);
}
j++;
}
temp = ans;
ans = 0;
j = 0;
m = 0; f = 0;
foreach (char c in l)
{
if (j % 2 == 0 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 0)
{
while (f < j || l[f] == 'B')
f++;
l[j] = 'G';
l[f] = 'B';
ans += (ulong)Math.Pow(f - j, 1);
}
else
{
while (m < j || l[m] == 'G')
m++;
l[j] = 'B';
l[m] = 'G';
ans += (ulong)Math.Pow(m - j, 1);
}
j++;
}
Console.WriteLine(ans < temp ? ans : temp);
}
else if (b == g + 1)
{
j = 0;
m = 0; ans = 0; f = 0;
foreach (char c in s)
{
if (j % 2 == 0 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1)
{
while (f < j || s[f] == 'B')
f++;
s[j] = 'G';
s[f] = 'B';
ans += (ulong)Math.Pow(f - j, 1);
}
else
{
while (m < j || s[m] == 'G')
m++;
s[j] = 'B';
s[m] = 'G';
ans += (ulong)Math.Pow(m - j, 1);
}
j++;
}
Console.WriteLine(ans);
}
else if (g == b + 1)
{
ans = 0;
j = 0;
m = 0; f = 0;
foreach (char c in l)
{
if (j % 2 == 0 && c == 'G')
{
j++;
continue;
}
else if (j % 2 == 1 && c == 'B')
{
j++;
continue;
}
else if (j % 2 == 0)
{
while (f < j || l[f] == 'B')
f++;
l[j] = 'G';
l[f] = 'B';
ans += (ulong)Math.Pow(f - j, 1);
}
else
{
while (m < j || l[m] == 'G')
m++;
l[j] = 'B';
l[m] = 'G';
ans += (ulong)Math.Pow(m - j, 1);
}
j++;
}
Console.WriteLine(ans);
}
else
Console.WriteLine(-1);
}
}
}
}
}
In our experience, we suggest you solve this Devu and his Class CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Devu and his Class CodeChef Solution.
“I hope this Devu and his Class CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.
Keep Learning!