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

cnt = 0
flag = 1
w_pos = 0
r_pos = 0
range_start = 0
range_end = n

while flag:
    for i in range(range_start, range_end):
        if c_list[i] == "W":
            w_pos = i
            range_start = i
            break
        if i == range_end-1:
            w_pos = range_end-1 
    for j in reversed(range(range_start, range_end)):
      if c_list[j] == "R":
        r_pos = j
        range_end = j
        break
      if j == range_start:
        r_pos = range_start 
    if w_pos < r_pos:
        c_list[w_pos] = "R"
        c_list[r_pos] = "W"
        cnt += 1
    else:
      flag = 0

print(cnt)

Submission status: Accepted

Solution code:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    C = input()

    counter = dict()
    for c in C:
        counter[c] = counter.get(c, 0) + 1
    r = counter.get(R, 0)

    _counter = dict()
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter.get(R, 0)

    ans = _r
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
K = int(input())
TMP = K

RW = list(input())
W=0
cnt=0

#print(RW)

while K >=0:
    if RW[K-1]=='W':
        K=K-1
        continue
    else:
        while W<K:
           # print(W,K)
            if RW[W]=="W":
                RW[K-1], RW[W] = RW[W], RW[K-1]
                #print(RW)
                cnt += 1
                # K=K-1
                break
            else:
                W=W+1
        K=K-1
        
#print(cnt)
K=TMP
sw = False
while K >=1:
    if sw == False:
        if RW[K-1]=="R":
            sw = True
    else:
        if RW[K-1]=="W":
            cnt += 1
            #print(cnt,K)

    K=K-1

print(cnt)

Submission status: Accepted

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

reds_cnt = S.count('R')
whites_in_reds = S[:reds_cnt].count('W')
print(whites_in_reds)

Submission status: Accepted

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

r = c.count('R')
w = c.count('W')
l = c[r:].count('R')
ans = min(r, w, l)
print(ans)

Submission status: Accepted

Solution code:
#!python3

import sys

def resolve():
    N = int(input())
    C = sys.stdin.readline().strip()

    ans = 0
    R = [0] * N
    W = [0] * N

    if C[0] == "W":
        W[0] += 1
    else:
        R[0] += 1

    for i in range(1, N):
        W[i] += W[i-1] + (1 if C[i] == "W" else 0)
        R[i] += R[i-1] + (1 if C[i] == "R" else 0)

    if W[-1] == 0 or R[-1] == 0:
        print(0)
        return

    print(W[R[-1]-1])

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
import math
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math as M
MOD=10**9+7
import random
#####################################
k=int(input())
s=input()
w=0
r=0
for i in s:
    if i=="W":
        w+=1
    else:
        r+=1
count=0
ans="R"*r + "W"*w
for i in range(k):
    if s[i]!=ans[i]:
        count+=1
print(int(M.ceil(count/2)))

Submission status: Accepted

Solution code:


import math

if __name__=="__main__":
    n=int(input())
    s=input()
    s=list(s)
    ##print(n,s)
    mid=n//2
    z=0
    leftcount=0
    rightcount=0
    for i in range(0,n):
        if(s[i]=="R"):
            z+=1
    for i in range(0,z):
        if(s[i]=="R"):
            leftcount+=1
            
    print(z-leftcount)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
sc = sorted(c)
nc = c.count("R")

cnt = 0
for i in range(nc):
    if c[i] != sc[i]:
        cnt += 1
print(cnt)

Submission status: Accepted

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

num_r1, num_r2 = 0, 0
num_w1, num_w2 = 0, 0
for i in range(int(N/2)):
  if c[i] == 'R':
    num_r1 += 1
  else:
    num_w1 += 1
for i in range(int(N/2), N):
  if c[i] == 'R':
    num_r2 += 1
  else:
    num_w2 += 1
#print('r:', num_r1, num_r2)
#print('w:', num_w1, num_w2)

count = 0
for i in range(num_r1+num_r2):
  if c[i] == 'W':
    count+=1
if count == 0:
  print(0)
else:
  print(count)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
c = np.array(list(input()))

count = 0

n_white = np.count_nonzero(c=='W')
n_red = np.count_nonzero(c=='R')

if n_red:
    if n_white:
        p_white = np.where(c == 'W')[0].tolist()
        p_red = np.where(c == 'R')[0].tolist()
        num = min(len(p_white), len(p_red))
        i = 0
        while (i < num):
            if p_white[i] < p_red[-1-i]:
                count += 1
            i += 1

