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:
# -*- coding: utf-8 -*-
# 標準入力を取得
N = int(input())
c = input()

# 求解処理
w = 0
r = c.count("R")
ans = max(w, r)
for n in range(N):
    c_n = c[n]
    if c_n == "W":
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))

# 結果出力
print(ans)

Submission status: Accepted

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

r_m = c.count('R')
w_m = 0
out = max(r_m, w_m)
for i in range(n):
    if c[i] == 'W':
        w_m += 1
    else:
        r_m -= 1
    out = min(out, max(w_m, r_m))
print(out)

Submission status: Accepted

Solution code:
n = int(input())
c = ','.join(input()).split(',')

ans = 0

ri = -1

if c.count('W') == 0:
    print(0)
    exit()

if c.count('R') == 0:
    print(0)
    exit()

for i in range(n):
    if c[i] == 'W':
        while c[ri] != 'R':
            ri -= 1
        
        if n < i-ri:
            break
        
        c[i], c[ri] = 'R', 'W'
        ans += 1
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
wr = str(input())

r_num = wr.count("R")
i_counts = [r_num]
rw_count = r_num
wr_count = 0
for i in range(num):
  if wr[i] == "W":
    wr_count += 1
  else:
    rw_count -= 1
  i_count = max(wr_count, rw_count)
  i_counts.append(i_count)

ans = min(i_counts)

print(ans)

Submission status: Accepted

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())

if __name__=='__main__':
    n = I()
    c = list(S())
    i,j = 0,n-1
    ans = 0
    while i<j:
        while c[i]=='R':
            i += 1
            if i==n:
                break
        while c[j]=='W':
            j -= 1
            if j==-1:
                break
        if i<j:
            c[i],c[j] = c[j],c[i]
            i += 1
            j -= 1
            ans += 1
    print(ans)
    exit()

Submission status: Accepted

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

W = N-R

cnt = min(R, W)

if R > W:
    for i in range(0,cnt):
        if list[N-i-1]=="R":
            ans +=1

if R<=W:
    for j in range(0,cnt):
        if list[j] == "W":
            ans +=1


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
l = 0
r = n-1
c = 0
while l < r:
  for i in range(l, r):
    if s[i] == "W":
      c += 1
      l = i+1
      break
  else:
    l = r
  for i in range(r, l-1, -1):
    if s[i] == "R":
      r = i-1
      c += 1
      break
  else:
    r = l

