Problem:


Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3



Sample input:
4
WWRR


Sample output:
2

Submission status: Accepted

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

# 色を変えるのは、１つの石のみで、遅い
# 入れ替える場合はは、赤と白が左と右にある場合２つの石に効果を上げられるのでよい
# まずは入れ替えを考え、これ以上入れ替えれない（効果が少ない時）色を変える
# 理想は、wwww,,,RRRRと並ぶこと

def is_ok(c):
  s = True
  # どこかにW->Rの変化がある場合、false
  for i, p in enumerate(c[:-1]):
    if p == 'W':
      if c[i+1] == 'R':
        s = False
        break
  return s

if is_ok(c):
  print(0)
  exit()


# 下では最小となっていない
# 最小になるためには、Wだけ、Rだけにする方が早い場合もある

# WとRの数で、並べると最後の正解の並び方がわかり、それと最初との差分を取ることができるはず
Wn = len([x for x in c if x == 'W'])
Rn = len([x for x in c if x == 'R'])
# print(Wn, Rn)
c_r = 'R'*Rn+'W'*Wn
# print(c_r)
# print(c)

w_cnt = 0
for i in range(len(c)):
  if c_r[i] != c[i]:
    w_cnt += 1
# print(w_cnt)

# 入れ替えで最小にする場合の最小数
# 間違いが偶数だとそのまま割り算で答え、奇数だと1を足した操作数
if w_cnt % 2 == 0:
  a_c = int(w_cnt/2)
else:
  a_c = int((w_cnt+1)/2)
# print('a_c', a_c)

# WRRの場合は、WをRに変える方が早い
c_rr = 'R' * len(c)
r_cnt = 0
for i in range(len(c)):
  if c_rr[i] != c[i]:
    r_cnt += 1
a_r = r_cnt
# print('a_r', a_r)

print(min([a_c, a_r]))

# WWRWRRR

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
import collections

n = int(input())
stones = list(input())
c_stones = copy.copy(stones)
R_count = collections.Counter(c_stones)['R']
if R_count ==0:
  print(0)
else:
  c_stones.reverse()
  last_R = len(c_stones)-c_stones.index("R")
  R_left_count = stones[:R_count].count("R")
  print(R_count - R_left_count)

Submission status: Accepted

Solution code:
import sys
from typing import List


def main():
    def readline() -> str:
        s = sys.stdin.readline()
        return s.rstrip()

    n: int = int(readline())
    c: List[str] = list(readline())

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

    l_p: int = c.index("W")
    r_p: int = -1
    operations: int = 0

    while n > l_p - r_p:
        if c[l_p] == "W":
            if c[r_p] == "R":
                c[l_p] = "R"
                c[r_p] = "W"
                operations += 1
                l_p += 1
            r_p -= 1
        else:
            if c[r_p] == "W":
                r_p -= 1
            l_p += 1

    print(operations)
    # print("".join(c))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def read():
    N = int(input())
    C = input()
    return N, C


def fun(N, C):
    rs = C.count('R')
    crs = C[:rs].count('R')
    return rs - crs


if __name__ == '__main__':
    N, C = read()
    print(fun(N, C))

Submission status: Accepted

Solution code:
import math

def minOPs():
    N = int(input())
    s = input()
    count = 0
    L = 0
    R = N-1
    while(L < R):
        if(s[L] == 'W' and s[R] == 'R'):
            L+=1
            R-=1
            count+=1
        elif(s[L] == 'R' and s[R] == 'W'):
            L+=1
            R-=1
        elif(s[L] == 'R' and s[R] == 'R'):
            L+=1
        else:
            R-=1
    return(count)

if __name__ == '__main__':
    print(minOPs())

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

w = s.count("W")
r = s.count("R")

a = ""     # 入力
b = ""     # 正解
cnt = 0

for t in s:
    if t == "W":
        b = b + "0"
    else:
        b = b + "1"

for i in range(r+w):
    if i < r:
        a = a + "1"
    else:
        a = a + "0"
    if a[i] != b[i]:
        cnt += 1