print(count)

Submission status: Accepted

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

prev = S[0]
cur = 'R'
count = 0
wNum = 0
changedPoint = -1
changed = False
for i in range(N-1, -1, -1):
    cur = S[i]
    changed = False
    if cur == 'R':
        for j in range(wNum, i):
            wNum = j + 1
            if S[j] == 'W':
                changedPoint = j
                changed = True
                break
        if changed == True:
            count += 1
        elif changedPoint != -1 and wNum < i:
            count += 1
            break
        else:
            break

    prev = cur
    if wNum == i-1:
        break

print(count)

Submission status: Accepted

Solution code:
n = int(input())
s_w = input()
r_cnt = sw_cnt = 0

for s in s_w:
  if s == 'R':
    r_cnt += 1
    
for i, s in enumerate(s_w):
  if i>=r_cnt and s!= 'W':
    sw_cnt += 1
print(min(sw_cnt, r_cnt, n-r_cnt))

Submission status: Accepted

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

red = [i for i, x in enumerate(c) if x == 'R']
white = [i for i, x in enumerate(c) if x == 'W']

if len(white) == 0 or len(red) == 0:
    print(0)
    
else:
    red.sort(reverse=True)
    red_p = 0
    white_p = 0
    
    count = 0
    while(1):
        if white_p >= len(white):
            break
        elif red[red_p] < white[white_p]:
            break
        else:
            red_p += 1
            red.append(white[white_p])
            white_p += 1
            count += 1
            
    print(count)

Submission status: Accepted

Solution code:
N = int(input().strip())
C = [c for c in input().strip()]

if "W" not in C or "R" not in C:
    print(0)
    import sys; sys.exit(0)

cnt = 0

i = 0
j = len(C) - 1
while i < j:
    while C[i] == "R":
        i += 1
    while C[j] == "W":
        j -= 1
    if i > j:
        break
    cnt += 1
    C[i] = "R"
    C[j] = "W"
    i += 1
    j -= 1

print(cnt)

Submission status: Accepted

Solution code:
def main(N, C):
    counta_R = 0
    counta_W = 0
    for i, c in enumerate(C):
        if c == 'R':
            counta_R += 1
        else:
            counta_W += 1
    if counta_R == 0 or counta_W == 0:
        print(0)
        exit()
    ideal = 'R' * counta_R + 'W' * counta_W
    print(C.count('W', 0, counta_R))


if __name__ == '__main__':
    N = int(input())
    C = input()
    main(N, C)

Submission status: Accepted

Solution code:
def solve():
  N = int(input())
  S = input()
  red = [S.count('R')]*(N+2)
  white = [0]*(N+2)
  ans = float('inf')
  for i in range(N):
    red[i+2] = red[i+1]
    white[i+1] = white[i]
    if S[i]=='W':
      white[i+1] += 1
    if S[i]=='R':
      red[i+2] -= 1
  for i in range(N+2):
    if red[i]==white[i]:
      return red[i]
    if white[i]>red[i]:
      return red[i]
  return ans
print(solve())

Submission status: Accepted

Solution code:
N=int(input())
rw=list(input())
s=len(rw)
i=0
j=s-1
count=0

while i<j:
  while (j>=0 and rw[j]=='W'):
        j-=1
  while (i<=s-1 and rw[i]=='R'):
        i+=1
  if i>=j:
    break
  if rw[i]=='W' and rw[j]=='R':
      rw[i]='R'
      rw[j]='W'
      count+=1
  i+=1
  j-=1

j=s-1
while (rw[j]=='W' and j>=0):
      j-=1
i=0 
while (i<j):
  if (rw[i]=='W'):
      count+=1
  i+=1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1

ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if r >= 0 and cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Submission status: Accepted

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

left = 0
right = N + 1
count = 0

while left <= right:
    if c[left] == 'W' and c[right] == 'R':
        count += 1
        left += 1
        right -= 1
    if c[left] == 'R':
        left += 1
    if c[right] == 'W':
        right -= 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
var = 0
i = 0
j = 0
while True:
  if i+j >= N:
    break
  else:
    if c[i] == "W":
      if c[-1-j] == "R":
        var += 1
        i += 1
        j += 1
      else:
        j += 1
    elif c[i] == "R":
      i += 1
print(var)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
elif white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    c = list(input())
    j = N-1
    count = 0
    for i in range(N):
        if c[i] =='W':
            while i<j:
                if c[j]  == 'R':
                    c[i] = 'R'
                    c[j] = 'W'
                    count += 1
                    break
                j -= 1
        if j < i:
            break

    if c.count('W')<count:
        print(c.count('W'))
    elif c.count('R')<count:
        print(c.count('R'))
    else:
        print(count)

