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 = 8
# S = "WRWWRWRR"
#
# N = 2
# S = "RR"
#
# N = 3
# S = "WWW"

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

def calculate(n, s):
    arr = list(s)

    wNum = arr.count("W")
    RNum = arr.count("R")

    if wNum == 0 or RNum == 0:
        print(0)
        return

    leftW = 0
    rightR = RNum



    result = []
    for i in range(n):
        if arr[i] == "R":
            rightR -= 1
        if arr[i] == "W":
            leftW += 1

        result.append(max(leftW, rightR))

    print(min(result))


calculate(N, S)

Submission status: Accepted

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

ans=0
i,j=0,N-1
while i<j:
    if s[i]=='R':
        i+=1
    elif s[j]=='W':
        j-=1
    else:
        ans+=1
        i+=1
        j-=1

print(ans)

Submission status: Accepted

Solution code:
import sys

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

#print(s)

k=0
i,j=0,N-1
while i<j:
    if s[i]=='R':
        i+=1
    elif s[j]=='W':
        j-=1
    else:
        s[i]='W'
        s[j]='R'
        k+=1
        i+=1
        j-=1

print(k)

Submission status: Accepted

Solution code:
n = int(input())
C = input()
cnt_w = 0
cnt_r = C.count('R')
ans = max(cnt_w, cnt_r)
for c in C:
    if c == 'W':
        cnt_w += 1
        temp = max(cnt_w, cnt_r)
        ans = min(ans, temp)
    if c == 'R':
        cnt_r -= 1
        temp = max(cnt_w, cnt_r)
        ans = min(ans, temp)

print(ans)

Submission status: Accepted

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

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

ans = 10**9
l,r = 0,R
for i in range(N+2):
  if 0 < i < N+1: 
    if C[i-1] == "R":
      r -= 1
    else:
      l += 1

  ans = min(ans, max(l,r))

print(ans)

Submission status: Accepted

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

R, L = 0, s.count("R")
times = max(R, L)
for c in s:
    if c == "W":
        R += 1
    else:
        L -= 1
    if times > max(R, L):
        times = max(R, L)
print(times)

Submission status: Accepted

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

a, b = 0, 0
a = C.count('R')
ans = a

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:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = readline().decode().rstrip()
W_cnt = S.count('W')
R_cnt = S.count('R')
ans = 0
for i in range(R_cnt):
    if S[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, C):
    C = list(C)
    ans = 0
    l = 0
    r = N - 1
    while l < r:
        while l < N and C[l] == 'R': l += 1
        while r >= 0 and C[r] == 'W': r -= 1
        if l < r:
            C[l], C[r] = C[r], C[l]
            ans += 1
    print(ans)

if __name__ == '__main__':
    input = sys.stdin.readline
    N = int(input())
    C = input().rstrip()
    main(N, C)

Submission status: Accepted

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

rc = s.count('R')

ans = 0
for stone in s:
    if stone == 'R':
        rc -= 1
    else:
        if rc > 0:
            rc -= 1
            ans += 1

print(ans)

Submission status: Accepted

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

count = 0
rui_red = [0 for _ in range(N)]
rui_white = [0 for _ in range(N)]
now_red = 0
now_white = 0
for i in range(N):
    if c[i] == "W":
        now_white += 1
        rui_white[i] = now_white
    if c[-1-i] == "R":
        now_red += 1
        rui_red[-1-i] = now_red


left = 0
right = N-1
for i in range(N+1):
    pre_left = left
    pre_right = right
    
    while rui_white[left] < i:
        left += 1
        if  left >= N:
             break
    while rui_red[right] < i:
        right -= 1
        if right < 0:
            break
    if left>=right:
        break
answer = i-1
count = 0
for i in range(pre_left+1, pre_right):
    if c[i] == "W":
        count+=1
    else:
        count-=1

print(answer)

Submission status: Accepted

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