print(c//2)

Submission status: Accepted

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

wc = 0
rc = N - 1

cnt = 0
while True:
    while wc < N and S[wc] == "R":
        wc += 1

    while rc >= 0 and S[rc] == "W":
        rc -= 1

    if wc == N or rc == -1 or wc > rc:
        break

    cnt += 1
    wc += 1
    rc -= 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
p = input()
w, r = 0,0
for i in range(n):
  if p[i] == "W":
    w += 1
  else:
    r += 1
ans = n
noww , nowr = 0,0
for i in range(n+1):
  ans = min ( ans , max(noww , r - nowr))
  if i < n and p[i] == "W":
    noww += 1
  else:
    nowr += 1
print(ans)

Submission status: Accepted

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

r_cnt = C.count('R')
w_cnt = 0
ans = 200000

if w_cnt <= r_cnt:
    ans = min(ans, r_cnt)
else:
    ans = min(ans, w_cnt)

for i in range(N):
    if C[i] == 'R':
        r_cnt -= 1
    else:
        w_cnt += 1

    if w_cnt <= r_cnt:
        ans = min(ans, r_cnt)
    else:
        ans = min(ans, w_cnt)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
R=c.count('R')
W=0
ans=max(R,W)
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 = input()

W = 0                  #仕切りより左側の白い石の個数
R = C.count('R')       #仕切りより右側の赤い石の個数

ans = max(W, R)

for i in range(N):
    if C[i] == 'W':    #仕切りの左側に'W'が移動する
        W += 1
    else:              #仕切りの左側に'R'が移動する
        R -= 1
    ans = min(ans, max(W, R))

print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/agc008/submissions/15248942
import sys
import math

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
c = input()

R = c.count('R')
left = c[:R]
ans = left.count('W')

print(ans)

Submission status: Accepted

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

# あかのインデックス
RR=[]

for i in range(n):
    if a[i]=='R':
        RR.append(i)


v=a.count('R')
ans=v
for i in range(len(RR)):
    if RR[i]+1<=v:
        ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
a, b = c.count('R'), 0
ans = max(a,b)

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

print(ans)

Submission status: Accepted

Solution code:
input()
a=input()
if 'W'not in a:print(0);exit()
a=a[a.index('W'):]
b=a.count('R')
print(b-a[:b].count('R'))

Submission status: Accepted

Solution code:
n=int(input())
c=input()
c=list(c)
a=sorted(c)
countw=0
countr=0

for i in range(n):
  if c[i]=='R' and a[i]=='W':
    countw+=1
  elif c[i]=='W' and a[i]=='R':
    countr+=1
    
ansmin=min(countw,countr)
ansmax=max(countw,countr)

print(ansmin+abs(ansmin-ansmax))

Submission status: Accepted

Solution code:
n = int(input())
c = input()
towhite = 0
tored = 0
#まずは石は全部白にすると考えた場合、赤を白にしないといけない数を計算（赤の数を数える）
for i in range(n):
    if c[i] == "R":
        towhite += 1
change = max(towhite, tored)
#仕切りを移動していく
for j in range(n):
    #仕切りを移動した先が赤なら変えなくていいからtowhiteから1引く。白なら仕切りの手前を赤に変えないといけないからtoredに1足す
    if c[j] =="R":
        towhite -= 1
    else:
        tored +=1
    #現在の仕切りでの操作数
    now = max(towhite, tored)
    #現在の仕切りでの操作数が少なくなったらchangeを更新
    change = min(change, now)

print(change)

Submission status: Accepted

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

def solve_fixed(p):
    w_cnt = 0
    r_cnt = 0

    for i in range(p):
        if(c[i] == "W"):
            w_cnt += 1
    
    for j in range(p,n):
        if(c[j] == "R"):
            r_cnt += 1
    
    return (w_cnt,r_cnt)

ans = 10**9
w_cnt,r_cnt = solve_fixed(0)

tmp = max(w_cnt,r_cnt)
ans = min(ans,tmp)
for i in range(n):
    
    if(c[i] == "W"):
        w_cnt += 1
    else:
        r_cnt -= 1

    tmp = max(w_cnt,r_cnt)
    ans = min(ans,tmp)
    

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
cs = input()
b=[]
for i in range(n):
  if cs[i] == "W":
    b.append(0)
  else:
    b.append(1)
a= sum(b)
ans = a- sum(b[:a])
print(ans)

Submission status: Accepted

Solution code:
#D - Alter Altar /
N = int(input())
C =[]
C = list(map(str,input()))
red_ko    = len(list(filter(lambda x :x == 'R',C)))
write_ko  = 0
total = max(write_ko,red_ko)
for i in range(N):
    if str(C[i]) == 'R' :
        red_ko -= 1
    else:
        write_ko += 1
    M = max(write_ko,red_ko)
    if total > M : total = M
# 出力
print(total)

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator

def solve():
    n = int(input())
    c = input()
    sushi = c.count("R")
    cnt = 0
    for i in range(sushi):
        if c[i] == "R":
            cnt += 1
    print(sushi - cnt)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

cand1 = cs[:cs.count('R')].count('W')
cand2 = cs[cs.count('W'):].count('R')
if cand1 < cand2:
  print(min(cand1, cand2))
else:
  print(max(cand1, cand2))

Submission status: Accepted

Solution code:
import numpy as np
N=int(input()) 
c=input()

#print(c.count('R'),c.count('W'))

ANS=np.zeros(N+1)
CR=np.zeros(N+1)
CW=np.zeros(N+1)

CR[0]=c.count('R')
CW[N]=c.count('W')

ANS[0]=c.count('R')
ANS[N]=c.count('W')


for i in range(1,N+1):
  if(c[i-1]=='R'):
    CR[i] = CR[i-1] - 1
    CW[i] = CW[i-1]
  else:
    CR[i] = CR[i-1]
    CW[i] = CW[i-1] + 1
  ANS[i]=max(CR[i],CW[i])

print(int(min(ANS)))
#print(CR,CW)

Submission status: Accepted

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


def main():
    N = input()
    C = input()

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

    print(a - b)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
print(len([i for i, j in zip(sorted(c), c) if i != j])//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    C = input()
    r = C.count("R")
    w = C.count("W")
    cnt = 0
    if w != 0:
        for i in range(r, N):
            if C[i] == "R":
                cnt += 1
        print(cnt)
    else:
        print(0)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

ans = r_cnt
r2w = r_cnt
w2r = 0

for c in C:
    if c == 'W':
        w2r += 1
    elif c == 'R':
        r2w -= 1
    ans = min(ans, max(r2w, w2r))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num = s.count("R")
L = s[:num]
ans = L.count("W")
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

str = list(input())

r = str.count('R')
c = N-r

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

Submission status: Accepted

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

white_left = [0 for _ in range(N+1)]
red_right = [0 for _ in range(N+1)]

for i in range(N):
  white_left[i+1] = white_left[i]
  if S[i] == 'W':
    white_left[i+1] += 1
    
for i in range(N):
  red_right[i+1] = red_right[i]
  if S[N-1-i] == 'R':
    red_right[i+1] += 1

ans = N
for i in range(N+1):
  ans = min(ans, max(white_left[i], red_right[N-i]))
print(ans)

Submission status: Accepted

Solution code:
def solve():
	n = int(input())
	s = input().strip()
	left = 0
	right = n-1
	total = 0
	while left < right:
		while left < right and s[left] == 'R':
			left+=1
		while left < right and s[right] == 'W':
			right-=1
		if left == right:
			break
		total+=1
		left+=1
		right-=1
	return total



print(solve())

Submission status: Accepted

Solution code:
num = int(input())
stn = list(input())
num_W = stn.count('W')
num_R = stn.count('R')
cnt = 0

if num_W >= num_R:
    for i in range(num_R):
        if stn[i] == "W":
            cnt += 1

else:
    for i in range(num_W):
        if stn[-i-1] == "R":
            cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
#Author: M@sud_P@rvez
from math import *

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

Submission status: Accepted

Solution code:
#abc174_d

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

r_cnt=c.count("R")
ans=c[:r_cnt].count("W")
print(ans)

Submission status: Accepted

Solution code:
num = int(input())
stn = list(input())
num_W = stn.count('W')
num_R = stn.count('R')
cnt = 0

if num_W >= num_R:
    for i in range(num_R):
        if stn[i] == "W":
            cnt += 1

else:
    for i in range(num_W):
        if stn[-i-1] == "R":
            cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
import sys
N = int(input())
word = input()
if word.count('W') == N:
  print(0)
  sys.exit()
num_R = word.count('R')
num_W,min_count= 0,float('inf')
for i in range(N):
  if word[i] == 'W':
    num_W += 1
  else:
    num_R -= 1
  min_count = min(min_count,max(num_W,num_R))
print(min_count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = s.count('R')
s = list(s)
i = 0
res = 0
while(i<cnt):
	if(s[i]=='W'):
		res+=1
	i+=1
print(res)

Submission status: Accepted

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

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


N = int(input())
s = list(input())

num_w = 0
for i in range(N):
    if s[i] == 'W':
        num_w += 1
num_r = N - num_w
# print(num_w, num_r)

# [解法]
# 例えば N = 4, s = WWRR を考える
# 条件を見たす並べ方は、
# RRRR, RRRW, RRWW, RWWW, WWWW
# の N+1 通り存在する
# ここで、以下のような N+1 通りの区切り d を考える
# d: 0   1   2   3   4
# i: | 0 | 1 | 2 | 3 |
# s: | W | W | R | R |
# すると、達成したいことは
# d より左側にある W の数 num_w_before_d を 0 にして、
# d より右側にある R の数 num_r_after_d を 0 にすることである
# これを達成するには、少なくとも max(num_w_before_d, num_r_after_d) の手数がかかる
# 従って、N+1 通りの d に対して max(num_w_before_d, num_r_after_d) を計算し、
# それらの最小値を求めれば良い


def count_w_before_d(num_w_before_d, d):
    if d == 0:
        return 0
    if s[d-1] == 'W':
        num_w_before_d += 1
    return num_w_before_d


def count_r_after_d(num_r_after_d, d):
    if d == 0:
        return num_r
    if s[d-1] == 'R':
        num_r_after_d -= 1
    return num_r_after_d

ans = INF
num_w_before_d = 0
num_r_after_d = num_r
for d in range(N+1):
    num_w_before_d = count_w_before_d(num_w_before_d, d)
    num_r_after_d = count_r_after_d(num_r_after_d, d)
    ans = min(ans, max(num_w_before_d, num_r_after_d))

print(ans)

Submission status: Accepted

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

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

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

def check(LW, RR):
    
    ct = min(LW, RR)

    if LW >= RR:
        ct += LW - RR
    elif LW < RR:
        ct += RR - LW

    return ct

a = Counter(c)
if not (a["W"] and a["R"]):
    print(0)
    exit()

L = Counter(c[:1])
R = Counter(c[1:])

LW = L["W"]
RR = R["R"]
mi = N
for i in range(1, N):
    mi = min(mi, check(LW, RR))
    
    if c[i] == "W":
        LW += 1
    else:
        RR -= 1
    
print(mi)

Submission status: Accepted

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

N = int(readline())
C = readline().rstrip()

ans = 0
left = 0
right = N - 1

while left < right:
  # Wを見つけるまで左からカーソル移動
  if C[left] == "R":
    left += 1
    continue
  while left < right and C[right] == "W":
    right -= 1
  if left == right:
    break
  ans += 1
  left += 1
  right -= 1
  
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# alter altar
length = int(input())
stone = input()
red = [0] * (length + 1)
white = [0] * (length + 1)
[red_num, white_num] = [0, 0]
for i in range(1, length + 1):
	if(stone[i-1] == "W"):
		white_num += 1
	if(stone[length-i] == "R"):
		red_num += 1
	white[i] = white_num
	red[length - i] = red_num
# print(white)
# print(red)
mini = 200000
for i in range(0, length+1):
	trial = max(white[i], red[i])
	mini = min(mini, trial)
print(mini)

Submission status: Accepted

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

whites = 0
reds = 0

for stone in stones:
    if stone == "W":
        whites += 1
    else:
        reds += 1

ops = 0
for stone in stones[N - whites :]:
    if stone == "R":
        ops += 1

print(ops)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = [i for i in input()]
w = C.count("W")
r = len(C)-w
B = ["R"]*r + ["W"]*w
ans = 0
for j in range(r):
    if C[j] != B[j]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = str(input())
Wsum = 0
Wright = 0
for i in range(N):
    if A[i] == "W":
        Wsum += 1
for i in range(N - Wsum):
    if A[i] == "W":
        Wright += 1
print(Wright)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
line = deque(list(input()))
num = 0
while True:
    if len(line) ==0:
        print(num)
        break
    if line[0] == "R":
        line.popleft()
        continue
    elif line[-1] == "W":
        line.pop()
        continue
    
    # now head is "W" and other "R" follows its right
    line.pop()
    line.popleft()
    num += 1

Submission status: Accepted

Solution code:
def q4():
    n = int(input())
    line = input()
    if 'WR' in line:
        r = line.count('R')
        safe = line[0:r].count('R')
    else:
        r = 0
        safe = 0
    print(r-safe)



if __name__ == "__main__":
    q4()

Submission status: Accepted

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

Submission status: Accepted

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

R = c.count("R")
W = len(c) - R

if R == 0 or W == 0:
  print(0)
  exit()

L_W = 0
ans = R+W
for i in range(N+1):
  maxWR = max(L_W,R)
  if ans > maxWR:
    ans = maxWR
  if L_W == W:
    break
  if c[i] == "R":
    R -= 1
  else:
    L_W += 1

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

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

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
from collections import Counter
w=Counter(c)["W"]
r=0
ans=w
for i in range(n-1,-1,-1):
    if c[i]=="W":
        w-=1
    else:
        r+=1
    ans=min(ans,max(r,w))
print(ans)

Submission status: Accepted

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

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

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

left = 0
right = n - 1
cnt = 0

while left < right:
  while left < right:
    if c[left] == "W":
      break;
    left += 1
  while left < right:
    if c[right] == "R":
      break;
    right -= 1
  if left < right:
    cnt += 1
  left += 1
  right -= 1
#

print(cnt)

Submission status: Accepted

Solution code:
n = input()
balls = list(input())

backindex = len(balls) - 1
frontindex = 0
actions = 0

while frontindex < backindex:
  if balls[frontindex] == 'W':
    while backindex > frontindex:
      if balls[backindex] == 'R':
        balls[frontindex] = 'R'
        balls[backindex] = 'W'
        actions += 1
        break
      backindex -= 1
  frontindex += 1

print(actions)

Submission status: Accepted

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

ans = 0
right = n-1
for i in range(n):
    if c[i] == "W":
        for j in range(right,-1,-1):
            if j <= i:
                print(ans)
                exit()
            if c[j] == "R":
                ans += 1
                right = j-1
                break
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8
n = int(input())
c = input()
red = c.count('R')
print(c[:red].count('W'))

Submission status: Accepted

Solution code:
import re
import sys

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

count_r = 0
for i in c:
    if i == 'R':
        count_r += 1
count_w = n - count_r
ans = min(count_r, count_w)
if ans == 0:
    print(0)
    sys.exit()

pattern = r"^R*W*$"
# print(re.fullmatch(pattern, "RRRWW"))
# print(re.search(pattern, "RRRWW"))
l = 0
r = n-1
count = 0
# while not re.match(pattern, ''.join(c)):
while l < r:
    while c[l] == 'R':
        l += 1
    while c[r] =='W':
        r -= 1
    if l >= r:
        break
    c[l] = 'R'
    c[r] = 'W'
    count += 1
    # print(c, l, r)
    

ans = min(count, ans)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
c = S()
# 最終的に RR...RWW...WWW という状態になる
# R の個数を r とすると、左から r 個の石のうち R はそのままで W だけ入れ替えたら良い
r = Counter(list(c))['R']
ans = 0
for elem in c[:r]:
    if elem == 'W':
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = input()
c = list(c)
arr = []
a = 0
b = c.count('R')
arr.append(max(a, b))
for i in range(n):
  if c[i] == 'W':
    a += 1
  elif c[i] == 'R':
    b -= 1
  arr.append(max(a, b))
ans = min(arr)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)

N = int(input())
c = "a" + input() + "a"

l = 0
r = len(c)-1

count = 0
while True:
    for i in range(1,N+5):
        if l+i == r:
            print(count)
            sys.exit()
        if c[l+i] == "W":
            break

    l = l + i

    for i in range(1,N+5):
        if l == r-i:
            print(count)
            sys.exit()
        if c[r-i] == "R":
            break

    r = r - i

    count += 1

Submission status: Accepted

Solution code:
N = int(input())
c = input()
a = c[0:len(c)//2]

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

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

Submission status: Accepted

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

# from numba import njit

from collections import Counter
# input = stdin.readline

# @njit
def solve(n,s):
  flag = False
  d = Counter(s)
  whiteCount = d["W"]
  redCount   = d["R"]
  res = min(s[:redCount].count("W"),s[redCount:].count("R"))
  
  return res



def main():
  N = int(input())
  s = input()
  print(solve(N,s))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

numR = c.count('R')
print(0 if numR == 0 or numR == len(c) else c[:numR].count('W'))

Submission status: Accepted

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

n_R = c.count('R')

m_R = c[:n_R].count('R')

print(n_R - m_R)

Submission status: Accepted

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

l,r=len(S),S.count('R')
print(0 if r==0 or r==l else S[:r].count('W'))

Submission status: Accepted

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

Submission status: Accepted

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

l = 0
r = N-1

while l < r:
    if s[l] == 'W' and s[r] == 'R':
        ans += 1
        l += 1
        r -= 1
    elif s[l] == 'W' and s[r] == 'W':
        r -= 1
    elif s[l] == 'R' and s[r] == 'R':
        l += 1
    else:
        l += 1
        r -= 1

print(ans)

Submission status: Accepted

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

w = c.count("W")
rr = c[-w:].count("R")

print(min(w,N-w,rr))

Submission status: Accepted

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

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

answer = total_count_R - left_count_R

print(answer)

Submission status: Accepted

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

dp = [0] * (N)

len_c_W = c.count('W')
len_c_R = c.count('R')
if (len_c_W == 0 or len_c_R == 0):
    print(0)
    exit()
cnt_W = 0
cnt_R = 0
for i in range(N):
    if c[i] == 'R':
        cnt_R += 1
    else:
        cnt_W += 1
    count = 0
    if (cnt_W <= (len_c_R - cnt_R)):
        count = len_c_R - cnt_R
    else:
        count = cnt_W

    dp[i] = count
print(min(dp))

Submission status: Accepted

Solution code:
'''
参考
https://qiita.com/tanakh/items/a312a9bd684658ab1e7b
'''
N = int(input())
C = list(map(str, input()))

left = 0
right = N - 1
ans = 0

while True:
    while left < N and C[left] != 'W':      # 左側から「白」が見つかるまでポインタをずらしていく
        left += 1
    while right >= 0 and C[right] != 'R':   # 右側から「赤」が見つかるまでポインタをずらしていく
        right -= 1
    if left >= right:                       # 停止条件
        break
    ans += 1                                # 見つかったら解をカウントアップ（赤白入れ替え）
    left += 1
    right -= 1

print(ans)

Submission status: Accepted

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


def main():
    n = int(input())
    c = input()
    r_count = c.count('R')
    ans = 0

    # コーナーケース: すべてWのとき
    if r_count == 0:
        print(0)
        exit()

    # 左側にすべてのRを寄せるイメージ
    # 左端からWをRに置き換える
    # ACはしたが、この考え方でよかったのか?
    for ci in c:
        if ci == 'W':
            ans += 1

        r_count -= 1

        if r_count == 0:
            print(ans)
            exit()

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n = int(input())
    c = input()
    r_count = c.count('R')
    ans = 0

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

    for ci in c:
        if ci == 'W':
            ans += 1

        r_count -= 1

        if r_count == 0:
            print(ans)
            exit()

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
c = input()
r = c.count('R')
ans = r - c[:r].count('R')
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    cs = input()
    color_list = []
    for i in range(n):
        if cs[i] == "W":
            color_list.append(0)
        else:
            color_list.append(1)
    r_count = sum(color_list)
    ans = r_count - sum(color_list[:r_count])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

W=[]
R=[]

for a_idx,a in enumerate(A):
    if a == 'W':
        W.append(a_idx)
    else:
        R.append(a_idx)
        
W.sort()
R.sort(reverse=True)


ans=0
for n in range(min(len(W),len(R))):
    if W[n] > R[n]:
        break
    ans+=1
    
print(ans)

Submission status: Accepted

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

import sys
input=sys.stdin.readline

n=int(input())
s=input()
ws=s.count('W')
rs=s.count('R')
ans=min(ws,rs)
posw=[]
posr=[]
for i in range(n):
    if s[i]=='W':
        posw.append(i)
    else:
        posr.append(i)
posr=posr[::-1]
for i in range(min(len(posw),len(posr))):
    if posw[i]>posr[i]:
        ans=min(ans,i)
        break
print(ans)