Submission status: Accepted

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

red = []
o = 1
r = 0
white = []
for i in c:
    if i == "W":
        white.append(o)
    if i == 'R':
       red.append(o);
       r += 1
    o+=1

w = n - r

rr = 0
for i in red:
    if i > r:
        rr += 1

print(rr)

Submission status: Accepted

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

r_num = c.count("R")
c_left = c[:r_num]
r_num_left = c_left.count("R")
print(r_num - r_num_left)

Submission status: Accepted

Solution code:
#from math import *
from collections import *
from random import *
from bisect import *
import sys
input=sys.stdin.readline
def lis():
    return list(map(int,input().split()))
def ma():
    return map(int,input().split())
def inp():
    return int(input())
n=inp()
s=input().rstrip('\n')
x=s.count('R')
if(x==0 or x==n):
    print(0)
    exit(0)
ind=[]
for i in range(n):
    if(s[i]=='R'):
        ind.append(i)
r=0
for i in range(x):
    if(s[i]=='R'):
        continue
    r+=1
print(r)

Submission status: Accepted

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
 
 
def main():
    N = int(input())
    S = input()
    l=0
    r=len(S)-1
    ans=0
    while(1):
        for i in range(l,len(S)):
            if S[i]=='W':
                l=i
                break
        for j in range(r,-1,-1):
            if S[j]=='R':
                r=j
                break
        if i<j:
            ans+=1
            l=i+1
            r=j-1
        else:
            break
    print(ans)

 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s=input()
isi=[]
for i in s:
    isi.append(i)

R_kosuu=isi.count("R")
irekaekosuu=0
for i in range(R_kosuu):
    if isi[i]=="W":
        irekaekosuu+=1


print(irekaekosuu)

Submission status: Accepted

Solution code:
s = int(input())
n = input()
str_list = list(n)
w_cnt = str_list.count('W')
r_cnt = str_list.count('R')
if w_cnt == 0 or r_cnt == 0:
    print(0)
else:
    w_result = str_list[r_cnt:]
    r_result = str_list[:r_cnt]
    r = w_result.count('R')
    w = r_result.count('W')
    if w <= r:
        print(w)
    else:
        print(r)

Submission status: Accepted

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

l = 0
r = n - 1
count = 0
while l < r:
    while l < n and cc[l] != "W":
        l += 1
    while 0 <= r and cc[r] != "R":
        r -= 1
    if 0 <= l < r < n:
        cc[l], cc[r] = cc[r], cc[l]
        count += 1
    else:
        break
print(count)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    c = input().rstrip('\n')
    r = c.count('R')
    s = 0
    for i in range(n):
        if c[i]=='R':
            w = c[s:i].count('W')
            ans += min(w,r)
            s = i
            r -= w+1
            if r <= 0:
                break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


cnt=0
Ws=s.count("W")
Rs=s.count("R")

for i in range(Rs):
    if s[i]=="R" :
        cnt+=1
        
if Rs-cnt<=N-Ws:
    if Rs-cnt<=N-Rs:
        print(Rs-cnt)

elif N-Ws<=N-Rs:
    print(N-Ws)

else:
    print(N-Rs)

Submission status: Accepted

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

left = 0
right = len(c)-1

count=0
if (right+1)%2==0:
    while (left-1)!=right:
        if c[left]=='R':
            left+=1 
        elif c[right]=='W':
            right-=1
        elif c[left]=='W' and c[right]=='R':
            left+=1
            right-=1
            count+=1
else :
    while left!=right:
        if c[left]=='R':
            left+=1 
        elif c[right]=='W':
            right-=1
        elif c[left]=='W' and c[right]=='R':
            left+=1
            right-=1
            count+=1


print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    Cs = list(input())
    #print(Cs)
    r = Cs.count('R')
    w = Cs.count('W')
    lis = ['R']*r +['W']*w
    #print(lis)
    count=0
    for i in range(len(lis)):
        if Cs[i]!=lis[i]:
            count+=1
    

    return int(count/2) if count%2==0 else int(count/2+1) 

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ls = []
for i in range(n):
    ls.append(s[i])
l = 0
r = n - 1
su = 0
while l < r:
    while ls[l] == 'R':
        l += 1
        if l >= n:
            break
    while ls[r] == 'W':
        r -= 1
        if r < 0:
            break
    if l >= r:
        break
    else:
        su += 1
        l += 1
        r -= 1
