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: Time Limit Exceeded

Solution code:
N = int(input())
C = input()
A = []
for c in C:
    if c=='W':
        A.append(0)
    else:
        A.append(1)
l = 0
r = N - 1
ans = 0
while l < r:
    if not A[l] and A[r]:
        ans+=1
        l +=1
        r-=1
    elif not A[l]:
        r-=1
    elif A[r]:
        l+=1
print(ans)

Submission status: Time Limit Exceeded

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

n=int(input())
s=list(input().strip())
count = 0
for i in range(n):
    if s[i]=='W':
        for j in range(n-1,i,-1):
            if s[j]=='R':
                s[i],s[j]=s[j],s[i]
                count+=1
                break
        else:
            break
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=input()
wlist=[]
rlist=[]
for i in range(n):
  if c[i]=='R':
    rlist.append(i)
  else:
    wlist.append(i)
rlist.sort(reverse=True)
cnt=0
for j in range(n):
  if rlist==[] or wlist==[]:
    break
  if rlist[0]>wlist[0]:
    cnt+=1
    rlist.remove(rlist[0])
    wlist.remove(wlist[0])
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if "R" not in C:
    print(0)
    exit()
count = 0
for i in range(N):
    if C[i] == 'W':

        index = C.rfind('R', i)
        if index >= 0:
            count += 1
        c = list(C)
        c[i] = 'R'
        c[index] = 'W'
        C = "".join(c)

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    N = int(input())
    C = list(input().strip())
    last = N
    ans = 0
    for i in range(N):
        if C[i] == 'W':
            for j in reversed(range(i+1, last)):
                if C[j] == 'R':
                    ans += 1
                    last = j
                    break
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
c = list(input())
color = [i == 'R' for i in c]
ans = 0
checked = 0
for right in range(N - 1, -1, -1):
    if color[right]:
        for left in range(checked, right):            
            if not color[left]:
                color[right], color[left] = color[left], color[right]
                ans += 1
                checked = left
                break
print(ans)

Submission status: Time Limit Exceeded

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

Num = []
for i in range(N):
    if (c[i] == "R"):
        Num.append(0)
    else:
        Num.append(1)

count = 0

for i in range(N):
    for j in range(N - 1, i, -1):
        if (Num[i] > Num[j]):
            Num[i], Num[j] = Num[j], Num[i]
            count += 1

print(count)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in C:
        if c == 'R':
            red_cnt += 1

    for i in range(red_cnt):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Submission status: Accepted

Solution code:
import heapq

def main():
    N = int(input())
    C = list(input())
    R = []
    W = []
    for i, c in enumerate(C):
        if c == 'R':
            R.append(-i)
        else:
            W.append(i)
    if len(R) == 0 or len(W) == 0:
        print(0)
        exit()
    heapq.heapify(R)
    heapq.heapify(W)
    ans = 0
    while True:
        max_r = -1 * heapq.heappop(R)
        min_w = heapq.heappop(W)
        if max_r < min_w:
            break
        else:
            heapq.heappush(R, -min_w)
            heapq.heappush(W, max_r)
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ls=[0 for _ in range(N)]
for i in range(N):
  if S[i]=='W':
    ls[i]=1
ans=0
i=0
j=N-1
while True:
  if ls[i]==1 and ls[j]==0:
    ls[i]=0
    ls[j]=1
    i+=1
    j-=1
    ans+=1
  elif ls[i]==0 and ls[j]==0:
    i+=1
  elif ls[i]==1 and ls[j]==1:
    j-=1
  else:
    i+=1
    j-=1
  if i>=j:
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input().rstrip())

left_cur = 0
right_cur = n - 1
ans = 0
flag = False
while(left_cur < right_cur and left_cur < n - 1 and right_cur > 0):
    while(s[left_cur] == 'R'):
        left_cur += 1
        if (left_cur == n - 1):
            flag = True
            break
    while(s[right_cur] == 'W'):
        right_cur -= 1
        if (right_cur == 0):
            flag = True
            break
    if(left_cur > right_cur):
        flag = True
    if(flag):
        break
    ans += 1
    left_cur += 1
    right_cur -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

for x in range(n):
  if c[x] != c_goal[x]:
    count += 1
    
count = count // 2
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Answer = 0
White = 0
Red = S.count('R')
for j in range(len(S)):
    i = S[j]
    if i == 'W':
        White += 1
    elif i == 'R' and White >0 and j+1 > Red:
        Answer += 1
        White -= 1