cnt = cnt // 2

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
String=input().replace("W","1").replace("R","2")
c=[0]*N
for i in range (N):
    c[i]=int(String[i])
s=sorted(c)
import bisect
a=bisect.bisect_right(s,1)
cnt=0
for j in range(N-a):
    if c[j]==1:
        cnt+=1
print(cnt)

Submission status: Accepted

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

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

left, right = 0, len(c) - 1
cnt = 0

while left < right:

    if c[left] == c[right] == 'W':
        right -= 1
        continue

    elif c[right] == c[left] == 'R':
        left += 1
        continue

    elif c[left+1] == 'R' and c[left] == 'W':
        c[left] = 'R'
        left += 1

    elif  c[right-1] == 'W' and c[right] == 'R':
        c[right] = 'W'
        right -= 1

    elif c[left] is 'W' and c[right] is 'R':
        c[left] = 'R'
        c[right] = 'W'

    else:
        left += 1
        right -= 1
        continue

    left += 1
    right -= 1

    cnt += 1

print(cnt)

Submission status: Accepted

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

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

from_l=0
from_r=len(c)
rp=0
while 1:
    from_l=c.find('W',from_l)
    if from_l==-1:
        from_l=len(c)
    from_r=c.rfind('R',0,from_r)

    if from_l>=from_r:
        break
    rp+=1
    from_l+=1
    if from_l==len(c):
        break
    
print(min(r,w,rp))

Submission status: Accepted

Solution code:
from sys import exit
n = int(input())
stone = list(input())
w_num = stone.count("W")
r_num = stone.count("R")
if w_num == n or r_num == n:
    print(0)
    exit()

####
#stone = ["W"] * 100000 + ["R"] * 10000
####

def w_serch(entry_point):
    for i in range(n):
        try:
            pointer_left = entry_point + i
            if stone[pointer_left] == "W":
                return pointer_left
        except:
            return n

def r_serch(entry_point):
    for i in range(n):
        try:
            pointer_right = entry_point - i
            if stone[pointer_right] == "R":
                return pointer_right
        except:
            return -1

counter = 0
pointer_l = 0
pointer_r = n-1
for i in range(n // 2):
    pointer_l = w_serch(pointer_l)
    pointer_r = r_serch(pointer_r)
    #print(stone, pointer_l, pointer_r)
    if pointer_l > pointer_r:
        break
    else:
        stone[pointer_l] = "R"
        stone[pointer_r] = "W"
    #print(stone, pointer_l, pointer_r)
    counter += 1
print(counter)

Submission status: Accepted

Solution code:
#coding:utf-8

N = int(input())

C = str(input())

Lis = [0,0] #["R","W"]

for i in C:
    if i == "R":
        Lis[0] += 1
    else:
        Lis[1] += 1

D = "R" * Lis[0] + "W" * Lis[1]

Lis = [0,0]

for s,t in zip(C,D):
    if (s != t) and (s == "R"):
        Lis[0] += 1
    elif (s != t) and (s == "W"):
        Lis[1] += 1

print("{}".format(max(Lis)))

Submission status: Accepted

Solution code:
N = int(input())
c = list(map(str,input()))
w = []
r = []
for i in range(N):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)
k = 0
W = len(w)
R = len(r)
L = min([W,R])
for i in range(L):
  if w[i] < r[R - i - 1]:
    k += 1
print(k)

Submission status: Accepted

Solution code:
import datetime
import string
import re
import math

N=int(input())
input = input()
str = []
for i in range(N):
    str.append(input[i])
str.reverse()

count = 0
begin = 0
end = len(str) - 1
while True:
    if begin < len(str):
        while str[begin] == 'W':
            begin += 1
            if begin == len(str):
                break

    if end >= 0:
        while str[end] == 'R':
            end -= 1
            if end == -1:
                break

    if begin < end:
        str[begin] = 'R'
        str[end] = 'W'
        begin += 1
        end -= 1
        count += 1
    elif begin >= len(str) and end > -1:
        if end != len(str) - 1:
            if str[end + 1] == 'R':
                str[end] = 'W'
                end -= 1
                count += 1
            else:
                end -= 1
        else:
            end -= 1
    elif begin < len(str) and end <= -1:
        if begin != 0:
            if str[begin - 1] == 'W':
                str[begin] = 'R'
                begin += 1
                count += 1
            else:
                begin += 1
        else:
            begin += 1
    else:
        print(count)
        break

