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:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# X, K, D = map(int, input().split())
# A = list(map(int, input().split()))
# C = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
C = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)


a = C.count("R")
b = C[:a].count("R")
print(a - b)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
r = 0
left, right = 0, n-1
while left < right:
    if c[left] == 'W' and c[right] == 'R':
        r += 1
        left += 1
        right -= 1
    if c[left]=='R':
        left += 1
    if c[right]=='W':
        right -= 1
print(r)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
r = 0
left, right = 0, n-1
while left < right:
    if c[left] == 'W' and c[right] == 'R':
        r += 1
        left += 1
        right -= 1
        continue
    if c[left]=='R':
        left += 1
    if c[right]=='W':
        right -= 1
print(r)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def main():
    n = int(input())
    s = input()
    # print("s: " + s)
    # 左に白が存在する赤
    # 右に赤が存在する白 <=> これらは交換しても良い
    # minimumな個数を交換すればいいはず
    # 塗り替えは実はする必要はない
    w = 0
    r = 0
    for i in range(len(s)):
        if s[i] == 'R':
            r += 1
    # print(len(s))
    # print(str(w) + " " + str(r))

    res_min = r
    for i in range(1, len(s) + 1):
        if s[i-1] == 'R':
            r -= 1
        elif s[i-1] == 'W':
            w += 1

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

    print(res_min)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def check(N, c):
    RinRight = c.count('R')
    WinLeft = 0
    r = RinRight
    for i in range(N):
        if c[i] == 'R':
            RinRight -= 1
        else: # c[i] == 'W'
            WinLeft +=1
        r = min(r, max(RinRight,WinLeft))

    return r


N = int(input())
c= input()
print(check(N,c))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

n=int(input().rstrip())
c=input().rstrip()
w,r=0,0
for i in c:
    if i=="R":r+=1
    else:w+=1
ans=0
for i in range(r):
    if c[i]=="W":ans+=1
print(ans)

Submission status: Accepted

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

r_count = 0
for i in range(N):
    if c[i]=="R":
        r_count+=1

r_remain = 0
for i in range(r_count,N):
    if c[i]=="R":
        r_remain+=1

print(r_remain)

Submission status: Accepted

Solution code:
N = int(input())
stone = input()
R = stone.count('R')
ans = stone.count('W', 0, R)
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np  # noqa

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


K = int(readline())
C = readline().rstrip().decode()

a = 0  # a: 赤から白に変える操作
b = 0  # b: 白から赤に変える操作
for i in range(len(C)):
    if C[i] == 'R':
        a += 1
mn = a
for i in range(len(C)):
    if C[i] == 'R':
        a -= 1
    else:
        b += 1
    mn = min(mn, min(a, b) + abs(a-b))
print(mn)

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 i in range(cnt):
    if(c[i]=='W'):
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
t1 = S.count('R')
S_R = S[:t1]
t2 = S_R.count('R')
print(t1-t2)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
 
nR = c.count("R")
 
if nR == 0 or nR == len(c):
    print(0)
else:
    print(c[:nR].count("W"))

Submission status: Accepted

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

R_num = s.count("R")
print(s[:R_num].count("W"))

Submission status: Accepted

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

ans = 0
low = 0
high = n-1

while low<high:
    if c[low] == 'W':
        
        while low<high and c[high]=='W':
            high -= 1
        
        if c[high] == 'R':
            ans+=1
            c[high], c[low] = c[low], c[high]
            
    low += 1    

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
r_count = s.count('R')
new_s = s[:r_count]
print(new_s.count('W'))

Submission status: Accepted

Solution code:
N = int(input())
C = str(input())
white = C.count('W')
Ccut = C[N-white:]
print(Ccut.count('R'))

Submission status: Accepted

Solution code:
n = int(input())
lst = list(input())
r = lst.count('R')

count = 0

for i in range(0, r):
  if lst[i] == 'R':
    count += 1
    
