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())
s = input()
nw = len([i for i in s if(i == 'W')])
ns = n - nw
i = 0
j = n-1
c = 0
while(i < j):
	if(s[i] == 'W' and s[j] == 'R'):
		c += 1
		i += 1
		j -= 1
	elif s[i] == 'R':
		i += 1
	elif s[j] == 'W':
		j -= 1
print(min([c,nw, ns]))

Submission status: Accepted

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

right = N-1
left = 0
cnt = 0
for i in range(N):
    if right <= left:
        break
        
    if c[right] == "W":
        right -= 1
    else:
        if c[left] == "R":
            left += 1
        # 交換
        else:
            cnt += 1
            left += 1
            right -= 1

print(cnt)

Submission status: Accepted

Solution code:

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

l = 0
r = n - 1
cnt = 0

while l < r:
    if c[l] == 'W' and c[r] == 'R':
        cnt += 1
        l += 1
        r -= 1
        continue
    else:
        while c[l] != 'W' and l < n - 1:
            l += 1
        while c[r] != 'R' and r > 0:
            r -= 1

print(cnt)

Submission status: Accepted

Solution code:
#n,d=map(int,input().split())
#xy = [list(map(int,input().split())) for i in range(n)]
n=int(input())
c=list(input())
left=0
right=0
aru=0
for i in range(n):
  if c[i]=="R":
    aru+=1
dab=n-aru

left=0
right=aru
hantei=1000000000
for i in range(n):
  if c[i]=="R":
    right-=1
  else:
    left+=1
  if max(left,right)<hantei:
    hantei= max(left,right)
ans1=hantei
print(min(ans1,aru,dab))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
ban = list(input())
count = 0
Rcount = 0

for i in range(len(ban)):
    if ban[i] == "R":
        Rcount += 1

for i in range(len(ban)):
    if ban[i] == "R" and Rcount <= i:
        count += 1

print(count)

Submission status: Accepted

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

    r_count = 0
    w_count = 0

    for cc in c:
        if cc == 'R':
            r_count += 1
        else:
            w_count += 1

    ans = 0
    for cc in c[:r_count]:
        if cc == 'W':
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
R_frag = 0
W_frag = 0
ans = 0
r = n - 1
l = 0
while l < r:
    if c[l] == "R":
        l += 1
    if c[r] == "W":
        r -= 1
    if c[l] == "W" and c[r] == "R":
        c[l] = "R"
        c[r] = "W"
        ans += 1
        l += 1
        r -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
rnum = 0
okrnum = 0
wnum = 0
for i in range(N):
  if(c[i]=="R"):
    rnum += 1
  else:
    wnum += 1
if(rnum == 0 or wnum == 0):
  print(0)
else:
  for i in range(rnum):
    if(c[i]=="R"):
      okrnum += 1
  #print(rnum , wnum, okrnum)
  #print("ans =",rnum-okrnum)
  print(rnum - okrnum)

Submission status: Accepted

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

r_count = c.count("R")
w_count = c.count("W")

temp_c = c[r_count:]
print(temp_c.count("R"))

Submission status: Accepted

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


t = c.count("R")
print(t-c[:t].count("R"))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = []
w = []
for i in range(n):
    if s[i] == 'W':
        w.append(i)
    else:
        r.append(i)
ans = 0
for i in w:
    if len(r) == 0:
        break
    if i > r[-1]:
        break
    r.pop()
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
def next_i(i: int, S: str):
    while i < len(S):
        if S[i] == "W":
            return i
        else:
            i += 1
    return -1


def prev_i(i: int, S: str):
    while 0 <= i:
        if S[i] == "R":
            return i
        else:
            i -= 1
    return -1


def main():
    N = int(input())
    S = input()
    ans = 0

    left = next_i(0, S)
    right = prev_i(N - 1, S)

    while 0 <= left < right:
        ans += 1
        left = next_i(left + 1, S)
        right = prev_i(right - 1, S)

    print(ans)


main()

Submission status: Accepted

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

i = 0
j = N-1
count = 0 
frag = True
Frag_f = True
Frag_b = True
while (frag):
    if i >= j:
        break
    if Frag_f == True:
        if C[i] == "R":
            i = i + 1
        else:
            Frag_f = False
    if Frag_b == True:
        if C[j] == "W":
            j = j - 1
        else:
            Frag_b = False
    
    if Frag_b == False and Frag_f == False:
        count += 1
        Frag_f = True
        Frag_b = True
        i = i+1
        j = j-1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = input()