print(su)

Submission status: Accepted

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

def main():
    N = int(input())
    c = input()
    ans = 0
    j = N - 1
    for i in range(N):
        if c[i] == 'W':
            while j > i:
                if c[j] == 'R':
                    ans += 1
                    j -= 1
                    break
                j -= 1
            else:
                break
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 整数の入力
import numpy as np

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


def check_condition(S):
    N = len(S)
    ind = np.where(S=="R")[0]
    if len(ind) == 0 or len(ind) == N:
        return True
    
    if np.max(ind) == len(ind) - 1:
        return True
    
    return False

def alter(S):
    if check_condition(S):
        return 0
    ind = np.where(S=="R")[0]
    num_r = len(ind)
    num_offset = np.sum(ind > num_r - 1)
    return num_offset

S = np.array(list(S))
print(alter(S))

Submission status: Accepted

Solution code:
n=int(input())
C=list(input())
if C.count("W")==0:
    print(0)
elif C.count("W")==1:
    if C[-1]=="W":
        print(0)
    else:
        print(1)
else:
        tmpw=C.count("W")
        #print(tmpw,C)
        #if tmpw%2==0:
        C=C[(n-tmpw):]
            #print(tmpw,C)
        print(C.count("R"))
        #else:
            #C=C[tmpw-1:]
            #print(tmpw,C)
            #print(C.count("R"))

Submission status: Accepted

Solution code:
N = int(input())
S = []
c = input()
S.extend(c)

if "W" in S and "R" in S:
    t = S.count("R")
    S = S[:t]
    r = S.count("W")
    print(r)
else:
    print(0)

Submission status: Accepted

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

ans = 0
w_s = 0
r_s = 0
for i in range(n):
    if i <= r-1 and stone[i] ==  'W':
        w_s += 1
    elif i > r-1 and stone[i] == 'R':
        r_s += 1
ans = max(w_s, r_s)
print(ans)

Submission status: Accepted

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

ans = 0
w = 0
r = 0
for i in range(rcount):
    if C[i] == "W":
        if r > 0:
            r -= 1
            ans += 1
        else:
            w += 1
    j = N - i - 1
    if C[j] == "R":
        if w > 0:
            w -= 1
            ans += 1
        else:
            r += 1
ans += w
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
ans = 0
counta=0
a = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
	if c[i]=='R':
		a[i] = 1

w = a.count(0)
r = a.count(1)
for j in range(r):
	b[j] = 1
if a==b:
	pass
else:
	for k in a:
		counta+=1
		if k==0:
			ans+=1
		if counta==r:
			break
print(ans)

Submission status: Accepted

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc174/abc174_d
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    c = list(i2s())
    l = 0
    r = N - 1
    ans = 0
    while True:
        while l < N and c[l] != 'W':
            l += 1
        while r >= 0 and c[r] != 'R':
            r -= 1
        if l < r and c[l] == 'W' and c[r] == 'R':
            c[l] = 'R'
            c[r] = 'W'
            ans += 1
        else:
            break
    print(ans)
    return

main()

Submission status: Accepted

Solution code:

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

import sys
if "R" not in c or "W" not in c:
    print (0)
    sys.exit()
    
ans = 0
for i in range(N-1,-1,-1):

    if c[i] == "R":
        while c[li] != "W" and li <= i:
            li += 1
        if li >= i:
            break
        c[i] = "W"
        c[li] = "R"
        ans += 1
print (ans)

Submission status: Accepted

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

def main_chk():
    N=int(input())
    C=input()

    chk = N - 1
    cnt = 0

    for i in range(N):
        if C[i] == 'W':
            p = chk
            chk = i
            for x in range(N-i):
                if C[p-x] == 'R':
                    chk = p - x
                    break
            if chk <= i:
                break
            else:
                cnt = cnt + 1
                chk = chk - 1
                if chk <= i + 1:
                    break

    print(cnt)

main_chk()

Submission status: Accepted

Solution code:
n = int(input())
c = input()
l = 0
j = 0
a = [0]*n
b = [0]*n
for i in range(n):
  if(c[i]=='W'):
    a[l] =i+1
    l +=1
l = 0
for i in range(n-1,0,-1):
  if(c[i]=='R'):
    b[j] = i+1
    j +=1
j = 0
while(a[l] < b[l] and not(a[l]==0 or b[l] == 0)):
      a[l]=0
      b[l]=0
      l+=1
      j+=1