print(r-count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=0
i=0
j=N-1
while i<j:
  while i<N:
    if S[i]=='W':
      break
    else:
      i+=1
  while j>0:
    if S[j]=='R':
      break
    else:
      j-=1
  if i<j:
    ans+=1
    i+=1
    j-=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq


def main():
    _ = int(input())
    A = list(input())
    R_inds = []
    W_inds = []
    for i in range(len(A)):
        if A[i] == "R":
            R_inds.append(i*(-1))
        else:
            W_inds.append(i)

    if len(R_inds) == 0 or len(W_inds) == 0:
        print(0)
        return

    heapq.heapify(R_inds)
    heapq.heapify(W_inds)
    cnt = 0
    r_i = abs(heapq.heappop(R_inds))
    w_i = heapq.heappop(W_inds)
    while r_i > w_i:
        cnt += 1
        r_i = abs(heapq.heappushpop(R_inds, w_i*(-1)))
        w_i = heapq.heappushpop(W_inds, r_i)

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = input()

b = Counter(s)

ans = min(b["R"],b["W"])

a = 0
for i in range(b["R"]):
    if s[i] == "W":
        a += 1
        
print(min(ans,a))

Submission status: Accepted

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

wr = rw = 0
for i in range(len(C_)):
    if C[i] != C_[i]:
        if C[i] == 'R' and C_[i] == 'W':
            rw += 1
        else:
            wr += 1

print(max(wr, rw))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    C = list(input())
    C_ = sorted(C)

    wr = rw = 0
    for i in range(len(C_)):
        if C[i] != C_[i]:
            if C[i] == "R" and C_[i] == "W":
                rw += 1
            else:
                wr += 1

    print(max(wr, rw))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

nR = c.count("R")

if nR == 0 or nR == len(c):
    print(0)
else:
    print(c[:nR].count("W"))

Submission status: Accepted

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

ans = 0
left = 0
right = N-1
while left < right:
    if c[left] != c[right]:
        if c[left] == 'W' and c[right] == 'R':
            ans += 1
        left += 1
        right -=1
        
    if c[left] == 'R' and c[right] == 'R':
        left += 1
    if c[left] == 'W' and c[right] == 'W':
        right -=1
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = [c for c in input()]
ans = 0
i = 0
j = n-1
while i < j:
    if S[i] == "W":
        if S[j] == "R":
            S[i] = "R"
            S[j] = "W"
            j -= 1
            i += 1
            ans += 1
        else:
            j -= 1
    else:
        i += 1
print(ans)

Submission status: Accepted

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

wr = c.count('WR')
r = c.count('R')
w = c.count('W')

if wr == 0:
    print(0)
else:
    c_sorted = 'R'*r + 'W'*w
    ans = 0
    for i in range(n):
        if c[i] != c_sorted[i]:
            ans += 1
        else:
            ans += 0
    print(ans//2)

Submission status: Accepted

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

R = 0
W = 0
for ci in c:
    if ci == 'R':
        R += 1

ans = max(R,W)
for ci in c:
    if ci == 'R':
        R -= 1
    else:
        W += 1
    tmp = max(R,W)
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
R=c.count('R')
L=c[:R].count('R')
print(R-L)

Submission status: Accepted

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

r = C.count("R")

cnt = 0
for i in range(r):
    if C[i] == "W":
        cnt += 1
        
print(cnt)

Submission status: Accepted

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

a = k.count("R")
b = k[:a].count("W")
print(b)

Submission status: Accepted

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

WL = [0]
RR = [C.count('R')]

for i in range(0, N):
    if C[i] == 'R':
        WL.append(WL[i])
        RR.append(RR[i] - 1)
    else:
        WL.append(WL[i] + 1)
        RR.append(RR[i])

MM = N
for i in range(0, N + 1):
    MM = min(MM, (max(WL[i], RR[i])))
print(MM)

Submission status: Accepted

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

Submission status: Accepted

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

W=0
R=0
for i in range(N):
    if s[i]=='R':
        R+=1

count = R

for p in range(N):
    if s[p]=='R':
        R-=1
    elif s[p]=='W':
        W+=1
    if max(R, W) < count:
        count = max(R, W)

print(count)

Submission status: Accepted

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

red = s.count('R')

ans = 0
for i in range(red):
  if s[i] == 'W':
    ans += 1
print (ans)

Submission status: Accepted

Solution code:
N=int(input())
c=str(input())
num_R = c.count("R")
num_W = 0
ans = []
if num_R >= num_W:
    ans.append(num_R)
else:
    ans.append(num_W)
#print(ans)
for i in range(len(c)):
    if c[i] == "R":
        num_R -= 1
    else:
        num_W += 1
    if num_R >= num_W:
        ans.append(num_R)
    else:
        ans.append(num_W)
    #print(ans)
print(min(ans))

Submission status: Accepted

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

total_count_R = stones.count('R')
left_count_R = stones[0:total_count_R].count('R')

answer = total_count_R - left_count_R

print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

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

N = int(input())
S = input()[:-1]
R = S.count('R')
accR = [0]

for Si in S:
    accR.append(accR[-1]+(1 if Si=='R' else 0))

ans = 10**18

for i in range(N+1):
    ans = min(ans, abs(i-R)+abs(i-accR[i]))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
a=0
i=0
j=len(c)-1
while(i<j):
  while(c[i]=="R" and i<j):
      i+=1
  while(c[j]=="W" and i<j):
      j-=1
  if i<j:
    a+=1
    i+=1
    j-=1
print(a)

Submission status: Accepted

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    N = int(input())
    stones = input()

    count = 0
    visited = 0
    r = stones[::-1]
    for i in range(N):
        if stones[i] == "W":
            j = r.find('R', visited, N-i)
            if j > -1:
                visited = j + 1
                count += 1

    return count


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n = int(input())
seq = input()
r = seq.count('R')
ans = 0
if r != n and r != 0:
    cnt = 0
    for i in xrange(n):
        if seq[i] == 'R':
            cnt += 1
        else:
            ans += 1
            cnt += 1
        if cnt == r:
            break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
wcount = [0] * n
rcount = [0] * n
for i in range(n):
    if c[i] == 'W':
        wcount[i] = 1
    if c[-(i+1)] == 'R':
        rcount[-(i+1)] = 1
    if i != 0:
        wcount[i] = wcount[i] + wcount[i-1]
        rcount[-(i+1)] = rcount[-(i+1)] + rcount[-i]
ans = n
for i in range(n+1):
    if i == 0:
        W = 0
        R = rcount[0]
    elif i == n:
        W = wcount[-1]
        R = 0
    else:
        W = wcount[i-1]
        R = rcount[i]
    tmp = max(W, R)
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

ans = 0

c = input()
redNum = c.count("R")
leftWhite = c.count("W", 0,redNum)
rightRed = c.count("R", redNum,N)

ans += min(leftWhite, rightRed)
if leftWhite > rightRed:
    ans += (leftWhite - rightRed)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=str(input())
rnum=c.count('R')
wnum=c.count('W')

rnum_front=c.count('R', 0, rnum)
wnum_back=c.count('W', N-wnum, N)

if rnum > wnum:
    print(wnum-wnum_back)
else:
    print(rnum-rnum_front)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    S = input()
    ri = S.count("R")  # 右側の換えるべき個数
    le = 0
    ans = max(ri, le)
    for s in S:
        if s == "R":
            ri -= 1
        else:
            le += 1
        ans = min(ans, max(ri, le))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = input()
left, right = 0, n - 1
cnt = 0
while left <= right:
    while left < n and c[left] == 'R':
        left += 1
    while right > -1 and c[right] == 'W':
        right -= 1
    if left == n or right == -1 or left >= right: break
    left += 1
    right -= 1
    cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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


l1 = list()
for i in range(N):
    if c[i] == 'R':
        l1.append(i)
        


num = 0

def swap(i, j):
    a = c[i]
    c[i] = c[j]
    c[j] = a
    return

if len(l1) >= 1:
    for i in range(N):
        if i > l1[-1]:
            break
        if c[i] == 'W':
             swap(i, l1.pop(-1))
             num += 1
        if len(l1) == 0:
            break
print(num)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
d=c.count("R")
e=c[:d].count("R")
print(d-e)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    c = ns()
    r = c.count('R')
    print(r - c[:r].count('R'))
    return

solve()

Submission status: Accepted

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

def main():
    N = int(input())
    c = input()
    cnt_r = 0
    cnt_w = 0
    for i in c:
        if i == 'R':
            cnt_r += 1
        else:
            cnt_w += 1
    
    w_left = 0
    r_right = cnt_r
    ans = N
    for i in range(N):
        ans = min(ans, max(w_left, r_right))

        if c[i] == 'W':
            w_left += 1
        else:
            r_right -= 1
    print(min(ans, max(w_left, r_right)))

main()

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
numW=0
numR=c.count("R")
W=n-numR
a=[]
if numR==0 or W==0:
    print(0)
else:
    for i in range(n):
        if c[i]=="W":
            numW+=1
        else:
            numR-=1
        a.append(max(numR,numW))
    print(min(a))

Submission status: Accepted

Solution code:
N = int(input())
C = input()
num_w = 0
num_r = 0
for i in range(N):
    if C[i] == 'R':
        num_r += 1
ans = 10**10
for i in range(N+1):
    if i == 0:
        ans = min(ans, num_r)
    elif i == N:
        ans = min(ans, num_w)
    else:
        if C[i-1] == 'W':
            num_w += 1
        else:
            num_r -=1 
        ans = min(ans, max(num_r, num_w))
print(ans)

Submission status: Accepted

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

    right = N-1
    left = 0

    cnt = 0
    while left < right:
        while left < N and C[left] == 'R':
            left += 1

        while right >= 0 and C[right] == 'W':
            right -= 1
        
        if left >= right:
            break
        
        C[left], C[right] = C[right], C[left]
        cnt += 1
    
    print(min(cnt,C.count("R"), C.count("W")))
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
C = input()
a = 0
rr = 0
lw = 0

for i in range(N):
    if C[i] == 'R':
        rr += 1
a = rr
for i in range(0, N):
    if C[i] == 'W':
        lw = lw+1
    else:
        rr = rr-1
    a = min(a, max(lw, rr))

print(a)

Submission status: Accepted

Solution code:
N=int(input())
c=input()
j_L=0
j_R=len(c)-1
ans=0
while True:
    while j_L<len(c) and c[j_L]=="R":j_L+=1
    while j_R>=0 and c[j_R]=="W":j_R-=1
    if j_L>=j_R:
        print(ans)
        exit()
    else:
        ans+=1
        j_L+=1
        j_R-=1

Submission status: Accepted

Solution code:
N=int(input())
c=input()
right_r=0
right_w=0
left_r=0
left_w=0
for i in range(N):
	if c[i]=='R':
		right_r+=1
	else:
		right_w+=1
ans=right_r
for j in range(N):
	cnt=0
	if c[j]=='W':
		left_w+=1
		right_w-=1
	else:
		left_r+=1
		right_r-=1
	if right_r<=left_w:
		cnt+=right_r
		cnt+=left_w-right_r
	else:
		cnt+=left_w
		cnt+=right_r-left_w
	if cnt<ans:
		ans=cnt
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def solve():
    N = int(input())
    C = input()

    num_r = C.count("R")
    num_w = C.count("W")
    ans = min(num_r, num_w)

    left_r = 0
    left_w = 0
    for i in range(N):
        left_r += C[i] == 'R'
        left_w += C[i] == 'W'

        right_r = num_r - left_r
        right_w = num_w - left_w
        ans = min(ans, min(left_w, right_r) + abs(left_w - right_r))

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = input()
    r = 0
    for i in range(n):
        if c[i]=='R':
            r += 1
    ans = 0
    for i in range(r):
        if c[i]=='W':
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7
#DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    N = iRaw()
    cs = sRaw()
    ac_sums =[0]
    for c in cs:
        if c=="W":
            ac_sums.append(ac_sums[-1]+1)
        else:
            ac_sums.append(ac_sums[-1])
    ans = 0
    nW = ac_sums[-1]
    nR = N-nW
    ans = INF
    for n_idx,ac_sum in enumerate(ac_sums):
        ans = min(ans,max(ac_sum,nR-(n_idx-ac_sum)))
    print(ans)
    
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

S = input()[:-1]
po = [0 for i in range(n)]

for i in range(n-1,-1,-1):
    if i == n-1:
        if S[i] == "R":
            po[i] += 1
    else:
        if S[i] == "R":
            po[i] += po[i+1]+1
        else:
            po[i] += po[i+1]
co = 0

for i in range(n):
    if po[i] - co > 0 and S[i] != "R":
        co += 1
print(co)

Submission status: Accepted

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

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

cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
rcount = len([1 for c in C if c == "R"])

count = 0
r = 0
w = 0
for i in range(rcount):
    c = C[i]
    if c == "W":
        if r > 0:
            r -= 1
            count += 1
        else:
            w += 1
    c = C[N-i-1]
    if c == "R":
        if w > 0:
            w -= 1
            count += 1
        else:
            r += 1
ans = count + w
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
len_r = 0
len_w = 0
for i in range(N):
    if c[i] == 'R':
        len_r += 1
len_w = N - len_r
n_r = 0
for j in range(len_r):
    if c[j] == 'R':
        n_r += 1
ans = len_r - n_r
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rs = s.count('R')
oks = s[:rs].count('R')
print(rs - oks)

Submission status: Accepted

Solution code:
N = int(input())
words = list(input())
ct_R = words.count('R')
ct_W = words.count('W')
a = words[0:ct_R]
W_in_a_count = a.count('W')
print(W_in_a_count)

Submission status: Accepted

Solution code:
n=int(input())
li=[]
red=0
white=0
count=0
li=list(input())
for i in li:
  if i == 'R':
    red+=1
  else:
    white+=1

checkw=0
for i in range(red):
  if li[i]=='W':
    checkw+=1

checkr=0
for i in range(white):
  if li[i+red]=='R':
    checkr+=1
sa=checkw-checkr
if sa==0:
  print(checkr)
elif sa>0:
  print(checkr+sa)
else:
  print(checkw+sa)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
c = [1 if _ == 'R' else 0 for _ in input()]
b = sum(c)

w=0
r=0

for i in range(n):
  if c[i] == 0 and i < b:
    r+=1
  if c[i] == 1 and i >= b:
    w+=1
 

print(max(w,r))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
cd = c[::-1]

rs = [0]*(N+1)
ws = [0]*(N+1)
ws[0]= 0
rs[0] = 0

for i in range(N):
    if(c[i]=="W"):
        rs[i+1]=rs[i]+1
    else:
        rs[i+1]=rs[i]

    if(cd[i]=="R"):
        ws[i+1]=ws[i]+1
    else:
        ws[i+1]=ws[i]
    
ws.reverse()

m = 1000000000
for i in range(N+1):
    M = max(rs[i],ws[i])
    if m>M:
        m = M
    
print(m)

Submission status: Accepted

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

t = n
lw, rr = 0, c.count('R')
for ci in c+'Z':
    swap = min(lw, rr)
    wtor, rtow = max(0, lw-swap), max(0, rr-swap)
    t = min(t, swap + wtor + rtow)
    lw = lw + (ci == 'W')
    rr = rr - (ci == 'R')

print(t)

Submission status: Accepted

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

lw, rr = [0]*(n+1), [c.count('R')]*(n+1)
for i in range(n):
    lw[i+1] = lw[i] + (c[i] == 'W')
    rr[i+1] = rr[i] - (c[i] == 'R')


def operation(left_white, right_red):
    swap = min(left_white, right_red)
    white_to_red, red_to_white = max(0, left_white-swap), max(0, right_red-swap)
    return swap + white_to_red + red_to_white


print(min(operation(white, red) for white, red in zip(lw, rr)))

Submission status: Accepted

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

rs = l.count("R")
print(l[:rs].count("W"))

Submission status: Accepted

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

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

Submission status: Accepted

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

rw = [0, 0]
for i in range(N):
  if c[i] == 'R':
    rw[0] += 1
  else:
    rw[1] += 1
    
ans = N
left_rw = [0, 0]
right_rw = rw[:]
for i in range(N+1):
  if i == 0:
    pass
  else:
    if c[i-1] == 'R':
      left_rw[0] += 1
      right_rw[0] -= 1
    else:
      left_rw[1] += 1
      right_rw[1] -= 1
      
  ans = min(max(left_rw[1], right_rw[0]), ans)
  
print(ans)

Submission status: Accepted

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

if 'WR' in c:
    x=c.count('R')
    y=c.count('R',0,x)
    count=x-y
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=0
for i in s:
  if(i=="R"):
    r+=1
ans=0
for j in range(r):
  if(s[j]=="W"):
    ans+=1
print(ans)

Submission status: Accepted

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

#右端から見ていって初めのRが出たらWとRをカウントする
cnt = 0
R = 0
W = 0

for i in range(n):
    if c[i] == "R":R += 1
    else:W += 1

for i in range(R):
    if c[i] == "W":cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import math
from decimal import *

n = int(input())
c = str(input())
w = c.count('W')
c = c[::-1]
ans= 0
for i in range(w):
    if(c[i]!= 'W'):
        ans+=1
print(ans)

Submission status: Accepted

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

rcnt=s.count("R")
wcnt=s[:rcnt].count("W")
print(wcnt)

Submission status: Accepted

Solution code:
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
#print(c)

t1=t2=True

n1=0
n2=0
num=0
while t1:
    t2=True
    if not 0<=N-1-n1<=N-1:
        t1=False
    if c[N-1-n1]=='W':
        n1+=1
        continue
    else:
        while t2:
            if c[n2]=='W':
                if n2<N-1-n1:
                    c[N-1-n1]='W'
                    c[n2]='R'
                    num+=1
                    t2=False
                    continue
                else:
                    t1=False 
            n2+=1
            if not 0<=n2<=N-1:
                t1=False
                t2=False
print(num)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
W = 0
R = c.count("R")
if N == R or R == 0:
  print("0")
else:
  cnt = N
  for i in range(N):
    if c[i] == "W":
      W = W + 1
    else:
      R = R - 1
    m = max(W, R)
    if m < cnt:
      cnt = m
  print(cnt)

Submission status: Accepted

Solution code:
N=int(input())

c=list(input())

minimum=len(c)
W=0
R=c.count('R')

for i in range(0,len(c)):
    if c[i]=='W':
        W += 1
    else :
        R -= 1
    min_i=max(W,R)
    if min_i < minimum:
        minimum=min_i

if c.count('R')==0:
    minimum=0

print(minimum)