if c.count('R') == n:
    print(0)
elif c.count('W') == n:
    print(0)
else:
    MIN = min(n-c.count('R'),n-c.count('W'))
    for i in range(n):
        if c[i] == 'W':
            red = c[i:].count('R')
            MIIN = red-c[i:i+red].count('R')
            print(min(MIIN,MIN))
            break

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
R_num=c.count("R")
#print(R_num)
pri_R=c[:R_num].count("R")
#print(pri_R)
print(R_num-pri_R)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


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 main():
    n = II()
    t = input()
    conter = cl.Counter(t)
    num_w = conter["W"]
    num_r = conter["R"]
    w_in_r = 0
    r_in_r = 0
    for i in range(num_r):
        if t[i] == "W":
            w_in_r += 1
        else:
            r_in_r += 1
    r_in_w = num_r - r_in_r
    print(max(w_in_r, r_in_w))


main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
c = list(input())

ans = 0
l = 0
r = N-1
while l <= r:
    flag = False
    for i in range(l,N):
        if c[i] == 'W':
            l = i
            flag = True
            break
    if not flag:
        l = N

    flag = False
    for i in range(r+1)[::-1]:
        if c[i] == 'R':
            r = i
            flag = True
            break
    if not flag:
        r = -1

    if r < l:
        break
    
    c[l] = 'R'
    c[r] = 'W'
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
l,r=0,n-1
ans=0
while l<r:
    while c[l]!='W':
        l+=1
        if l>n-1:
            break
    while c[r]!='R':
        r-=1
        if r<0:
            break
    if l<r:
        ans+=1
        l+=1
        r-=1
print(ans)

Submission status: Accepted

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


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N = int(input())
    C = input()

    left = 0
    right = 1

    ans = 0

    while (left+right<N):
        if C[left]=='W':
            if C[-right]=='R':
                ans += 1
                left += 1
                right += 1
            else:
                right += 1
        else:
            left += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

N = int(input())
cs = list(input())


count_R = cs.count("R")
count_W = cs.count("W")

W_check = cs[: count_R]
R_check = cs[count_R:]

#print(W_check, R_check)

print(min(W_check.count("W"), R_check.count("R")))

Submission status: Accepted

Solution code:
N=int(input())
S= list(input())
W_count=S.count('W')
R_count=S.count('R')
count=0
if((W_count==0) or (R_count==0)):
    print(0)
