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:
n = int(input())
c = list(input())

r = c.count('R')  
wr = c[:r].count('W')  

print(wr)

Submission status: Accepted

Solution code:
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    N = ri()
    c = rs()
    x = 0
    y = c.count('R')

    for e in c:
        if x == y:
            break
        elif e == 'W':
            x += 1
        else:
            y -= 1

    print(y)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(n):
    if(s[i] == "R"):
        count += 1

count2 = 0
for i in range(count):
    if(s[i] == "W"):
        count2 += 1

print(count2)

Submission status: Accepted

Solution code:
n = int(input())
list = input()
count_r = list.count('R')
ans = list[:count_r].count('W')

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=input()
i=list(input())
Wn=i.count("W")
print(Wn-i[(len(i)-Wn):].count("W"))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
c = list(input())

right_R = N
for i in range(N-1,-1,-1):
    if c[i] == 'R':
        right_R = i
        break

left_W = -1
for i in range(N):
    if c[i] == 'W':
        left_W = i
        break

if right_R == N or left_W == -1:
    print(0)
    sys.exit(0)

ans = 0
p = left_W
q = right_R
while p < q:
    for i in range(p+1,N):
        if c[i] == 'W':
            p = i
            break
        if i == N-1:
            p = N
    for i in range(q-1,-1,-1):
        if c[i] == 'R':
            q = i
            break
        if i == 0:
            q = -1
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input().rstrip())
red = []
white = []
stones = input().rstrip()
for idx, stone in enumerate(stones):
  if stone == "R":
    red.append(idx)
  if stone == "W":
    white.append(idx)
red_len = len(red)
white_len = len(white)
cnt = 0
for i in range(min(red_len, white_len)):
  red_idx = red[-1-i]
  white_idx = white[i]
  if red_idx> white_idx:
    cnt += 1
  else:
    break
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
C=input()
s=0
e=N-1
ans=0
while(s<e):
  while(C[s]=="R" and s<e):
    s+=1
  while(C[e]=="W" and s<e):
    e-=1
  if s<e:
    s+=1
    e-=1
    ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()
c = input()
i = 0
while 1:
    l = c.find('W')
    r = c.rfind('R')
    if l == -1 or r == -1 or l >= r:
        break
    c = c[:l] + 'R' + c[l + 1:r] + 'W'  + c[r + 1:]
    i += 1
print(i)

Submission status: Accepted

Solution code:
N=int(input())
stones = list(input())
l = 0
r = N-1
count = 0

while(True):
  while(stones[l] == "R" and l < N-1):
    l += 1
  while(stones[r] == "W" and r > 0):
    r -= 1
  if (l >= r):
    print(count)
    exit()
  else:
    count += 1
    l += 1
    r -= 1

Submission status: Accepted

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

left = string[:0]
right = string[0:]
w, r = left.count("W"), right.count("R")
count = max(w,r)

for i in range(1,n+1):
    next_str = string[i-1]
    if next_str == "W":
        w +=1
    else:
        r -= 1
    tmp = max(w,r)
    if tmp < count:
        count = tmp
        
print(count)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
cnt = [0 for _ in range(n)]
red = 0
for i in range(n):
    if c[i] == 'R':
        red += 1
    cnt[i] = red
print(red - cnt[red-1])

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n = int(input())
    c = str(input())

    #output
    #Rがr個、Wがw個あるとする。
    #最終的にはRRRR...WWWW...の状態にすれば良い。
    #もとの状態と比べた時、RとWが一致していないものを考える。
    #WRの個数とRWの個数をそれぞれx, yとすると、求める操作の回数はmax(x, y)となる。
    w, r = 0, 0
    for i in range(n):
        if c[i] == "W":
            w += 1
        else:
            r += 1
    x, y = 0, 0
    for j in range(r):
        if c[j] == "W":
            x += 1
    for k in range(r+1, r+w):
        if c[k] == "R":
            y += 1
    print(max(x, y))

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
C = [x for x in str(input())]
cnt_R = C.count('R')
swap_W = C[:cnt_R].count('W')
ans = swap_W
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
W = "W"
R = "R"

N = int(input())
C = list(input())
numR = C.count(R)
result = 0
for i in range(numR):
    if C[i] == W:
        result += 1
