solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
n = int(input()) roaches = input() b_roaches_total = 0 b_roaches_even = 0 r_roaches_total = 0 r_roaches_even = 0 for i in range(len(roaches)): if roaches[i] == 'b': b_roaches_total += 1 if i % 2 == 0: b_roaches_even += 1 else: r_roaches_total += 1 if i % 2 == 0: r_roaches_even += 1 b_roaches_odd = b_roaches_total - b_roaches_even r_roaches_odd = r_roaches_total - r_roaches_even if b_roaches_odd >= b_roaches_even and r_roaches_even >= r_roaches_odd: print(max(b_roaches_even, r_roaches_odd)) else: print(max(b_roaches_odd, r_roaches_even))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; string s; cin >> s; if (n == 1) cout << 0 << endl; else { int b1 = 0, b2 = 0, r1 = 0, r2 = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') r1++; } else { if (s[i] == 'b') b1++; } } int c1 = max(b1, r1); for (i = 0; i < n; i++) { if (i % 2 == 1) { if (s[i] == 'r') r2++; } else { if (s[i] == 'b') b2++; } } int c2 = max(b2, r2); cout << min(c1, c2) << endl; } return 0; }
8
CPP
import math,sys,bisect,heapq from collections import defaultdict,Counter,deque from itertools import groupby,accumulate #sys.setrecursionlimit(200000000) int1 = lambda x: int(x) - 1 input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) ilelec = lambda: map(int1,input().split()) alelec = lambda: list(map(int1, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] #MOD = 1000000000 + 7 def Y(c): print(["NO","YES"][c]) def y(c): print(["no","yes"][c]) def Yy(c): print(["No","Yes"][c]) N = int(input()) s = [*input()] x = "rb"*((N//2)+1) y = "br"*((N//2)+1) mini = N if N==1: print(0) exit(0) def fun(t): P = [];Q= [] for i in range(N): if s[i] != t[i]: if s[i] == 'b': P.append(i) else: Q.append(i) #mm = min(len(P),len(Q)) nn = max(len(P),len(Q)) return nn print(min(fun(x),fun(y)))
8
PYTHON3
n = int(input()) s = input()#rbbrr ans11, ans12, ans21, ans22 = 0, 0, 0, 0#ans1 start with r for i in range(n): if i % 2 == 0: if s[i] == 'r': ans21 += 1 if s[i] == 'b': ans12 += 1 else: if s[i] == 'r': ans11 += 1 if s[i] == 'b': ans22 += 1 print((min(max(ans11, ans12), max(ans21, ans22))))
8
PYTHON3
n = int(input()) s = str(input()) def get_for(c): w = [0 , 0] for i in range(len(s)): if s[i] != c: w[ i % 2 ] += 1 c = 'r' if c == 'b' else 'b' return max(w[0], w[1]) print(min(get_for('r') , get_for('b')))
8
PYTHON3
num = int(input()) s = input() cnt_b1, cnt_r1, cnt_b2, cnt_r2 = 0, 0, 0, 0 for i, c in enumerate(s): if i % 2: if c == 'r': cnt_r1 += 1 else: cnt_b1 += 1 else: if c == 'r': cnt_r2 += 1 else: cnt_b2 += 1 print(min(max(cnt_b2, cnt_r1), max(cnt_r2, cnt_b1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(char c) { return '\'' + string(1, c) + '\''; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } string to_str(const int &n) { ostringstream stm; stm << n; return stm.str(); } string to_str(const long long &n) { ostringstream stm; stm << n; return stm.str(); } template <class T> string tostring(T x, int len = 0) { stringstream ss; ss << x; string r = ss.str(); if (int((r).size()) < len) r = string(len - int((r).size()), '0') + r; return r; } template <class T> void convert(string x, T &r) { stringstream ss(x); ss >> r; } long long powmod(long long a, long long b) { long long res = 1; a %= 1000000007; for (; b; b >>= 1) { if (b & 1) res = res * a % 1000000007; a = a * a % 1000000007; } return res; } long long modinv(long long a) { return powmod(a, 1000000007 - 2); } long long modinv(long long a, long long m) { return powmod(a, m - 2); } long long nCkMp(long long n, long long k) { long long numerator = 1, denominator = 1; for (int i = 0; i < k; i++) numerator = (numerator * (n - i)) % 1000000007; for (int i = 1; i <= k; i++) denominator = (denominator * i) % 1000000007; return (numerator * modinv(denominator)) % 1000000007; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int TT = 1, tt; clock_t t1; void solve() { t1 = clock(); int n, a = 0, b = 0, c = 0, d = 0; string s; (cin >> (n)); (cin >> (s)); for (auto i = (0); i < (n); i++) { if (i % 2) { if (s[i] == 'r') a++; } else if (s[i] == 'b') b++; } for (auto i = (0); i < (n); i++) { if (i % 2) { if (s[i] == 'b') c++; } else if (s[i] == 'r') d++; } (cout << (min(max(a, b), max(c, d))) << endl); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (tt = 0; tt < TT; tt++) solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, s1, s2; cin >> s; for (int i = 0; i < n; i++) { if (i % 2 == 0) { s1 += 'r'; s2 += 'b'; } else { s1 += 'b'; s2 += 'r'; } } int r = 0, b = 0; int res1 = 0, res2 = 0; for (int i = 0; i < n; i++) { if (s[i] != s1[i]) { if (s[i] == 'r') ++r; if (s[i] == 'b') ++b; } } res1 = max(b, r); b = 0; r = 0; for (int i = 0; i < n; i++) if (s[i] != s2[i]) { if (s[i] == 'r') ++r; if (s[i] == 'b') ++b; } res2 = max(b, r); cout << min(res1, res2); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, l, n, x = 0, y = 0, p = 0, q = 0; string s; cin >> n; char m[n + 1]; cin >> s; if (s[0] == 'r') for (i = 0; i < n;) { m[i] = 'r'; i++; if (i >= n) break; m[i] = 'b'; i++; if (i >= n) break; } else for (i = 0; i < n;) { m[i] = 'b'; i++; if (i >= n) break; m[i] = 'r'; i++; if (i >= n) break; } for (i = 0; i < n; i++) { if (m[i] != s[i]) { if (m[i] == 'r') x++; if (m[i] == 'b') y++; } } if (s[0] == 'r') for (i = 0; i < n;) { m[i] = 'b'; i++; if (i >= n) break; m[i] = 'r'; i++; if (i >= n) break; } else for (i = 0; i < n;) { m[i] = 'r'; i++; if (i >= n) break; m[i] = 'b'; i++; if (i >= n) break; } for (i = 0; i < n; i++) { if (m[i] != s[i]) { if (m[i] == 'r') p++; if (m[i] == 'b') q++; } } cout << min(max(x, y), max(p, q)); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; const int inf = 0x3f3f3f3f; int t, n; int main() { int n; scanf("%d", &n); getchar(); char str[100005]; scanf("%s", str); int sl = strlen(str); int r = 0; int b = 0; int cr = 0, cb = 0; for (int i = 0; i < sl; i++) { if (i % 2 == 0) { if (str[i] != 'r') cr++; } else { if (str[i] != 'b') cb++; } } int cha = min(cr, cb); cha += (cr - cha) + cb - cha; cr = 0; cb = 0; for (int i = 0; i < sl; i++) { if (i % 2 == 0) { if (str[i] != 'b') cb++; } else { if (str[i] != 'r') cr++; } } int cha2 = min(cr, cb); cha2 += (cr - cha2) + cb - cha2; printf("%d\n", min(cha, cha2)); return 0; }
8
CPP
#Juli n = int(input()) s = input() r_p = 0 r_i = 0 b_p = 0 b_i = 0 for i in range(n): if(i%2==0): if(s[i]=='b'): b_p+=1 else: r_p+=1 else: if(s[i]=='b'): b_i+=1 else: r_i+=1 m1 = min(r_i,b_p) sol1 = b_p-m1+r_i-m1 + m1 m2 = min(b_i,r_p) sol2 = b_i-m2+r_p-m2+m2 print(min(sol1,sol2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, len, r1 = 0, b1 = 0, r2 = 0, b2 = 0, ans1, ans2, i; char str[100005]; cin >> n; cin >> str; len = strlen(str); for (i = 0; i <= len - 1; i++) { if (i % 2 == 0 && str[i] != 'r') r1++; if (i % 2 == 1 && str[i] != 'b') b1++; } ans1 = max(r1, b1); for (i = 0; i <= len - 1; i++) { if (i % 2 == 0 && str[i] != 'b') b2++; if (i % 2 == 1 && str[i] != 'r') r2++; } ans2 = max(r2, b2); cout << min(ans1, ans2) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; char m = 'b'; int ans = 0; int k = 0, bm = 0, rm = 0; while (k < n) { if (s[k] == m) { if (m == 'b') m = 'r'; else m = 'b'; k++; } else { if (s[k] == 'b') bm++; else rm++; if (m == 'b') m = 'r'; else m = 'b'; k++; } } int ans1 = min(bm, rm) + abs(rm - bm); m = 'r'; k = 0, bm = 0, rm = 0; while (k < n) { if (s[k] == m) { if (m == 'b') m = 'r'; else m = 'b'; k++; } else { if (s[k] == 'b') bm++; else rm++; if (m == 'b') m = 'r'; else m = 'b'; k++; } } int ans2 = min(bm, rm) + abs(rm - bm); cout << min(ans1, ans2) << endl; return 0; }
8
CPP
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() # for _ in range(int(ri())): n = int(ri()) s = ri() s = [0 if i == 'r' else 1 for i in s] cnt1,cnt2 = 0,0 start = 1 for i in range(len(s)): if s[i] == start: start = start^1 continue else: if s[i] == 0: cnt1+=1 else: cnt2+=1 start = start^1 ans1 = max(cnt1, cnt2) cnt1,cnt2 = 0,0 start = 0 for i in range(len(s)): if s[i] == start: start = start^1 continue else: if s[i] == 0: cnt1+=1 else: cnt2+=1 start = start ^ 1 ans2 = max(cnt1, cnt2) print(min(ans1,ans2))
8
PYTHON3
mod = 1000000007 MOD = 998244353 ii = lambda : int(input()) si = lambda : input() dgl = lambda : list(map(int, input())) f = lambda : map(int, input().split()) il = lambda : list(map(int, input().split())) it = lambda : tuple(map(int, input().split())) ls = lambda : list(input()) n = ii() s = ls() s1='br'*10**5 s2='rb'*10**5 b1,w1,b2,w2=0,0,0,0 for i in range(n): if s[i] != s1[i]: if s[i]=='b': b1+=1 else: w1+=1 if s[i] != s2[i]: if s[i]=='b': b2+=1 else: w2+=1 print(min(max(b1,w1),max(b2,w2)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void swap(int &a, int &b) { int c = a; a = b; b = c; } bool prime(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; } void solve() { int n, b1 = 0, r1 = 0, b2 = 0, r2 = 0; cin >> n; string l; cin >> l; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (l[i] == 'r') b1++; else r2++; } else { if (l[i] == 'b') r1++; else b2++; } } int a1 = max(b1, r1); int a2 = max(b2, r2); cout << min(a1, a2); } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { return a > b ? a : b; } long long int min(long long int a, long long int b) { return a + b - max(a, b); } int arr[101]; int main() { cin.sync_with_stdio(false); int i, j, n, b = 0, r = 0; string str; cin >> n; cin >> str; for (i = 0; i < str.size(); i++) if (str[i] == 'b') b++; else r++; int ans = INT_MAX; int misPlacedB = 0, misPlacedR = 0; for (i = 0; i < str.size(); i++) { if (i % 2 == 0) { if (str[i] == 'r') { continue; } misPlacedR++; } else { if (str[i] == 'b') { continue; } misPlacedB++; } } int here; here = abs(misPlacedB - misPlacedR); ans = min(ans, min(misPlacedB, misPlacedR) + here); misPlacedR = misPlacedB = 0; for (i = 0; i < str.size(); i++) { if (i % 2 == 1) { if (str[i] == 'r') { continue; } misPlacedR++; } else { if (str[i] == 'b') { continue; } misPlacedB++; } } here = abs(misPlacedB - misPlacedR); ans = min(ans, min(misPlacedB, misPlacedR) + here); cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> int main() { int n, i, j; int top = 1; char d = 'r'; scanf("%d", &n); char a[n + 1]; scanf("%s", a); int p = 0, q = 0, r = 0, s = 0; int t, u; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (a[i] == d) p++; else q++; } else { if (a[i] == d) r++; else s++; } } if (s >= p) t = s; else t = p; if (q >= r) u = q; else u = r; if (t <= u) printf("%d", t); else printf("%d", u); return 0; }
8
CPP
n = int(input()) s = input() a = [0] * n for i in range(n): if s[i] == 'r':a[i] = 1 u = v = 0 for i in range(n): if i % 2 == 0: if a[i] != 1:u += 1 else: if a[i] != 0:v += 1 ans1 = max(u, v) u = v = 0 for i in range(n): if i % 2 == 0: if a[i] != 0:u += 1 else: if a[i] != 1:v += 1 ans2 = max(u, v) print(min(ans1, ans2))
8
PYTHON3
n=int(input()) a=input() b="rb"*n c="br"*n bb=0 cc=0 for i in range(n): if(a[i]==b[i]):bb+=1 if(c[i]==a[i]):cc+=1 if(bb>cc): q=0 w=0 for i in range(n): if(i%2==0): if(a[i]=="b"):q+=1 else: if(a[i]=="r"):w+=1 if(q>w):print(q) else:print(w) else: q=0 w=0 for i in range(n): if(i%2==0): if(a[i]=="r"):q+=1 else: if(a[i]=="b"):w+=1 if(q>w):print(q) else:print(w)
8
PYTHON3
def build_line(first, n): elems = ['r', 'b'] result = [] for _ in range(n): result.append(elems[first%2]) first += 1 return ''.join(result) def compare_lines(expected, actual): red_unordered = 0 black_unordered = 0 for i in range(len(actual)): if expected[i] != actual[i]: if actual[i] == 'r': red_unordered += 1 else: black_unordered += 1 return max(red_unordered, black_unordered) if __name__ == '__main__': n = int(input()) cockroaches = input() red_res = compare_lines(build_line(0, n), cockroaches) black_res = compare_lines(build_line(1, n), cockroaches) print(min(red_res, black_res))
8
PYTHON3
n = int(input()) s = input() r,b = 0,0 for x in s: if(x=='r'): r += 1 else: b += 1 trr = [x for x in s] if(n): krr = trr[:] c = 0 i = 0 for p in range(0,n,2): if(krr[p]=='b'): i +=1 j = 0 for p in range(1,n,2): if(krr[p]=='r'): j += 1 krr = trr[:] lrr = [max(i,j)] c = 0 i = 0 for p in range(0,n,2): if(krr[p]=='r'): i +=1 j = 0 for p in range(1,n,2): if(krr[p]=='b'): j += 1 lrr.append(max(i,j)) print(min(lrr))
8
PYTHON3
cnt_b1, cnt_r1, cnt_b2, cnt_r2 = 0, 0, 0, 0 num = int(input()) s = input() for i, c in enumerate(s): if i % 2 == 0: if c == 'r': cnt_r1 += 1 else: cnt_b1 += 1 else: if c == 'r': cnt_r2 += 1 else: cnt_b2 += 1 print(min(max(cnt_b2, cnt_r1), max(cnt_r2, cnt_b1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, t1, t2, cnt, ans, sum; char s[100010]; while (~scanf("%d", &n)) { scanf("%s", s); t1 = t2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != 'b') t1++; } else { if (s[i] != 'r') t2++; } } ans = max(t1, t2); t1 = t2 = 0; for (int i = 0; i < n; i++) { if (i % 2) { if (s[i] != 'b') t1++; } else { if (s[i] != 'r') t2++; } } cnt = max(t1, t2); sum = min(cnt, ans); printf("%d\n", sum); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i; string a; cin >> n >> a; int len = a.size(); char s; int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; s = a[0]; for (i = 0; i < len; i += 2) { if (a[i] == s) sum1++; else sum2++; } for (i = 1; i < len; i += 2) { if (a[i] == s) sum3++; else sum4++; } int ans1 = max(sum1, sum4), ans2 = max(sum2, sum3); cout << min(ans1, ans2) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i, be = 0, bo = 0, re = 0, ro = 0, bero = 0, bore = 0; cin >> n; string s; cin >> s; for (i = 0; i < n; i++) { if ((i + 1) % 2 == 0) { if (s[i] == 'b') be++; else if (s[i] == 'r') re++; } if ((i + 1) % 2 != 0) { if (s[i] == 'b') bo++; else if (s[i] == 'r') ro++; } } bero = be + ro; bore = bo + re; if (bero < bore) cout << max(be, ro); else cout << max(bo, re); return 0; }
8
CPP
n = int(input()) ar = list(input()) kek = 0 for i in range(n): if i % 2 == 0 and ar[i] == 'r': kek += 1 if i % 2 == 1 and ar[i] == 'b': kek += 1 flag = 0 if n - kek < kek: kek = n - kek flag = 1 a, b = 0, 0 for i in range(n): if flag == 0: if i % 2 == 0 and ar[i] == 'r': a += 1 if i % 2 == 1 and ar[i] == 'b': b += 1 else: if i % 2 == 0 and ar[i] == 'b': a += 1 if i % 2 == 1 and ar[i] == 'r': b += 1 print(max(0, kek - min(a, b)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long result = 1; while (b > 0) { if (b % 2 == 1) { result *= a; } a *= a; b /= 2; } return result; } long long gcd(long long x, long long y) { long long r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } long long countSetBits(long long x) { long long Count = 0; while (x > 0) { if (x & 1) Count++; x = x >> 1; } return Count; } bool isPerfectSquare(long long n) { long long sr = sqrt(n); if (sr * sr == n) return true; else return false; } long long mod(long long x, long long M) { return ((x % M + M) % M); } long long add(long long a, long long b, long long M) { return mod(mod(a, M) + mod(b, M), M); } long long mul(long long a, long long b, long long M) { return mod(mod(a, M) * mod(b, M), M); } long long powerM(long long a, long long b, long long M) { long long res = 1ll; while (b) { if (b % 2ll == 1ll) { res = mul(a, res, M); } a = mul(a, a, M); b /= 2ll; } return res; } long long nCr(long long n, long long k) { if (n < k) return 0; if (k == 0) return 1; long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); } long long nCrM(long long n, long long r, long long M) { if (n < r) return 0; if (r == 0) return 1; vector<long long> fact(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i, M); } return mul(mul(fact[n], modInverse(fact[r], M), M), modInverse(fact[n - r], M), M); } void solve() { long long n; cin >> n; string s; cin >> s; long long l1 = 0, l2 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0 && s[i] != 'r') { l1++; } else if (i % 2 == 1 && s[i] != 'b') { l1++; } } for (long long i = 0; i < n; i++) { if (i % 2 == 1 && s[i] != 'r') { l2++; } else if (i % 2 == 0 && s[i] != 'b') { l2++; } } long long ans = 0, ans1 = 0, ans2 = 0, ans3 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') { continue; } else { ans++; } } else { if (s[i] == 'b') { continue; } else { ans1++; } } } for (long long i = 0; i < n; i++) { if (i % 2 == 1) { if (s[i] == 'r') { continue; } else { ans2++; } } else { if (s[i] == 'b') { continue; } else { ans3++; } } } cout << min(max(ans, ans1), max(ans2, ans3)) << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, r1 = 0, b1 = 0, r2 = 0, b2 = 0, minn, r = 0, b = 0, max1 = 0, max2 = 0; string s; s.clear(); cin >> n; cin >> s; for (i = 0; i < n; i += 2) { if (s[i] != 'r') r1++; } for (i = 1; i < n; i += 2) { if (s[i] != 'b') b1++; } max1 = max(r1, b1); for (i = 0; i < n; i += 2) { if (s[i] != 'b') b2++; } for (i = 1; i < n; i += 2) { if (s[i] != 'r') r2++; } max2 = max(r2, b2); minn = min(max1, max2); cout << minn << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int x[N], t[N]; int main() { int i, j, n; string s; cin >> n >> s; for (i = 0; i < n; ++i) if (s[i] == 'r') x[i] = 1; else x[i] = 0; int p = 0, ret = 1e9, cur = 0; for (i = 0; i < n; ++i) t[i] = p, p ^= 1; p = 1; for (i = 0, j = 0; i < n; ++i) { if (x[i] != t[i]) { for (j = max(j, i + 1); j < n; ++j) if (x[j] == t[i] && x[j] != t[j]) { swap(x[j], x[i]), ++j; ++cur; break; } if (x[i] != t[i]) ++cur; } } ret = cur; for (i = 0; i < n; ++i) if (s[i] == 'r') x[i] = 1; else x[i] = 0; for (i = 0; i < n; ++i) t[i] = p, p ^= 1; cur = 0; for (i = 0, j = 0; i < n; ++i) { if (x[i] != t[i]) { for (j = max(j, i + 1); j < n; ++j) if (x[j] == t[i] && x[j] != t[j]) { swap(x[j], x[i]); ++j; ++cur; break; } if (x[i] != t[i]) ++cur; } } ret = min(ret, cur); cout << ret << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; char s[100001]; int main() { int n; scanf("%d", &n); scanf("%s", s); int r[2] = {0, 0}, b[2] = {0, 0}; for (int i = 0; i < n; ++i) { if (s[i] == 'r') { r[i % 2]++; } else { b[i % 2]++; } } printf("%d\n", min(max(r[0], b[1]), max(r[1], b[0]))); return 0; }
8
CPP
while True: try: n = int(input()) line = input() lenght = len(line) #正确的交错方式 cpline1 = ['r','b']*(lenght//2 + 1) cpline2 = ['b','r']*(lenght//2 + 1) #不同的r与不同的b数量 dr1 , db1 = 0, 0 for i in range(lenght): if(line[i] != cpline1[i]): if line[i] == 'r': dr1 += 1 else: db1 += 1 ans1 = max(dr1, db1) dr2 , db2 = 0, 0 for i in range(lenght): if(line[i] != cpline2[i]): if line[i] == 'r': dr2 += 1 else: db2 += 1 ans2 = max(dr2, db2) print(min(ans1, ans2)) except EOFError: break
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string input; cin >> input; int balances[4] = {0, 0, 0, 0}; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (input[i] == 'r') { balances[0]++; } else { balances[1]++; } } else { if (input[i] == 'r') { balances[2]++; } else { balances[3]++; } } } cout << min(max(balances[1], balances[2]), max(balances[0], balances[3])) << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int i, n; cin >> n; string A; cin >> A; int b[2], r[2]; b[0] = b[1] = r[0] = r[1] = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (A[i] == 'r') { r[0]++; } else { b[1]++; } } else { if (A[i] == 'r') { r[1]++; } else { b[0]++; } } } int temp; temp = min(b[0], r[0]); if (temp != b[0]) { r[0] = b[0]; b[0] = temp; } temp = min(b[1], r[1]); if (temp != b[1]) { r[1] = b[1]; b[1] = temp; } cout << min(r[1], r[0]) << "\n"; return 0; }
8
CPP
n=int(input()) line=input() start1, start2, r2b1, r2b2, b2r1, b2r2 = 'r', 'b', 0, 0, 0, 0 idx=0 while(idx<n): b2r1+=(start1=='r' and line[idx]=='b') r2b1+=(start1=='b' and line[idx]=='r') b2r2+=(start2=='r' and line[idx]=='b') r2b2+=(start2=='b' and line[idx]=='r') start1=['r', 'b'][start1=='r'] start2=['r', 'b'][start2=='r'] idx+=1 print(min(max(r2b1, b2r1), max(r2b2, b2r2)))
8
PYTHON3
n=int(input()) m=input() rb={'r':0,'b':0} br={'r':0,'b':0} for i in range(n): if i%2==0: if m[i]=='b': br['b']+=1 else: rb['r']+=1 else: if m[i]=='r': br['r']+=1 else: rb['b']+=1 rbs=min(rb['b'],rb['r'])+abs(rb['b']-rb['r']) brs=min(br['b'],br['r'])+abs(br['b']-br['r']) print(min(rbs,brs))
8
PYTHON3
n, s = int(input()), input() def f(x): a, b = s[::2].count(x[0]), s[1::2].count(x[1]) return min(a, b) + abs(a - b) print(min(f('rb'), f('br')))
8
PYTHON3
n = int(input()) s = input() xb, yb, xr, yr = 0, 0, 0, 0 #rbrb... for i in range(n): if (i % 2 == 0 and s[i] == 'b'): xr += 1 elif (i % 2 == 1 and s[i] == 'r'): yr += 1 #brbr... for i in range(n): if (i % 2 == 0 and s[i] == 'r'): yb += 1 elif (i % 2 == 1 and s[i] == 'b'): xb += 1 print(min(max(yb, xb), max(xr, yr)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long inf = 9999999999LL; const long long mo = 1000000007LL; int main() { int n, i, j, c1 = 0, c2 = 0, x, y, c3 = 0, c4 = 0; string second, r, b; cin >> n >> second; r = second; b = second; for (i = 0; i < n; i++) { if (i % 2) r[i] = 'b'; else r[i] = 'r'; if (i % 2) b[i] = 'r'; else b[i] = 'b'; } for (i = 0; i < n; i++) { if (second[i] != r[i]) { if (second[i] == 'r') c1++; else c2++; } if (second[i] != b[i]) { if (second[i] == 'r') c3++; else c4++; } } x = max(c1, c2); y = max(c3, c4); cout << min(x, y) << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long int inf = 1e18; int main() { long long int n, r = 0, b = 0; cin >> n; string second; cin >> second; for (long long int i = (0); i < (n); i++) { if (second[i] == 'r') { r++; } else { b++; } } long long int ans = 0; if (n % 2) { long long int k1 = 0, k2 = 0; ans += (max(0ll, min(max(r, b) - n / 2, max(r, b) - (1 + n / 2)))); for (long long int i = (0); i < (n); i++) { if (i % 2 && second[i] == 'r') { k1++; } else if (i % 2 == 0 && second[i] == 'b') { k1++; } else if (i % 2 == 0 && second[i] == 'r') { k2++; } else if (i % 2 && second[i] == 'b') { k2++; } } k1 = max(0ll, k1 - ans); k2 = max(0ll, k2 - ans); if (r > b) { cout << min(ans + (k1) / 2, ans + 1 + (k2) / 2); return 0; } else { cout << min(ans + (k1) / 2 + 1, ans + (k2) / 2); return 0; } } else { ans += max(r, b) - n / 2; long long int k1 = 0, k2 = 0; for (long long int i = (0); i < (n); i++) { if (i % 2 && second[i] == 'r') { k1++; } else if (i % 2 == 0 && second[i] == 'b') { k1++; } else if (i % 2 == 0 && second[i] == 'r') { k2++; } else if (i % 2 && second[i] == 'b') { k2++; } } k1 = max(0ll, k1 - ans); k2 = max(0ll, k2 - ans); cout << min(ans + (k1) / 2, ans + (k2) / 2); } return 0; }
8
CPP
import sys,copy n=int(input()) roaches=list(input().strip()) roaches2=copy.deepcopy(roaches) #case 1: 1st cockroach unchanged b_swaps = 0 r_swaps = 0 for i in range(1,n): if roaches[i]=='b': if roaches[i-1]=='b': roaches[i]='r' b_swaps+=1 else: if roaches[i-1]=='r': roaches[i]='b' r_swaps+=1 sol1=max(r_swaps,b_swaps) #case 2: 1st cockroach changed b_swaps = 0 r_swaps = 0 #print('Initial...',*roaches,sep=' ') #print('Initial2...',*roaches2,sep=' ') if roaches2[0]=='b': roaches2[0]='r' b_swaps+=1 else: roaches2[0]='b' r_swaps+=1 for i in range(1,n): if roaches2[i]=='b': if roaches2[i-1]=='b': roaches2[i]='r' b_swaps+=1 else: if roaches2[i-1]=='r': roaches2[i]='b' r_swaps+=1 sol2=max(r_swaps,b_swaps) #print('r_swaps',r_swaps,'b_swaps',b_swaps) print(sol1 if sol1<sol2 else sol2) #print(sol2)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; char a[111111]; char c[2] = {'r', 'b'}; int main() { int n; scanf("%d%s", &n, a); int ra = 0, rc = 0, d = 0; int ans1 = 0, ans2 = 0; int diff = 0, t = 0; for (int i = 0; i < n; i++) { if (a[i] != c[t]) { ++diff; if (a[i] == 'r') ++ra; else ++rc; } t = (t + 1) % 2; } d = abs(ra - rc); ans1 = d + (diff - d) / 2; ra = rc = d = diff = 0; t = 1; for (int i = 0; i < n; i++) { if (a[i] != c[t]) { ++diff; if (a[i] == 'r') ++ra; else ++rc; } t = (t + 1) % 2; } d = abs(ra - rc); ans2 = d + (diff - d) / 2; printf("%d\n", min(ans1, ans2)); return 0; }
8
CPP
import math n = int(input()) insects = input() resr1 = 0 resb1 = 0 for i in range(n): if i % 2 == 0 and insects[i] == "r": resr1 += 1 elif i % 2 == 1 and insects[i] == "b": resb1 += 1 res1 = min(resr1, resb1) + abs((resr1 - resb1)) resr2 = 0 resb2 = 0 for i in range(n): if i % 2 == 0 and insects[i] == "b": resb2 += 1 elif i % 2 == 1 and insects[i] == "r": resr2 += 1 res2 = min(resr2, resb2) + abs((resr2 - resb2)) print(min(res1, res2))
8
PYTHON3
n = int(input()) xs = input() r1, r2, b1, b2 = 0, 0, 0, 0 for i, c in enumerate(xs): if (i % 2) == 0: if c == 'r': r1 += 1 else: b2 += 1 else: if c == 'r': r2 += 1 else: b1 += 1 print(min(max(r1, b1), max(r2, b2)))
8
PYTHON3
input() S=4*[0] e=0 for c in input(): S[e|('r'!=c)<<1]+=1 e=not e print(min(max(S[0],S[3]),max(S[1],S[2])))
8
PYTHON3
n = int(input()) s = input() # check how many b and r are not in position # max of that will be required for the change # min of that will go in the swap part and the rest will go in painting part sb,sr = 0,0 # for brbrbr..... for i in range(0,n): if i%2 and s[i] != 'b': sb += 1 if not i%2 and s[i] != 'r': sr += 1 maxx = max(sr,sb) sr,sb = 0,0 # for rbrbrbr.... for i in range(0,n): if i%2 and s[i] != 'r': sr += 1 if not i%2 and s[i] != 'b': sb += 1 maxx1 = max(sr,sb) print(min(maxx,maxx1))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; int r1 = 0, r2 = 0, b1 = 0, b2 = 0; for (int x = 0; x < n; ++x) { if (x % 2) { if (s[x] == 'r') ; else ++r1; if (s[x] == 'b') ; else ++b2; } else { if (s[x] == 'b') ; else ++b1; if (s[x] == 'r') ; else ++r2; } } int res1 = max(r1, b1), res2 = max(r2, b2); cout << min(res1, res2) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long N, r1, r2, b1, b2, ans1, ans2; char A[100005]; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; if (A[i] == 'b') { if (i % 2) b1++; else b2++; } else { if (i % 2) r2++; else r1++; } } ans1 = max(r1, b1) - min(r1, b1) + (min(r1, b1) % 2 ? 1 + (min(r1, b1) - 1) : min(r1, b1)); ans2 = max(r2, b2) - min(r2, b2) + (min(r2, b2) % 2 ? 1 + (min(r2, b2) - 1) : min(r2, b2)); cout << min(ans1, ans2); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int oo = 0xfffffff; const int maxn = 100000 + 1; char str[maxn]; int solve(char b, char r) { int x, y, ans; x = y = ans = 0; for (int i = 0; str[i]; i++) { if (i % 2 == 0) { if (str[i] == b) continue; if (str[i] == r) { if (x) x--; else ans++, y++; } } else { if (str[i] == r) continue; if (str[i] == b) { if (y) y--; else ans++, x++; } } } return ans; } int main() { int n; while (~scanf("%d", &n)) { scanf("%s", str); printf("%d\n", min(solve('b', 'r'), solve('r', 'b'))); } return 0; }
8
CPP
n = int(input()) a = input() def calc(s, t): dm = {'r': 0, 'b': 1} ct = [0] * 2 for i in range(n): if s[i] != t[i]: ct[dm[s[i]]] += 1 return max(ct) ans = [ calc(a, ''.join(('rb'[i & 1] for i in range(n)))), calc(a, ''.join(('br'[i & 1] for i in range(n)))), ] print(min(ans))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename TA, typename TB> ostream &operator<<(ostream &ostm, const pair<TA, TB> &p) { ostm << "(" << p.first << "," << p.second << ")"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const vector<T> &v) { ostm << "[ "; for (auto i : v) ostm << i << " "; ostm << "]"; return ostm; } template <typename TA, typename TB> ostream &operator<<(ostream &ostm, const map<TA, TB> &mp) { ostm << "[ "; for (auto it : mp) ostm << *it << " "; ostm << "]"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const set<T> &s) { ostm << "[ "; for (auto it : s) ostm << *it << " "; ostm << "]"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const stack<T> &inp) { stack<T> st = inp; ostm << "[ "; while (!st.empty()) { ostm << st.top() << " "; st.pop(); } ostm << "]"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const queue<T> &inp) { queue<T> q = inp; ostm << "[ "; while (!q.empty()) { ostm << q.front() << " "; q.pop(); } ostm << "]"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const priority_queue<T> &inp) { priority_queue<T> pq = inp; ostm << "[ "; while (!pq.empty()) { ostm << pq.top() << " "; pq.pop(); } ostm << "]"; return ostm; } template <typename T> ostream &operator<<(ostream &ostm, const deque<T> &inp) { deque<T> dq = inp; ostm << "[ "; while (!dq.empty()) { ostm << dq.front() << " "; dq.pop_front(); } ostm << "]"; return ostm; } inline int lowbit(int &x) { return x & -x; } inline long long lowbit(long long &x) { return x & -x; } template <typename T> inline T _pow(T b, int n) { T a = 1; while (n) { if (n & 1) a *= b; b *= b, n >>= 1; } return a; } template <typename T> inline T _pow(T b, int n, T mod) { T a = 1 % mod; while (n) { if (n & 1) a = a * b % mod; b = b * b % mod, n >>= 1; } return a; } inline int gtx() { const int N = 1048576; static char __buffer[N]; static char *__p = __buffer, *__end = __buffer; if (__p == __end) { if ((__end = __buffer + fread(__buffer, 1, N, stdin)) == __buffer) return EOF; __p = __buffer; } return *__p++; } template <typename T> inline bool rit(T &x) { char c = 0; bool fg = 0; while (c = gtx(), (c < '0' && c != '-') || c > '9') if (c == EOF) return false; c == '-' ? (fg = 1, x = 0) : (x = c - '0'); while (c = gtx(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if (fg) x = -x; return true; } template <typename T, typename... Args> inline bool rit(T &x, Args &...args) { return rit(x) && rit(args...); } inline void pit(int x) { printf("%d", x); } inline void pln(long long x) { printf("%I64d", x); } template <typename... Args> inline void pit(int x, Args... args) { printf("%d ", x); pit(args...); } template <typename... Args> inline void pln(long long x, Args... args) { printf("%I64d ", x); pit(args...); } const long double PI = 3.14159265358979323846264338327950288; const long double eps = 1e-8; const long long mod = 1e9 + 7; int startB(int n, int b, int r, string &s) { int ans = 0; int cb = 0, cr = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0 && s[i] != 'b') { ++ans; ++cb; } if (i % 2 && s[i] != 'r') { ++ans; ++cr; } } return ans - min(cb, cr); } int startR(int n, int b, int r, string &s) { int ans = 0; int cb = 0, cr = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0 && s[i] != 'r') { ++ans; ++cb; } if (i % 2 && s[i] != 'b') { ++ans; ++cr; } } return ans - min(cb, cr); } int main() { int n; string s; cin >> n >> s; int b = 0, r = 0; for (char c : s) { if (c == 'b') ++b; else ++r; } int ans = min({startB(n, b, r, s), startR(n, b, r, s)}); printAns:; cout << ans << '\n'; }
8
CPP
#include <bits/stdc++.h> using namespace std; queue<pair<int, int> > q; vector<pair<int, int> > v[100005]; char a[100005]; int n; void s(int &x) { int t; scanf("%d", &t); x = t; } long long pow(long long x, long long y) { if (y == 1LL) return x % 1000000007; long long temp = pow(x, y / 2LL); if (y % 2LL == 0LL) return temp * temp % 1000000007; return ((temp * temp % 1000000007) * x) % 1000000007; } int main() { s(n); scanf("%s", a); int cola = 0, colb = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && a[i] == 'b') colb++; if (i % 2 == 1 && a[i] == 'r') cola++; } int temp = min(cola, colb); int ans = temp + max(cola - temp, colb - temp); cola = colb = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1 && a[i] == 'b') cola++; if (i % 2 == 0 && a[i] == 'r') colb++; } temp = min(cola, colb); ans = min(ans, temp + max(cola - temp, colb - temp)); printf("%d", ans); }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 2e5 + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long n, ans; string s; cin >> n >> s; long long btor = (s[0] == 'b'), rtob = 0; char lastch = 'r'; for (int i = 1; i < n; i++) { if (s[i] == lastch) lastch == 'r' ? rtob++ : btor++; lastch = (lastch == 'r' ? 'b' : 'r'); } ans = max(rtob, btor); btor = 0, rtob = (s[0] == 'r'); lastch = 'b'; for (int i = 1; i < n; i++) { if (s[i] == lastch) lastch == 'r' ? rtob++ : btor++; lastch = (lastch == 'r' ? 'b' : 'r'); } ans = min(ans, max(btor, rtob)); cout << ans; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n; int main() { string s; cin >> n; cin >> s; string r, b; r = b = ""; for (int i = 0; i < n; i++) { if (i % 2 == 0) { r += 'r'; b += 'b'; } else { r += 'b'; b += 'r'; } } int rr = 0, bb = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r') rr++; else bb++; } string rs = s; int res = 0, resr = 0, resb = 0; int rrs = (n + 1) / 2; if (rrs > rr) { for (int i = 0; i < n; i++) { if (rs[i] == 'b' && r[i] == 'r') { rr++; rs[i] = 'r'; resr++; } if (rr == rrs) break; } } else { for (int i = 0; i < n; i++) { if (rs[i] == 'r' && r[i] == 'b') { rr--; rs[i] = 'b'; resr++; } if (rr == rrs) break; } } int tr = 0; for (int i = 0; i < n; i++) { if (rs[i] != r[i]) tr++; } resr += tr / 2; int bbs = (n + 1) / 2; string bs = s; if (bbs > bb) { for (int i = 0; i < n; i++) { if (bs[i] == 'r' && b[i] == 'b') { bb++; bs[i] = 'b'; resb++; } if (bb == bbs) break; } } else { for (int i = 0; i < n; i++) { if (bs[i] == 'b' && b[i] == 'r') { bb--; bs[i] = 'r'; resb++; } if (bb == bbs) break; } } int tb = 0; for (int i = 0; i < n; i++) { if (bs[i] != b[i]) tb++; } resb += tb / 2; res = min(resb, resr); cout << res << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; } int main(int argc, char const *argv[]) { int n; string str; cin >> n; cin >> str; string cmp; char ch[] = {'r', 'b'}; for (int(i) = 0; (i) < (int)(n); ++(i)) cmp.push_back(ch[i & 1]); int res = 0; stack<char> stk; for (int(i) = 0; (i) < (int)(n); ++(i)) { if (str[i] != cmp[i]) { if (stk.empty()) { stk.push(str[i]); } else { if (stk.top() != str[i]) stk.pop(), res++; else stk.push(str[i]); } } } res += stk.size(); while (stk.size()) stk.pop(); cmp.clear(); for (int(i) = 0; (i) < (int)(n); ++(i)) cmp.push_back(ch[(i + 1) & 1]); int res2 = 0; for (int(i) = 0; (i) < (int)(n); ++(i)) { if (str[i] != cmp[i]) { if (stk.empty()) { stk.push(str[i]); } else { if (stk.top() != str[i]) stk.pop(), res2++; else stk.push(str[i]); } } } res2 += stk.size(); cout << min(res, res2); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { string x; map<char, int> visit1, visit2; int a = 0, b = 0, n, c, ans = 0; scanf("%d", &n); cin >> x; for (int i = 0; i < x.length(); i++) { if (i % 2 == 0) { if (x[i] == 'b') { a++; visit1[x[i]]++; } else { b++; visit2[x[i]]++; } } else { if (x[i] == 'r') { a++; visit1[x[i]]++; } else { b++; visit2[x[i]]++; } } } if (!min(a, b)) printf("0"); else { if (a <= b) { ans += min(visit1['r'], visit1['b']); ans += abs(visit1['r'] - visit1['b']); } else { ans += min(visit2['r'], visit2['b']); ans += abs(visit2['r'] - visit2['b']); } printf("%d", ans); } }
8
CPP
#include <bits/stdc++.h> using namespace std; char str[100005]; int len; int main() { scanf("%d", &len); scanf("%s", str); int ans = 0; int wr = 0, wb = 0; for (int i = 0; i < len; i++) { if (i & 1) if (str[i] == 'r') wr++; ; if ((i & 1) == 0) if (str[i] == 'b') wb++; } ans = max(wr, wb); wr = 0; wb = 0; for (int i = 0; i < len; i++) { if (i & 1) if (str[i] == 'b') wb++; if ((i & 1) == 0) if (str[i] == 'r') wr++; } ans = min(ans, max(wr, wb)); printf("%d\n", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, cnt[2][2]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { char x; cin >> x; cnt[0][0] += (x == 'r' && i % 2 == 0); cnt[0][1] += (x == 'b' && i & 1); cnt[1][0] += (x == 'r' && i & 1); cnt[1][1] += (x == 'b' && i % 2 == 0); } int ans = max(cnt[0][1], cnt[0][0]); ans = min(ans, max(cnt[1][1], cnt[1][0])); cout << ans << "\n"; return 0; }
8
CPP
from collections import * n, s, ans, mem = int(input()), list(input()), 0, defaultdict(int) for i in range(0, n, 2): if s[i] == 'r': if i == n - 1 or i < n - 1 and s[i + 1] == 'b': mem['rb'] += 1 else: mem['rr'] += 1 else: if i == n - 1 or i < n - 1 and s[i + 1] == 'r': mem['br'] += 1 else: mem['bb'] += 1 ans += max(mem['bb'], mem['rr']) + min(mem['rb'], mem['br']) if n & 1: if mem['bb'] < mem['rr']: if s[-1] == 'b' and mem['rb'] > mem['br']: ans -= 1 elif mem['bb'] > mem['rr']: if s[-1] == 'r' and mem['rb'] < mem['br']: ans -= 1 print(ans)
8
PYTHON3
a = int(input()) mas = input() count1 = 0 count2 = 0 count3 = 0 count4 = 0 for i in range(0, (a), 2): #rbrb if (mas[i] == 'b'): count1 += 1 for i in range(1, (a), 2): if (mas[i] == 'r'): count2 += 1 for i in range(1, (a), 2): #brbr if (mas[i] == 'b'): count3 += 1 for i in range(0, (a), 2): if (mas[i] == 'r'): count4 += 1 # print(count1) # print(count2) # print(count3) # print(count4) print(min(max(count2,count1),max(count4,count3)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, i, j, sum1, sum2, l, flag, maxn, c, q, p; int b[2]; char d; char a[100010]; int main() { while (cin >> n) { memset(b, 0, sizeof(b)); sum1 = sum2 = 0; memset(a, q, sizeof(a)); for (i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 'b') { sum1++; } else if (a[i] == 'r') { sum2++; } } flag = n % 2; l = n / 2 + flag; if (sum1 >= sum2) { maxn = sum1; d = 'b'; } else { maxn = sum2; d = 'r'; } c = maxn - l; q = p = 0; for (j = 0; j < l; j++) { if (a[j * 2 + 1] != d) { q++; } if (a[j * 2 + 2] != d) { p++; } } if (q > p) { q = p; } cout << q + c << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a; int r = 0, b = 0, r1 = 0, b1 = 0, n; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (i % 2 == 0) { if (a == 'b') b++; if (a == 'r') r1++; } else { if (a == 'r') r++; if (a == 'b') b1++; } } r = max(r, b); r1 = max(r1, b1); cout << min(r, r1); return 0; }
8
CPP
def main(): n = int(input()) s = input() rodd = 0 bodd = 0 reven = 0 beven = 0 for (i,c) in enumerate(s): if i % 2 == 1: if c == 'r': rodd += 1 else: bodd += 1 else: if c == 'r': reven += 1 else: beven += 1 print(min(abs(bodd-reven) + min(bodd, reven), abs(rodd-beven) + min(rodd, beven))) main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int b = 0, r = 0, n, k, m, ans = 0; cin >> n; char a[n], rr[n], bb[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i] == 'b') ++b; else ++r; if (i % 2 == 0) { rr[i] = 'r'; bb[i] = 'b'; } else { rr[i] = 'b'; bb[i] = 'r'; } } int nr = 0, nb = 0, nr1 = 0, nb1 = 0; for (int i = 0; i < n; ++i) { if (a[i] != rr[i]) { if (a[i] == 'r') ++nr; else ++nb; } if (a[i] != bb[i]) { if (a[i] == 'r') ++nr1; else ++nb1; } } int ans1; ans = max(nr, nb); ans1 = max(nr1, nb1); cout << min(ans, ans1); }
8
CPP
n = int(input()) s1 = input() s2a = '' da = 0 ar = 0 ab = 0 s2b = '' db = 0 br = 0 bb = 0 pairs = int(n // 2) carry = n % 2 for _ in range(pairs): s2a += 'rb' s2b += 'br' if carry: s2a += 'r' s2b += 'b' if s1 == s2a or s1 == s2b: print(0) exit() for i in range(n): if not s1[i] == s2a[i]: da += 1 if s1[i] == 'r': ar += 1 else: ab += 1 if not s1[i] == s2b[i]: db += 1 if s1[i] == 'r': br += 1 else: bb += 1 da -= min(ar, ab) db -= min(br, bb) print(min(da, db))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100005], aa[100005]; int cmp1[100005], cmp2[100005]; int main() { int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'r') a[i] = aa[i] = 0; else a[i] = aa[i] = 1; } for (int i = 0; i < n; i++) { if (i % 2 == 0) cmp1[i] = 1; else cmp1[i] = 0; if (i % 2 == 0) cmp2[i] = 0; else cmp2[i] = 1; } int ans = 100000; int cnt1 = 0, cnt2 = 0; ; for (int i = 0; i < n; i++) { if (a[i] != cmp1[i]) { if (a[i] == 0) cnt1++; else cnt2++; } } int k1 = (n + 1) / 2; int k2 = n / 2; if (cnt1 < cnt2) { int t = cnt1; cnt1 = cnt2; cnt2 = t; } if (cnt1 == cnt2) { ans = min(cnt1, ans); } else { ans = min(cnt1, ans); } cnt1 = cnt2 = 0; for (int i = 0; i < n; i++) { if (aa[i] != cmp2[i]) { if (aa[i] == 0) cnt1++; else cnt2++; } } k1 = (n + 1) / 2; k2 = n / 2; if (cnt1 < cnt2) { int tt = cnt1; cnt1 = cnt2; cnt2 = tt; } if (cnt1 == cnt2) { ans = min(cnt1, ans); } else { ans = min(cnt1, ans); } cout << ans << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); char s[200000], s1[200000], s2[200000]; int n, cr = 0, cb = 0, k1, k2, cr1 = 0, cb1 = 0; cin >> n; for (int i = 0; i < n; i++) cin >> s[i]; if (s[0] == 'r') { for (int i = 0; i < n; i++) { if (i % 2 == 0) { s1[i] = 'r'; s2[i] = 'b'; } else { s1[i] = 'b'; s2[i] = 'r'; } } } else { for (int i = 0; i < n; i++) { if (i % 2 == 0) { s1[i] = 'b'; s2[i] = 'r'; } else { s1[i] = 'r'; s2[i] = 'b'; } } } for (int i = 0; i < n; i++) { if (s[i] == 'r' && s1[i] == 'b') cr++; if (s[i] == 'r' && s2[i] == 'b') cr1++; if (s[i] == 'b' && s1[i] == 'r') cb++; if (s[i] == 'b' && s2[i] == 'r') cb1++; } if (cr >= cb) k1 = cb; else { k1 = cr; } if (cr1 >= cb1) k2 = cb1; else { k2 = cr1; } cout << min((k1 + abs(cr - cb)), k2 + abs(cr1 - cb1)); }
8
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; constexpr int INF = numeric_limits<int>::max() - 1; constexpr ll LINF = numeric_limits<ll>::max() - 1; constexpr ld EPS = numeric_limits<ld>::epsilon(); int n; string s; void input() { cin >> n >> s; } int f1() { int badr = 0, badb = 0; for (int i = 0; i < n; i += 2) badr += s[i] == 'r'; for (int i = 1; i < n; i += 2) badb += s[i] == 'b'; return max(badr, badb); } int f2() { int badr = 0, badb = 0; for (int i = 1; i < n; i += 2) badr += s[i] == 'r'; for (int i = 0; i < n; i += 2) badb += s[i] == 'b'; return max(badr, badb); } void solve() { cout << min(f1(), f2()) << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(16); cout << fixed; int _times = 1; while (_times--) { input(); solve(); } }
8
CPP
import os import sys from io import BytesIO, IOBase import heapq as h from bisect import bisect_left, bisect_right import time from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os 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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from collections import defaultdict as dd, deque as dq, Counter as dc import math, string start_time = time.time() def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def getMat(n): return [getInts() for _ in range(n)] def get_ints():return map(int, sys.stdin.readline().split()) n=int(input()) s=input() # s1=str();ans1=0 for i in range(n): if i%2==0:s1+='r' else:s1+='b' ans1-=abs(s.count('r')-s1.count('r')) ans11=abs(ans1) for i in range(n): if s[i]!=s1[i]:ans1+=1 ans11+=ans1//2 # s2=str();ans2=0 for i in range(n): if i%2==0:s2+='b' else:s2+='r' ans2=0 ans2-=abs(s.count('r')-s2.count('r')) ans22=abs(ans2) for i in range(n): if s[i]!=s2[i]:ans2+=1 ans22+=ans2//2 print(min(ans11,ans22))
8
PYTHON3
from sys import * n=int(stdin.readline()) s=stdin.readline() m1,m2,ans=0,0,0 for i in range(len(s)): if i%2 and s[i]=="r": m1+=1 if not (i%2) and s[i]=="b": m2+=1 ans=max(m1,m2) m1,m2=0,0 for i in range(len(s)): if i%2 and s[i]=="b": m1+=1 if not (i%2) and s[i]=="r": m2+=1 print(min(ans,max(m1,m2)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char s[1000000]; int n, i; scanf("%d", &n); scanf("%s", s); char s1[1000000], s2[1000000]; int b, r; for (i = 0; i < n; i++) { if (i % 2 == 0) { s1[i] = 'b'; } else { s1[i] = 'r'; } } for (i = 0; i < n; i++) { if (i % 2 == 0) { s2[i] = 'r'; } else { s2[i] = 'b'; } } int ans1 = 0, ans2 = 0; b = r = 0; for (i = 0; i < n; i++) { if (s1[i] != s[i]) { if (s1[i] == 'b') b++; else r++; } } ans1 = max(b, r); b = r = 0; for (i = 0; i < n; i++) { if (s[i] != s2[i]) { if (s2[i] == 'r') r++; else b++; } } ans2 = max(r, b); int f = min(ans1, ans2); printf("%d", f); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long solve(string s, string res) { long long n = (long long)(s.size()); long long a[n]; for (long long i = 0; i < n; i++) { if (s[i] > res[i]) { a[i] = 1; } else if (s[i] == res[i]) { a[i] = 0; } else { a[i] = -1; } } long long cnt1 = count(a, a + n, 1), cnt2 = count(a, a + n, -1); return min(cnt1, cnt2) + abs(cnt1 - cnt2); } int main() { ios::sync_with_stdio(0); long long n; cin >> n; string s; cin >> s; string tmp1 = "", tmp2 = ""; for (long long i = 0; i < n; i++) { if (i % 2) { tmp1 += "r", tmp2 += "b"; } else { tmp1 += "b", tmp2 += "r"; } } cout << min(solve(s, tmp1), solve(s, tmp2)); }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 0, a, b, c, i, j, k, n, m; cin >> n; string str; cin >> str; long long int ans = 0; long long int mis1 = 0, mis2 = 0, mis3 = 0, mis4 = 0; for (i = 0; i < str.length(); i++) { if (i % 2 == 0 && str[i] == 'r') mis1++; if (i % 2 == 0 && str[i] == 'b') mis3++; if (i % 2 == 1 && str[i] == 'b') mis2++; if (i % 2 == 1 && str[i] == 'r') mis4++; } ans = min(mis1, mis2) + max(mis1, mis2) - min(mis1, mis2); ans = min(ans, min(mis3, mis4) + max(mis3, mis4) - min(mis3, mis4)); cout << ans << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int crc[2][2]; int main() { int n; string s; bitset<100005> cr; cin >> n; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'r') cr[i] = 1; else cr[i] = 0; } for (int color = 0; color <= 1; ++color) for (int offset = 0; offset <= 1; ++offset) { int nr = 0; for (int i = offset; i < n; i += 2) if (cr[i] == color) ++nr; crc[color][offset] = nr; } int mintotal = n; for (int color = 0; color <= 1; ++color) for (int offset = 0; offset <= 1; ++offset) { int total = 0; if (crc[color][offset] < (n - offset + 1) / 2) { total += (n - offset + 1) / 2 - crc[color][offset]; if (crc[1 - color][1 - offset] < (n + offset) / 2 - total) total += (n + offset) / 2 - total - crc[1 - color][1 - offset]; } else { total = (n + offset) / 2 - crc[1 - color][1 - offset]; } mintotal = min(total, mintotal); } cout << mintotal; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; map<string, int> dist; map<string, bool> visited; queue<string> q; int main() { int n; string s, des1 = "", des2 = ""; cin >> n >> s; for (int i = 0; i < n; i++) { if (i % 2 == 0) { des1 += "r"; des2 += "b"; } else { des1 += "b"; des2 += "r"; } } int wrongR1 = 0, wrongR2 = 0, wrongB1 = 0, wrongB2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') { wrongR2++; } else { wrongB1++; } } else { if (s[i] == 'r') { wrongR1++; } else { wrongB2++; } } } cout << min(min(wrongR1, wrongB1) + abs(wrongR1 - wrongB1), min(wrongR2, wrongB2) + abs(wrongR2 - wrongB2)); }
8
CPP
def solve(s, p): c = [0, 0] m = 0 for i in range(len(s)): if s[i] != p[i]: if s[i] == 'b': if c[1] > 0: c[1] -= 1 else: c[0] += 1 m += 1 else: if c[0] > 0: c[0] -= 1 else: c[1] += 1 m += 1 return m n = int(input()) s = input() p1 = 'rb'*(n//2+1) p2 = 'br'*(n//2+1) print(min(solve(s,p1),solve(s,p2)))
8
PYTHON3
def cal( s , c ): w = [0 , 0] for i in range(len(s)): if s[i] != c: w[ i % 2 ] += 1 c = 'r' if c == 'b' else 'b' return max(w[0], w[1]) n = int( input() ) s = input() print( min( cal( s , 'r' ) , cal( s , 'b' ) ) )
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void printVector(vector<int> v) { for_each(v.begin(), v.end(), [](int i) { std::cout << i << " "; }); cout << '\n'; } struct myclass { void operator()(int i) { std::cout << ' ' << i; } } myobject; int main() { ios_base::sync_with_stdio(false); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int cSize; cin >> cSize; string cockroach; cin >> cockroach; bool flag; int red = 0, black = 0; int firstAnswer; int secondAnswer; for (int i = 0; i < cSize; i++) { if (i % 2 == 0) { if (cockroach[i] != 'r') black++; } else if (cockroach[i] != 'b') red++; } int swaps = min(red, black); int recolor = max(red, black) - swaps; firstAnswer = recolor + swaps; red = 0, black = 0; for (int i = 0; i < cSize; i++) { if (i % 2 == 0) { if (cockroach[i] != 'b') red++; } else if (cockroach[i] != 'r') black++; } swaps = min(red, black); recolor = max(red, black) - swaps; secondAnswer = swaps + recolor; cout << (min(firstAnswer, secondAnswer)); return 0; }
8
CPP
'''input 3 rbr ''' n = int(input()) c = input() e1, o1, e2, o2 = 0, 0, 0, 0 for x in range(0, n, 2): if c[x] != "r": e1 += 1 for y in range(1, n, 2): if c[y] != "b": o1 += 1 for x in range(0, n, 2): if c[x] != "b": e2 += 1 for y in range(1, n, 2): if c[y] != "r": o2 += 1 print(min(max(e1, o1), max(e2, o2)))
8
PYTHON3
n = int(input()) w = input().strip() red_1 = 0 red_2 = 0 bck_1 = 0 bck_2 = 0 for i in range(len(w)): if (w[i] == 'r'): if i % 2: red_2 += 1 else: red_1 += 1 if (w[i] == 'b'): if i % 2: bck_2 += 1 else: bck_1 += 1 print(min(max(red_2, bck_1), max(bck_2, red_1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int r = 0, b = 0; for (int i = 0; i < n; i++) { if (i & 1) { if (s[i] == 'b') b++; } else { if (s[i] == 'r') r++; } } int ans = max(r, b); r = 0, b = 0; for (int i = 0; i < n; i++) { if (i & 1) { if (s[i] == 'r') r++; } else { if (s[i] == 'b') b++; } } ans = min(ans, max(r, b)); cout << ans << endl; }
8
CPP
n = int(input()) s = list(input()) order = "rb" data0 = sum(map(lambda x, y: order.index(x) == 0 and y % 2 == 0 , s, range(n))) data1 = sum(map(lambda x, y: order.index(x) == 1 and y % 2 == 1 , s, range(n))) count1 = max(data0, data1) order = "br" data0 = sum(map(lambda x, y: order.index(x) == 0 and y % 2 == 0 , s, range(n))) data1 = sum(map(lambda x, y: order.index(x) == 1 and y % 2 == 1 , s, range(n))) count2 = max(data0, data1) print(min(count1, count2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; vector<int> zog, frd; for (int i = 0; i < (int)(n); ++i) { if (i % 2 == 0 && s[i] != 'r') zog.push_back(s[i]); else if (i % 2 != 0 && s[i] != 'b') frd.push_back(s[i]); } long long x1 = 0, x2 = 0; x1 = min(zog.size(), frd.size()) + max(zog.size(), frd.size()) - min(zog.size(), frd.size()); zog.clear(); frd.clear(); for (int i = 0; i < n; i++) { if (i % 2 == 0 && s[i] != 'b') zog.push_back(s[i]); else if (i % 2 != 0 && s[i] != 'r') frd.push_back(s[i]); } x2 = min(zog.size(), frd.size()) + max(zog.size(), frd.size()) - min(zog.size(), frd.size()); cout << min(x1, x2); return 0; }
8
CPP
import sys n = int(input()) if n == 1: print(0) sys.exit(0) s = list(input()) s2 = [i for i in s] c = 0 r = 0 b = 0 for i in range(n): if i%2 == 1 and s[i] != 'r': b += 1 if i%2 == 0 and s[i] != 'b': r += 1 c1 = max(r, b) r2 = 0 b2 = 0 for i in range(n): if i%2 == 0 and s[i] != 'r': b2 += 1 if i%2 == 1 and s[i] != 'b': r2 += 1 c2 = max(r2, b2) print(min(c1, c2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MOD = 1e9 + 7; int N; char S[100005], SS[100005]; bool ok1[100005] = {0}, ok2[100005] = {0}; vector<int> r, b, rr, bb; int main() { scanf("%d", &N); scanf("%s", &S); for (int i = N - 1; i >= 0; i--) { if (S[i] == 'r') r.push_back(i), rr.push_back(i); if (S[i] == 'b') b.push_back(i), bb.push_back(i); SS[i] = S[i]; } int ans1 = 0, ans2 = 0; for (int i = 0; i < N; i++) { char c = (i % 2 == 0) ? 'r' : 'b'; if (S[i] == c) ok1[i] = 1; } for (int i = 0; i < N; i++) { char c = (i % 2 == 0) ? 'r' : 'b'; if (((int)(r).size()) > 0 && i >= r.back()) r.pop_back(); if (((int)(b).size()) > 0 && i >= b.back()) b.pop_back(); if (S[i] != c) { if (S[i] == 'r') { bool ok = 0; while (((int)(b).size()) > 0) { if (ok1[b.back()]) b.pop_back(); else { swap(S[i], S[b.back()]), b.pop_back(), ans1++, ok = 1; break; } } if (!ok) ans1++; } else { bool ok = 0; while (((int)(r).size()) > 0) { if (ok1[r.back()]) r.pop_back(); else { swap(S[i], S[r.back()]), r.pop_back(), ans1++, ok = 1; break; } } if (!ok) ans1++; } } } for (int i = 0; i < N; i++) { char c = (i % 2 == 0) ? 'b' : 'r'; if (SS[i] == c) ok2[i] = 1; } for (int i = 0; i < N; i++) { char c = (i % 2 == 0) ? 'b' : 'r'; if (((int)(rr).size()) > 0 && i >= rr.back()) rr.pop_back(); if (((int)(bb).size()) > 0 && i >= bb.back()) bb.pop_back(); if (SS[i] != c) { if (SS[i] == 'r') { bool ok = 0; while (((int)(bb).size()) > 0) { if (ok2[bb.back()]) bb.pop_back(); else { swap(SS[i], SS[bb.back()]), bb.pop_back(), ans2++, ok = 1; break; } } if (!ok) ans2++; } else { bool ok = 0; while (((int)(rr).size()) > 0) { if (ok2[rr.back()]) rr.pop_back(); else { swap(SS[i], SS[rr.back()]), rr.pop_back(), ans2++, ok = 1; break; } } if (!ok) ans2++; } } } printf("%d\n", min(ans1, ans2)); return 0; }
8
CPP
a = input() def a0o1(character): if character == "r": return 0 if character == "b": return 1 def isPar(par): return par == 0 b = list(map(a0o1, input())) par = 0 #a1 = 0101010101010101 #a2 = 1010101010101010 a1_miss_0 = 0 a1_miss_1 = 0 a2_miss_0 = 0 a2_miss_1 = 0 for x in b: a1_miss_0 += (x== 0 and isPar(par)) a1_miss_1 += (x== 1 and not isPar(par)) a2_miss_0 += (x== 0 and not isPar(par)) a2_miss_1 += (x== 1 and isPar(par)) par = (par+1)%2 a1_changes = abs(a1_miss_0 - a1_miss_1) a1_swaps = max(a1_miss_0, a1_miss_1) - a1_changes a_1 = a1_changes + a1_swaps a2_changes = abs(a2_miss_0 - a2_miss_1) a2_swaps = max(a2_miss_0, a2_miss_1) - a2_changes a_2 = a2_changes + a2_swaps print(min(a_1,a_2))
8
PYTHON3
cnt_b1, cnt_r1, cnt_b2, cnt_r2 = 0, 0, 0, 0 num = input() s = input() for i, c in enumerate(s): if i % 2: if c == 'r': cnt_r1 += 1 else: cnt_b1 += 1 else: if c == 'r': cnt_r2 += 1 else: cnt_b2 += 1 print(min(max(cnt_b2, cnt_r1), max(cnt_r2, cnt_b1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; char a[110000]; int main() { int n; scanf("%d", &n); scanf("%s", a); int m1 = 0, m2 = 0; int k1 = 0, k2 = 0; for (int i = 0; i < n; i++) { if (a[i] == 'b') { if (i % 2 == 0) m1++; else m2++; } else if (a[i] == 'r') { if (i % 2 == 0) k1++; else k2++; } } printf("%d\n", min(max(m1, k2), max(m2, k1))); }
8
CPP
#include <bits/stdc++.h> using namespace std; char s[100500]; int main() { int n; cin >> n; scanf("%s", s); int r1 = 0, r2 = 0; int b1 = 0, b2 = 0; for (int i = 0; i < n; i++) { if (i % 2) { if (s[i] == 'b') b1++; else r1++; } else { if (s[i] == 'b') b2++; else r2++; } } cout << min(n - r1 - b2 - min(r2, b1), n - r2 - b1 - min(r1, b2)) << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long n, r1 = 0, r2 = 0, b1 = 0, b2 = 0, i, k; cin >> n; string a; cin >> a; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (a[i] == 'r') r1++; else b2++; } else { if (a[i] == 'b') b1++; else r2++; } } k = min(max(r1, b1), max(r2, b2)); cout << k; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a = 0, b = 0, res = 0; string s; cin >> n >> s; for (int i = 0; i < n; i++) i& 1 ? a += s[i] == 'r' : b += s[i] == 'b'; res = max(a, b); a = 0; b = 0; for (int i = 0; i < n; i++) i& 1 ? a += s[i] == 'b' : b += s[i] == 'r'; cout << min(res, max(a, b)) << endl; return 0; }
8
CPP
#Ashish_Sagar n=int(input()) l=list(input()) final1=[] final2=[] for i in range(len(l)): if i%2==0: final1.append('r') final2.append('b') else: final1.append('b') final2.append('r') #for first sample nr=0 nb=0 for i in range(n): if final1[i]=='r' and final1[i]!=l[i]: nr+=1 elif final1[i]=='b' and final1[i]!=l[i]: nb+=1 maxx1=max(nr,nb) #for second sample nr=0 nb=0 for i in range(n): if final2[i]=='r' and final2[i]!=l[i]: nr+=1 elif final2[i]=='b' and final2[i]!=l[i]: nb+=1 maxx2=max(nr,nb) print(min(maxx1,maxx2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int count; cin >> count; string arr; cin >> arr; char first_color = arr[0] == 'r' ? 'r' : 'b'; int FCS = 0; int SCS = 0; for (int i = 0; i < count; i++) { if (i % 2 == 0 && arr[i] != first_color) { ++SCS; } else if (i % 2 == 1 && arr[i] == first_color) { ++FCS; } } int first_state = 0; first_state = FCS > SCS ? FCS : SCS; char first_color_2 = arr[0] == 'r' ? 'b' : 'r'; int FCS_2 = 0; int SCS_2 = 0; for (int i = 0; i < count; i++) { if (i % 2 == 0 && arr[i] != first_color_2) { ++SCS_2; } else if (i % 2 == 1 && arr[i] == first_color_2) { ++FCS_2; } } int second_state = 0; second_state = FCS_2 > SCS_2 ? FCS_2 : SCS_2; cout << (first_state < second_state ? first_state : second_state); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int l; string s; cin >> l >> s; int i; int r = 0, b = 0; for (i = 0; i < l; i++) { if (i & 1 && s[i] == 'r') r++; if ((!(i & 1)) && s[i] == 'b') b++; } int cnt = max(r, b); r = b = 0; for (i = 0; i < l; i++) { if ((!(i & 1)) && s[i] == 'r') r++; if (i & 1 && s[i] == 'b') b++; } int cnt1 = max(r, b); cout << min(cnt, cnt1) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a = 0, b = 0, c = 0, d = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') d++; else { a++; } } else { if (s[i] == 'r') b++; else c++; } } cout << min(min(a, b) + a + b - 2 * min(a, b), min(c, d) + c + d - 2 * min(c, d)) << endl; }
8
CPP
# from sys import stdin # stdin=open('input.txt') # def input(): # return stdin.readline()[:-1] # from sys import stdout # stdout=open('output.txt',mode='w+') # def print1(x, end='\n'): # stdout.write(str(x) +end) # a, b = map(int, input().split()) # l = list(map(int, input().split())) # CODE BEGINS HERE................. # import copy # import math # from sys import setrecursionlimit # setrecursionlimit(10**7) # from itertools import combinations T = 1 # T = int(input()) for t in range(T): # print('Case #' + str(t + 1) + ': ', end = '') n = int(input()) s = input() wrong_red = 0 wrong_black = 0 for i in range(n): if ((i % 2 == 0) and (s[i] == 'r')): wrong_red += 1 if ((i % 2 == 1) and (s[i] == 'b')): wrong_black += 1 if wrong_black < wrong_red: total_moves = wrong_black + (wrong_red - wrong_black) else: total_moves = wrong_red + (wrong_black - wrong_red) wrong_red = 0 wrong_black = 0 for i in range(n): if ((i % 2 == 1) and (s[i] == 'r')): wrong_red += 1 if ((i % 2 == 0) and (s[i] == 'b')): wrong_black += 1 if wrong_black < wrong_red: total_moves = min(total_moves, wrong_black + (wrong_red - wrong_black)) else: total_moves = min(total_moves, wrong_red + (wrong_black - wrong_red)) print(total_moves) # # CODE ENDS HERE.................... # stdout.close()
8
PYTHON3
a=int(input()) b=input() s1=0; s2=0; ss1=0; ss2=0 for i in range(0,a): if b[i]=='r' and i%2==1: s1+=1 elif b[i]=='r': ss1+=1 elif b[i]=='b' and i%2==0: s2+=1 else: ss2+=1 s=min(max(s1,s2),max(ss1,ss2)) print(s)
8
PYTHON3
import sys n = int(int(input())) s = input() if n % 2 == 0: s2 = 'rb'*(int(n/2)) s3 = 'br'*(int(n/2)) else: s2 = 'rb'*(int(n/2)) + 'r' s3 = 'br'*(int(n/2)) + 'b' rcnt = bcnt = rcnt2 = bcnt2 = 0 for i in range(n): if s[i] != s2[i] and s2[i] == 'r': rcnt += 1 elif s[i] != s2[i] and s2[i] == 'b': bcnt += 1 if s[i] != s3[i] and s3[i] == 'r': rcnt2 += 1 elif s[i] != s3[i] and s3[i] == 'b': bcnt2 += 1 cnt = max(rcnt,bcnt) cnt2 = max(rcnt2,bcnt2) print(min(cnt,cnt2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long l, u, v, w, x, y, z, a, b, c, d, e, f, t = 1, tc; long long flg, sz, cnt, gt, ans, mx, mn; long long m, k, n, i, j; long long low, hi, md, inp[200007], sm, ff; scanf("%lld", &n); string st, cp, age; cin >> st; cp += 'r'; a = b = 0; for (int i = 0; i < n; i++) { if (st[i] == cp[i]) ; else if (st[i] == 'r') a++; else b++; if (i % 2 == 0) cp += 'b'; else cp += 'r'; } x = max(a, b); cp = "b"; a = b = 0; for (int i = 0; i < n; i++) { if (st[i] == cp[i]) ; else if (st[i] == 'r') a++; else b++; if (i % 2) cp += 'b'; else cp += 'r'; } y = max(a, b); cout << min(x, y) << endl; return 0; }
8
CPP
def B(): n = int(input()) lst = list(input()) for i in range(len(lst)): if lst[i] == "r": lst[i] = 1 else: lst[i] = -1 rCo = 0 bCo = 0 co = 0 cur = lst[0] if n == 1: print("0") return 0 for i in range(len(lst)): if lst[i] == cur: cur *= -1 continue else: if lst[i] == 1: if bCo != 0: bCo -= 1 else: rCo += 1 co += 1 else: if rCo != 0: rCo -= 1 else: bCo += 1 co += 1 cur *= -1 first = co rCo = 0 bCo = 0 co = 0 cur = lst[0] * -1 for i in range(len(lst)): if lst[i] == cur: cur *= -1 continue else: if lst[i] == 1: if bCo != 0: bCo -= 1 else: rCo += 1 co += 1 else: if rCo != 0: rCo -= 1 else: bCo += 1 co += 1 cur *= -1 print(min(co, first)) return 0 if __name__ == "__main__": B()
8
PYTHON3