Problem:


Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3



Sample input:
4
WWRR


Sample output:
2

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    C = input().strip()
    R = C.count('R')
    if R == 0 or R == N:
        print(0)
        return
    leftR = 0
    pos = 0
    ans = N - R
    for i, c in enumerate(C):
        if c == 'R':
            leftR += 1
        leftW = i + 1 - leftR
        rightR = R - leftR
        #print(leftW, rightR)
        changes = abs(rightR - leftW)
        swaps = min(rightR, leftW)
        ans = min(ans, swaps + changes)
    print(ans)

solve()

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
cnt_r = 0
for c in C:
    if c == "R":
        cnt_r += 1
for i in range(cnt_r):
    if C[i] == "R":
        cnt_r -= 1
print(cnt_r)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
s2=sorted(s)
t=0
for i in range(n):
  if s[i]!=s2[i]:
    t+=1
print(t//2)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
R = c.count('R')
W = 0
ans = max(R,W)
for i in range(N):
    if c[i] == 'R':
        R -= 1
    else:
        W += 1
    ans = min(ans,max(W,R))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

ans = n
w2r = 0
r2w = s.count("R")
ans = min(ans, max(w2r, r2w))

for i in range(n):
    if s[i]=="W":
        w2r += 1
    else:
        r2w -= 1
    ans = min(ans, max(w2r, r2w))
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
red_count = 0
for s in S:
    if s == 'R':
        red_count += 1
ans = 0
for i in range(red_count):
    if S[i] == 'W':
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()

n_w  = c.count("R")

goal = []
for i in range(n_w):
    goal.append("R")
for j in range(n-n_w):
    goal.append("W")

W2R = 0
R2W = 0

for i in range(n):
    if c[i]=="W" and goal[i]=="R":
        W2R += 1
    elif c[i]=="R" and goal[i]=="W":
        R2W += 1
        
ans = max(W2R, R2W)
print(ans)

Submission status: Accepted

Solution code:
def solve(n, c):
    num = len(list(filter(lambda _: _=="R", c)))
    if num in [0,n]:
        return 0
    w = [0] * n
    w[0] = int(c[0] == "W")
    for i in range(1, n):
        w[i] = w[i-1] + int(c[i] == "W")
    return min(max(w[i], num+w[i]-i-1) for i in range(n))

n = int(input())
c = input()
print(solve(n, c))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

R = [0]
W = [0]
r = 0
w = 0

for i in range(N):
    if S[i]=="W":
        w+=1
    else:
        r+=1
    R.append(r)
    W.append(w)
ans = N
for i in range(N+1):
    ans = min(ans,max(W[i],R[N]-R[i]))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = s.count('W')
print(0 if w == 0 else s[-w:].count('R'))

Submission status: Accepted

Solution code:
N = int(input())
c = input()
ans = cnt = c.count('R')
for x in c[:cnt]:
    if x == 'R':
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input().rstrip())
c = list(input().rstrip())
nr = c.count('R')
result = c[nr:].count('R')
print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = s.count('W')
if w == 0:
  print(0)
else:
  print(s[-w:].count('R'))

Submission status: Accepted

Solution code:
N = int(input())
c = input()

count = 0
if not(c.find('R') == -1 or c.find('W') == -1):
    idx_W = -1
    idx_R = -1
    while True:
        idx_W = c.find('W')
        idx_R = c.rfind('R')
        if idx_W >= idx_R:
            break
        c = c[:idx_W] + 'R' + c[idx_W+1:idx_R] + 'W' + c[idx_R+1:]
        count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
red_to_white = c.count("R")
ans = red_to_white
while_to_red = 0

for i in c:
    if i == "W":
        while_to_red += 1 
    else:
        red_to_white -= 1
    ans = min(ans,max(while_to_red,red_to_white))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
res = 0
l, r = 0, n - 1
while l < r:
    if s[l] != 'W':
        l += 1
    elif s[r] != 'R':
        r -= 1
    else:
        res += 1
        l += 1; r -= 1
print(res)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

n = int(input())
s = input()
i, j = 0, n - 1
ans = 0
while i < j:
	while i < j and s[i] == 'R':
		i += 1
	while i < j and s[j] == 'W':
		j -= 1
	if i < j:
		ans += 1
	i += 1
	j -= 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=input()