print(result)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n = ri()
    C = input()
    if C[0] == "R":
        R = [1]
        W = [0]
    else:
        R = [0]
        W = [1]

    for c in C[1:]:
        if c == "R":
            R.append(1 + R[-1])
            W.append(W[-1])
        else:
            W.append(1 + W[-1])
            R.append(R[-1])

    best_cost = min(R[-1], W[-1])
    for i in range(n):
        rl = R[i]
        wl = W[i]
        rr = R[-1] - rl
        wr = W[-1] - wl
        swaps = min(wl, rr)
        flips = wl + rr - 2 * swaps
        best_cost = min(best_cost, flips + swaps)
    print (best_cost)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
from collections import Counter
d = Counter(list(S))
allr = d["R"]
leftd = Counter(list(S[:allr]))
leftr = leftd["R"]
print(allr-leftr)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
w = c.count("W")
r = c.count("R")
wr = c[len(c) - w:].count("W")

print(min(r, w - wr))

Submission status: Accepted

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

red = c.count('R')
print(c[red:].count('R'))

Submission status: Accepted

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

for i in range(N):
    if C[i] == 'W':
        red_start = i
        break
    if i == N-1:
        red_start = N-1

for i in range(1, N+1):
    if C[-i] == 'R':
        white_start = i
        break
    if i == N:
        white_start = N

red_pos = red_start
white_pos = white_start    

while red_pos + white_pos < N:
    answer += 1
    for i in range(red_pos+1, N):
        if C[i] == 'W':
            red_pos = i
            break
        if i == N-1:
            red_pos = N-1
    for i in range(white_pos+1, N+1):
        if C[-i] == 'R':
            white_pos = i
            break
        if i == N:
            white_pos = N

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
ls=list(input())
left=0
right=N-1
count=0
while True:
  while left<N and ls[left]=='R':
    left+=1
  while right>0 and ls[right]=='W':
    right-=1
  if left>=right:
    break
  left+=1
  right-=1
  count+=1
print(count)

Submission status: Accepted

Solution code:
N = int(input())

c = input()

l = len(c)
a = 0
b = 0

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

for i in range(a, l):
    if c[i] == "R":
        b += 1

print(b)

Submission status: Accepted

Solution code:
N = int(input())
c = list(str(input()))
r = c.count('R')
w = 0
for i in range(r):
    if c[i] == 'W':
        w += 1
print(w)

Submission status: Accepted

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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())
C = input()

left = 0
right = N-1
count = 0

while left < right:
    if C[left] == 'W' and C[right] == 'R':
        count += 1
        left += 1
        right -= 1

    if C[left] == 'R':
        left += 1
    if C[right] == 'W':
        right -= 1

print(count)

Submission status: Accepted

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

r = s.count("R")

print(s[:r].count("W"))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
redPref = [0 for i in range(0, N)]

if S[0] == 'R':
    redPref[0] += 1

for i in range(1, N):
    redPref[i] += redPref[i - 1]
    if S[i] == 'R':
        redPref[i] += 1

def query(l, r, pref):
    a, b = 0, pref[r]
    if l - 1 >= 0:
        a = pref[l - 1]
    return b - a


ans = min(N - redPref[-1], redPref[-1])

for i in range(0, N - 1):
    leftRed = query(0, i, redPref)
    leftWhite = i - leftRed + 1

    rightRed = query(i + 1, N - 1, redPref)
    rightWhite = ((N - 1) - (i + 1) + 1) - rightRed

    swaps = min(leftWhite, rightRed)
    leftWhite -= swaps
    rightRed -= swaps
    leftRed += swaps
    rightWhite += swaps

    ans = min(ans, leftWhite + rightRed + swaps)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
c = stdin.readline().rstrip()
R_cnt = c.count('R')
L_cnt = 0
for i in range(R_cnt):
  if c[i] == 'R':
    L_cnt += 1
print(R_cnt - L_cnt)

Submission status: Accepted

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

num_red = c.count('R')
print(num_red - c[:num_red].count('R'))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
O = input()
ridx = [i for i, s in enumerate(O) if s == 'R']
n = len([i for i in ridx if i >= len(ridx)])
print(n)

Submission status: Accepted

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