Submission status: Accepted

Solution code:
from collections import Counter

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

a = Counter(c)
b = c[::-1]

cnt = 0
for i in range(a['W']):
    if b[i] == "R":
        cnt += 1
        
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
R = (C.count('R'))
C2 = C[0:R]
R2 = (C2.count('R'))

print(R-R2)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    C = list(input())

    W = deque()
    R = deque()
    for i in range(n):
        if C[i] == "W":
            W.append(i)
        if C[i] == "R":
            R.appendleft(i)

    res = 0
    i = 0
    cnt = 0
    while R:
        if C[i] == "W":
            cnt += 1
        if cnt:
            r = R.popleft()
            w = W.popleft()
            C[r], C[w] = C[w], C[r]
            cnt -= 1
            res += 1
        else:
            R.pop()
        i += 1
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

ans = 0
while True:
    left_white_idx = S.find('W')
    right_red_idx  = S.rfind('R')

    if left_white_idx == -1 or right_red_idx == -1 or left_white_idx > right_red_idx:
        break

    # 左のRの連続とW　Rと右のWの連続を切り取った文字列に更新
    S = S[left_white_idx+1:right_red_idx]

    ans = ans + 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s1 = input()
s2 = s1[::-1]
i1 = 0
i2 = 0
ans = 0
while i1 < n:
  if s1[i1]=='W':
    while i2 < n:
      if s2[i2]=='R':
        ans += 1
        i2 += 1
        break
      elif i1+i2 >= n-2:
        break
      else:
        i2 += 1
  if i1+i2 >= n-2:
    break
  i1 += 1
print(ans)

Submission status: Accepted

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

R_count = C.count('R')
W_count = C.count('W')
# print(f'{R_count=}, {W_count=}')

if R_count == 0 or W_count == 0:
    print(0)
    exit()

# print(f'start {C=}')

i, j = 0, -1
cnt = 0
while i < N + j:
    if C[i] == 'W':
        while i < N + j:
            if C[j] == 'R':
                C[i] = 'R'
                C[j] = 'W'
                cnt += 1
                # print(f'{C=}, {i=}, {j=}')
                i += 1
                j -= 1
                break
            else:
                j -= 1
    else:
        i += 1

# print(f'end {C=}')
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()
R_count=0
w_count=0
for i in S:
  if(i=="R"):
    R_count+=1


for i in np.arange(R_count):
  if(S[i]=="W"):
    w_count+=1

print(w_count)

Submission status: Accepted

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

R_count = c.count("R")
W_count = c[:R_count].count("W")

print(W_count)

Submission status: Accepted

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

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

ref_c = ["R"]*r_cnt + ["W"]*w_cnt

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

ans = min(r_cnt, w_cnt, int(cnt/2))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
temparr = input()
dicts = {}
dicts["W"] = 0 
dicts["R"] = 0 
for i in temparr:
    dicts[i] += 1 

if dicts["W"] == 0 or dicts["R"] == 0 :
    print(0)
else:
    arr = []
    for i in temparr:
        arr.append(i)
    count = 0 
    index = 0 
    #print(arr)
    while dicts["R"]:
        if arr[index] == 'R':
            dicts["R"] -= 1 
        else:
            dicts["R"] -= 1 
            count += 1 
        index += 1
        #print(count)
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
cl = list(c)
wi = 0
wnum=0
ri = n-1
rnum=0
br=0
bw=0
res1 = n
break1 = 0
for i in range(n):
    while(cl[wi] == "R" and bw==0):
        wi+=1
        if(wi >= n-1):
            bw=1
            break
    if(cl[wi] == "W"):
        wnum+=1
    while(cl[ri] == "W" and br==0):
        ri-=1
        if(ri <= 0):
            br=1
            break
    if(cl[ri] == "R"):
        rnum+=1
    if(ri < wi and break1 == 0):
        res1 = i
        break1 = 1
    wi+=1
    if(wi >= n-1):
        wi=0
        bw=1
    ri-=1
    if(ri <= 0):
        ri=n-1
        br=1

    if(br*bw==1):
        break