dp=[0]
bp=[0]
for i in range(N):
  if c[i]=='W':
    dp.append(dp[-1]+1)
    bp.append(bp[-1])
  else:
    dp.append(dp[-1])
    bp.append(bp[-1]+1)
ans=10**9
for i in range(N+1):
  ans=min(max(dp[i],bp[-1]-bp[i]),ans)
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
S = input()

c = S.count("R")
res = S[c:].count("R")
print(res)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = [s for s in input()]
    ans = 10**9
    w = 0
    r = S.count("R")
    for i in range(N+1):
        ans = min(ans, max(w, r))
        if i == N:
            break
        if S[i] == "W":
            w += 1
        else:
            r -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
c=input()
r=c.count("R")
print(min(c[:r].count("W"),c[r:].count("R")))

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n = ni()
cs = input()
r = cs.count("R")
print(cs[r:].count("R"))

Submission status: Accepted

Solution code:
n = int(input())
c = input()
cnt = 0
ans = 0

for i in range(n):
    if c[i] == 'R':
        cnt += 1
    
for j in range(cnt):
    if c[j] == 'W':
        ans += 1
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
sw = [0]*(n+2)
sr = [0]*(n+2)
for i in range(1,n+1):
    sw[i] = sw[i-1] + 1*(c[i-1]=="W")
    sr[n+1-i] = sr[n+2-i] + 1*(c[n-i]=="R")