tmp = C
tmp2 = len(tmp.replace("W",""))
tmp3 = C[0:tmp2]
ans = len(tmp3.replace("R",""))   
print(ans)

Submission status: Accepted

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


W = C.count("W")
R = C.count("R")

# 仕切りより左側にある白石の数
w = 0
# 仕切りより右側にある赤石の数
r = R

ans = 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())

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

for i in range(r):
  if c[i] == 'W':
    count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
s=input()
A=[]
for i in range(N):
    A.append(s[i])

cntW=0
for i in range(N):
    if A[i]=="W":
        cntW+=1
cntR=N-cntW

changeW=0
changeR=0
for i in range(cntR):
    if A[i]=="W":
        changeW+=1
for i in range(cntR,N):
    if A[i]=="R":
        changeR+=1

if changeR==changeW:
    ans=changeW
else:
    #minN=min(changeW,changeR)
    maxN=max(changeR,changeW)
    ans=maxN
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = 0
r = n - 1
cnt = 0

while l<r:
    while s[l] == 'R' and l < r:
        l += 1
    while s[r] == 'W' and l < r:
        r -= 1
    if l>=r:
        break
    cnt += 1
    l += 1
    r -= 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
cl = str(input())
cl_R = cl.count("R")
# print(cl_R)
cl_leftR = cl[:cl_R].count("R")
# print(cl_leftR)
print(cl_R-cl_leftR)

Submission status: Accepted

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

def main():
    N = int(input())
    c = list(input())[:-1]
    # bad_pairs = []
    w_count = 0
    r_count = 0
    for i in range(N):
        if c[i] == 'W':
            w_count += 1
        else:
            r_count += 1
        # if c[i] == 'W' and i < N - 1 and c[i + 1] == 'R':
        #     bad_pairs.append({ 'W': i, 'R': i + 1 })
    # if len(bad_pairs) == 0:
    #     print(0)
    #     exit(0)
    if w_count == N or r_count == N:
        print(0)
        exit(0)
    no_exchange = 0
    for i in range(r_count):
        if c[i] == 'R':
            no_exchange += 1
    # print(no_exchange, w_count, r_count)
    print(r_count - no_exchange)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

i, j = 0, n - 1
if c.count('W') == 0:
    count=0
elif c.count('R') == 0:
    count = 0
else:
    white_cnt = []
    red_cnt = []
    for i in range(n):
        if c[i] == 'W':
            white_cnt.append(i)
        else:
            red_cnt.append(i)
    while 1:
        white_num = white_cnt.pop(0)
        red_num = red_cnt.pop()
        if white_num < red_num:
            count += 1
        if len(white_cnt) == 0:
            break
        if len(red_cnt) == 0:
            break