count = 0
rui_red = [0 for _ in range(N)]
rui_white = [0 for _ in range(N)]
now_red = 0
now_white = 0
for i in range(N):
    if c[i] == "W":
        now_white += 1
        rui_white[i] = now_white
    if c[-1-i] == "R":
        now_red += 1
        rui_red[-1-i] = now_red

if N == 2 and c == "WR":
    print(1)
    sys.exit()

left = 0
right = N-1
for i in range(N):
    pre_left = left
    pre_right = right
    
    while rui_white[left] < i:
        left += 1
        if  left >= N:
             break
    while rui_red[right] < i:
        right -= 1
        if right < 0:
            break
    
    if left>=right:
        break
answer = i-1
count = 0
for i in range(pre_left+1, pre_right):
    if c[i] == "W":
        count+=1
    else:
        count-=1

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s = input()
ans = 0
for i in s:
	if i=="R":
		ans+=1

for i in range(0,ans):
	if s[i]=="R":
		ans-=1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math

#setrecursionlimit(10**6)
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')
MOD = 10**9 + 7

def main():
	n = int(rl())
	s = rll()[0]
	ans = 0
	A = [c for c in s]
	W = deque()
	for i, c in enumerate(A):
		if c == "W":
			W.append(i)
	for i in range(len(A)-1, -1, -1):
		if A[i] == "R" and W and W[0] < i:
			W.append(W.popleft())
			ans += 1
	print(ans)

	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())#ciが R のとき赤、W のとき白
a = c.count("R")
b = 0
count = max(a, b)
# a: R->W, b: W->Rの回数
for i in range(n):
  if c[i] == "R":
    a = a - 1
  else:
    b = b + 1
  ans = max(a, b)
  count = min(ans, count)
print(count)

Submission status: Accepted

Solution code:
_,s=open(0)
print(s[:s.count('R')].count('W'))

Submission status: Accepted

Solution code:
n,s,*w=open(0)
r=[-1]
n=int(n)
a=i=0
while i<n:
  if'W'>s[i]:r+=i,
  else:w+=i,
  i+=1
for w in w:
  if r.pop()>w:a+=1
  else:break
print(a)

Submission status: Accepted

Solution code:
import sys
N = int(input())
c = [str(x) for x in list(input())]
R = sum([1 for i, x in enumerate(c) if(x=='R')])
W = 0
if(R==N)or(R==0):
  print(0)
  sys.exit()
ans = R
i = 0
while True:
  if(i==N):
    print(ans)
    sys.exit()
  if(c[i] == 'W'):
    W += 1
  else:
    R -= 1
  n_ans = max(W,R)
  if(n_ans>ans):
    print(ans)
    sys.exit()
  ans = n_ans
  i += 1

Submission status: Accepted

Solution code:
n = input()
m = input()
maxr = m.count("R")
maxw = m.count("W")

min_move = len(m)
max_r = m.count("R")
max_w = m.count("W")
right = 0
left = 0
for i in range(len(m)):
    if m[i] == "W":
        left += 1
    else:
        max_r -= 1
    if max(left,max_r) < min_move:
        min_move = max(left,max_r)
if maxw == 0 or maxr == 0:
    print(0)
