solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; template <class T> inline void rread(T& num) { num = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar(); num *= f; } const long long inf = 1e16; const int maxn = 1e5 + 10, mod = 1e9 + 7; const long double PI = acos(-1.0); long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b, a % b, d, y, x); y -= x * (a / b); } } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, m; int dp[maxn][3]; char s[maxn]; int main() { while (cin >> n) { scanf("%s", s); int f1 = 0, f2 = 0, ans1 = 0, ans2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && s[i] == 'r') f1++; if (i % 2 == 1 && s[i] == 'b') f2++; } ans2 = 0; ans1 = max(f1, f2); f1 = 0; f2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && s[i] == 'b') f1++; if (i % 2 == 1 && s[i] == 'r') f2++; } ans2 = max(f1, f2); cout << min(ans1, ans2) << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; const long long INF = 1e18L; long long ar[N]; long long dp[2], dp2[2]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long T = 1; while (T--) { long long n; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'b') { dp[i & 1]++; } else { dp2[i & 1]++; } } cout << min(max(dp[0], dp2[1]), max(dp[1], dp2[0])); } cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n"; return 0; }
8
CPP
n = int(input()) s = input() a_r, a_b = 0, 0 b_r, b_b = 0, 0 for i in range(n): if i % 2 == 0: if s[i] == 'r': a_r += 1 else: b_b += 1 else: if s[i] == 'b': a_b += 1 else: b_r += 1 print(min(max(a_r, a_b), max(b_r, b_b)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n; cin >> s; int rtb = 0, btr = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'b') btr++; } else { if (s[i] == 'r') rtb++; } } int rstep = max(rtb, btr); rtb = 0; btr = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (s[i] == 'b') btr++; } else { if (s[i] == 'r') rtb++; } } int bstep = max(rtb, btr); cout << min(rstep, bstep); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-2; const int N = 1e5 + 50; const double PI = acos(-1.); const double E = 2.71828182845904523536; const int MOD = 1e9 + 7; char s[N]; int main() { int n; cin >> n >> s; int a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r' && i % 2 == 0) a++; if (s[i] == 'b' && i % 2 == 1) b++; } int ans = min(a, b) + max(a, b) - min(a, b); a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r' && i % 2 == 1) a++; if (s[i] == 'b' && i % 2 == 0) b++; } ans = min(min(a, b) + max(a, b) - min(a, b), ans); cout << ans << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int cnt_b1 = 0, cnt_b2 = 0, cnt_r1 = 0, cnt_r2 = 0; for (int i = 0; i < n; i++) { if ((i % 2 == 0) && (s[i] == 'r')) { cnt_r1++; } else if ((i % 2 == 1) && (s[i] == 'b')) cnt_r2++; } int ans = abs(cnt_r1 - cnt_r2) + min(cnt_r1, cnt_r2); for (int i = 0; i < n; i++) { if ((i % 2 == 0) && (s[i] == 'b')) { cnt_b1++; } else if ((i % 2 == 1) && (s[i] == 'r')) cnt_b2++; } int ans1 = abs(cnt_b1 - cnt_b2) + min(cnt_b1, cnt_b2); ans = min(ans, ans1); cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> char s[1000100]; int n; int check(int op) { int p = 0, q = 0; for (int i = 0; i < n; i++) { int num = (s[i] == 'r') ? 0 : 1; if (num != op) { if (num) p++; else q++; } op ^= 1; } return abs(p - q) + std::min(p, q); } int main() { scanf("%d", &n); scanf("%s", s); std::cout << std::min(check(0), check(1)) << std::endl; return 0; }
8
CPP
__author__ = 'Think' n=int(input()) initial=input() roaches={(0, "r"):0, (0, "b"):0, (1, "r"):0, (1, "b"):0} for i in range(n): roaches[(i%2, initial[i])]+=1 print(min(max(roaches[(0, "r")], roaches[1, "b"]), max(roaches[(1, "r")], roaches[0, "b"])))
8
PYTHON3
n, s = int(input()), input() a, b = s[::2].count('r'), s[1::2].count('r') print(min(max((n + 1) // 2 - a, b), max(a, n // 2 - b)))
8
PYTHON3
n=int(input()) s=input() r1=0 b1=0 r2=0 b2=0 for i in range(0,n): if s[i]=='r' and i % 2==0: r1+=1 if s[i]=='b' and i % 2==1: b1+=1 if s[i]=='r' and i % 2==1: r2+=1 if s[i]=='b' and i % 2==0: b2+=1 x1=max(r2,b2) x2=max(r1,b1) print(min(x1,x2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> T max(T a, T b, T c) { return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)); } template <class T> T min(T a, T b, T c) { return ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)); } const int MOD = (1e9) + 7; const int MAXN = (1e5) + 9; int T = 1, N = 1; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); while (T--) { int n; cin >> n; string str; cin >> str; int foo = 0, bar = 0; int a = 0, b = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && str[i] == 'r') { a++; } else if (i % 2 == 1 && str[i] == 'b') { b++; } foo = max(a, b); } a = b = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && str[i] == 'b') { a++; } else if (i % 2 == 1 && str[i] == 'r') { b++; } bar = max(a, b); } cout << min(foo, bar) << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n; char ss[maxn]; int main() { scanf("%d", &n); scanf("%s", &ss); int evr = 0, evb = 0, odr = 0, odb = 0, cnt; for (int i = 0; i < n; i++) { if (i & 1) { if (ss[i] == 'r') { odr++; } else { odb++; } } else { if (ss[i] == 'r') { evr++; } else { evb++; } } } cnt = min(max(evb, odr), max(evr, odb)); printf("%d\n", cnt); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[100005]; int main() { int n, r = 0, b = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> a[i]; if (i % 2 == 1) { if (a[i] == 'b') b++; } else { if (a[i] == 'r') r++; } } int ans = min(r, b) + max(r - b, b - r); r = 0, b = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) { if (a[i] == 'r') r++; } else { if (a[i] == 'b') b++; } } ans = min(ans, min(r, b) + max(r - b, b - r)); printf("%d\n", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int calc(char l, string t) { int r = 0, b = 0; int k = 0, n = t.length(); for (int i = 0; i < n; i++) { if (l == t[i]) { if (l == 'r') { l = 'b'; r++; } else { l = 'r'; b++; } k++; } else l = t[i]; } if (min(b, r) == 0) return k; int c = min(b, r); if (c * 2 >= k) return k / 2 + k % 2; k -= c * 2; return c + k; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string t; cin >> n >> t; cout << min(calc(t[0], t), calc(t[0] == 'r' ? 'b' : 'r', t)); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5, inf = 1e9; char str[maxn]; int n, s[maxn], f[maxn][2]; int main() { int i, cnt1 = 0, cnt2 = 0, wrong1 = 0, wrong2 = 0, wrong3 = 0, wrong4 = 0; scanf("%d", &n); int m = (n + 1) >> 1; scanf("%s", str + 1); for (i = 1; i <= n; i++) { if (str[i] == 'r') cnt1++; else cnt2++; if (str[i] == 'r' && (i & 1)) wrong3++; if (str[i] == 'r' && !(i & 1)) wrong1++; if (str[i] == 'b' && (i & 1)) wrong2++; if (str[i] == 'b' && !(i & 1)) wrong4++; } int ans1 = min(wrong1, wrong2) + abs(wrong1 - wrong2); int ans2 = min(wrong3, wrong4) + abs(wrong3 - wrong4); cout << min(ans1, ans2) << endl; return 0; }
8
CPP
#!/usr/bin/env python #-*-coding:utf-8 -*- input() S=4*[0] e=0 for c in input(): S[e|('r'!=c)<<1]+=1 e=not e print(min(abs(S[0]-S[3])+min(S[0],S[3]),abs(S[1]-S[2])+min(S[1],S[2])))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int getint() { int res = 0, fh = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') fh = -1, ch = getchar(); while (ch <= '9' && ch >= '0') res = res * 10 + ch - '0', ch = getchar(); return res * fh; } inline long long getll() { long long res = 0, fh = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') fh = -1, ch = getchar(); while (ch <= '9' && ch >= '0') res = res * 10 + ch - '0', ch = getchar(); return res * fh; } int getc() { char ch = getchar(); while (ch != 'b' && ch != 'r') ch = getchar(); if (ch == 'b') return 1; else return 0; } int n, d[10]; int a[100001]; int ans; int main() { n = getint(); for (int i = 1; i <= n; i++) a[i] = getc(); for (int i = 1; i <= n; i++) { int now = i & 1; if (a[i] != now) d[now]++; } ans = max(d[0], d[1]); int res = 0; d[0] = 0; d[1] = 0; for (int i = 1; i <= n; i++) { int now = !(i & 1); if (a[i] != now) d[now]++; } res = max(d[0], d[1]); ans = min(ans, res); printf("%d", ans); return 0; }
8
CPP
n = int(input()) string = input() # rbrbrb def solve(s, l1, l2): answer = 0 counter1 = 0 counter2 = 0 for (n, i) in enumerate(s): if n % 2 == 0: if i != l1: if counter1 > 0: counter1 -= 1 else: counter2 += 1 answer += 1 else: if i != l2: if counter2 > 0: counter2 -= 1 else: counter1 += 1 answer += 1 return answer print(min(solve(string, 'r', 'b'), solve(string, 'b', 'r')))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; void solve() { int n; string s; cin >> n >> s; int a = 0, b = 0, c = 0, d = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r') { if ((i + 1) % 2 == 0) a += 1; else b += 1; } else { if ((i + 1) % 2 == 0) c += 1; else d += 1; } } int ans = max(c, b); ans = min(ans, max(a, d)); cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; ; while (t--) solve(); }
8
CPP
import sys def read(): return sys.stdin.readline().strip() def printf(a, sep = ' ', end = '\n'): sys.stdout.write(sep.join(map(str, a)) + end) #printf([n]) def readf(): return [int(i) for i in read().split()] def dist(s, t, n): k = 0 r = 0 b = 0 for i in range(n): if(s[i] != t[i]): if(s[i] == "b"): b += 1 else: r += 1 k += 1 q = min(b, r) k -= q*2 return q + k def main(): n = int(read()) t = [str(i) for i in read()] a = ["r" if i%2 == 0 else "b" for i in range(n)] b = ["r" if i%2 == 1 else "b" for i in range(n)] printf([min(dist(a, t, n), dist(b, t, n))]) main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; vector<pair<long long, long long> > v; int main() { ios_base::sync_with_stdio(0); long long n; cin >> n; v.resize(n); string s; cin >> s; long long ans = 0; long long lr = 0, lb = 0; for (int i = 0; i < n; ++i) { if (i % 2) { if (s[i] != 'r') { ++lb; } } else { if (s[i] != 'b') { ++lr; } } } ans = max(lr, lb); lr = 0, lb = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0) { if (s[i] != 'r') { ++lb; } } else { if (s[i] != 'b') { ++lr; } } } ans = min(max(lr, lb), ans); cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool is_prime(long long n) { if (n == 1) { return false; } long long i = 2; while (i * i <= n) { if (n % i == 0) { return false; } i += 1; } return true; } long long countSetBits(long long n) { long long count = 0; while (n) { count += n & 1; n >>= 1; } return count; } long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a); a = (a * a); b = b / 2; } return ans; } int main() { long long t = 1; while (t--) { long long n, i, j, d, m, c = 0, ans = 0, r = 0, b = 0; string s; cin >> n >> s; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != 'r') r++; } else { if (s[i] != 'b') b++; } } long long x = max(r, b); r = 0; b = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != 'b') b++; } else { if (s[i] != 'r') r++; } } long long y = max(r, b); cout << min(x, y); } }
8
CPP
n = int(input()) opt = input() ans1, ans2, ans3, ans4 = 0,0,0,0 for i in range(len(opt)): if i % 2: if opt[i]=='r': ans1+=1 else: ans2+=1 else: if opt[i]=='r': ans3+=1 else: ans4+=1 ans = min(max(ans1, ans4), max(ans2, ans3)) print(ans)
8
PYTHON3
# print("Input n") n = int(input()) # print("Input the string") st = input() bfirstbwrong = 0 bfirstrwrong = 0 rfirstbwrong = 0 rfirstrwrong = 0 for i in range(len(st)): ch = st[i] if i%2 == 0: # Starts with b if ch == 'b': rfirstbwrong += 1 else: bfirstrwrong += 1 else: # Starts with r if ch == 'b': bfirstbwrong += 1 else: rfirstrwrong += 1 # b first calculation banswer = max(bfirstbwrong, bfirstrwrong) ranswer = max(rfirstbwrong, rfirstrwrong) print(min(banswer, ranswer))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return (a / gcd(a, b)) * b; } inline double dist(double x1, double y1, double x2, double y2) { return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); } template <class T> inline void deb(T a) { cout << a << '\n'; } template <class T1, class T2> inline void deb(T1 a, T2 b) { cout << a << ' ' << b << '\n'; } template <class T1, class T2, class T3> inline void deb(T1 a, T2 b, T3 c) { cout << a << ' ' << b << ' ' << c << '\n'; } template <class T> T power(T a, T b) { if (b == 0) return 1; else if (b % 2 == 1) return a * power(a, b - 1); else { T k = power(a, b / 2); return k * k; } } int main() { int t, i, j, tc = 0; int a, b, n; string s; while (scanf("%d", &n) == 1) { cin >> s; int r = 0, b = 0; for (i = 0; i <= n - 1; ++i) { if (s[i] == 'r') r++; else b++; } int c1 = 0, c2 = 0; for (i = 0; i <= n - 1; ++i) { if ((i % 2) && (s[i] == 'r')) c1++; else if (!(i % 2) && (s[i] == 'b')) c2++; } int ans1 = max(c1, c2); c1 = 0, c2 = 0; for (i = 0; i <= n - 1; ++i) { if ((i % 2) && (s[i] == 'b')) c1++; else if (!(i % 2) && (s[i] == 'r')) c2++; } int ans2 = max(c1, c2); int ans = min(ans1, ans2); printf("%d\n", ans); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string opat; cin >> opat; int count = 0; string pat = opat; char desired = 'r'; set<int> wrongR; set<int> wrongB; for (long long int i = 0; i < pat.size(); i++) { if (pat[i] != desired) { if (desired == 'r') { wrongR.insert(i); } else { wrongB.insert(i); } } if (desired == 'r') { desired = 'b'; } else { desired = 'r'; } } count = min(wrongR.size(), wrongB.size()); count += max(wrongR.size(), wrongB.size()) - count; int answer = count; desired = 'b'; wrongR.clear(); wrongB.clear(); for (long long int i = 0; i < pat.size(); i++) { if (pat[i] != desired) { if (desired == 'r') { wrongR.insert(i); } else { wrongB.insert(i); } } if (desired == 'r') { desired = 'b'; } else { desired = 'r'; } } count = min(wrongR.size(), wrongB.size()); count += max(wrongR.size(), wrongB.size()) - count; answer = min(answer, count); cout << answer; }
8
CPP
n = int(input()) s = input() p1, p2 = 0, 0 for i in range(n): if i % 2 and s[i] != 'r': p1 += 1 if not i % 2 and s[i] != 'b': p2 += 1 r1 = max(p1, p2) p1, p2 = 0, 0 for i in range(n): if i % 2 and s[i] != 'b': p1 += 1 if not i % 2 and s[i] != 'r': p2 += 1 r2 = max(p1, p2) print(min(r1, r2))
8
PYTHON3
n = int(input()) s = input() r = 0 r1 = 0 b = 0 b1 = 0 for i in range(n) : if i % 2 == 0: if s[i] != 'r': r = r + 1 else: b1=b1+1 else : if s[i] != 'b': b = b + 1 else : r1 = r1 + 1 print(min(max(r, b), max(r1, b1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } void eff() { long long n; scanf("%lld", &n); string s; cin >> s; long long countForRed = 0, countForBlack = 0; if (n == 1) { printf("0\n"); return; } for (long long i = 0; i < n; i += 2) { if (s[i] == 'b') { countForRed++; } } for (long long i = 1; i < n; i += 2) { if (s[i] == 'r') { countForBlack++; } } long long ans = (long long)1e9; long long ans1 = 0; ans1 += min(countForBlack, countForRed); ans1 += ((countForRed - ans1) + (countForBlack - ans1)); ans = min(ans, ans1); countForRed = 0, countForBlack = 0; for (long long i = 0; i < n; i += 2) { if (s[i] == 'r') { countForBlack++; } } for (long long i = 1; i < n; i += 2) { if (s[i] == 'b') { countForRed++; } } long long ans2 = 0; ans2 += min(countForBlack, countForRed); ans2 += ((countForRed - ans2) + (countForBlack - ans2)); ans = min(ans, ans2); cout << ans << endl; } int main() { eff(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string a; cin >> a; int c1 = 0, c2 = 0, c3 = 0, c4 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0 && a[i] != 'r') c1++; if (i % 2 == 1 && a[i] != 'b') c2++; } for (long long i = 0; i < n; i++) { if (i % 2 == 0 && a[i] != 'b') c3++; if (i % 2 == 1 && a[i] != 'r') c4++; } cout << min(max(c1, c2), max(c3, c4)); }
8
CPP
def f(p): cnt1 = sum(a[i] != p[i] == 1 for i in range(n)) cnt2 = sum(a[i] != p[i] == 0 for i in range(n)) res = max(cnt1, cnt2) return res n = int(input()) a = [int(i == 'r') for i in input()] p1 = [i % 2 for i in range(n)] p2 = [(i + 1) % 2 for i in range(n)] ans = min(f(p1), f(p2)) print(ans)
8
PYTHON3
n, s = int(input()), input() a, b = s[::2].count('r'), s[1::2].count('r') print(min(max((n + 1) // 2 - a, b), max(a, n // 2 - b))) # Made By Mostafa_Khaled
8
PYTHON3
n = int(input()) s = input() srr = 0 srb = 0 sbr = 0 sbb = 0 ansr = 0 ansb = 0 ans = 0 for i in range(n): if s[i] == 'r': if (i + 1) % 2 == 0: srr += 1 else: sbr += 1 else: if (i + 1) % 2 == 0: sbb += 1 else: srb += 1 if (srr + srb) < (sbr + sbb): ansr, ansb = srr, srb else: ansr, ansb = sbr, sbb x = min(ansr, ansb) ans = x + (max(ansr, ansb) - x) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string s, s1; int main() { ios_base::sync_with_stdio(false); ; int n, r1, r2, b1, b2, c1, c2, i, j; r1 = r2 = b1 = b2 = 0; cin >> n; cin >> s; s1 = s; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') b2++; else r1++; } else { if (s[i] == 'r') b1++; else r2++; } } c1 = max(r1, b1); c2 = max(r2, b2); cout << min(c1, c2); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char ch[111111], tmp[111111]; int main() { int n; scanf("%d\n", &n); for (int i = 1; i <= n; i++) scanf("%c", &ch[i]); int t1 = 0, t2 = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) if (ch[i] == 'b') t1++; if (i % 2 == 0) if (ch[i] == 'r') t2++; } int ans = max(t1, t2); t1 = 0, t2 = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) if (ch[i] == 'r') t1++; if (i % 2 == 0) if (ch[i] == 'b') t2++; } ans = min(ans, max(t1, t2)); printf("%d\n", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int S = 100003; const int MOD = 1e9 + 7; const double pi = 2 * acos(0.0); int _I() { int x; scanf("%d", &x); return x; } long long _LL() { long long x; scanf("%lld", &x); return x; } int dirX[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dirY[] = {0, 1, 0, -1, 1, -1, -1, 1}; int rX[] = {1, 1, 2, 2, -1, -1, -2, -2}; int rY[] = {2, -2, 1, -1, 2, -2, 1, -1}; template <class T> T Distance(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }; int sset(int N, int pos) { return N = N | (1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int reset(int N, int pos) { return N = N & ~(1 << pos); } int main() { int n = _I(); string s; cin >> s; int r = 0, b = 0, x = 0, y = 0; for (int i = 0; i < n; i++) { if (i & 1 && s[i] == 'r') r++; else if (!(i & 1) && s[i] == 'b') b++; } x += min(r, b); x += abs(r - b); r = 0, b = 0; for (int i = 0; i < n; i++) { if (i & 1 && s[i] == 'b') b++; else if (!(i & 1) && s[i] == 'r') r++; } y += min(r, b); y += abs(r - b); printf("%d\n", min(x, y)); return 0; }
8
CPP
n = int(input()) s = input() black = "" red = "" for i in range(n): if i%2 == 0: black += 'b' red += 'r' else: black += 'r' red += 'b' black_b = 0 black_r = 0 red_b = 0 red_r = 0 for i in range(n): if s[i]!=black[i]: if s[i] == 'b': black_r += 1 else: black_b += 1 if s[i] != red[i]: if (s [i] == 'b'): red_r += 1 else: red_b += 1 ans1 = max (red_b, red_r); ans2= max (black_b, black_r); print(min(ans1,ans2))
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 == 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; cin >> n; char s[n + 5]; for (int i = 1; i <= n; i++) { cin >> s[i]; } int r = 0, b = 0; int y = 0, x = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0 && s[i] != 'b') b++; else if (i % 2 == 1 && s[i] != 'r') r++; if (i % 2 == 0 && s[i] != 'r') y++; else if (i % 2 == 1 && s[i] != 'b') x++; } r = max(b, r); x = max(y, x); cout << min(r, x) << endl; }
8
CPP
n = int(input()) colors = input() r1 = 0 b1 = 0 r2 = 0 b2 = 0 for i in range(n): if i % 2 == 1: if colors[i] == "b": r1 += 1 else: b2 += 1 else: if colors[i] == "r": b1 += 1 else: r2 += 1 maximum = min(max(r1, b1), max(r2, b2)) print(maximum)
8
PYTHON3
##n = int(input()) ##a = list(map(int, input().split())) ##print(" ".join(map(str, res))) def calc(s, t): n = len(s) rb = 0 br = 0 for i in range(n): if s[i] == 'r' and t[i] == 'b': rb += 1 elif s[i] == 'b' and t[i] == 'r': br += 1 return max(rb, br) n = int(input()) s = list(input()) t = [] for i in range(int(n/2)): t.append('r') t.append('b') if n%2 > 0: t.append('r') res = calc(s, t) t.clear() for i in range(int(n/2)): t.append('b') t.append('r') if n%2 > 0: t.append('b') res = min(res, calc(s, t)) print(res)
8
PYTHON3
n = int(input().strip()) a = list(map(lambda x:{'r':0, 'b':1}[x], input())) cb = 0 cr = 0 ccb = 0 ccr = 0 for i,c in enumerate(a): cb += (i&1)&(c^0) cr += (~(i&1))&(c^1) ccb += (i&1)&(c^1) ccr += (~(i&1))&(c^0) ans1 = cb+cr - min(cb,cr) ans2 = ccb+ccr-min(ccb,ccr) print(min(ans1,ans2))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int i, n, a = 0, b = 0, c = 0, d = 0; cin >> n; string s; cin >> s; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'b') a++; else c++; } else { if (s[i] == 'r') b++; else d++; } } cout << min(max(a, b), max(c, d)); return 0; }
8
CPP
n=int(input()) s=input() b=0 r=0 p=s[0] for i in range(1,n): if s[i]==p: if s[i]=='b': b+=1 p='r' else: r+=1 p='b' else: p=s[i] o=max(b,r) b=0 r=0 if s[0]=='b': p='r' b+=1 else: p='b' r+=1 for i in range(1,n): if s[i]==p: if s[i]=='b': b+=1 p='r' else: r+=1 p='b' else: p=s[i] print(min(o,max(b,r)))
8
PYTHON3
def main(n, s): a = 0 b = 0 c = 0 d = 0 for i in range(n): if s[i] == 'r' and i % 2 == 0: a += 1 if s[i] == 'b' and i % 2 == 1: b += 1 if s[i] == 'b' and i % 2 == 0: c += 1 if s[i] == 'r' and i % 2 == 1: d += 1 return min(max(a, b), max(c, d)) print(main(int(input()), list(input())))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class TAT> inline void read(TAT &a) { static char cc; static bool f; while ((cc = getchar()) != '-' && (cc < '0' || cc > '9')) ; if (cc == '-') f = 1, a = 0; else f = 0, a = cc - '0'; while ((cc = getchar()) >= '0' && cc <= '9') a = a * 10 + cc - '0'; if (f) a = -a; } template <class TAT> inline void write(TAT a) { static char cc[27]; static int ct; if (a == 0) { putchar('0'); return; } if (a < 0) { a = -a; putchar('-'); } ct = 0; while (a) cc[++ct] = a % 10 + '0', a /= 10; while (ct) putchar(cc[ct--]); } template <class TAT> inline void Ckmin(TAT &a, const TAT &b) { if (a > b) a = b; } template <class TAT> inline void Ckmax(TAT &a, const TAT &b) { if (a < b) a = b; } void begin() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } void end() { fclose(stdin); fclose(stdout); } const int maxn = 1e5 + 115; int n; char S[maxn]; char C[2] = {'b', 'r'}; int main() { int Ans = 2e9, Cnt[2], nt; read(n); scanf("%s", S + 1); nt = 0; Cnt[0] = Cnt[1] = 0; for (int i = 1; i <= n; ++i) { Cnt[nt] += (S[i] != C[nt]); nt ^= 1; } Ckmin(Ans, max(Cnt[0], Cnt[1])); nt = 1; Cnt[0] = Cnt[1] = 0; for (int i = 1; i <= n; ++i) { Cnt[nt] += (S[i] != C[nt]); nt ^= 1; } Ckmin(Ans, max(Cnt[0], Cnt[1])); write(Ans); putchar('\n'); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n; int x[2], y[2]; char s[1234567]; int main() { ios::sync_with_stdio(0); scanf(" %d %s", &n, s); for (int i = 0; i < n; i++) { if (s[i] == 'r') x[i & 1]++; else y[i & 1]++; } cout << min(max(x[0], y[1]), max(x[1], y[0])); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, count = 0; cin >> n; string x, z; cin >> x; z = x; for (i = 0; i < n; i++) { if (x[i] == 'r') x[i] = '1'; else x[i] = '0'; } char y[n]; for (i = 0; i < n; i++) { if (i % 2 == 0) y[i] = '1'; else y[i] = '0'; } i = 0; long long int count1 = 0, count2 = 0; while (i < n) { if (x[i] != y[i]) { if (x[i] == '1') count1++; else count2++; i++; } else i++; } count = max(count1, count2); count1 = 0, count2 = 0; x = z; for (i = 0; i < n; i++) { if (x[i] == 'r') x[i] = '1'; else x[i] = '0'; } for (i = 0; i < n; i++) { if (i % 2 == 0) y[i] = '0'; else y[i] = '1'; } i = 0; long long int r = 0; while (i < n) { if (x[i] != y[i]) { if (x[i] == '1') count1++; else count2++; i++; } else i++; } r = max(count1, count2); cout << min(count, r) << "\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; const long long INF = 1e16; const long long MAXN = 1000; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long cantb1 = 0, cantr1 = 0, cantb2 = 0, cantr2 = 0; char c[n]; for (long long i = 0; i < n; i++) { cin >> c[i]; } for (long long i = 0; i < n; i++) { if (i % 2 != 0 && c[i] != 'b') { cantb1++; } if (i % 2 == 0 && c[i] != 'r') { cantr1++; } } for (long long i = 0; i < n; i++) { if (i % 2 != 0 && c[i] != 'r') { cantr2++; } if (i % 2 == 0 && c[i] != 'b') { cantb2++; } } long long swap1 = min(cantr1, cantb1); long long paint1 = max(cantr1, cantb1) - swap1; long long res1 = swap1 + paint1; long long swap2 = min(cantr2, cantb2); long long paint2 = max(cantr2, cantb2) - swap2; long long res2 = swap2 + paint2; cout << min(res1, res2) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a; cin >> a; int count1 = 0, count2 = 0, count3 = 0, count4 = 0; for (int i = 0; i < a.length(); i++) { if (a[i] == 'r' && i % 2 == 0) count1++; if (a[i] == 'b' && i % 2 == 0) count3++; if (a[i] == 'b' && i % 2 != 0) count2++; if (a[i] == 'r' && i % 2 != 0) count4++; } cout << min(max(count1, count2), max(count3, count4)) << endl; }
8
CPP
n=input() n=int(n) adjust=input() xr=0 xb=0 yr=0 yb=0 for i in range(0,len(adjust)): if i%2==0: if adjust[i]=='r': xr+=1 else: xb+=1 else: if adjust[i]=='r': yr+=1 else: yb+=1 sum=min(max(xr,yb),max(yr,xb)) print(sum)
8
PYTHON3
# You lost the game. n = int(input()) s = str(input()) r = b = 0 m = 0 m2 = 0 for i in range(n): if s[i] == "r": r += 1 else: b += 1 if n % 2: if r > b: for i in range(n): if s[i] == "b" and i % 2 == 0: m += 1 for i in range(n): if s[i] == "b" and i % 2: m2 += 1 print(min(m+(n-1)//2-b,m2+(n+1)//2-b)) else: for i in range(n): if s[i] == "r" and i % 2 == 0: m += 1 for i in range(n): if s[i] == "r" and i % 2: m2 += 1 print(min(m+(n-1)//2-r,m2+(n+1)//2-r)) else: if r > b: for i in range(n): if s[i] == "b" and i % 2 == 0: m += 1 for i in range(n): if s[i] == "b" and i % 2: m2 += 1 print(min(m,m2)+n//2-b) else: for i in range(n): if s[i] == "r" and i % 2 == 0: m += 1 for i in range(n): if s[i] == "r" and i % 2: m2 += 1 print(min(m,m2)+n//2-r)
8
PYTHON3
n=int(input()) s=input() e="rb"*(n//2)+"r"*(n%2) w="br"*(n//2)+"b"*(n%2) lenn=len(s) f,r,b,rr,bb=0,0,0,0,0 for i in range(lenn): if s[i]!=e[i]: if s[i]=='r': r+=1 else: b+=1 if s[i]!=w[i]: if s[i]=='r': rr+=1 else: bb+=1 f=min(max(r,b),max(rr,bb)) print(f)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { long long n; cin >> n; string s; cin >> s; long long moves = 0; long long x = 0; long long y = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != 'r') { x++; } } else { if (s[i] != 'b') { y++; } } } moves = max(x, y); x = 0; y = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] != 'b') { x++; } } else { if (s[i] != 'r') { y++; } } } long long val = max(x, y); moves = min(moves, val); cout << moves << "\n"; } return 0; }
8
CPP
n = int(input()) s = input() cnt1=0 cnt2=0 cnt3=0 cnt4=0 for i in range (0,n): if i%2 == 0 : if s[i] == 'r' : cnt1 = cnt1+1 elif s[i] == 'b': cnt2 = cnt2+1 else : if s[i] == 'r' : cnt3 = cnt3+1 elif s[i] == 'b': cnt4 = cnt4+1 print(min(max(cnt1,cnt4),max(cnt2,cnt3)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[1000005]; int main() { int n; scanf("%d", &n); scanf("%s", s); int c1 = 0, c2 = 0; int ans1, ans2; for (int i = 0; i < n; i++) { if (s[i] == 'r' && i % 2 == 1) c1++; if (s[i] == 'b' && i % 2 == 0) c2++; } ans1 = max(c1, c2); c1 = 0; c2 = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r' && i % 2 == 0) c1++; if (s[i] == 'b' && i % 2 == 1) c2++; } ans2 = max(c1, c2); printf("%d\n", min(ans1, ans2)); return 0; }
8
CPP
class CodeforcesTask719BSolution: def __init__(self): self.result = '' self.order = '' def read_input(self): input() self.order = input() def process_task(self): # rbrb... x = 0 y = 0 for i, a in enumerate(self.order): if i % 2 and a != 'b': x += 1 elif not i % 2 and a != 'r': y += 1 ans1 = max(x, y) # brbr... x = 0 y = 0 for i, a in enumerate(self.order): if i % 2 and a != 'r': x += 1 elif not i % 2 and a != 'b': y += 1 ans2 = max(x, y) self.result = str(min(ans1, ans2)) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask719BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int find_res(char s[], char c) { int i, k = 0; if (c == 'r') k = 1; else k = 0; int x = 0, y = 0; for (i = 0; i < strlen(s); i++) { if (k == 1 && s[i] != 'r') x++; else if (k == 0 && s[i] != 'b') y++; k = 1 - k; } return (x > y ? x : y); } int main() { char str[100010]; int n; while (scanf("%d", &n) != EOF) { scanf("%s", str); int x, y; x = find_res(str, 'r'); y = find_res(str, 'b'); printf("%d\n", (x < y ? x : y)); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, i, a1, a2, b1, b2, res; string s; int main() { cin >> n >> s; for (i = 0; i < n; i++) { if (s[i] == 'b') if (i % 2 == 0) a1++; else a2++; else if (i % 2 == 0) b1++; else b2++; } res = min(max(a2, b1), max(a1, b2)); cout << res; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a[100000]; int r = 0, b = 0, r1 = 0, b1 = 0, n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) { if (a[i] == 'b') b++; if (a[i] == 'r') r1++; } else { if (a[i] == 'r') r++; if (a[i] == 'b') b1++; } } r = max(r, b); r1 = max(r1, b1); cout << min(r, r1); return 0; }
8
CPP
#include <bits/stdc++.h> const long long mod = 1e9 + 7; const int x = 1; using namespace std; long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res *= a, res %= mod; a *= a; a %= mod; b >>= 1; } return res; } long long isprime(long long n) { int i; if (n == 2) return 1; if (n % 2 == 0) return 0; for (i = 3; i <= sqrt(n); i += 2) if (n % i == 0) return 0; return 1; } int main() { int n; cin >> n; string s; cin >> s; char p[n]; char p1[n]; for (int i = 0; i < n; i++) { if (i % 2 == 0) { p[i] = 'r'; } else { p[i] = 'b'; } } for (int i = 1; i <= n; i++) { if (i % 2 != 0) { p1[i - 1] = 'b'; } else { p1[i - 1] = 'r'; } } int x = 0, x1 = 0; int y = 0, y1 = 0; for (int i = 0; i < n; i++) { if (p[i] != s[i] && s[i] == 'r') { x++; } else if (p[i] != s[i] && s[i] == 'b') { y++; } else if (p1[i] != s[i] && s[i] == 'r') { x1++; } else if (p1[i] != s[i] && s[i] == 'b') { y1++; } } int a = max(x, y); int b = max(x1, y1); cout << min(a, b); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int inf = 2e9; const int mod = 1e9 + 7; const long long INF = 2e18; int n, a[N], d[N], e[N], p[2]; string s; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> s; for (int i = 1; i <= n; ++i) { a[i] = (s[i - 1] == 'r' ? 1 : 0); ++p[a[i]]; d[i] = e[i] = inf; } int b = p[0], r = p[1], curb = 0, curr = 0; for (int i = 1; i <= n; ++i) { if (i & 1) { if (!a[i]) { d[i] = min(d[i], d[i - 1]); --b; } else { if (!b) { d[i] = min(d[i], d[i - 1] + 1); --r; } else { d[i] = min(d[i], d[i - 1]); --b; if (curb) --curb; else ++curr, ++d[i]; } } } else { if (a[i]) { d[i] = min(d[i], d[i - 1]); --r; } else { if (!r) { d[i] = min(d[i], d[i - 1] + 1); --b; } else { d[i] = min(d[i], d[i - 1]); --r; if (curr) --curr; else ++curb, ++d[i]; } } } } b = p[0], r = p[1], curb = 0, curr = 0; for (int i = 1; i <= n; ++i) { if (!(i & 1)) { if (!a[i]) { e[i] = min(e[i], e[i - 1]); --b; } else { if (!b) { e[i] = min(e[i], e[i - 1] + 1); --r; } else { e[i] = min(e[i], e[i - 1]); --b; if (curb) --curb; else ++curr, ++e[i]; } } } else { if (a[i]) { e[i] = min(e[i], e[i - 1]); --r; } else { if (!r) { e[i] = min(e[i], e[i - 1] + 1); --b; } else { e[i] = min(e[i], e[i - 1]); --r; if (curr) --curr; else ++curb, ++e[i]; } } } } cout << min(d[n], e[n]); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string in, rb, br; int n, iler, ileb, wynikrb, wynikbr, zle, pozb, pozr; int main() { ios_base::sync_with_stdio(0); cin >> n; cin >> in; for (int i = 0; i < n; i++) { if (in[i] == 'r') { iler++; } else { ileb++; } } for (int i = 0; i < n; i++) { if (in[i] == 'r' && i % 2 == 1) { zle++; pozr++; } if (in[i] == 'b' && i % 2 == 0) { zle++; pozb++; } } wynikrb += max(pozb, pozr); zle = 0; pozr = 0; pozb = 0; for (int i = 0; i < n; i++) { if (in[i] == 'r' && i % 2 == 0) { zle++; pozr++; } if (in[i] == 'b' && i % 2 == 1) { zle++; pozb++; } } wynikbr += max(pozr, pozb); cout << min(wynikrb, wynikbr); }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; int a, b, c, d; a = b = c = d = 0; int x; string s; cin >> n >> s; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'b' && i % 2) { a++; } else if (s[i] == 'r' && i % 2 == 0) { b++; } } for (int i = 0; i < s.size(); ++i) { if (s[i] == 'r' && i % 2) { c++; } else if (s[i] == 'b' && i % 2 == 0) { d++; } } if (!a || !b || !c || !d) { if (a == b && !a) cout << 0; else if (c == d && !c) cout << 0; else { if (!a) cout << b; else if (!b) cout << a; else if (!c) cout << d; else if (!d) cout << c; } } else { cout << min(max(a, b), max(c, d)); } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int OO = 1e9 + 1e4; int main() { int n = 0, ans = OO, r_b = 0, b_r = 0; string arr[2]; string str = ""; cin >> n >> str; bool alt = 1; for (int i = 0; i < n; i++, alt ^= 1) if (alt) arr[0] += 'r', arr[1] += 'b'; else arr[0] += 'b', arr[1] += 'r'; for (int z = 0; z < 2; z++) { string str2 = arr[z]; for (int i = 0; i < n; i++) r_b += (str2[i] != str[i] && str[i] == 'r'), b_r += (str2[i] != str[i] && str[i] == 'b'); ans = min(ans, max(r_b, b_r)); r_b = b_r = 0; } cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int mod = 1e9 + 7; const int N = 1e5 + 10; int n; int func1(string s) { vector<int> v[2]; for (int i = 0; i <= n - 1; ++i) { if (i & 1 && s[i] == 'r') v[0].push_back(i); if (!(i & 1) && s[i] == 'b') v[1].push_back(i); } int ans = max((int)v[0].size(), (int)v[1].size()); return ans; } int func2(string s) { vector<int> v[2]; for (int i = 0; i <= n - 1; ++i) { if (i & 1 && s[i] == 'b') v[0].push_back(i); if (!(i & 1) && s[i] == 'r') v[1].push_back(i); } int ans = max((int)v[0].size(), (int)v[1].size()); return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); string s; cin >> n; cin >> s; cout << min(func1(s), func2(s)); 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): if(start1=='r'): if(line[idx]=='b'): b2r1+=1 start1='b' else: if(line[idx]=='r'): r2b1+=1 start1='r' if(start2=='r'): if(line[idx]=='b'): b2r2+=1 start2='b' else: if(line[idx]=='r'): r2b2+=1 start2='r' idx+=1 print(min(max(r2b1, b2r1), max(r2b2, b2r2)))
8
PYTHON3
# -*- coding: utf-8 -*- """ Created on Sat Sep 17 19:15:41 2016 @author: bigu """ n = int(input()) line = input() black0 = black1 = red0 = red1 = 0 for i,cock in enumerate(line): if i%2==0 and cock == 'r': red0+=1 elif i%2==1 and cock == 'r': red1+=1 elif i%2==0 and cock == 'b': black0+=1 elif i%2==1 and cock == 'b': black1+=1 ans = min(max(red0,black1),max(red1,black0)) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); } long long max(long long a, long long b) { if (a > b) return a; else return b; } long long min(long long a, long long b) { if (a < b) return a; else return b; } long long pow(long long B, long long P) { long long S = 1; for (long long i = 1; i <= P; i++) S = S * B; return S; } int fx4[] = {1, -1, 0, 0}; int fy4[] = {0, 0, 1, -1}; string make(int N, char C) { string S; S += C; for (int i = 1; i < N; i++) { if (S[i - 1] == 'r') S += 'b'; else S += 'r'; } return S; } int check(string S, string S1, int N) { int R = 0, B = 0; for (int i = 0; i < N; i++) { if (S[i] != S1[i]) { if (S1[i] == 'r') R++; else B++; } } return max(R, B); } int main() { FastIO(); int N; string S; cin >> N; cin >> S; string S1 = make(N, 'r'); string S2 = make(N, 'b'); int X = check(S, S1, N); int Y = check(S, S2, N); cout << min(X, Y) << endl; return 0; }
8
CPP
#!/usr/bin/env python #-*-coding:utf-8 -*- 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
from sys import stdin n = int(input()) ss = input() fx=fy=sx=sy=0 for i in range(len(ss)): if i%2==0:#brbrbr#rbrbrbr if ss[i]=='b': sx+=1 if ss[i]=='r': fy+=1 else:#brbrbr#rbrbrbr if ss[i]=='b': fx+=1 if ss[i]=='r': sy+=1 print(min(max(fx,fy),max(sx,sy)))
8
PYTHON3
#include <bits/stdc++.h> int main() { int n; char color[100000]; scanf("%d", &n); scanf("%s", color); int i; int r1 = 0, r2 = 0, b1 = 0, b2 = 0; for (i = 0; i < n; i++) { if (i % 2 == 0 && color[i] == 'r') r1++; else if (i % 2 == 1 && color[i] == 'r') r2++; else if (i % 2 == 0 && color[i] == 'b') b1++; else b2++; } int r, b; if (b2 > r1) r = b2; else r = r1; if (b1 > r2) b = b1; else b = r2; if (r > b) printf("%d", b); else printf("%d", r); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d, e, f, g, h, i, j, k, m, n, o, p, q, r, s, t, u, v, w, x, y, z, l; long long int tc; cin >> n; string ss; cin >> ss; stack<char> st, sp, sl, sd; string rr = "", rt = ""; for (i = 0; i < n; i++) { if (i % 2 == 0) rr += 'b'; else rr += 'r'; if (i % 2 == 0) rt += 'r'; else rt += 'b'; } for (i = 0; i < n; i++) { if (ss[i] != rr[i] && ss[i] == 'r') { st.push(ss[i]); } else if (ss[i] != rr[i] && ss[i] == 'b') { sp.push(ss[i]); } if (ss[i] != rt[i] && ss[i] == 'b') { sl.push(ss[i]); } else if (ss[i] != rt[i] && ss[i] == 'r') { sd.push(ss[i]); } } long long int ans = 0, ans2 = 0; while (!st.empty() && !sp.empty()) { ans++; st.pop(); sp.pop(); } ans += st.size(); ans += sp.size(); while (!sl.empty() && !sd.empty()) { ans2++; sl.pop(); sd.pop(); } ans2 += sl.size(); ans2 += sd.size(); c = min(ans, ans2); cout << c << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string x; int w2r, w2b, w1r, w1b, ans2, ans; int main() { cin >> w2r >> x; w2r = 0; for (int i = 0; i < x.size(); i++) { if (i & 1) { if (x[i] == 'r') w1r++; } else { if (x[i] == 'b') w1b++; } } ans = max(w1b, w1r); for (int i = 0; i < x.size(); i++) { if (i & 1) { if (x[i] == 'b') w2b++; } else { if (x[i] == 'r') w2r++; } } ans2 = max(w2r, w2b); cout << min(ans, ans2) << endl; }
8
CPP
n = int(input()) line = input() b = r = 0 inverse_b = inverse_r = 0 flag = 0 for i in range(0, n): flag ^= 1 if line[i] == 'b' and flag == 0: b += 1 if line[i] == 'r' and flag == 1: r += 1 if line[i] == 'b' and flag == 1: inverse_r += 1 if line[i] == 'r' and flag == 0: inverse_b += 1 print(min(max(r, b), max(inverse_r, inverse_b)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; char str[100010]; int main() { while (scanf("%d", &n) != EOF) { scanf("%s", str); int br = 0, rb = 0; for (int i = 0; i < n; i++) { if (i % 2 && str[i] == 'r') rb++; else if (i % 2 == 0 && str[i] == 'b') br++; } int ans1 = max(br, rb); br = rb = 0; for (int i = 0; i < n; i++) { if (i % 2 && str[i] == 'b') br++; else if (i % 2 == 0 && str[i] == 'r') rb++; } int ans2 = max(br, rb); printf("%d\n", min(ans1, ans2)); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, x = 0, y = 0, p = 0, q = 0; char a, b; string s; cin >> n >> s; if (s[0] == 'b') a = 'b', b = 'r'; else b = 'b', a = 'r'; if (n == 2) { if (s[0] != s[1]) cout << 0; else cout << 1; } else if (n == 1) cout << 0; else { for (int i = 0; i < n; ++i) { if (i & 1) { if (s[i] != b) x++; else p++; } else { if (s[i] != a) y++; else q++; } } cout << min(max(x, y), max(p, q)); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char alt(char c) { if (c == 'r') return 'b'; else return 'r'; } int main() { ios_base::sync_with_stdio(false); long long n, t, i, j, k, m, ans = 0, an; string a, b, c; cin >> n; cin >> a; b = a; char cc = 'r'; n = m = 0; k = j = 0; for (i = 0; i < a.size(); i += 2) { if (a[i] != cc) n++; if (a[i] != alt(cc)) m++; } cc = alt(cc); for (i = 1; i < a.size(); i += 2) { if (a[i] != cc) k++; if (a[i] != alt(cc)) j++; } an = min(max(n, k), max(m, j)); cout << an << "\n"; return 0; }
8
CPP
a = int(input()) b = input() z1 = 0 x1 = 0 for i in range(a): if i % 2 == 1: if b[i] == 'r': z1 += 1 elif i % 2 == 0: if b[i] == 'b': x1 += 1 y1 = max(z1, x1) z2 = 0 x2 = 0 for i in range(a): if i % 2 == 1: if b[i] == 'b': z2 += 1 elif i % 2 == 0: if b[i] == 'r': x2 += 1 y2 = max(z2, x2) if y1 <= y2: print(y1) else: print(y2)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int mxN = 1e5 + 5; const long long int MOD = 1e9 + 7; long long int one = 0; long long int ceil1(long long int a, long long int b) { if (a % b == 0) return (a / b); else return ((a / b) + 1); } void solve() { long long int n; cin >> n; string s; cin >> s; long long int x = 0, y = 0; for (long long int i = 0; i < n; i++) { if (i & 1) { if (s[i] != 'r') x++; } else { if (s[i] != 'b') y++; } } long long int ans = INT_MAX; ans = min(ans, max(x, y)); x = 0, y = 0; for (long long int i = 0; i < n; i++) { if (i & 1) { if (s[i] != 'b') x++; } else { if (s[i] != 'r') y++; } } ans = min(ans, max(x, y)); cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; while (t--) { one++; solve(); } return 0; }
8
CPP
def func1(n): if n % 2 == 0: return 'rb' * (n // 2) else: return 'rb' * ((n - 1) // 2) + 'r' def func2(n): if n % 2 == 0: return 'br' * (n // 2) else: return 'br' * ((n - 1) // 2) + 'b' n = int(input()) s = input() x1 = func1(n) x2 = func2(n) a1, a2 = [], [] i = 0 while i < n: if s[i] != x1[i]: a1.append(s[i]) i += 1 j = 0 while j < n: if s[j] != x2[j]: a2.append(s[j]) j += 1 cnt1b, cnt1r = a1.count('b'), a1.count('r') cnt2b, cnt2r = a2.count('b'), a2.count('r') if len(a1) < len(a2): print(max(cnt1b, cnt1r)) else: print(max(cnt2b, cnt2r))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int dx2[] = {1, -1, -1, 1, 0, 0, -1, 1}; int dy2[] = {1, -1, 1, -1, 1, -1, 0, 0}; int kmx[] = {-1, -1, 1, 1, 2, -2, 2, -2}; int kmy[] = {2, -2, 2, -2, -1, -1, 1, 1}; class Timer { public: clock_t T; Timer() { T = clock(); } ~Timer() { fprintf(stderr, "\n%.3f\n", double(clock() - T) / CLOCKS_PER_SEC); } }; int read() { int x; scanf("%d", &x); return x; } long long readL() { long long x; scanf("%lld", &x); return x; } const int N = 1e5 + 5; int n; char s[N]; int main() { n = read(); scanf("%s", s); int cntb = 0; int cntr = 0; for (int i = 0; i < n; i++) { if (s[i] == 'b') cntb++; else cntr++; } int colb1 = (n / 2) > cntb ? (n / 2) - cntb : 0; int colr1 = ((n + 1) / 2) > cntr ? ((n + 1) / 2) - cntr : 0; int colr2 = (n / 2) > cntr ? (n / 2) - cntr : 0; int colb2 = ((n + 1) / 2) > cntb ? ((n + 1) / 2) - cntb : 0; int t1 = colb1; int t2 = colb2; int t3 = colr1; int t4 = colr2; int e1 = 0; int e2 = 0; for (int i = 0; i < n; i++) { if (i & 1) { if (s[i] != 'b') { if (t1) t1--; else e1++; } } else { if (s[i] != 'r') { if (t3) t3--; else e1++; } } if (i & 1) { if (s[i] != 'r') { if (t4) t4--; else e2++; } } else { if (s[i] != 'b') { if (t2) t2--; else e2++; } } } printf("%d\n", min(e1 / 2 + colb1 + colr1, e2 / 2 + colr2 + colb2)); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char s[100010]; int main() { int n; scanf("%d", &n); scanf("%s", s); int res = n + 1; for (int i = 0; i < 2; i++) { int now = i; int diff1 = 0, diff2 = 0; for (int j = 0; j < n; j++) { int val; if (s[j] == 'r') { val = 0; } else { val = 1; } if (val != now) { if (now == 0) { diff1++; } else { diff2++; } } now = 1 - now; } int tres = max(diff1, diff2); res = min(res, tres); } printf("%d\n", res); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; int a = 0, b = 0, a1 = 0, b1 = 0; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (s[i] == 'b') a++; else if (s[i] == 'r') a1++; } else { if (s[i] == 'r') b++; else if (s[i] == 'b') b1++; } } int num; num = min(a, b) + fabs(a - b); int sum; sum = min(a1, b1) + fabs(a1 - b1); int end; end = min(num, sum); cout << end << endl; }
8
CPP
n=int(input()) s=input() if s.count('b')==n or s.count('r')==n: print(n//2) else: s_r_count_r=0 s_r_count_b=0 s_b_count_r=0 s_b_count_b=0 for i in range(n): if (i%2==0 and s[i]=='r') or (i%2==1 and s[i]=='b'): if i%2==0: s_b_count_b+=1 else: s_b_count_r+=1 else: if i%2==0: s_r_count_r+=1 else: s_r_count_b+=1 print(min(max(s_r_count_r,s_r_count_b),max(s_b_count_r,s_b_count_b)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { string str; cin >> str; int odd1 = 0, odd2 = 0, even1 = 0, even2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (str[i] == 'b') even1++; else even2++; } else { if (str[i] == 'r') odd1++; else odd2++; } } cout << min(max(odd1, even1), max(odd2, even2)) << 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; int c = 0, d = 0; for (int i = 0; i <= n - 1; i++) { if (i % 2 == 0 && s[i] == 'b') a++; if (i % 2 != 0 && s[i] == 'r') b++; if (i % 2 == 0 && s[i] == 'r') c++; if (i % 2 != 0 && s[i] == 'b') d++; } cout << min(max(a, b), max(c, d)); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using VB = vector<bool>; using VVB = vector<VB>; using VD = vector<double>; using VVD = vector<VD>; using VS = vector<string>; using PII = pair<int, int>; using VPII = vector<PII>; using VL = vector<long long>; using VVL = vector<VL>; int n; string s; int solve() { int n = (int)s.size(); if (n == 1) return 0; int ret = 0; int nr = 0, nb = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0) nr += s[i] != 'r'; else nb += s[i] != 'b'; } ret += min(nr, nb); ret += abs(nr - nb); nr = nb = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0) nr += s[i] != 'b'; else nb += s[i] != 'r'; } int tmp = min(nr, nb); tmp += abs(nr - nb); return min(ret, tmp); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; cout << solve() << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i, cntb, cntr, cntm, ans; string s; cntb = cntr = 0; cin >> n >> s; for (i = (0); i < (n); ++i) { if (i & 1) { if (s[i] == 'b') { cntb++; } } else { if (s[i] == 'r') { cntr++; } } } cntm = min(cntb, cntr); cntb -= cntm; cntr -= cntm; ans = cntm + cntb + cntr; cntb = cntr = 0; for (i = (0); i < (n); ++i) { if (i & 1) { if (s[i] == 'r') { cntr++; } } else { if (s[i] == 'b') { cntb++; } } } cntm = min(cntb, cntr); cntb -= cntm; cntr -= cntm; ans = min(ans, cntm + cntb + cntr); cout << ans << endl; }
8
CPP
n=int(input()) ch=input() r1,r2,b1,b2=0,0,0,0 for i in range(n): if (i%2==0): if (ch[i]!='r'): r1+=1 if (ch[i]!='b'): b2+=1 if (i%2!=0): if (ch[i]=='r'): b1+=1 if (ch[i]=='b') : r2+=1 print(min(max(r1,b1),max(r2,b2)))
8
PYTHON3
n = int(input()) s = input() k = s ans, c, sr, sb = 0, 0, 0, 0 black = True red = False for i in s: if black and i == 'b': if sr > 0: sr = sr - 1 else: c = c + 1 sb = sb + 1 elif red and i == 'r': if sb > 0: sb = sb - 1 else: c = c + 1 sr = sr + 1 black, red = red, black black = False red = True for i in s: if black and i == 'b': if sr > 0: sr = sr - 1 else: ans = ans + 1 sb = sb + 1 elif red and i == 'r': if sb > 0: sb = sb - 1 else: ans = ans + 1 sr = sr + 1 black, red = red, black print(min(ans, c))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, b = 0, r = 0, a; string input; cin >> n; cin >> input; if (n == 1) cout << 0 << "\n"; else if (n == 2) { if (input[0] == input[1]) cout << 1 << endl; else cout << 0 << "\n"; } else { for (i = 0; i < n; i += 2) if (input[i] != 'b') b++; for (i = 1; i < n; i += 2) if (input[i] != 'r') r++; a = max(b, r); b = 0; r = 0; for (i = 0; i < n; i += 2) if (input[i] != 'r') b++; for (i = 1; i < n; i += 2) if (input[i] != 'b') r++; b = max(b, r); cout << min(a, b) << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long int k; cin >> k; string s; cin >> s; long int ob = 0, ort = 0, eb = 0, er = 0; for (long int i = 0; i < k && i != k + 1; i += 2) { if (s[i] == 'r') er++; else eb++; } for (int i = 1; i < k && i != k + 1; i += 2) { if (s[i] == 'r') ort++; else ob++; } long int ans = 0; if (er + ob > eb + ort) { if (eb > ort) ans = eb; else ans = ort; } else { if (er > ob) ans = er; else ans = ob; } cout << ans << endl; return 0; }
8
CPP
def makeAns(turn): qtdRed = 0 qtdBlack = 0 for c in crock: if(c == 'r' and turn == 1): qtdRed+=1 elif(c == 'b' and turn == 0): qtdBlack+=1 turn = (turn+1)%2 return max(qtdRed,qtdBlack) def verifica(lista): curr = lista[0] for i in range(1,len(lista)): if(curr == 'r' and not(lista[i] == 'b')): return False elif(curr == 'b' and not(lista[i] == 'r')): return False else: curr = lista[i] return True n = int(input()) crock = list(input()) if(verifica(crock)): print(0) else: print(min(makeAns(0),makeAns(1)))
8
PYTHON3
n=int(input()) s=input() a,b,c,d=0,0,0,0 for i in range(n): if(s[i]=='r' and i%2==0): a=a+1 elif(s[i]=='b' and i%2==1): b=b+1 elif(s[i]=='r' and i%2==1): c=c+1 elif(s[i]=='b' and i%2==0): d=d+1 print(min(max(a,b),max(c,d)))
8
PYTHON3
length=int(input()) count1=int(0) count2=int(0) count3=int(0) count4=int(0) string=input() for i in range(0,length): if i%2==0: if string[i]!='r': count1+=1 elif string[i]!='b': count2+=1 else: if string[i]=='r': count3+=1 elif string[i]=='b': count4+=1 moves1=int(max(count1,count3)) moves2=int(max(count2,count4)) answer=int(min(moves1,moves2)) print(answer)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string s; int n; int oddb, evenb, oddr, evenr; int main() { cin >> n; cin >> s; int ans = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'b') evenb++; else if (s[i] == 'r') evenr++; } else if (i % 2 == 1) { if (s[i] == 'r') oddr++; else if (s[i] == 'b') oddb++; } } ans = min(max(oddr, evenb), max(oddb, evenr)); cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int xd[] = {0, 1, 0, -1}; int yd[] = {1, 0, -1, 0}; int xx[] = {0, 1, 0, -1, 1, -1, 1, -1}; int yy[] = {1, 0, -1, 0, 1, 1, -1, -1}; int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1}; int ky[] = {1, 2, -1, -2, -2, -1, 1, 2}; bool islucky(int x) { int four = 0, seven = 0; while (x) { if (x % 10 != 7 && x % 10 != 4) return false; if (x % 10 == 7) seven++; else four++; x = x / 10; } return four == seven; } bool is_prime(long long x) { if (x == 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } long long C(int k, int l) { if (l == k) return 1; if (l * 2 > k) l = k - l; long long res = 1, temp = l; for (int i = k - l + 1; i <= k; i++) { res *= i / temp; temp--; } return res; } long long sumOfDigit(int x) { int res = 0; while (x) { res += x % 10; x /= 10; } return res; } uint64_t bigPow(uint64_t base, uint64_t p) { uint64_t temp = 1; while (p--) { temp *= base; } return temp; } vector<int> soso; bool prime[10000001] = {0}; void SieveOfEratosthenes(int n) { soso.push_back(2); for (int p = 3; p * p <= n; p += 2) { if (prime[p] == false) { for (int i = p * 2; i <= n; i += p) prime[i] = true; } } for (int i = 3; i <= n; i += 2) if (!prime[i]) soso.push_back(i); } int main() { int n; cin >> n; string s, str1 = "", str2 = ""; cin >> s; for (int i = 0; i < n; i++) { if (i % 2) { str1 += "r"; str2 += "b"; } else { str1 += "b"; str2 += "r"; } } int ans1 = 0, ans2 = 0, x = 0, y = 0, z = 0, w = 0; for (int i = 0; i < n; i++) { if (s[i] == 'r' && str1[i] == 'b') x++; if (s[i] == 'b' && str1[i] == 'r') y++; if (s[i] == 'r' && str2[i] == 'b') z++; if (s[i] == 'b' && str2[i] == 'r') w++; } cout << min(max(x, y), max(z, w)); }
8
CPP
# Description of the problem can be found at http://codeforces.com/problemset/problem/719/B n = int(input()) s = input() r = 0 r1 = 0 b = 0 b1 = 0 for i in range(n) : if i % 2 == 0: if s[i] != 'r': r = r + 1 else: b1=b1+1 else : if s[i] != 'b': b = b + 1 else : r1 = r1 + 1 print(min(max(r, b), max(r1, b1)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void anatolyAndCockroaches() { int n; char c; cin >> n; int w1 = 0, w2 = 0, w3 = 0, w4 = 0; for (int i = 0; i < n; i++) { cin >> c; if (i % 2) { if (c != 'r') ++w1; if (c != 'b') ++w3; } else { if (c != 'b') ++w2; if (c != 'r') ++w4; } } cout << min(abs(w1 - w2) + min(w1, w2), abs(w3 - w4) + min(w3, w4)) << endl; } int main() { anatolyAndCockroaches(); return 0; }
8
CPP