print(min(res1, rnum, wnum))

Submission status: Accepted

Solution code:
#200802D
N = int(input())
#C = list(map(str,input().split()))
C = list(str(input()))

#print(C)
Rnum = C.count('R')

ans = 0
#print(C[0])

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
stone = input()
red_stone = stone.count("R")
white_stone = n - red_stone

left_red_stone = stone[:red_stone].count("R")
right_white_stone = stone[-white_stone:].count("W")

if left_red_stone == red_stone:
  print(0)
elif left_red_stone < right_white_stone:
  print(red_stone-left_red_stone)
else:
  print(white_stone-right_white_stone)

Submission status: Accepted

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

cnt = 0
w_last_index = N-1
for i in range(N):
  if c[i] == 'W':
    flag = True
    for j in range(w_last_index,i,-1):
      if c[j] == 'R':
        c[j] = 'W'
        w_last_index=j
        cnt+=1
        flag = False
        break
    if flag:
      break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

c = list(input())

n_r = c.count('R')
n_w = c[:n_r]
# print(n_w)
if 'R' in n_w:
    n_x = n_w.count('R')
else:
    n_x = 0

print(n_r - n_x)


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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

N = int(sys.stdin.readline().rstrip())
S = [x for x in input()]

C = Counter(S)

W = C["W"]
R = C["R"]

SS = S[:R]
CC = Counter(SS)
RR = CC["W"]

print(RR)

Submission status: Accepted

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

res = 0
left, right = c.find('W'), c.rfind('R')
while left != -1 and right != -1 and left < right:
    res += 1
    left += 1
    right -= 1
    while left < n and c[left] != 'W':
        left += 1
    while right >= 0 and c[right] != 'R':
        right -= 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0

rcnt = C.count('R')

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

print(ans)

Submission status: Accepted

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

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

true_list = 'R' * r_num + 'W' * w_num

if c_str == 'R' * N:
    print(0)
else:
    dif = 0
    for i in range(N):
        if c_str[i] != true_list[i]:
            dif += 1

    print(dif // 2)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
List=list(S)
flag=True
mid = ""
res = 0
n = List.count("R")
k=0
for i in range(n):
  if List[i] == "W":
    res += 1
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = list(input())
a = 0
B = []
for i in range(N):
    if A[i] == 'W':
        a += 1
    B.append(a)
ans = min(B[N - a - 1], a, N - a)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(str,input().split()))
c=list("".join(l))
w=c.count('W')
cc=c[-w:]
r=cc.count('W')
print(w-r)

Submission status: Accepted

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

S = 0
E = 1
Ans = 0

while S < N - E:
  while C[S] == 'R' and S < N-1:
    S += 1
    
  while C[E*-1] == 'W' and E < N:
    E += 1
    
  if S >= N - E:
    break
  else:
    S += 1
    E += 1
  
  Ans += 1

print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
c_list = list(input())
start = 0
end = n
ans = 0

while 1:
    for i in range(start, n):
        start = i
        if c_list[i] == "W":
            break
    for i in reversed(range(end)):
        end = i
        if c_list[i] == "R":
            break
    if start >= end:
        break

    c_list[start], c_list[end] = c_list[end], c_list[start]
    start += 1
    ans += 1

print(ans)

Submission status: Accepted

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

if words == ['R'] * n or words == ['W'] * n:
  print(0)
else:
  count = 0
  key = n - 1
  flag = False
  while words[key] != 'R':
    key -= 1
  for i in range(n):
    #print(key)
    if key == i or key == i - 1:
      break
    if words[i] == 'R':
      continue
    if words[i] == 'W':
      words[i], words[key] = words[key], words[i]
      count += 1
      key -= 1
      while words[key] != 'R':
        key -= 1
    #print(words)
  print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
N = int(input())
C = input()