else:
    list_test=['R']*R_count+['W']*W_count
    for i,j in zip(S,list_test):
        if(i!=j):
            count+=1
    print(count//2)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
array = list(S)
leftW = None
rightR = None
for i in range(N):
    if array[i] == 'W':
        leftW = i
        break
for i in range(N):
    j = N-i-1
    if array[j] == 'R':
        rightR = j
        break
if leftW == None or rightR == None:
    print(0)
    exit()
counter = 0
while leftW < rightR:
    array[leftW] = 'R'
    array[rightR] = 'W'
    for i in range(leftW, N):
        if array[i] == 'W':
            leftW = i
            break
    for i in range(rightR):
        j = rightR - i
        if array[j] == 'R':
            rightR = j
            break
    counter += 1

print(counter)

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
ans = 0
r = 0
l = N - 1
while r < l:
    if C[r] == 'W':
        while True:
            if C[l] == 'R':
                ans += 1
                l -= 1
                break
            else:
                l -= 1
                if r == l:
                    break
    r += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug  2 21:49:26 2020

@author: naoki
"""

N = int(input()) # N = the number of stones 
stones = input() # input the colors of stones 
count1 = stones.count("R") #the number of red stones 
stones2 = stones[count1:]
count2 = stones2.count("R")
print(count2)

Submission status: Accepted

Solution code:
n=int(input())
data=input()
cnt=0
wcnt=0
for i in data:
    if i=="R":
        cnt+=1
for i in range(cnt):
    if data[i]=="W":
        wcnt+=1
print(wcnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
C = list(input())
C1 = Counter(C)
ans = 0
for i in range(C1["R"]):
    if C[i] == "W":
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int (input())
s=list(input())

w_count = 0
r_count = 0

for item in s:
    if (item == "W"):
        w_count+=1
    elif (item == "R"):
        r_count+=1

target = ["R"] * r_count + ["W"] * w_count

ans_count = 0
for i in range(n):
    if (s[i] != target[i]):
        ans_count+=1
print (int(ans_count / 2))

Submission status: Accepted

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

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

r_count = c.count('R')
w_count = c.count('W', 0, r_count)

print(w_count)

Submission status: Accepted

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

a = c.count('R')
b = c[:a].count('W')
print(b)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
C = input()

i = 0
w = deque()
r = deque()
for c in C:
    if c == 'W':
        w.appendleft(i)
    else:
        r.append(i)
    i += 1

ans = 0
while len(w) != 0 and len(r) != 0:
    ww = w.pop()
    rr = r.pop()
    if ww > rr:
        break
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(S)
S_target = 'R' * c['R'] + 'W' * c['W']

ans = 0
for a, b in zip(S, S_target):
    if b == 'W':
        break
    if a != b:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
left = 0
right = len(c)-1
ans = 0
while left < right:
    while left < len(c):
        if c[left] == "W":
            break
        left += 1
    while right > -1:
        if c[right] == "R":
            break
        right -= 1
    if left < right:
        ans += 1
    left += 1
    right -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys

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

r = c.count("R")
if r == 0 :
    print(0)
    sys.exit()   
rs = c[:r]
rsw = rs.count("W")
a = rsw

w = list(reversed(c)).index("R")
w = (N - 1) - w
ws = c[:w]
b = ws.count("W")
print(min(a,b))

Submission status: Accepted

Solution code:
N = int(input())
c = list(str(input()))
cnt = 0
w = c.count("W")
r = N - w

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

Submission status: Accepted

Solution code:
n = int(input(''))
clist = input('')

wcnt=clist.count("W")
rcnt=clist.count("R")

clist1 = clist[0:rcnt]

wcnt2 = clist1.count("W")

print(wcnt2)

Submission status: Accepted

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

ans=20000000
num=0
for i in range(n):
     if c[i]=='W':
          num+=1
ans=min(ans,num)


l=0
for i in range(n):
     if c[i]=='W':
          l=i
          break
r=0
for i in range(n):
     if c[i]=='R':
          r=i

sum=0

while(l<r):
     sum+=1
     c[l]='R'
     c[r]='W'
     for i in range(l+1,n):
          if c[i]=='W':
               l=i
               break
     for i in range(r,0,-1):
          if c[i]=='R':
               r=i
               break
     

ans=min(ans,sum)
print(ans)

Submission status: Accepted

Solution code:
a = int(input())
l=list(input())
b = l.count("R")
s = 0
for i in range(b):
  if l[i]=="W":
    s = s+1
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
C = list(input())
# print(n)
# print(C)
l = 0
r = n-1
c = 0
while l < r:
    # print('l {} r {}'.format(l,r))
    # print('C[l] {} C[r] {}'.format(C[l],C[r]))

    if C[l]=='W' and C[r]=='R':
        C[l],C[r] = C[r],C[l]
        l += 1
        r -= 1
        c += 1
        # print('C {}'.format(C))

    elif C[l]!='W':
        l += 1
    elif C[r]!='R':
        r -= 1

print(c)

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:
N=int(input())
S=list(input())
RS=S[::-1]
#print(RS)
RSS=[]
F=0
W=0
R=0
for i in range(len(RS)):
    if F==0:
        if RS[i]=="R":
            RSS.append("R")
            R+=1
            F=1
    else:
        RSS.append(RS[i])
        if RS[i]=="R":
            R+=1
        else:
            W+=1

SS=RSS[::-1]
#print(SS,R,W)

LW=[]
LR=[]
for i in range(len(SS)):
    if SS[i]=="W":
        LW.append(i)
    else:
        LR.append(i)
LR=LR[::-1]


LW.append(10**7)
LR.append(-1)
#print(LW,LR)
for i in range(len(LR)):
    if LW[i]>LR[i]:
        cnt=i
        break
print(min(cnt,W,R))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
n = int(input())
color = list(input())

i = 0
j = n-1
count = 0

while(i<j):
  found1 = 0
  found2 = 0
  for k in range(n-1-i):
    if color[i+k] == 'W':
      i = i+k
      found1 = 1
      break
  for k in range(j):
    if color[j-k] == 'R':
      j = j-k
      found2 = 1
      break

  if found1*found2 == 0:
    break
  elif i>j:
    break
  else:
    count += 1
    i+=1
    j-=1
      

print(count)

Submission status: Accepted

Solution code:
import sys

n = int(input())

s = input()

count_w = s.count('W')
count_r = n - count_w

if count_w == 0 or count_r == 0:
    print(0)
    sys.exit()

sa = s[:count_r]
sb = s[count_r:]
num_sa = sa.count('W')
num_sb = sb.count('R')
if num_sa <= num_sb:
    print(num_sa)
else:
    print(num_sb)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
ans = 0
cnt = c.count("W")
ans = c[N-cnt:].count("R")
if cnt != 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
n = int(input())

s = input()

cnt = 0

for i in range(n):
    if s[i] == "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()
firstW = -1
for i in range(N):
    if C[i] == "W":
        firstW = i
        break
for i in range(N-1,-1,-1):
    if C[i] == "R":
        firstR = i
        break

def checkR(ri,wi):
    for i in range(ri,wi,-1):
        if C[i] == "R":
            return i
    else:
        return -1

def checkW(ri,wi):
    for i in range(wi,ri):
        if C[i] == "W":
            return i
    else:
        return -1


if len(set(C)) == 1:
    print(0)
elif firstW > firstR:
    print(0)
else:
    ri = firstR
    wi = firstW
    cnt = 0
    while 1:
        wi = checkW(ri,wi+1)
        ri = checkR(ri-1,wi)
        if wi == -1 or ri == -1:
            break
        cnt += 1
    print(cnt+1)

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
i=0
j=len(c)-1
ans=0
while i<j:
    while i<len(c) and c[i]=='R':
        i+=1
    while j>=0 and c[j]=='W':
        j-=1
    if i<j:
        ans+=1
        i+=1
        j-=1
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
import sys
n = int(input())
c = input()
wcnt = 0
for i in range(n):
    if(c[i] == 'W'):
        wcnt += 1
sw = 0
for i in range(len(c)-wcnt):
    if(c[i] == 'W'):
        sw += 1
print(min(min(wcnt, len(c)-wcnt), sw))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w,r=0,0
for i in range(n):
    if s[i]=='W': w+=1
    else : r+=1
c=0
for i in range(r):
    if s[i]=='R': c+=1
print(r-c)

Submission status: Accepted

Solution code:
n = int(input())
c = str(input())
# print(list(c))

import collections
a = collections.Counter(c)
t = a["R"]
# print(a)
m = min(a["W"],a["R"])
# print(m)

co = 0

for i,j in enumerate(c):
    if co >=m or i>=t:
        break
    else:

        if j == "W":
            co +=1
        # print(i,j)
        
# print(m,co)
print(min(m,co))

Submission status: Accepted

Solution code:
#! coding: utf-8

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

judged = True
red_flg = True
for c in C:
    if (red_flg == True) and (c == 'W'): red_flg = False
    if (red_flg == False) and (c == 'R'): judged = False

red_cnt = C.count('R')

if (judged):
    print(0)
else:
    print(C[red_cnt:].count('R'))

Submission status: Accepted

Solution code:
import heapq
N = int(input())
c = input()
w = []
r = []
for i, s in enumerate(c):
    if s == 'R':
        r.append(-i)
    else:
        w.append(i)
minrw = min(len(r), len(w))
        
heapq.heapify(r)
heapq.heapify(w)
ans = 0
while r and w:
    rq = -heapq.heappop(r)
    wq = heapq.heappop(w)
    if rq < wq: break
    else:
        heapq.heappush(r, -wq)
        heapq.heappush(w, rq)
        ans += 1
        if ans >= minrw:
            ans = minrw
            break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys
input = sys.stdin.readline
n = int(input())
c = str(input()).replace("\n","")
cl = list(c)
# print(cl)
rnum = cl.count("R")

if rnum == 0:
  print(0)
  sys.exit()

rnum_f = cl[:rnum].count("R")
print(min(rnum,rnum-rnum_f))

Submission status: Accepted

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

Submission status: Accepted

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

rc=l.count('R')

ltmp=l[:(rc)]
rctmp=ltmp.count('R')
print(rc-rctmp)

Submission status: Accepted

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

w = s.count('W')
r = s.count('R')
sw = s.count('W',0-w)

rw = 2*10**5
if r >=1:
  rw = w-sw
  
print(min(w,r,rw))

Submission status: Accepted

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


N=int(input())

T=input()

S=[]
Wcount=0
Rcount=0
for i in range(N):
    S.append(T[i])
    if T[i]=="W":
        Wcount+=1
    else:
        Rcount+=1

search_count=min(Wcount,Rcount)
ans=0

pos=0
Rpos=N-1

#print(S)

while ans<search_count and pos!=Rpos:
    if S[pos]=="W":
        while pos!=Rpos:
            if S[Rpos]=="R":
                #print("kita")
                S[pos]="R"
                S[Rpos]="W"
                ans+=1
                pos+=1
                break
            else:
                Rpos-=1
    else:
        pos+=1

#print(S)




print(ans)
#print(Wcount,Rcount)

Submission status: Accepted

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

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

rc = c.count('R')
wc = c.count('W')

if wc == 0 or rc == 0:
    print(0)
else:
    rstr = c[0:rc]
    wstr = c[rc:]
    print(max(rstr.count('W'), wstr.count('R')))

Submission status: Accepted

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

count = 0
left = 0
right = N-1
while True:
    while left <= N-1 and c[left] != 'W':
        left += 1
    while 0 <= right and c[right] != 'R':
        right -= 1
    if not left < right:
        break
    c[left] = 'R'
    c[right] = 'W'
    count += 1

print(count)

Submission status: Accepted

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

idxl=-1
idxr=N
cnt=0
# 単独ははじく

while(1):
  while(1):
    idxl=idxl+1
    if(idxl >= N):
      break
    if(c[idxl]=="W"):
      break
  if(idxl >= N):
    break
  while(1):
    idxr=idxr-1
    if(idxr <= 0):
      break
    if(c[idxr]=="R"):
      break
  if(idxr <= 0):
    break
#  print(idxl)
#  print(idxr)
  if(idxl>=idxr):
    break;
  cnt = cnt+1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
string = input()
Rcount = string.count('R')
fixRcount = string.count('R', Rcount, n)
print(fixRcount)

Submission status: Accepted

Solution code:
n = int(input())

c = list(input())

w_count = c.count("W")
r_count = c.count("R")

right_center = c[r_count:]
right_w_count = right_center.count("W")

print(w_count - right_w_count)

Submission status: Accepted

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

def main():
    n = int(input())
    cs = input()

    c = 0
    c_r = 0
    c_w = 0
    for i, c in enumerate(cs):
        if c == "R":
            c_r += 1
        else:
            c_w += 1
    if (c_r == 0) or (c_w == 0):
        print(0)
        return
    op = 0
    for i, c in enumerate(cs):
        if c_r == 0:
            break
        if c == "W":
            op += 1
        c_r -= 1
    print(op)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
C=input()
from collections import Counter
count=Counter(C)
#print(count)
countR=count["R"]
countW=count["W"]
missR=0
missW=0
for i in range(N):
    now=C[i]
    if now=="W" and i<N-countW:
        missW+=1
    if now=="R" and i>countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)

ans=min(missW, missR)+abs(missR-missW)

print(ans)
#print(missR, missW)

Submission status: Accepted

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

ans = 0
W = -1
for i in range(N):
    if S[i] == "W":
        W = i
        break
if W == -1:
    print(ans)
    sys.exit()

R = N
for i in range(N-1,-1,-1):
    if S[i] == "R":
        R = i
        break
if R == N:
    print(ans)
    sys.exit()

if(R<=W):
    print(ans)
    sys.exit()

ans += 1

while True:
    flagW = False
    flagR = False

    for i in range(W+1,R):
        if S[i] == "W":
            W = i
            flagW = True
            break
    
    for j in range(R-1, W, -1):
        if S[j] == "R":
            R = j
            flagR = True
            break
    
    if flagW and flagR:
        ans += 1
        continue
    else:
        print(ans)
        sys.exit()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


N=int(input())
List=input()
AList=list(List)
#print(AList)
#print(sorted(List))
RCNT=AList.count("R")
BList=sorted(List)
#print(List[0])

#print(RCNT)

ANUM=0
BNUM=0
ans=0
i=0
while i<N:
    if AList[i]=="R":
        ANUM+=1
    if BList[i]=="R":
        BNUM+=1
        
    #print(BNUM,ANUM)
    if AList[i]!=BList[i]:
        ans+=1
    if BNUM==RCNT or ANUM==RCNT:
        print(ans)
        quit()
    
    i+=1
    
print(ans)

Submission status: Accepted

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

r = S.count('R')
w = S.count('W')

if r == N or r == 0:
  print(0)
  exit()

a = S[:r].count('W')

print(a)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
c = list(input())
c_s = sorted(c)
count = 0
W = 0
R = 0

for i in range(n):
    if c_s[i] == 'R' and c[i] == 'W':
        W += 1
    if c_s[i] == 'W' and c[i] == 'R':
        R += 1
print(max(R,W))

Submission status: Accepted

Solution code:
N = int(input())
c = input()
lr_dict = dict()
lr_dict["W"] = {"R": 0,
                "W": 0
                }
lr_dict["R"] = {"R": 0,
                "W": 0
                }
n_r = 0
n_w = 0
for i in range(N):
    if c[i] == "W":
        n_w += 1
    else:
        n_r += 1
l_w = 0
r_r = 0
for i in range(n_r):
    if c[i] == "W":
        l_w += 1
for i in range(n_r, N):
    if c[i] == "R":
        r_r += 1
print(max(l_w, r_r))

Submission status: Accepted

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

ans = 0
w = 0
r = 0
j = N-1
for i in range(N):
    if c[i] == "W":
        while i < j:
            if c[j] == "R":
                ans += 1
                j -= 1
                break
            j -= 1
    if j - i == 1:
        break
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    cs = input()

    white_l = n
    for i in range(n):
        if cs[i]=='W':
            white_l = i
            break
    red_r = -1
    for i in range(n-1, white_l, -1):
        if cs[i]=='R':
            red_r = i
            break

    manu = 0
    while white_l < red_r:
        manu += 1

        for i in range(white_l+1, n):
            if cs[i]=='W':
                white_l = i
                break
        else:
            break

        for i in range(red_r-1, white_l, -1):
            if cs[i]=='R':
                red_r = i
                break
        else:
            break

    print(manu)

main()

Submission status: Accepted

Solution code:
n = int(input())
stones = list(input())
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
goal = 'R' * r_num + 'W' * w_num

index = 0
poplist = list()
for i in stones:
    if i == goal[index]:
        poplist.append(index)
    index += 1
for i in sorted(poplist, reverse=True):
    stones.pop(i)
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
print(min(w_num, r_num))

Submission status: Accepted

Solution code:
N=int(input())
d=input()
r=d.count('R')
w=N-r
ans=r
leftr,leftw,rightr,rightw=0,0,r,N-r
for i in range(N):
  if d[i]=='R':
    leftr+=1
    rightr-=1
  else:
    leftw+=1
    rightw-=1
  if leftw==rightr:
    f=rightr
  elif leftw<rightr:
    f=rightr
  else:
    f=leftw
  ans=min(ans,f)
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
c = input()

n = c.count("R")
ans = 0
for i in range(n):
	if c[i] != "R":
		ans += 1

print(ans)

Submission status: Accepted

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

arr =[]
for i in S:
    arr.append(i)


Rnum =arr.count('R')
Wnum =arr.count('W')

ans =0
for i in range(Rnum):
    if arr[i] =='W':
        ans +=1

print(ans)

Submission status: Accepted

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

rw_after=sorted(rw)

count=0
for i in range(N):
    if rw[i] != rw_after[i]:
        count+=1
print(int(count/2))

Submission status: Accepted

Solution code:
N = int(input())
stones = list(input())
left = -1
right = N
ans = 0
while left < right:
    l = left
    r = right
    for i in range(l+1,N):
        if stones[i] == "W":
            left = i
            break
    else:
        left = N
    for i in reversed(range(right)):
        if stones[i] == "R":
            right = i
            break
    else:
        right = -1
    if left < right:
        ans += 1
print(ans)

Submission status: Accepted

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

if "R" not in l or "W" not in l:
    print(0)
    exit()

cnt = 0

i = 0
k = n-1

while i < k:
    if l[i] == "W" and l[k] == "R":
        l[i], l[k] = l[k], l[i]
        cnt += 1
    if l[i] == "R":
        i += 1
    if l[k] == "W":
        k -= 1
print(cnt)