print(j)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
t = input()
*c, = map(int, t.replace(*"W1").replace(*"R0"))

"""
1100 -> 0101 -> 0011
00
10110100 -> 00111100 -> 00011110 -> 00001111
"""

a = 0
i, j = 0, 1
while i + j < n:
    if c[i] == 1 and c[-j] == 0:
        a += 1
        i += 1
        j += 1
    elif c[i] == c[-j] == 0:
        i += 1
    elif c[i] == c[-j] == 1:
        j += 1
    else:
        i += 1
        j += 1
print(a)

Submission status: Accepted

Solution code:
N = int(input())

C = input()

if N % 2 == 0:
  mid = N//2
else:
  mid = N//2+1

Rcount = C.count("R")
rcount = C.count("R", 0, Rcount)
print(Rcount-rcount)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    word = list(input())
    l, r = 0, n - 1
    cnt = 0
    while l < r:
        while l < r and word[l] == 'R':
            l += 1
        while l < r and word[r] == 'W':
            r -= 1
        if l < r:
            word[l], word[r] = word[r], word[l]
            cnt += 1
            l += 1
            r -= 1
    print(cnt)
  
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
stones = [l for l in input()]

cnt = 0
memo = N-1
for i in range(N):
    for j in range(memo, i , -1):
        if stones[i] == 'R':
            break
        else :
            if stones[j] == 'R':
                stones[i] = 'R'
                stones[j] = 'W'
                cnt += 1
                break
    memo = j
    if memo == i:
        break
            
print(cnt)

Submission status: Accepted

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

r_c = c.count('R')
w_c = c.count('W')

c_left = c[:r_c]
print(c_left.count('W'))

Submission status: Accepted

Solution code:
N=int(input())
s=input()
l=[]
c=0#Rの数
for i in range(N):
  l.append(s[i])
  if s[i]=="R":
    c+=1
a=0
for i in range(c):
  if l[i]=="W":
    a+=1
print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# template: v1.3.1

import io
import sys
import math

### DEBUG ###
_DEB = 0  # 1:ON / 0:OFF

_INPUT = """\
200000
"""+("R"*200000)
_EXPECTED = """\
2

"""
if _DEB:
    sys.stdin = io.StringIO(_INPUT)
    print("!! Debug Mode !!")

def logd(str):
    if _DEB: print(f"[deb] {str}")


### INPUT ####
n = int(input())
c = input()
c=c.replace("R","0")
c=c.replace("W","1")
c=[int(x) for x in c]

### Solve ####
def solve():
    # Process
    ans=0
    id1=0
    for i in reversed(range(n)):
        if id1 > i:
            break
        id0=-1
        if c[i] == 0:
            id0=i
            not_found=True
            for j in range(id1,id0):
                if c[j]==1:
                    id1=j
                    c[i]=1
                    c[j]=0
                    ans+=1
                    not_found=False
                    break
            if not_found:
                break
                
            if[i]==0:
                c[i]=1
                ans+=1
        

    # OUPUT
    ans = str(ans)
    print(ans)
    return ans

### MAIN ###
if __name__ == "__main__":
    ans = solve()

    # Checker
    if _DEB:
        if _EXPECTED.strip() == ans.strip(): print("\n!! Success !!")
        else: print(f"\n!! Failed... !!\nANSWER:   {ans}\nExpected: {_EXPECTED}")

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
C = input()
flag = False
cnt = 0
for i in range(n):
    if not flag and C[i] == 'W':
        flag = True
    if flag:
        if C[i] == 'R':
            cnt += 1
        # elif C[i] == 'W':
        #     if i + 1 < n:
        #         if C[i+1] == 'W':
        #             pass
        #         else:
        #             cnt = max(cnt-1,0)
        #     else:
        #         cnt = max(cnt-1,0)
r = C.count('R')
w = n-r
nya = 0
for i in range(r,n):
    if C[i] == 'R':
        nya += 1
pya = 0
for i in range(r):
    if C[i] == 'W':
        pya += 1
a = 0
for i in range(n-1):
    if C[i] == 'W' and C[i+1] == 'R':
        a += 1
print(min(max(a,cnt), nya, pya))

Submission status: Accepted

Solution code:
from collections import deque

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

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
  if(len(rd) == 0 or len(wd) == 0):
    break
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l_s = len(s)
l = [0 for x in range(l_s)]
for i in range(l_s):
    if s[i] == 'W':
        l[i] = 1
    else:
        l[i] = 0