ans = 0
i = -1
j = N
while( True ):
  
  i += 1
  while( i<j ):
    if( C[i] == 'W' ):
      break
    else:
      i += 1
  
  j -= 1
  while( j>i ):
    if( C[j] == 'R' ):
      break
    else:
      j -= 1
  
  if ( i<j ):
    ans += 1
  else:
    break

print(ans)

Submission status: Accepted

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

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

l = 0
r = len(C) - 1
ans = 0

while l < r:
    l = C.find('W', l)
    r = C.rfind('R', l, r + 1)
    #print('l = {}'.format(l))
    #print('r = {}'.format(r))
    if l == -1 or r == -1 or l > r:
        #print('kita')
        break

    l += 1
    r -= 1

    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = str(input())
r = c.count('R')
w = c.count('W')
rc = c[0:r].count('R')
lst = list(c)
ans = min(r - rc, w)
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

def main():
    N=int(input())
    C=list(input())
    res=0
    #WRRW WRWRW
    R=0
    W=0
    for i in range(len(C)):
        if C[i]=="W":
            W+=1
        else:
            R+=1
    for i in range(len(C)):
        if i<R:
            if C[i]=="W":
                res+=1
    print(res)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
c = input()
Wcou = 0
Rcou = 0
ans = 0
for i in range(N):
    if(c[i] == "W"):
        Wcou += 1
    else:
        Rcou += 1
tmp = ""
for i in range(Rcou):
    tmp = tmp + "R"
for i in range(Wcou):
    tmp = tmp + "W"
for i in range(N):
    if(tmp[i] != c[i]):
        ans += 1