else:
    print(min_move)

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    for i in range(min(len(r_index),len(w_index))):
        w=w_index[ans]
        r=r_index[ans]
        if w-r>0:
             break
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
sc = sorted(c)
print(sum(c[i] != sc[i] for i in range(n)) // 2)

Submission status: Accepted

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

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

C_fin = ["R" if i < num_R else "W" for i in range(len(C))]

count = 0
for c, c_fin in zip(C, C_fin):
    if(c == c_fin):
        count += 1

ans = (len(C) - count)//2

print(ans)

Submission status: Accepted

Solution code:
n = int(input().strip())
a = bytearray(input().strip(), encoding='ascii')
h, t = 0, len(a) - 1

ans = 0
while h < t:
    while a[h] == ord('R') and h < t:
        h += 1
    while a[t] == ord('W') and h < t:
        t -= 1
    if h >= t:
        break
    if a[h] == ord('W') and a[t] == ord('R'):
        a[h] = ord('R')
        a[h] = ord('W')
        h += 1
        t -= 1
        ans += 1
    elif a[h] == ord('W') and a[t] == ord('W'):
        a[h] = ord('R')
        h += 1
        ans += 1
    elif a[h] == ord('R') and a[t] == ord('R'):
        t -= 1
        ans += 1
    else:
        assert False

print(ans)

Submission status: Accepted

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

d = list(c)

i = 0
j = N - 1
result = 0
while True:
    while i < N and d[i] != 'W':
        i += 1
    while j > 0 and d[j] != 'R':
        j -= 1
    if i == N or j == -1 or i >= j:
        break
    d[i] = 'R'
    d[j] = 'W'
    result += 1
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

if(stone == "R" * N):
    print(0)
elif(stone == "W" * N):
    print(0)
else:
    b = stone.count("R")
    c = stone[0:b].count("R")
    print(b-c)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
cuml = 0
count = []
for i in range(N):
  if s[i] == 'R':
    cuml += 1
  count.append(cuml)
if cuml == 0: print(0)
else: print(cuml - count[cuml-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
i=0;j=n-1;ans=0
s=list(input())
while j > i:
    if (s[i]=="W" and s[j]=="R"):
        s[i],s[j]=s[j],s[i]
        j-=1
        i+=1
        ans+=1
    elif s[i]=="W" and s[j]=="W":
        j-=1
    elif s[i]=="R" and s[j]=="R":
        i+=1
    elif s[i]=="R" and s[j]=="W":
        j-=1
        i+=1
print(ans)

Submission status: Accepted

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

a = 0
for i in range(N):
    if c[i] == 'R':
        a += 1
b = 0

ans = max(a,b)

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

Submission status: Accepted

Solution code:
n=int(input())
c=input()
r=c.count("R")
w=c.count("W")

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

Submission status: Accepted

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

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

if r==0 or w==0:
    print(0)

else:
    l=c.count('R', 0, r)

    print(r-l)

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

l = 0
r = N - 1
count = 0
while l < r:
    if C[l] == 'R' and C[r] == 'W':
        l += 1
        r -= 1
    elif C[l] == 'R' and C[r] == 'R':
        l += 1
    elif C[r] == 'W' and C[r] == 'W':
        r -= 1
    elif C[l] == 'W' and C[r] == 'R':
        l += 1
        r -= 1
        count += 1

print(count)

Submission status: Accepted

Solution code:
# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion
class union_find:
    def __init__(self, n):
        self.n = n
        self.rank = [0]*n
        self.parent = [int(j) for j in range(n)]
    
    def union(self,i,j):
        i = self.find(i)
        j = self.find(j)
        if self.rank[i] == self.rank[j]:
            self.parent[i] = j
            self.rank[j] += 1

        elif self.rank[i] > self.rank[j]:
            self.parent[j] = i
        else:
            self.parent[i] = j

    def find(self, i):
        temp = i
        if self.parent[temp] != temp:
            self.parent[temp] = self.find(self.parent[temp])
        return self.parent[temp]
from math import log2, ceil
from collections import deque
def main():
# Enter your code here. Read input from STDIN. Print output to STDOUT
    # for t in range(int(input())):
        
        n = int(input())
        # n,k  =[int(j) for j in input().split()]
        s = input()
        i=0
        j = n-1
        ans = 0
        while(i<j):
            if(s[i]=='R' and s[j]=='W'):
                i+=1
                j-=1
            elif(s[i]=='W' and s[j]=='R'):
                ans+=1
                i+=1
                j-=1
            elif((s[i]=='R' and s[j]=='R')):
                i+=1
            else:
                j-=1
        print(ans)
        


        # n,k,z  =[int(j) for j in input().split()]
        



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = input()
W = sum([1 for c in C if c == 'W'])
R = len(C) - W
print(sum([1 for c in C[R:] if c == 'R']))

Submission status: Accepted

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

nowL = 0 # 今見てる左側の位置
nowR = n-1 # 今見てる右側の位置
ans = 0 # swapした回数

while(True):
    while(nowL < n-1):
        # 左側で、右に移動しながら次のWを探す
        if(s[nowL]=='W'):
            break
        else:
            nowL+=1
    while(nowR > 0):
        # 右側で、次のRを探す
        if(s[nowR]=='R'):
            break
        else:
            nowR-=1
    if(nowR <= nowL):
        #見てる位置がぶつかったら探索終了
        break
    
    nowL+=1
    nowR-=1
    ans+=1
    
    
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
C = input()

r_count = C.count('R')
left_r_count = C[:r_count].count('R')
print(r_count-left_r_count)

Submission status: Accepted

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

ans = 0

r = c.count("R")
for i in range(r):
    if c[i] == "W":
        ans+=1
print(ans)

Submission status: Accepted

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

#i corresponds to the right side boundary of c[i]
for i in range(n):
  if c[i]=='W':
    w+=1
  if c[i]=='R':
    r-=1
  list[i+1]=max(r,w)
print(min(list))

Submission status: Accepted

Solution code:
def alter_altar(s):
    tmp = 'R' * s.count('R') + 'W' * s.count('W')
    ans = 0
    for i in range(len(s)):
        if s[i] == tmp[i]:
            pass
        else:
            ans += 1
    return ans//2
if __name__ == "__main__":
    _ = input()
    s = input()
    print(alter_altar(s))

Submission status: Accepted

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


# 赤の個数を数える
R = c.count('R')

# 白の個数を数える
W = c.count('W')

# 左から赤をすべて並べ、白はすべて右側に寄せることを考える
# 赤と白の石の間に仕切りを作ると仕切りの位置は左からR番目の石の右側にある
# つまり仕切りより左にある白い石の数だけ操作をすればよい

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

print(ans)

Submission status: Accepted

Solution code:
# rr..wwにする します

n = int(input())
c = list(input())
w_count = c.count("W")
if w_count == 0 or w_count == n:
    print(0)
    exit()
ans1 = 0
for i in range(n - 1, n - w_count - 1, -1):
    if c[i] == "R":
        ans1 += 1
print(ans1)

Submission status: Accepted

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

a = 0
b = 0
for i in range(N):
    if (c[i] == 'R'):
        a +=1
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:
n = int(input())
s = input()
number_of_W_in_prefix = [0]
number_of_R_in_suffix = [0]
for i in range(n):
    number_of_W_in_prefix.append(number_of_W_in_prefix[i] + (s[i] == 'W'))
    number_of_R_in_suffix.append(number_of_R_in_suffix[i] + (s[n - i - 1] == 'R'))
result = n
for k in range(n + 1):
    result = min(result, max(number_of_W_in_prefix[k], number_of_R_in_suffix[n - k]))
print(result)

Submission status: Accepted

Solution code:
N = int(input())

c = list(map(str, input()))

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

res = max(r, w)

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

    now = max(r, w)
    res = min(res, now)

print(res)

Submission status: Accepted

Solution code:
_=input();*C,=input();print(len([''for _ in range(C.count('W'))if C and C.pop()=='R']))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
c=input()
a=0
b=0
for i in range(N):
    if c[i]=='R':
        a+=1

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:
n = int(input())
C = list(input())
l, r, c = 0, n-1, 0
while l < r:
    if C[l] != 'W' and C[r] != 'R': l += 1; r -= 1; continue
    if C[l] != 'W': l += 1; continue
    if C[r] != 'R': r -= 1; continue
    l += 1
    r -= 1
    c += 1
print(c)

Submission status: Accepted

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

R, W = 0, 0  # 石の個数
red, white = [], []  # 石の位置
for i in range(N):
    if C[i] == 'R':
        red.append(i)
        R += 1
    else:
        white.append(i)
        W += 1

ans = 10**10
for i in range(N+1):  # 並べ終わったとき、0番目からi番目までRが並んでいる
    ans = min(ans, max(bisect.bisect_left(white, i),
                       R-bisect.bisect_left(red, i)))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
c = [char for char in c]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    stone_white_total, stone_red_total = C.count('W'), C.count('R')

    stone_white_left, stone_red_right = [0], [stone_red_total]
    stone_white, stone_red = 0, stone_red_total
    for stone in C:
        if stone == 'W':
            stone_white += 1
        else:
            stone_red -= 1
        stone_white_left.append(stone_white)
        stone_red_right.append(stone_red)

    return min([max(w, r) for w, r in zip(stone_white_left, stone_red_right)])

print(d_alter_altar())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = 0
r = 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:
import sys
read = sys.stdin.read
def main():
    n = int(input())
    a = input()
    if not 'WR' in a:
        print(0)
        sys.exit()
    a = list(a)
    rnum = a.count('R')
    rl = 0
    rr = 0
    for i1 in range(rnum):
        rl += a[i1] == 'W'
    for j1 in range(n - rnum):
        rr += a[rnum + j1] == 'R'
    r = max(rl, rr)
    print(r)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s =list(input())
curr = 0
l  = n-1
count = 0
while(l>=0 and l>curr):
    #print(l,curr,s)
    if s[l]=='R' and s[curr]=='R' and l==curr:
        l-=1
        curr+=1
    elif s[l]=='R' and s[curr]=='W':
        s[curr]='R'
        s[l]='W'
        count+=1
        l-=1
        curr+=1
    elif s[l]=='R' and s[curr]=='R':
        curr+=1
    else:
        l-=1
print(count)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
def main():
    n = int(input())
    a = input()
    if not 'WR' in a:
        print(0)
        sys.exit()
    a = list(a)
    rnum = a.count('R')
    r = 0
    for i1 in range(rnum):
        r += a[i1] == 'W'
    for j1 in range(n - rnum):
        r += a[rnum + j1] == 'R'
    r = r // 2
    print(r)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N=int(sys.stdin.readline().strip())
C=sys.stdin.readline().strip()
L=[0]
R=[0]
for c in C:
    if c=="W":
        L.append(L[-1]+1)
    else:
        L.append(L[-1])

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

ans=float("inf")
for i in range(N+1):
    ans=min(ans,max(L[i],R[i]))
print ans

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections
n = int(input())
c = input()

#方針は、全て同じ色にする or 左側にR右側にW
cnt = collections.Counter(c)
w_cnt = cnt['W']
r_cnt = cnt['R']
w_right_cnt = w_cnt
r_right_cnt = r_cnt
w_left_cnt = 0
r_left_cnt = 0
if w_cnt == n or r_cnt == n:
    print(0)
    exit()
ans = n
for i in range(n):
    #仕切りの両側のWとRの数を数える
    if c[i] == 'W':
        w_left_cnt += 1
        w_right_cnt -= 1
    if c[i] == 'R':
        r_left_cnt += 1
        r_right_cnt -= 1
    #解説PDFの・が2つの部分を調べる
    if w_left_cnt <= r_right_cnt:
        ans = min(r_right_cnt, ans)
    elif w_left_cnt > r_right_cnt:
        ans = min(w_left_cnt, ans)
print(ans)

Submission status: Accepted

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

R = C.count("R")

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
n = int(input())
c = input()
r = c.count("R")
w = c.count("W")
s = c[:r]
s = s.count("W")
print(s)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
n = int(input())
c = input()
r = c.count("R")
w = c.count("W")
s = c[:r]
s = s.count("W")
print(min(w, s))

Submission status: Accepted

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

i = 0
j = N-1
ans = 0
while True:
    if j <= i:
        break
    c = C[i]
    rc = C[j]
    if c=="W" and rc=="R":
        ans += 1
        i += 1
        j -= 1
    elif c=="W" and rc=="W":
        j -= 1
    elif rc=="R" and rc=="R":
        i += 1
    else:
        i += 1
        j -= 1
print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

i = 0
j = len(c)-1
cnt = 0

while i < j:
  if c[i] == "R":
    i += 1
    continue
  if c[j] == "W":
    j -= 1
    continue
  i += 1
  j -= 1
  cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
_w = c.count('W')
_r = c.count('R')
ans = min(_w, _r)
d = [0] * n # i番目までのwの個数
d[0] = 1 if c[0] == 'W' else 0
for i in range(n-1):
  d[i] = d[i-1] + 1 if c[i] == 'W' else d[i-1]
for i in range(1, n - 1):
  l = d[i-1] #c[:i].count('W')
  r = _r - (i-d[i-1])
  ans = min(ans,max(l,r))
print(ans)

Submission status: Accepted

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

r=C.count("R")
s=C[0:r].count("R")

print(r-s)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
n = int(input())
c = list(input())
s = Counter(c)
if s['R'] == 0 or s['W'] == 0:
  print('0')
  sys.exit()
pos_w=0
pos_r=n-1
count = 0
for i in range(pos_w, n):
  if c[i] != 'W':
    continue
  pos_w = i
  for j in range(pos_r, -1, -1):
    if c[j] != 'R':
      continue
    pos_r = j
    if pos_w > pos_r:
      print(count)
      sys.exit()
    c[i] = 'R'
    c[j] = 'W'
    count += 1
    break

Submission status: Accepted

Solution code:
N = int(input())
S = input()
part = 0
leftW = 0
rightR = S.count('R')
moves = [rightR]

for i in range(N):
	if S[i] == 'R':  # rightR:decrease
		rightR -= 1
	else:          # leftW:increase
		leftW += 1
	moves.append(max(leftW, rightR))
# print(moves)
ans = min(moves[i] for i in range(N+1))
print(ans)

Submission status: Accepted

Solution code:
_,t=open(0)
print(t[:t.count('R')].count('W'))

Submission status: Accepted

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

r = S.count("R")
T = S[0:r]

print(r-T.count("R"))

Submission status: Accepted

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

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

print(a-b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
s = input().strip()
print(s[:s.count('R')].count('W'))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()
c = LS2()

# 最終形は、R…RW…W

w = [0]*N  # i番目までにある'W'の個数
r = [0]*N  # i番目までにある'R'の個数
for i in range(N):
    if i == 0:
        if c[i] == 'W':
            w[i] = 1
        else:
            r[i] = 1
    else:
        if c[i] == 'W':
            w[i] = w[i-1]+1
            r[i] = r[i-1]
        else:
            w[i] = w[i-1]
            r[i] = r[i-1]+1

b = r[-1]  # 'R'の個数

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

print(min(max(w[i],b-r[i]) for i in range(N)))  # 境界がi番目

Submission status: Accepted

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

a = s.count('R')
b = s[:a].count('R')

print(a-b)

Submission status: Accepted

Solution code:
def main():
    input()  # n
    stones = input()
    reds = stones.count('R')
    print(stones[:reds].count('W'))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import ceil
n=int(input())
s=input()
lst=list(s)
i=0
j=n-1
c=0
while i<=j:
    if lst[j]=='R' and lst[i]=='W':
        temp=lst[j]
        lst[j]=lst[i]
        lst[i]=temp
        j-=1
        i+=1
        c+=1
        #print('1')
    elif lst[j]=='R' and lst[i]=='R':
        i+=1
        #print('2')
    elif lst[j]=='W' and lst[i]=='W':
        j-=1
        #print('3')
    elif lst[j]=='W' and lst[i]=='R':
        i+=1
        j-=1
        #print('4')
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

rCount=c.count('R')
wCount=c[0:rCount].count('W')
print(wCount)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
R_count = s.count("R")
 
new_s = s[R_count:N+1]
R_new = new_s.count("R")
print(R_new)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
W = 0
R = 0

for x in c:
  if x=='R':
    R += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

def solve():
    N = int(readline())
    C = readline().rstrip()

    reds = [i for i in range(len(C)) if C[i] == 'R']
    whites = [i for i in range(len(C)) if C[i] == 'W']

    i = 0
    n = min(len(reds), len(whites))
    while i < n and reds[-1-i] > whites[i]:
        i += 1
    
    print(i)

solve()