print(count)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
C = stdin.readline().rstrip()
rc = C.count('R')
ans = 0
for i in range(rc):
    if C[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
C = input()
r = deque()
lr = 0
for i in range(N):
  if C[i] == 'R':
    lr += 1
    r.append(i)
ans = 0
for i in range(N):
  if lr == 0:
    break
  if C[i] == 'W':
    if i < r[-1]:
      ans += 1
      r.pop()
      lr -= 1
    else:
      break
print(ans)

Submission status: Accepted

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

N = int(input())
c = input()

r_total = 0
w_total = 0
for i in c:
  if i == 'R':
    r_total += 1
  elif i == 'W':
    w_total += 1

w_left = 0
r_left = 0

ans = r_total # 仕切りが左端の場合

# iが仕切りの位置
for i in range(N):
  cnt = 0
  if c[i] == 'R':
    r_left += 1
    
  elif c[i] == 'W':
    w_left += 1
    
  if w_left <= r_total - r_left:
    cnt += w_left # 仕切りより左の白と右の赤を入れ替え
    cnt += (r_total - r_left) - w_left # 仕切りより右の赤を白に変更
  else:
    cnt += r_total - r_left  # 仕切りより左の白と右の赤を入れ替え
    cnt += w_left - (r_total - r_left) # 仕切りより左の白を赤に変更
  ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C=list(input())
W=(C.count('W'))
R=N-W
WW=0
RR=0
X=[0]*(N-1)
if W>0 and R>0:
  for i in range(N-1):
    if C[i]=='W':
      WW+=1
    RR=(N-i-1)-(W-WW)
    X[i]=max(WW,RR)
print(min(X))

Submission status: Accepted

Solution code:
ina = input()
inb = input()

l = int(ina)
cw1 = inb.count("W")
cw2 = inb[-cw1:].count("W")
print(cw1-cw2)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
r = c.count('R')
ans = 0

for i in range(r):
    if c[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
r = c.count('R')
w = c.count('W')
ans = 0

for i in range(r):
    if c[i] == 'R':
        pass
    elif c[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
RR=0
X=0
if 'R' in type and 'W' in type:
    for L in range(N):
      R=RR
      if L>=N-1-R:
        break
      if C[L]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)

Submission status: Accepted

Solution code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
RR=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      R=RR
      if L>=N-1-R:
        break
      if C[i]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)

Submission status: Accepted

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

ans = s.count("R")
fans = s[0:ans].count("R")
print(ans-fans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
r_cnt = c.count("R")
ans = r_cnt - c[0:r_cnt].count("R")
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
moji = input()
r_cnt = 0
for i in range(len(moji)):
    if moji[i] == "R":
        r_cnt += 1

count = 0
for i in range(r_cnt):
    if moji[i] == "W":
        count += 1
print(count)

Submission status: Accepted

Solution code:
param_n = int(input())
param_str = str(input())

#print(param_n)print(param_str)

list_param_str = list(param_str)

#print(list_param_str)

param_R_count = list_param_str.count('R')

#print(param_R_count)

param_R_count_before_separation = list_param_str[0:param_R_count].count('R')

#
print(param_R_count - param_R_count_before_separation)

Submission status: Accepted

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

R_count = 0
W_count = 0
for i in range(len(stone)):
  if stone[i] == "R":
    R_count = R_count + 1
  else:
    W_count = W_count + 1

W = 0
R = R_count
try_num_prev = -1
for i in range(N):
  if R_count == 0 or W_count == 0:
    try_num = 0
    break
  
  if W <= R:
    try_num = R
  else:
    try_num = W
  
  #print(try_num_prev, try_num)
  if try_num_prev >= 0 and try_num_prev < try_num:
    try_num = try_num_prev
    break
  else:
    try_num_prev = try_num
    if stone[i] == "R":
      R = R - 1
    else:
      W = W + 1
    
print(try_num)

Submission status: Accepted

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

print(cou_r-count)

Submission status: Accepted

Solution code:
N=int(input())
C=input()
ans=0
while C!="":
  if C[0]=="R":
    C=C[1:]
  elif C[len(C)-1]=="W":
    C=C[0:len(C)-1]
  else:
    C=C[1:len(C)-1]
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import time
sys.setrecursionlimit(int(1e6))
if False:
    dprint = print
else:
    def dprint(*args):
        pass

N = list(map(int, input().split()))[0]
s = list(input())

w = 0
r = s.count('R')
min_op = max(w, r)
dprint('w, r = ', w, r)

p = 0
while(p < N):
    if (s[p] == 'W'):
        w = w + 1
    else: # (s[p] = 'R')
        r = r - 1
    min_op = min(min_op, max(w, r))
    dprint('p, w, r = ', p, w, r)
    
    p = p + 1

print(min_op)

Submission status: Accepted

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

num_r = 0
for i in range(N) :
  if c[i] == 'R' :
    num_r += 1
    
ans = 0
for i in range(num_r, N) :
  if c[i] == 'R' :
    ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
def main():
    n = int(input())
    c_array = list(input())
    
    w = 0
    r = c_array.count("R")
    ans = r
    
    for i in range(0, n):
        if c_array[i] == "W":
            w += 1
        else:
            r -= 1
        # print(w, r)
        tmp = max(w, r)
        # print(tmp)
        if tmp <= ans:
            ans = tmp
    
    print(ans)
    
main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
res = 2000000 
aw=s.count('W')
ar=s.count('R')
if (aw == 0 or ar == 0):
	res=0
pw=0
pr=0
for i in range(n-1):
	if (s[i] == 'R'):
		pr=pr+1
	else:
		pw=pw+1
	if (max(pw,ar-pr) < res):
		res = max(pw,ar-pr)

print(str(res))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
# D - Alter Altar
N = int(input())
c = input()

white = c.count('W')
red = c.count('R')

left = [0, 0]
right = [white, red]
ans = min(white, red)

for i in range(0, N):
	if c[i] == 'W':
		left[0] += 1
		right[0] -= 1
	else:
		left[1] += 1
		right[1] -= 1
	
	count = max(left[0], right[1])
	ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(input())
es = [0,N,0]

def p(X):
    print(X)

def list_change(X):
    for i in range(N):
        if X[i] == 'W':
            es[0] = i
            for j in reversed(range(es[1])):
                if X[j] == 'R':
                    es[1] = j
                    if i<j :
                        X[i] = 'R'
                        X[j] = 'W'
                        es[2] +=1
                        break
                elif j == 0:
                    es[1] = 1
                


list_change(X)
p(es[2])

Submission status: Accepted

Solution code:
n = int(input())
c = input()
w = 0
r = c.count('R')
ans = r

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

Submission status: Accepted

Solution code:
n = int(input())
c = input()
lst = []
w = 0
r = 0
for i in range(n):
  if c[i] == 'R':
    r += 1
ans = max(w, r)

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

Submission status: Accepted

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

def main():

    reds = []
    for i in range(N):
        if C[i] == "R":
            reds.append(i +1)

    l = len(reds)
    ans = 0
    for i in range(l):
        if reds[i] > l:
            ans += 1

    print(ans)

main()

Submission status: Accepted

Solution code:
"""
4
WWRR
2
"""
import itertools

n = int(input())
s = list(input())
left_w = [0]
right_r = [0]
for l in s:
    if l == "W":
        left_w.append(1)
    else:
        left_w.append(0)
lacc = list(itertools.accumulate(left_w))
for r in s[::-1]:
    if r == "R":
        right_r.append(1)
    else:
        right_r.append(0)
racc = list(itertools.accumulate(right_r))
racc = racc[::-1]
print(min(map(max, zip(lacc, racc))))

Submission status: Accepted

Solution code:
def solve(n, c_str):
    cum_sum_w_left = [0] * (n + 1)
    cum_sum_r_right = [0] * (n + 1)
    # cum_sum_w_left
    cum_sum_w = 0
    for i in range(n):
        if c_str[i] == "W":
            cum_sum_w += 1
        cum_sum_w_left[i + 1] = cum_sum_w
    # cum_sum_r_right
    cum_sum_r = 0
    for i in range(n - 1, -1, -1):
        if c_str[i] == "R":
            cum_sum_r += 1
        cum_sum_r_right[i] = cum_sum_r
    # print(cum_sum_w_left)
    # print(cum_sum_r_right)
    res = n + 1
    for i in range(n + 1):
        res = min(res, max(cum_sum_w_left[i], cum_sum_r_right[i]))
    # print(res)
    return res


def main():
    n = int(input())
    c_str = input()
    res = solve(n, c_str)
    print(res)


def test():
    assert solve(4, "WWRR") == 2
    assert solve(2, "RR") == 0
    assert solve(8, "WRWWRWRR") == 3


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
c = input()
ans = [-1] * (N+1)

for i in range(0, N+1):
  if i == 0:
    l = c[:i]
    r = c[i:]
    W = l.count("W")
    R = r.count("R")
  else:
    if (c[i-1]) == "W":
      W += 1
    elif (c[i-1]) == "R":
      R -= 1

  ans[i] = max(R, W)
      
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
C = input()
a = b = ans = 0
for c in C:
    if c == 'R':
        a += 1
ans = max(a, b)
for c in C:
    if c == 'R':
        a -= 1
    else:
        b += 1
    ans = min(ans, max(a, b))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  lc = list(input().strip())
  
  i = 0
  j = n - 1
  cnt = 0
  while i < j:
    while i < n:
      if lc[i] == 'R':
        i += 1
      else:
        break
    while 0 < j:
      if lc[j] == 'W':
        j -= 1
      else:
        break        
    if i < j:
      cnt += 1
    i += 1
    j -= 1
    
  print(cnt)

if __name__=='__main__':
  main()

Submission status: Accepted

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

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

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
ans = 0
r = [0] * n
w = [0] * n
for i in range(n):
  if s[i] == 'W':
    w[i] += 1
  else:
    r[i] += 1
for i in range(1, n):
  w[i] += w[i - 1]
  r[i] += r[i - 1]
rn = r[n - 1]
ans = n
if rn == 0:
  ans = 0
for i in range(n):
  c = (i + 1)
  ri = r[i]
  left = rn - ri
  need = c - ri
  ans = min(ans, max(left, need))  

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
count=0
for i in range(n):
    if s[i]=="R":
        count=count+1
count1=0
for i in range(count):
    if s[i]=="R":
        count1=count1+1
print(count-count1)

Submission status: Accepted

Solution code:

n = int(input())
c = input()

w2r = 0 # 白 → 赤に変える数
r2w = 0 # 赤 → 白に変える数
for i in range(n):
    if c[i] == 'R': r2w += 1 # 初期値は赤い石の数

needed = lambda a,b: a + b - min(a,b)
# 必要な手数は W2R と R2W の計 - ペアで変えれる数（min(w2r, r2w))
ans = needed(w2r,r2w)
for i in range(n):
    if c[i] == 'R':
        r2w -= 1
    else:
        w2r += 1
    ans = min(ans, needed(w2r,r2w))

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())
s=input()

wcount=0
for i in range(n):
    if s[i]=="R" :
        wcount+=1

wwcount=0
for i in range(wcount):
    if s[i]=="R" :
        wwcount+=1

print(wcount-wwcount)

Submission status: Accepted

Solution code:
def calc_min_op(n, stone, left_red):
    ans = float('inf')
    for i in range(n+1):
        # stone[:i] は R, stone[i:] は W にするときの最小回数
        left_excess_w = i - left_red[i]
        right_excess_r = left_red[n] - left_red[i]
        operations = max(left_excess_w, right_excess_r)
        ans = min(ans, operations)
    return ans


def main():
    n = int(input())
    stone = input()
    # stone[:i] のうち R であるものの数
    left_red = [0] * (n + 1)
    for i in range(1, n + 1):
        left_red[i] = left_red[i-1] + (1 if stone[i-1] == 'R' else 0)
    print(calc_min_op(n, stone, left_red))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

r = []
for i in range(len(c)):
    if c[i] == "R":
        r.append(i+1)

l = len(r)
ans = 0
for i in range(l):
    if r[i] > l:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(len(s) - 1):
    if s[i] + s[i + 1] == 'WR':
        ans += 1

r_num = s.count('R')
w_num = s.count('W')

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

print(min(r_num, min(w_num, change_num)))

Submission status: Accepted

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

white=[0]*(N+1)
red=[0]*(N+1)

for i in range(N):
    if C[i]=="W":
        white[i+1]=white[i]+1
        red[i+1]=red[i]
    else:
        white[i+1]=white[i]
        red[i+1]=red[i]+1

if white[-1]==N or red[-1]==N:
    print(0)
    exit()

ans=float("inf")
for i in range(1,N):
    wcnt=white[i]
    rcnt=red[-1]-red[i]
    m=max(wcnt,rcnt)
    ans=min(m,ans)

print(ans)

Submission status: Accepted

Solution code:
import sys

n = int(input())
s = input()
r = s.count('R')

print(r - s.count('R', 0, r))

Submission status: Accepted

Solution code:
a = int(input())
b = list(input())

c = b.count("R")
R_count = 0
change_count = 0

while R_count < c:
    if b[R_count] == "W":
        R_count +=1
        change_count +=1
    else : R_count +=1

print(change_count)

Submission status: Accepted

Solution code:
n = int(input())
color = list(input())
left = 0
right = len(color) - 1
ans = 0
while left<right:
    if(color[left]=='W' and color[right]=='R'):
        ans += 1
        left += 1
        right -=1
    if color[left] == 'R':
        left+=1
    if color[right] == 'W':
        right -=1
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
mo = 10**9+7

def main():
    N = II()
    C = IS()
    r = 0
    w = 0
    for c in C:
        if c=='R': r+=1
        else : w +=1
    ans = 0

    for c in C:
        if(r==0 or w==0):
            break
        else:
            if c == 'W':
                r-=1
                ans+=1
            if c == 'R':
                r-=1
    print(ans)




if __name__ == '__main__':
    main()