ans = 10**6
for i in range(n+1):
    ans = min(ans, max(sw[i],sr[i+1]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
a = C.count('R')
b = C.count('R', 0, a)
print(a - b)

Submission status: Accepted

Solution code:
n=int(input())
hoge=input()
r=hoge.count("R")
print(min(hoge[:r].count("W"),hoge[r:].count("R")))

Submission status: Accepted

Solution code:
N = int(input())
cn = input()
sto = list(cn)
count = 0

r = sto.count("R")
for i in range(r):
    if sto[i] == "W":
      count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()


def calculate(n, s):
    arr = list(s)
    result = []


    leftW = 0
    rightR = arr.count('R')


    if rightR == 0:
        print(0)
        return

    for i in range(n):
        val = arr[i]

        if val == 'R':
            rightR -= 1

        if val == 'W':
            leftW += 1

        result.append(max(leftW,rightR))

    if len(result) == 0:
        print(0)
    else:
        print(min(result))

calculate(N, S)

Submission status: Accepted

Solution code:
N = int(input())

C = list(input())

res = 0
j = N - 1
for i in range(N):
    if C[i] == "W":
        while i < j:
            if C[j] == "R":
                res += 1
                C[i] = "R"
                C[j] = "W"
                break
            j -= 1
    if i >= j:
        break

print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

white_to_red = 0
red_to_white = s.count('R')

ans = red_to_white
for i in range(n):
    if s[i] == 'W':
        white_to_red += 1
    else:
        red_to_white -= 1
    ans = min(ans, max(white_to_red, red_to_white))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
red_count = c.count('R')
ans = 0
for i in range(red_count):
    if c[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
stn = input()
m = stn.count('R')
r = m
w = 0
for i in range(N):
    if stn[i]=='R':
        r -= 1
    else:
        w += 1

    m = min(m, max(r,w))

print(m)

Submission status: Accepted

Solution code:
N = int(input())
L = input()

l = 0
r = N-1
count = 0

while l < r:
    if L[l] == 'W' and L[r] == 'R':
        count += 1
        l += 1
        r -= 1
        continue

    if L[l] != 'W':
        l += 1
    
    if L[r] != 'R':
        r -= 1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(input())
R=a.count("R")
W=0

count=R
for i in range(n):
    if a[i]=="R":
        R-=1
    else:
        W=W+1
    count=min(count,max(R,W))
print(count)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
num_R = c.count("R")
num_R_2 = c[:num_R].count("R")
ans = num_R - num_R_2
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    c=list(input())
    w=[0]*(N+1)
    r=[0]*(N+1)
    
    for i in range(N):
        if c[i]=="W":
            w[i+1]+=1
        else:
            r[i]+=1
            
    for i in range(N):
        w[i+1]+=w[i]
        
    for i in range(N,0,-1):
        r[i-1]+=r[i]
        
    ans=N
    for i in range(N+1):
        temp=max(w[i],r[i])
        ans=min(ans,temp)
        
    print(ans)

        
            

    

main()

Submission status: Accepted

Solution code:
N = int(input())
C = input()

R, W = [], []
R_ = []
nR, nW = 0, 0
for c in C:
    if c == 'R':
        nR += 1
    else:
        nW += 1
    R.append(nR)
    W.append(nW)

if nR == 0 or nW == 0:
    print(0)
    exit()

for r in R:
    R_.append(nR - r)

ans = N

for i in range(N):
    r, w, r_ = R[i], W[i], R_[i]
    if r_ > w:
        ans = min(r_, ans)
    else:
        ans = min(w, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
print(c[:(c.count('R'))].count("W"))

Submission status: Accepted

Solution code:
n=int(input())
c=input()
print(c[:(c.count('R'))].count('W'))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
red = 0
white = 0
for x in s:
    if x=='R':
        red += 1
    else:
        white += 1
reqr = 0
for i in range(red):
    if s[i]=='W':
        reqr += 1
print(reqr)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
wc = c.count("W")
rc = c.count("R")

ans = min(c[rc:].count("R"), c[:-wc].count("W"))
print(ans)

Submission status: Accepted

Solution code:
n,c = input(),input()
print(c[:(c.count("R"))].count("W"))

Submission status: Accepted

Solution code:
n = int(input())
c = input()

l = n - 1
r = 0

ans = 0

while r <= l:
    if c[r] == "W" and c[l] == "R":
        ans += 1
        r += 1
        l -= 1

    else:
        if c[r] == "R":
            r += 1
        if c[l] == "W":
            l -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
C = [c for c in input()]

ans = 0
l,r = 0, n-1
while(l < r):
    while(C[l] == 'R'): 
        l += 1
        if (n == l): break
    
    while(C[r] == 'W' or -1 == r):
        r -= 1
        if (-1 == r): break

    if (l < r):
        ans += 1
    l += 1
    r -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
WR = input()

i = 0
j = len(WR) - 1

num = 0

while(i < j):
  if(WR[j] == "R"):
    if(WR[i] == "W"):
      num += 1
      i += 1
      j -= 1
    else:
      i += 1
  else:
    j -= 1

print(num)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

r = S.count('R')
w = 0
ans = [max(r,w)]

for i in range(N):
    if S[i] == 'W':
        w += 1
    else:
        r -= 1
    ans.append(max(w,r))

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
C = input()
r, w = 0, 0

for c in C:
    if c == "R":
        r += 1
    else:
        w += 1
ans = 0
if r <= w:
    for c in C[:r]:
        if c == "W":
            ans += 1
    print(ans)
else:
    for c in C[r:]:
        if c == "R":
            ans += 1
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = str(input())

ans = 0

rgt = 0
lft = N-1

while(rgt < lft):
    if C[rgt] == "W" and C[lft] == "R":
        ans += 1
        rgt += 1
        lft -= 1


    
    else:
        if C[rgt] == "R":
            rgt += 1
        if C[lft] == "W":
            lft -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
count = 0
i, j = 0, n - 1

while i < j:
  if c[i] == 'W' and c[j] == 'R':
    count += 1
    i += 1
    j -= 1
  elif c[i] == 'W':
    j -= 1
  elif c[j] == 'R':
    i += 1
  else:
    i += 1
    j -= 1

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
cs = input()
r_num = 0
w_num = 0
# RRR|WWWW にする
r_num = cs.count('R')
w_num = cs.count('W')

if r_num == 0 or w_num == 0:
    print(0)
else:
    ans = []
    W = 0
    rl_num = 0
    for c in cs:
        if c == 'R':
            rl_num += 1
        elif c == 'W':
            W += 1
        R = r_num - rl_num
        ans.append(max(W, R))

    print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
c = input()
ans_c = "".join(sorted(c))

total = 0
miss_dct = {"R": 0, "W": 0}
for i in range(n):
    if c[i] != ans_c[i]:
        total += 1
        miss_dct[c[i]] += 1

swap = min(miss_dct["R"], miss_dct["W"])
cnt = swap
total -= swap*2
print(cnt+total)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=0
ri=len(s)-1
for li,ss in enumerate(s):
  if ss=="W":
    while ri>li and s[ri]=="W":
      ri-=1
    if ri<=li:
      break
    ri-=1
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
rcnt=s.count("R")
# "R"をすべて左に寄せるならば、(0 ～ "R"個数-1)分の配列範囲はすべて"R"となる。
r_expected_arr=s[:rcnt]
# "R"期待範囲に"W"が存在する場合、"R"との交換が要求される。
# その場合、文字列中に"R"個数分の"R"が存在するため、交換対象の"R"は必ず存在する。
w_exchange_needed=r_expected_arr.count("W")
print(w_exchange_needed)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cr = s.count("R")
cw = len(s) - cr

s_out = s[cr:]
cr_out = s_out.count("R")

print(min(cw, cr_out))

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
r=n-1
ans=0
for i in range(n):
    if s[i]=='W':
        while i<r:
            if s[r]=='R':
                s[i]='R'
                s[r]='W'
                ans+=1
                break
            r-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
s2 = sorted(s)
A = 0
for i in range(n):
  if s[i]!= s2[i]:
    A+=1
    
print(A//2)

Submission status: Accepted

Solution code:
n = int(input())
c = input()

w = 0
r = c.count('R')

ans = max(w,r)

for i in range(n):
    if c[i] == 'W':
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())

r = c.count("R")
w=0
ans=r

for i in range(n):
    if c[i]=="W":
        w+=1
    else:
        r-=1
    ans=min(ans,max(r,w))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
c_w = 0
c_r = 0
for i in range(len(S)):
    if S[i] == 'W':
        c_w += 1
    else:
        c_r += 1
p_1 = c_w
p_2 = c_r
p_3 = 0
for i in range(c_r):
    if S[i] == 'W':
        p_3 += 1
print(min(p_1,p_2,p_3))

Submission status: Accepted

Solution code:
n = int(input())
c = input()

count = c.count('R')
ans = 0

for i in range(n):
    if c[i] == 'R' and i >= count:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
inp=input()
num=[0]*(n+1)

k=0
for i in range(n):
  if inp[i]=='R':k+=1
  num[i+1]=k
R=num[-1]
W=n-R

ans=float('inf')
for i in range(n+1):
  rl=num[i]
  wl=i-rl
  rr=R-rl
  wr=W-wl
  swap=min(wl,rr)
  change=max(wl,rr)-swap
  ans=min(ans,swap+change)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()

lft = 0
rht = 0

ans = 0

flag = False

for i in range(n):
    #print(i,rht)
    if flag == True:
        break
    if n-1-rht <= i:
        break
    if c[i] == 'W':
        for j in range(n-i):
            if n-1-j-rht == i:
                flag = True
                break
            if c[n-1-j-rht] == 'R':
                ans += 1
                rht += j+1
                break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(input())

w=A.count("W")
r=A.count("R")

W=0 ; R=r
ans=R
for i in range(n):
    if A[i]=="W": W+=1
    else: R-=1
    ans=min(ans, max(W,R))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = [str(c) for c in s]

def sea(a, x, y, n):
  while x < n-1:
    if a[x] == 'W':
      break
    x += 1
    
  while y > 0:
    if a[y] == 'R':
      break
    y -= 1
  
  return[x, y]

z = [0, n-1]

z = sea(a, z[0] ,z[1], n)
c = 0
while z[0] < z[1]:
  a[z[0]], a[z[1]] = a[z[1]], a[z[0]]
  c += 1
  z = sea(a,z[0], z[1],n)
  
print(c)

Submission status: Accepted

Solution code:
N=int(input())
C =input()

r = C.count("R")
ans = 0
for i in range(N):
    if C[i] == "R" and r <= i:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

l = 0
r = n - 1
ans = 0
while r > l:

    while r > l:
        if s[l] == 'W': break
        else: l += 1
    while r > l:
        if s[r] == 'R': break
        else: r -= 1
    if r > l:
        ans += 1
        r -= 1
        l += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
C=input()

r=C.count('R')
print(r-C[:r].count('R'))

Submission status: Accepted

Solution code:
n = int(input())
c = input()

cnt = 0
i = 0
j = n-1
while i < j:
    if c[i] == 'W':
        while i < j:
            if c[j] == 'R':
                cnt += 1
                j -= 1
                break
            j -= 1
    i += 1

print(cnt)

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    C = list(input())
    R_cnt = C.count("R")#Rの個数を数える
    #方針 RR...RWWW...Wの形になれば良いので
    #RR..WRWR/WWW...RRWW  ・・・/の位置がRの個数番目であれば, そこまでのWの個数分「/」の右側のRと交換すればよい
    cnt = 0
    for i in range(R_cnt):
        if C[i] == "R":
            cnt+=1
    print(R_cnt - cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = input()

R = C.count('R')  # 赤の個数
cnt = 0
for i in range(R):
    if C[i] == 'R':
        cnt += 1
print(R - cnt)

Submission status: Accepted

Solution code:
N = int(input())
C = []
for c in list(input()):
  a = 0
  if c == "R":
    a = 1
  C.append(a)
L = -1
R = N
cnt = 0
while L < R:
#  print(str(L) + " " + str(R))
  L += 1
  if L >= R:
    break
  if C[L] != 0:
    continue
  while L < R:
    R -= 1
    if L >= R:
      break
    if C[R] != 1:
      continue
    cnt += 1
    break     
print(cnt)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    c = input()
    r = c.count("R")
    ans = 0
    for i in range(r, n):
        if c[i] == "R":
            ans += 1
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
c = list(input())
if c.count("W")*c.count("R") == 0:
    print(0)
    exit()
ans = 0
d = deque(c)
while d:
    flag = False
    while d:
        if flag:
            break
        lef = d.popleft()
        if lef == "W":
            while d:
                rig = d.pop()
                if rig == "R":
                    ans += 1
                    flag = True
                    break
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
a = list(input()) #１文字ずつ
# print(a)

allW=0
allR=0
for i in range(N):
    if a[i]=='W':
        allW+=1
    elif a[i]=='R':
        allR+=1
# print('allW,allR:',allW,allR)

W=0
R=allR
out = max(W,R)
ans = out
for shikiri in range(N):
    if a[shikiri] =='W':
        W+=1
    elif a[shikiri]=='R':
        R-=1
    out = max(W,R)
    ans = min(ans,out)
    # print('out,ans:',out,ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
num_reds = [0] * N
num_whites = [0] * N
num_reds[0] += 1 if s[0] == 'R' else 0
num_whites[0] += 1 if s[0] == 'W' else 0
for i in range(1, N):
    if s[i] == 'R':
        num_reds[i] += 1
    else:
        num_whites[i] += 1
    num_reds[i] += num_reds[i - 1]
    num_whites[i] += num_whites[i - 1]

ans = N
for n in range(1, N):
    # x = (n - num_reds[n - 1]) + ((N - n) - (num_whites[N - 1] - num_whites[n - 1]))
    a = n - num_reds[n - 1]
    b = (N - n) - (num_whites[N - 1] - num_whites[n - 1])
    x = a + b - min(a, b)
    ans = min(ans, x)
ans = min(ans, num_reds[N - 1])
ans = min(ans, num_whites[N - 1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
R = C.count('R')
c = 0
for i in range(R):
    if not C[i] == 'R':
        c += 1

print(c)

Submission status: Accepted

Solution code:
n=int(input());s=input();a=b=0
for i in s:
	if i=='R':a+=1
for i in range(a):
	if s[i]=='W':b+=1
print(b)

Submission status: Accepted

Solution code:
n, X = int(input()), input()
count_w, count = X.count('W'), 0
for i in range(n - 1, n -1 - count_w, -1):
  count += 1 if X[i] == 'R' else 0
  
print(count)

Submission status: Accepted

Solution code:
from math import floor, ceil
N = int(input())
WHITE = 0
RED = 1
str = list(map(str, list(input())))
r = []
w = []

for i in range(N):
  w.append(i) if str[i] == "W" else r.append(i)
  str[i] = WHITE if str[i] == "W" else RED
r.reverse()
max_count = min(len(r), len(w))
count = 0
for i in range(max_count):
  if r[i] - w[i] > 0:
    count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())

Rnum = C.count('R')
print(C[:Rnum].count('W'))

Submission status: Accepted

Solution code:
def count(l):
    res = {'W':0, 'R':0}
    for d in l:
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return res
n = int(input())
string = input()
cnt = count(string)
swap = 0
for i in range(cnt['R']):
    if string[i] == 'W':
        swap += 1
print(min(swap, cnt['R'], cnt['W']))

Submission status: Accepted

Solution code:
from enum import Enum

def main():
  initialStones = readProblem()
  minOperationCount = getMinOperationCount(initialStones)
  print(minOperationCount)

def getMinOperationCount(initialStones):
  whiteOnLeft = 0
  redOnRight = 0  # count just bellow
  for stone in initialStones:
    if Color[stone] == Color.R:
      redOnRight += 1
  minOperationCount = len(initialStones)  # effectively infinite
  for separator in range(len(initialStones) + 1):
    operationCount = max(whiteOnLeft, redOnRight)
    if minOperationCount > operationCount:
      minOperationCount = operationCount
    
    if separator < len(initialStones):
      if Color[initialStones[separator]] == Color.R:
        redOnRight -= 1
      else:
        whiteOnLeft += 1
    else:
      pass  # already we have min value
  return minOperationCount

def readProblem():
  input()  # not use
  return input()

class Color(Enum):
  R = 'Red'
  W = 'White'
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
li=0
ri=len(s)
cnt=0
while li<ri:
  if s[li] == "W":
    while li<ri:
      ri-=1
      if s[ri] == "R":
        cnt+=1
        break
      
  li+=1
print(cnt)

Submission status: Accepted

Solution code:

N = int(input())
C = input()
r = 0
w = 0   
b = 0
r = C.count('R')
for i in range(r):
    if (C[i]=='W'):
        b+=1
print(b)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())

c_sort = sorted(c)

# print(c_sort)

r = 0
w = 0

for cc, ss in zip(c,c_sort):
    if cc == 'W' and ss == 'R':
        r += 1
    elif cc == 'R' and ss == 'W':
        w += 1
    else:
        continue

print(max(r,w))

Submission status: Accepted

Solution code:
N = int(input())
s = input()
l = 0
r = len(s) - 1
ans = 0
while True:
    while l < len(s) - 1 and s[l] == 'R':
        l += 1
    while r >= 0 and s[r] == 'W':
        r -= 1

    if l < r:
        ans += 1
        l += 1
        r -= 1
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
k=list(input())
n1=k.count("R")
ideal_lis=["R"]*n1
ans = 0
for i in range(n1):
    if k[i]!=ideal_lis[i]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    C = input()
    r_all = C.count('R')
    # ある場所を境に右側をRとする.
    # |WWWWWWW
    # R|WWWWWW
    # RR|WWWWW
    # .......
    # RRRRRR|W
    # RRRRRRR|
    move = [0] * (n+1)
    # 実際の並びにおいて、|の左のW右のRの数をw, rとする
    w = 0
    r = r_all
    # 初期値はすべてWを目指すので
    move[0] = max(0, r_all)
    for idx, c in enumerate(C):
        if c == 'W':
            w += 1
        else:
            r = r - 1 
        move[idx+1] = max(w, r)
    ans = min(move)
    print(ans) 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = input()

r = 0
for v in c:
    if v == "R":
        r += 1
ans = min(r, n - r)
w = 0
for v in c:
    if v == "W":
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()

leftw=0
rightr=0

for i in range(n):
    if s[i]=='R':
        rightr+=1

ans=rightr

for i in range(n):
    if s[i]=='W':
        leftw+=1
    else:
        rightr-=1
    ans=min(ans,max(leftw,rightr))

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

N = int(input())

c = input()

a = c.count('R')

b = 0
ans = max(a, b)

for i in range(len(c)):
    if c[i] == 'R':
        a -= 1
    else:
        b += 1
    now = max(a, b)    
    ans = min(ans, now)

print(ans)

Submission status: Accepted

Solution code:
#二分探査
n=int(input())
C=list(input())
w=0
r=C.count('R')
ans=max(w,r)

for i in range(n):
  if C[i]=='W':
    w+=1
  if C[i]=='R':
    r-=1
  if ans>max(w,r):
    ans=max(w,r)
    
print(ans)


#以下のコードはO(n**2)なのでn<2*10**5では重すぎる
"""
n=int(input())
C=list(input()) #listにすると文字列が１文字ごとに１要素となる
cnt=0
flag1=False
flag2=True

for i in range(n):
  if C[i]=='W':
    for t in range(1,n-i):
      if C[-t]=='R':
        C[i]='R'
        C[-t]='W'
        cnt+=1
        flag2=False
        break
    if flag2:
      flag1=True
  if flag1:
    break

print(cnt)
"""

Submission status: Accepted

Solution code:
N = int(input())

li = list(input())

r_count = li.count('R')

ideal_li = list('W' for _ in range(len(li)))

for i in range(r_count):
    ideal_li[i] = 'R'

unmatch_count = 0
for i in range(len(li)):
    if li[i] != ideal_li[i]:
        unmatch_count += 1

print(unmatch_count // 2)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
w=0
r=0
for i in range(n):
    if c[i]=="W":
        pass
    else:
        r+=1
ans=max(w,r)
for i in range(n):
    if c[i]=="R":
        r-=1
    else:
        w+=1
    ans=min(ans,max(w,r))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
l = 0
r = N-1
ans = 0
while (r-l) > 0:
    if C[l] == "W" and C[r] == "R":
        C[l], C[r] = C[r], C[l]
        l += 1
        r -= 1
        ans += 1
        continue
    if C[l] == "R":
        l += 1
    if C[r] == "W":
        r -= 1
print(ans)

Submission status: Accepted

Solution code:
a=map(int,input())

b=input()

c=b.count("R")
d=b.count("W",0,c)
print(d)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
red=s.count('R')
white=s.count('W')
mn=min(red,white)
ct=0
for i in range (red):
    if s[i]=='W':
        ct+=1
mn=min(mn,ct)
print(mn)

Submission status: Accepted

Solution code:
n = int(input())
c = str(input())

a, b = 0, 0

a = c.count('R')
ans = max(a, b)

for i in range(n):
    if c[i] == 'R':
        a -= 1
    else:
        b += 1
    ans = min(ans, max(a, b))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
a = c.count("R")
ans = 0

for i in range(a):
    if c[i] == "W":
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n = I()
mychr = input().rstrip()

result = float('inf')
red = 0
for i in mychr:
    if i == "R":
        red += 1

R = 0
W = 0

for index, i in enumerate(mychr):
    if i == "R":
        R+=1
    else:
        W+=1
    temp = max(W,red-R)
    result = min(result, temp)

if W != n:
    print(result)
else:
    print(0)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    C = input()

    r = 0
    cnt = []
    for c in C:
        if c == "R":
            r += 1
        cnt.append(r)
    cnt.append(cnt[-1])

    if not r or r == len(C):
        print(0)
        return

    ans = float("inf")
    for i in range(1, N):
        ans = min(ans, max(i - cnt[i - 1], cnt[-1] - cnt[i]))
    print(ans)


main()

Submission status: Accepted

Solution code:
n = int(input())
cl = list(input())

ans = cl.count('R')
r = cl.count('R')
w = 0
for i in range(n):
    if cl[i] == 'W':
        w += 1
    else:
        r -=1
    ans=min(max(r,w),ans)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
red=c.count('R')
ok=c[:red].count('R')
print(red-ok)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    c = input()

    return N, c


def main(N: int, c: str) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 石の個数（2 <= N <= 200000）
        c (str): 石の色
    """
    # 求解処理
    w = 0
    r = c.count("R")
    ans = max(w, r)
    for n in range(N):
        c_n = c[n]
        if c_n == "W":
            w += 1
        else:
            r -= 1
        ans = min(ans, max(w, r))

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, c = get_input()

    # メイン処理
    main(N, c)