p_0 = l_s-1
p_1 = 0
cnt = 0
while (p_0 >= p_1+1):
    while (l[p_0] != 0 and p_0 != 0):
        p_0 -= 1
    while (l[p_1] != 1 and p_1 != l_s-1):
        p_1 += 1
    if p_0 > p_1:
        cnt += 1
    p_0 -= 1
    p_1 += 1
print(cnt)

Submission status: Accepted

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

count = 0

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

Submission status: Accepted

Solution code:
n=int(input())
a=input()
con=0
ans=n
for i in range(n):
  if i==ans:
      break
  if a[i]=="W":
    con+=1
    while ans>=0:
      ans-=1
      if i==ans:
        print(con-1)
        exit()
      if a[ans]=="R":
        break  
print(con)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
w=[i for i, x in enumerate(c) if x == 'W']
r=[i for i, x in enumerate(c) if x == 'R']
wc=len(w)
rc=len(r)
wt=list(range(rc,wc+rc))
tand = set(w) & set(wt)
print(wc-len(tand))

Submission status: Accepted

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

Submission status: Accepted

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

cR = c.count('R')
cW = c.count('W')


if all([i == 'R' for i in c[:cR]]):
    print(0)
    exit()

if all([i == 'W' for i in c[cR:cR+cW]]):
    print(0)
    exit()

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

print(ans)

Submission status: Accepted

Solution code:
numStn = int(input())
stnData = list(input())

ttlW = stnData.count("W")
ttlR = stnData.count("R")

cnt = ttlR - stnData[:ttlR].count("R")

print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
arr = input()

dp = [[0] * (n + 1) for _ in range(2)]
dp[1][0] = arr.count("R")

for i in range(1, n + 1):
    if arr[i - 1] == "W":
        dp[0][i] = dp[0][i - 1] + 1
        dp[1][i] = dp[1][i - 1]
    if arr[i - 1] == "R":
        dp[0][i] = dp[0][i - 1]
        dp[1][i] = dp[1][i - 1] - 1


dp = np.array(dp)

print(dp.min(axis=0).max())

Submission status: Accepted

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

n = int(input())
C = input()

C = list(C)

d = {0:0}
tot = 0

for i,c in enumerate(C):
    if c == 'R':
        tot += 1
    d[i+1] = tot

ans = tot-d[tot]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input().split()[0]

length = len(S)

W_count = S.count('W')
R_count = S.count('R')

c = 0
if R_count < W_count:
    
    for i in range(R_count):
        s = S[i]
        if s !='R':
            c+=1
        
            
else:
    for i in range(W_count):
        s = S[-1-i]
        if s != 'W':
            c+=1
print(c)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num_r = s.count("R")
slist = list(s)
sstr = str(slist[0:num_r])
count = sstr.count("W")
print (count)

Submission status: Accepted

Solution code:

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

R_list=C.split("W")
numR=len(''.join(R_list))
numW=N-numR

ans=min(numW,numR)

indR=numR

CRlis=''.join(list(C)[indR:])


R_Clis=CRlis.split("W")
numCR=len(''.join(R_Clis))

if numCR<ans:
    ans=numCR
print(ans)

Submission status: Accepted

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

w_list = [0 for i in range(n+1)]
r_list = [0 for i in range(n+1)]

w = 0
r = 0
f = True
for i in range(1,n+1):
    if a[i-1] == "W":
        w += 1
        if w == 1:
            f = True
    else:
        f = False
    w_list[i] = w

for i in range(n-1,-1,-1):
    if a[i] == "R":
        r += 1
    r_list[i] = r

if w==0 or r == 0 or f == True:
    print(0)
    exit()

ans = n
for i in range(n+1):
    ans = min(ans,max(w_list[i],r_list[i]))
print(ans)

Submission status: Accepted

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

move_right = 0
move_left = N-1

count = 0

while move_right < move_left:
    if C[move_right] == 'R':
        move_right += 1

    else:
        while C[move_left] != 'R' and move_left >= 0:
            move_left -= 1

        if move_left <= move_right:
            # 入れ替えできるものがなかったので置き換え
            if C[move_right + 1] == 'R':
                count += 1

        # 入れ替え
        elif C[move_left] == 'R':
            count += 1

        move_right += 1
        move_left -= 1


print(count)

Submission status: Accepted

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

from collections import Counter

if "R" not in C:
    print(0)
    exit()
count = 0

c = Counter(C)
k = c["R"]
D = C[:k]
d = Counter(D)
print(d["W"])