print(Answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count('W')
r=s.count('R')

ans=10**9
if w==0 or r==0:
  print(0)
  exit(0)

left_white, right_red=0,r
for i in range(n):
  if s[i]=='W':
    left_white+=1
  else:
    right_red-=1
  ans=min(ans,max(left_white,right_red))
  
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
def main():
    n = int(input())
    rc = tuple(input())
    rc2 = [c == 'W' for c in rc]
    r1 = sum(rc2) # 白をすべて赤にした場合
    r2 = n - r1  # 赤をすべて白にした場合。
    r3 = sum(rc2[:r2]) # 赤を左につめるのに邪魔になる白の数
    print(min(r1, r2, r3))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    chars = readline().decode()

    cntW = [0]
    cntR = [0] 
    for i in range(N):
        cntW.append(cntW[i] + (1 if chars[i] == 'W' else 0))
        cntR.append(cntR[i] + (1 if chars[i] == 'R' else 0))

    ans = int(1e9)
    for i in range(N+1):
        leftW = cntW[i]
        rightR = cntR[N]-cntR[i] 

        if leftW <= rightR:
            tmp = leftW + (rightR - leftW)
        else:
            tmp = rightR + (leftW - rightR)
        ans = min(tmp, ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def abc174d():
    n = int(input())
    s = input()
    cnt = s.count('R')
    if cnt == len(s):
        print(0)
        return
    start = 0
    while s[start] == 'R':
        start += 1
        cnt -= 1
    ans = cnt
    for i in range(start, start+cnt):
        if s[i] == 'R':
            ans -= 1
    print(ans)
abc174d()

Submission status: Accepted

Solution code:
n = int(input())
c = 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:
#参考：https://marco-note.net/abc-174-work-log/
n = int(input())
c = input()

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

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

Submission status: Accepted

Solution code:
from sys import stdin

input = stdin.readline
import math

def solve():
    n = int(input())
    a = list(input().strip())
    left = 0
    rpos = [i for i in range(n) if a[i] == 'R']
    if not rpos:
        print(0)
        return
    right = len(rpos) - 1
    cnt = 0
    while right >= 0 and left < rpos[right]:
        if a[left] == 'W':
            cnt += 1
            right -= 1
        left += 1
    print(cnt)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import bisect

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

R_count = bisect.bisect_right(c_sorted, 'R')
ans = 0
for i in range(R_count):
    if c[i] != 'R':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    s = list(input())
    
    #完成品の並びにする
    ss = sorted(s)
    
    cnt = 0
    #切り替わりの地点まで＝Wが見つかる部分まで
    if "W" in s:
        sind = ss.index("W")

        #sindまでに含まれるWの数？
        cnt = 0
        for i in range(sind):
            if s[i] == "W":
                cnt += 1

    print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
R = C.count("R")
ans = 0

for r in range(R):
  if C[r]=="W":
    ans+=1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s = str(input())
r = 0
w = 0
d = 0
for i in range(n):
    if (s[i] == 'R'):
        r += 1
    else:
        w += 1
sd = sorted(s)
for i in range(n):
    if (s[i] != sd[i]):
        d += 1

if (w == 0 or r == 0):
    print(0)
else:
    print(min(r, w, int(d/2)))

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(C)
ans1 = min(cnt["R"], cnt["W"])

ans2 = 0
l, r = 0, N-1
while l < r:
    if C[l] == "W" and C[r] == "R":
        ans2 += 1
        l += 1
        r -= 1
    else:
        if C[l] == "R":
            l += 1
        if C[r] == "W":
            r -= 1

ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

def input(): return sys.stdin.readline()[:-1]
mod = 10**9 + 7
def readInt():return int(input())
def readIntList():return list(map(int,input().split()))
def readStringList():return list(input())
def readStringListWithSpace():return list(input().split())
def readString():return input()

n = readInt()
text = readStringList()
import collections
c  = collections.Counter(text)
if c['W'] == 0:
    print("0")
    exit()
i,j,count = 0,len(text)-1,0

while i <= j and i < len(text):
    if text[i] == 'W':
        while text[j] != 'R' and j > 0:
            j -= 1
        if i <= j and j > 0:
            text[i],text[j] = text[j],text[i]
            count += 1
    i += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

if cl[:cl.count('R')].count('W') != 0:
    print(min(cl[:cl.count('R')].count('W'), cl.count('R')))
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC174 D
import sys
sys.setrecursionlimit(3*10**5)

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

def search(l,r):
    # 半開区間
    if l==r:
        return 0
    if S[l]=='R':
        return search(l+1,r)
    if S[r-1]=='W':
        return search(l,r-1)
    return search(l+1,r-1)+1
print(search(0,N))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
res = 0
tem = s.find('WR')
if tem == -1:
    print(res)
else:
    i, j = 0, len(s)-1
    while i<j:
        while i < j and s[i] != 'W': i+=1
        while i < j and s[j] != 'R':j -= 1
        if s[i] == 'W' and s[j] == 'R':
            res += 1
            i += 1
            j -= 1
    print(res)

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:
from collections import Counter
N = int(input())
s = input()
c = Counter(s)
w, r = 0, c['R']
ans = max(w, r)
for i in range(N):
    if s[i] == 'W': w += 1
    else: r -= 1 
    ans = min(ans, max(w, r))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
r=[]
w=[]
for i in range(n):
  if c[i]=='R':
    r.append(i)
  else:
    w.append(i)
r.reverse()
ans=0
for i in range(min(len(r),len(w))):
  if r[i]>w[i]:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mat = lambda x, y, v: [[v]*y for _ in range(x)]
ten = lambda x, y, z, v: [mat(y, z, v) for _ in range(x)]
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
C = rs()

l, r = 0, N-1
ans = 0
while l < r:
	while C[l] == 'R' and l < N-1:
		l += 1
	while C[r] == 'W' and r > 0:
		r -= 1
	if l >= r: break
	l += 1
	r -= 1
	ans += 1
print(ans)

Submission status: Accepted

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

r_n = 0
ans = 0
for c in C:
    if c == 'R':
        r_n += 1

for i in range(r_n):
    if C[i] == 'W':
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
Rn = c.count("R") # 区切りの右側に存在するRの数
Wn = 0 # 区切りの左側に存在するWの数
ans = N
for i in range(N+1):
    if i == 0:
        ans = Rn
    else:
        if c[i-1] == "R":
            Rn -= 1
        else:
            Wn += 1
        ans = min(ans, min(Rn, Wn) + abs(Rn - Wn))
print(ans)

Submission status: Accepted

Solution code:
def solve(string):
    _, c = string.split()
    r = c.count("R")
    return str(c[:r].count("W"))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    n, c = string.split()
    n, c = int(n), list(c)
    wi, ri = 0, n - 1
    while wi < n and c[wi] == "R":
        wi += 1
    while ri >= 0 and c[ri] == "W":
        ri -= 1
    ans = 0
    while wi < n and ri >= 0 and wi < ri:
        c[wi], c[ri], ans = c[ri], c[wi], ans + 1
        wi, ri = wi + 1, ri - 1
        while wi < n and c[wi] == "R":
            wi += 1
        while ri >= 0 and c[ri] == "W":
            ri -= 1
    return str(ans)
 
 
if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def solve(string):
    n, c = string.split()
    n, c = int(n), list(c)
    wi, ri = 0, n - 1
    while wi < n and c[wi] == "R":
        wi += 1
    while ri >= 0 and c[ri] == "W":
        ri -= 1
    ans = 0
    while wi < n and ri >= 0 and wi < ri:
        c[wi], c[ri], ans = c[ri], c[wi], ans + 1
        wi, ri = wi + 1, ri - 1
        while wi < n and c[wi] == "R":
            wi += 1
        while ri >= 0 and c[ri] == "W":
            ri -= 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
n = int(input())
c = input()
p = []
for i in range(n):
  if c[i]=='R':
    p += [i]
print(len([1 for i in p if i>=len(p)]))

Submission status: Accepted

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

C = C.replace('W', '1').replace('R', '0')
C = list(C)
sort_C = sorted(C)

cnt = 0

for i in range(N):
    if C[i] != sort_C[i]:
        cnt += 1

print(math.ceil(cnt/2))

Submission status: Accepted

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

import math

rkosu=c.count("R")
wkosu=n-rkosu

ca=["R"]*rkosu+["W"]*wkosu

count=0
for i in range(n):
    if c[i] != ca[i]:
        count+=1

ans=math.ceil(count/2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li_c = input()
r_index = [i for i,x in enumerate(li_c) if x=="R"]
w_index = [i for i,x in enumerate(li_c) if x=="W"]
r_num = len(r_index)
w_num = len(w_index)

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

Submission status: Accepted

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

R_r = c.count("R")
W_l = 0
ans = N
for i in range(N+1):
  t = max(R_r,W_l)
  ans = min(ans, t)
  if R_r == 0:
    break
  if c[i] == "R":
    R_r -= 1
  else:
    W_l += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
n = I()
s = input()
C = Counter(s)
w,r = 0,0
for k,v in C.items():
    if k == 'W':
        w += v
    else:
        r += v
p = 'R' * r + 'W' * w
ans = 0
for i in range(n):
    if p[i] == s[i]:
        pass
    else:
        ans += 1
print(ans//2)

Submission status: Accepted

Solution code:
number = int(input())
date = input()
date = list(date)
cnt =0
cnt2 =0

for i in range(number):
  if date[i] =="R":
    cnt +=1

for s in range(cnt):
  if date[s] =="W":
    cnt2 +=1
print(cnt2)

Submission status: Accepted

Solution code:
n = int(input())

s = list(input())

count_red = s.count('R')
count_white = 0

A = []
for i in range(n):
    if s[i] == 'R':
        count_red -= 1
    if s[i] == 'W':
        count_white += 1
    A.append(max(count_red, count_white))

if len(set(s)) == 1 and list(set(s)) == ['W']:
    print(0)
else:
    print(min(A))

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

W_total = C.count("W")
R_total = N - W_total
w_cur = 0
r_cur = R_total
ans = R_total
cur = 0

for i in range(N):
    if C[i] == "W":
        w_cur += 1
    else:
        r_cur -= 1

    ans = min(ans, min(w_cur, r_cur) + abs(w_cur - r_cur))

print(ans)

Submission status: Accepted

Solution code:

# 初期入力
from bisect import bisect_left
import sys
#input = sys.stdin.readline  #文字列では使わない
N = int(input())
c =input().strip()
ans =0
r =[i for i, x in enumerate(c) if x == 'R'] #全体の中でRのIndex
r_num =len(r) #全体でRの数

ans =bisect_left(r,r_num) #呪われないためのRとWの境界
print(r_num -ans) #境界より右側のRの数

Submission status: Accepted

Solution code:
# 初期入力
from bisect import bisect_left
import sys
#input = sys.stdin.readline  #文字列では使わない
N = int(input())
c =input().strip()
ans =0
w =[i for i, x in enumerate(c) if x == 'W']
r =[i for i, x in enumerate(c) if x == 'R']
w_num =len(w) #全体でWの数
r_num =len(r) #全体でRの数

ans =bisect_left(r,r_num)
print(r_num -ans)

Submission status: Accepted

Solution code:
N=int(input())
c=input()
R=c.count('R')
RR=c[0:R].count('R')
print(min(R-RR,R,N-R))

Submission status: Accepted

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

    w_count = 0
    for i in range(n):
        if c[i] == 'W':
            w_count += 1

    w_count_ano = 0
    for i in range(w_count):
        if c[- 1 - i] == 'W':
            w_count_ano += 1

    count = w_count - w_count_ano
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
class Alter:
    def __init__(self, N, C):
        self.N = N
        self.C = C

        whites = 0
        for c in C:
            if c == "W":
                whites += 1
        self.white_count = whites
        self.red_count = N - whites

    def solve(self):
        result = 0
        if self.white_count == self.red_count:
            for i in range(self.red_count):
                if self.C[i] == "W":
                    result += 1
            print(result)
        elif self.white_count > self.red_count:
            for i in range(self.red_count):
                if self.C[i] == "W":
                    result += 1
            print(result)
        else:
            for i in range(self.white_count):
                w_i = self.N - i - 1
                if self.C[w_i] == "R":
                    result += 1
            print(result)

N = int(input())

c_line = input()
C = [None] * N
for i, c in enumerate(c_line):
    C[i] = c

alter = Alter(N, C)
alter.solve()

Submission status: Accepted

Solution code:
_,C=input(),input()
a=C.count('R')
print(a-C[:a].count('R'))

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
count = list()
W = 0
R = 0

for i in C:
    if i == "R":
        R += 1

if R == 0:
    count.append(0)

for i in C:

    if i == "W":
        W += 1
    else:
        R -= 1

    if W <= R:
        count.append(R)
    else:
        count.append(W)

print(min(count))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
C = input()

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

Submission status: Accepted

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

w = c.count("W")
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())
str = input()

r_sum = 0
for i in range(N):
    if str[i] == "R":
        r_sum += 1
r_sum2 = 0
for i in range(r_sum):
    if str[i] == "R":
        r_sum2 += 1

print(r_sum - r_sum2)

Submission status: Accepted

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

# それぞれの色の数をカウント
w = c.count("W")
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())
rednum=C.count('R')
rednumleft=0
for i in range(rednum):
    if C[i]=='R':
        rednumleft+=1
print(rednum-rednumleft)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
c = list(input())
num = Counter(c)
r = num["R"]

ans = ["R"]*r + ["W"]*(n-r)
cnt = 0

for i in range(n):
    if c[i] != ans[i]:
        cnt += 1
        
print(cnt//2)

Submission status: Accepted

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

def main():
    n = int(input())
    s = input()
    a_list = []
    for i in range(n):
        if s[i] == "W":
            a_list.append(0)
        else:
            a_list.append(1)
    r_count = sum(a_list)
    print(r_count - sum(a_list[0:r_count]))


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
c = stdin.readline()[:-1]

red = c.count("R")
ans = red
numwhite = 0
for i in range(n):
    numwhite += 1 if c[i] == "W" else 0
    numred = i - numwhite + 1
    resred = red - numred
    ans = min(ans,max(resred,numwhite))
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
c = stdin.readline()[:-1]

red = c.count("R")
ans = red
numwhite = 0
for i in range(n):
    numwhite += 1 if c[i] == "W" else 0
    numred = i - numwhite + 1
    resred = red - numred
    if resred == numwhite:
        ans = min(ans,resred)
print(ans)

Submission status: Accepted

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

y = 0
if x != 0:
    for j in range(x):
        if c[j] == "W":
            y += 1

print(y)

Submission status: Accepted

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

a = 0
b = 0

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

sx = len(set(x))
if sx == 1:
    print(0)
    exit()

cnt_r = x.count("R")
cnt = 0
for i in range(cnt_r):
    if x[i] == "W":
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
wr = input()
R_cnt = wr.count('R')
ans = wr.count('W',0,R_cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    rcount = s.count('R')
    diff = s[0:rcount].count("W")

    print(diff)




if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math, sys

N=int(input())
S=input()
R_count=0
if "R" not in S:
    print(0)
    sys.exit()
if "W" not in S:
    print(0)
    sys.exit()
if "R" not in S[S.index("W"):]:
    print(0)
    sys.exit()
for i in range(N):
    if S[i]=="R":
        R_count+=1

S=S[:R_count]
ans=S.count("W")
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
# cs = ['W' for i in range(200000)]
cs = input()

w_count = 0
for c in cs:
    if c == 'W':
        w_count += 1

if w_count == 0:
    print(0)
    exit()

rest = cs[-w_count:]

answer = 0
for c in rest:
    if c == 'R':
        answer += 1
print(answer)

Submission status: Accepted

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

i = 0
j = N - 1
count = 0

while t:
	if c[i] == "R":
		i += 1
	elif c[j] == "W":
		j -= 1
	elif c[i] == "W" and c[j] == "R":
		c[i] = "R"
		c[j] = "W"
		count += 1	
	if i >= j:
		t = False
		break
print(count)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    N = map(int, input().split())
    input_str = input()
    r_cnt=0;
    for s in input_str:
        # print(s)
        if s =='R':
            r_cnt+=1
    ans = 0
    for i in range(r_cnt):
        if input_str[i] == 'W':
            ans+=1
    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
WWRR"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
RR"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """8
WRWWRWRR"""
        output = """3"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n=int(input())
C = list(input())
ans = 0
N = n-1
for l in range(n):
    if C[l] == "W":
        for r in range(N,0,-1):
            if r<=l:
                print(ans)
                exit()
            if C[r] == "R":
                #print(C)
                C[l] = "R"
                C[r] = "W"
                #print(C)
                #print("-----------------")
                ans += 1
                N = r-1
                break
print(ans)

Submission status: Accepted

Solution code:
# D
from collections import Counter
n = int(input())
c = list(input())
d = Counter(c)
# print(d)
# print(d["R"])
e=Counter(c[:d["R"]])
# print(e["R"])
print(d["R"]-e["R"])

Submission status: Accepted

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

r_cnt=c.count('R')
w_cnt=c.count('W')

last_c='R'*r_cnt+'W'*w_cnt

num=0
for i in range(n):
  if c[i]!=last_c[i]:
    num+=1

print((num+2-1)//2)

Submission status: Accepted

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

r_cnt = A.count('R')

ans = 0
for i in range(r_cnt):
    if A[i] != 'R':
        ans += 1

print(ans)