print(str(ans//2))

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    _ = readint()
    c = readstr()
    '''
    WRWR -> RRWW
    RWRR -> RRRR
    WWWRR -> RRWWW
    WRWWR -> RRWWW
    WWRRWRRR -> RRRRRWWW
    WRRRWRRR

    c[:rc]までをすべてRにすればよい

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

w = []
r = []
for i in range(len(data)):
  if data[i] == "W":
    w.append(i)
  else:
    r.append(i)

ans = 0
if len(w) == 0:
  print(ans)
elif len(r) == 0:
  print(ans)
else:
  r = r[::-1]
  for i in range(min(len(w), len(r))):
    tmp_w = w[i]
    tmp_r = r[i]
    if tmp_w <= tmp_r:
      r.append(tmp_w)
      w.append(tmp_r)
      ans += 1
    else:
      break
  print(ans)

Submission status: Accepted

Solution code:
k = int(input())
s = list(input())

R_num=0
for i in range(k):
    if s[i]=="R":
        R_num+=1
count=0
for i in range(R_num):
    
    if s[i]=="W":
        count+=1
 
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
arr=[i for i in s]

ar=sorted(arr)


ans=0
for i in range(len(arr)):
    if arr[i]!=ar[i]:
        ans+=1
if ans%2==0:
    print(ans//2)
else:
    print((ans//2)+1)

Submission status: Accepted

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

rtow = c.count('R')
wtor = 0

anses = []
for i in range(n + 1):
  anses.append(max(rtow, wtor))
  if i == n:
    break
  if c[i] == 'W':
    wtor += 1
  else:
    rtow -= 1

print(min(anses))

Submission status: Accepted

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

a=list(a)

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

for i in range(0,t):
    if a[i]!="R":
        s=s+1
print(s)

Submission status: Accepted

Solution code:
from collections import Counter

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

l = 0
r = N - 1

cnt = 0
while l < r:
    while l < r and C[l] == 'R':
        l += 1

    while l < r and C[r] == 'W':
        r -= 1

    if l < r:
        cnt += 1

    l += 1
    r -= 1


d = Counter(C)
min_ = min(d['W'], d['R'])
print(min(min_, cnt))

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
ans = 0
FLG = False
j = N - 1
for i, color in enumerate(c):
    if color == "W":
        while c[j] == "W":
            j -= 1
            if j <= i:
                FLG = True
                break
        if FLG:
            break
        ans += 1
        c[i], c[j] = c[j], c[i]

print(ans)

Submission status: Accepted

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

if s.count('W')==0 or s.count('R')==0:
    print 0
    exit(0)
ans=-1
for i in xrange(n-1, -1, -1):
    if s[i]=='R':
        ans=s[:i].count('W')
        break
w=[]
r=[]
for i in xrange(n):
    if s[i]=='W':
        w.append(i)
for i in xrange(n-1,-1,-1):
    if s[i]=='R':
        r.append(i)

for i in xrange(min(len(w), len(r))):
    if w[i]>r[i]:
        ans=min(ans, i)

print ans

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
N = int(input())
Cn = list(input())

Cc = copy.copy(Cn)
if (Cn[0] == "R"):
    Rflg = True
else:
    Rflg = False
ans = 0
K = N-1    
allw = False
for i in range(N):
    if Rflg and Cc[i] == "R":
        continue
    else:
        Rflg = True
    if Cc[i] == "W":
        for j in range(K, i, -1):
            if Cc[j] == "R":
#                 print("change")
                ans += 1
                Cc[i], Cc[j] = Cc[j], Cc[i]
                K = j
#                 print(Cc)
                break
        else:
            allw = True

    else:
#         print("replace")
        ans += 1
        Cc[i] = "W"
#         print(Cc)
    if allw:
        break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
all_r=c.count('R')
l_w=0
l_r=0
ans=0
for i in range(n):
  if l_w==all_r-l_r:
      ans=l_w
      break
  if c[i]=='W':
    l_w+=1
  else:
    l_r+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
list1 = list(input())
RN = list1.count('R')
ii = 0
for i in range(N):
    if list1[i] == 'W' and RN != 0:
        RN += -1
        ii += 1
    elif list1[i] == 'R' and RN != 0:
        RN += -1
    elif RN == 0:
        break
print(ii)

Submission status: Accepted

Solution code:
X = int(input())
S = input().strip()
res = 0
for x, y in zip(S, sorted(S)):
    res += int(x != y)
print(res//2)

Submission status: Accepted

Solution code:
N = int(input())
clst = list(input())
cnt = 0
k = -1

if 'R' not in clst or 'W' not in clst:
    pass

else:
    rnum = [i for i, c in enumerate(clst) if c == 'R']
    wnum = [j for j, c in enumerate(clst) if c == 'W'][::-1]

    t = min(len(rnum), len(wnum))

    for i in range(1, t+1):
        if rnum[-i] > wnum[-i]:
            cnt += 1
        else:
            break

print(cnt)

Submission status: Accepted

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

ans = 0

red_index = c.rfind('R')
white_index = c.find('W')
if red_index < 0:
    print(0)
    exit()
if white_index >= red_index or white_index < 0:
    print(0)
    exit()
ans += 1

for i in range(N):
    red_end = red_index
    white_start = white_index + 1
    red_index = c.rfind('R', 0, red_end)
    white_index = c.find('W', white_start, N)
    if red_index < 0:
        break
    if white_index >= red_index or white_index < 0:
        break
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = [l for l in input()]
ideal = ['R']*(c.count('R')) + ['W']*(c.count('W'))

w_to_r=0
r_to_w=0
for i_, c_ in zip(ideal, c):
  if (i_=='R') and (c_=='W'):
    w_to_r+=1
  if (i_=='W') and (c_=='R'):
    r_to_w+=1

print(max(w_to_r, r_to_w))

Submission status: Accepted

Solution code:
n = int(input())
stones = input()
left = 0
right = n-1

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

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


class saidan:
    def __init__(self,n,cl):
        self.n = n
        self.cl = cl
        self.l = 0
        self.r = n

    def check(self):
        for i in range(self.l,n):
            if cl[i] == 0:
                self.l = i 
                break
        else:
            self.l = self.n-1 


        for i in range(n-self.r,n):
            if cl[self.n-1-i] == 1:
                self.r = self.n-i
                break
            else:
                self.r = 0

        return self.r - self.l

    def swap(self):
        # print('swap', self.l,self.r)
        # print(self.cl)
        cl[self.l], cl[self.r-1] = cl[self.r-1],cl[self.l]

n = ii()
c = input()

cl = []
for i in range(n):
    if c[i] == 'R':
        cl.append(1)
    else:
        cl.append(0)

sd = saidan(n,cl)

ans = 0
while 1:
    if sd.check() <= 1:
        break
    
    sd.swap()
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
red = []
white = []
s = input()

for i in range(n):
    if s[i] == "R":
        red.append(i)
    else:
        white.append(i)

roop_min_num = min(len(red), len(white))

res = 0
for i in range(roop_min_num):
    if white[i] < red[-i - 1]:
        res += 1
print(res)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
 
def main():
    n=int(input())
    c=list(input())
    hidari=0
    migi=n-1
    ans=0
    while True:
        while c[hidari] != 'W':
            hidari +=1
            if hidari == migi:
                break
        while c[migi] != 'R':
            migi -=1
            if hidari == migi:
                break
        if hidari == migi:
            break
        ans +=1
        hidari +=1
        if hidari == migi:
                break
        migi -=1
        if hidari == migi:
            break
    print(ans)

        
if __name__=='__main__':
    main()

Submission status: Accepted

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

w_num = c_array.count('W')
r_num = n-w_num
def get_ans(half):
    wl_num = c_array[:half].count('W')
    rr_num = c_array[half:].count('R')
    ans = min(wl_num, rr_num) + abs(wl_num-rr_num)
    return ans

ans = min(get_ans(r_num), w_num, r_num)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

from queue import Queue

n = int(input())
c = list(input())
# c = "W" * (10**5) + "R" * (10**5)
# c = list(c)


queue = Queue()
stack = []
for i in range(len(c)):
    if c[i] == "W":
        queue.put(i)
    else:
        stack.append(i)

def is_ok(c):
    for i in range(len(c)-1):
        if c[i] == "W" and c[i+1] == "R":
            return False
    return True

ans = 0
while True:
    if queue.empty() or stack == []:
        break
    i = queue.get()
    j = stack.pop()
    if j < i:
        break
    ans += 1

print(ans)

Submission status: Accepted

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

count = 0
for i in c:
    if i == "R":
        count += 1

count2 = 0
for i in c[:count]:
    if i == "R":
        count2 += 1

print(count - count2)

Submission status: Accepted

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

n=b.count('R')
m=b[0:n]
ans=m.count('W')
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
stones = input()
w = stones.count('W')
wend = stones[-w:].count('W')
print(w-wend)

Submission status: Accepted

Solution code:
N = int(input())
li = list(input())
an = 0
a = 0
b = len(li)-1
end = True

while True:
    while True:
        if a == len(li) -1 or a > b:
            end = False
            break

        if li[a] == 'W':
            break
        else:
            a += 1

    if end:
        while True:
            if b == 0 or a > b:
                end = False
                break

            if li[b] == 'R':
                break
            else:
                b -= 1

    if end:
        li[a] = 'R'
        li[b] = 'W'
        a+= 1
        b-= 1
        an += 1
    else:
        break
print(an)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
A = int(S.count("R"))
if A == N:
  print(0)
  exit()
s = S[:A]
B = int(s.count("R"))
print(A-B)

Submission status: Accepted

Solution code:
N = int(input())
L = [0]
for i in input():
    if i == 'R':
        L.append(1)
    else:
        L.append(0)

r = sum(L)
print(r - sum(L[1:r+1]))

Submission status: Accepted

Solution code:
n=int(input())
s=input().strip()
p=s.count('W')
c=0
for j in range(n-p,n):
    #print(j)
    if s[j]!='W':
        c=c+1
print(c)

Submission status: Accepted

Solution code:
n = int(input())
C = input()
cnt = 0

m = n
for i in range(n):
    if C[i] == 'W':
        for j in reversed(range(i,m)):
            if C[j] == 'R':
                cnt += 1
                m = j
                break
        else:
            break

print(cnt)

Submission status: Accepted

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

l = 0
r = N-1
ans=0

while True:
    while c[l] == 'R':
        l += 1
        if l==N:
            break

    while c[r] == 'W':
        r -= 1
        if r==-1:
            break

    if l-r == 1:
        print(ans)
        break
    else:
        c[l] = 'R'
        c[r] = 'W'
        ans += 1