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:
from collections import Counter


N = int(input())
C = input()
C_cntr = Counter(C)

all_R_cnt = 0
all_W_cnt = 0
R_W_cnt = 0
for i in range(N):
    if C[i] == "W":
        all_R_cnt += 1
    elif C[i] == "R":
        all_W_cnt += 1
    if i < C_cntr.get("R", 0) and C[i] == "W":
        R_W_cnt += 1

print(min(all_R_cnt, all_W_cnt, R_W_cnt))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from heapq import heappop,heappush
def main():
    N = int(input())
    S = input()
    W = []
    R = []
    ans = 0
    for i,s in enumerate(S):
        if s == 'W':
            heappush(W,i)
        else:
            heappush(R,-i)
    while (W and R):
        l = heappop(W)#小さい方から
        r = -heappop(R)#大きい方から
        if l < r:
            ans += 1#白が赤より左にあれば入れ替える
            heappush(W,r)
            heappush(R,-l)

        else:
            break#そうでなければ終了
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

x = c.count('R')
y = c.count('R', 0, x)
print(x-y)

Submission status: Accepted

Solution code:
import heapq
wl = []
rl = []
n = int(input())
c = input()
for i in range(n):
    if c[i] == "W":
        heapq.heappush(wl,i)
    else:
        heapq.heappush(rl,-i)
cnt = 0
# print(wl,rl)
if wl == [] or rl == []:
    print(0)
else:
    while wl[0] < -rl[0]:
        cnt += 1
        heapq.heappop(rl)
        heapq.heappop(wl)
        if wl == [] or rl == []:
            print(cnt)
            exit()
    print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
c = [None] * N
r_cnt = 0
for n,s in enumerate(input()):
    c[n] = s
    if s == "R":
        r_cnt += 1
w_cnt = 0
for i in range(r_cnt):
    if c[i] == "W":
        w_cnt += 1
print(w_cnt)

Submission status: Accepted

Solution code:
N = int(input())

c = input()

k = -1
for i in range(N):
    if c[i] == 'W' and k == -1:
        k = i 

if k == -1 or k == N-1:
    print(0)
else:
    w = [0] * N
    r = [0] * N

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    c = input()
    cnt = 0
    left = 0
    right = len(c) - 1
    while True:
        while c[right] == "W" and left < right:
            right -= 1
        while c[left] == "R" and left < right:
            left += 1
        if left >= right:
            return cnt
        cnt += 1
        right -= 1
        left += 1
    return cnt

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

Submission status: Accepted

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

r = c.count("R")
w = n - r
cnt = 0
ans = 0
for i in c:
    if cnt == r:
        break
    if i == "W":
        ans += 1
    cnt += 1
print(min(r, w, ans))

Submission status: Accepted

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


def main():
    s = 0
    l = list(C)
    i, j = 0, N - 1
    while i != j:
        if l[i] == "R":
            i += 1
        else:
            if l[j] == "W":
                j -= 1
            else:
                l[i] = "R"
                l[j] = "W"
                i += 1
                s += 1
    
    ans = min(C.count("R"), s)
    print(ans)
                

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
i = 0
j = n-1
while j>i:
    if c[i] == 'W' and c[j] == 'R':
        ans+=1
        i+=1
        j-=1
    elif c[i] == 'W' and c[j] == 'W':
        j-=1
    elif c[i] == 'R' and c[j] == 'R':
        i+=1
    else: # R W
        i+=1
        j-=1
print(ans)

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
_ = input()
s = input()
s2 = sorted(s)
ans = 0
for i, j in zip(s, s2):
    if i != j: ans += 1

print(ans // 2)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num_r=s.count('R')
if num_r==n or num_r==0:
    print('0')
    exit()

red=[]
for i in range(n):
    if s[i]=='R':
        red.append(i)
ans = num_r
for j in red:
    if j+1 <=num_r:
        ans-=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
i,j=0,n-1
ans=0
while 1:
    if i>=j:
        break
    if c[i]=="W" and c[j]=="R":
        c[i]="R"
        c[j]="W"
        ans+=1
    if c[i]!="W":
        i+=1
    if c[j]!="R":
        j-=1
for i in range(n-1):
    if c[i]=="W" and c[i+1]=="R":
        c[i]="R"
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c_str = input()
c_list = []
for i in range(n):
    c_list.append(c_str[i])

count = c_list.count("W")
count -= c_list[-count:].count("W")
print(count)

Submission status: Accepted

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

red_cnt = c.count("R")
ret = max( c[:red_cnt].count("W"), c[red_cnt:].count("R"))
print( ret )

Submission status: Accepted

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

import sys


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


def main():
    k = int(std_in())
    c = std_in()
    counter = 0
    while 1:
        l = c.find("W")
        r = c.rfind("R")
        if l == -1 or r == -1:
            break
        elif l < r:
            c = c[:l] + c[r] + c[l+1: r] + c[l] + c[r+1:]
            counter += 1
        else:
            break
    print(counter)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
l = int(input())-1
n = input()+" "
s = 0
ans = 0
a = n[0]
while(s <= l):
  #print(s, l)
  while(n[s]=="R" and s <= l):
    s+=1
  while(n[l]=="W" and s <= l):
    l-=1
  if(s-1!=l):ans+=1
  s+=1
  l-=1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=input()
left=0
right=N-1
ans=0
if all(c[i]=='R' for i in range(N)):
    print('0')
    exit()
if all(c[i]=='W'for i in range(N)):
    print('0')
    exit()
while left<right:
    if c[left]=='R':#これより左に白石なし
        left+=1
    if c[right]=='W':#これより右に赤石なし
        right-=1
    if c[left]=='W' and c[right]=='R':
        left+=1
        right-=1
        ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def read():
    N = int(input().strip())
    C = list(input().strip())
    return N, C


def solve(N, C):
    ans = 0
    l = 0
    r = N-1
    while l < r:
        while l < N and C[l] == "R":
            l += 1
        while 0 <= r and C[r] == "W":
            r -= 1
        if l < r:
            ans += 1
            l += 1
            r -= 1
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
r = c.count("R")
w = c.count("W")
true = []
true += ["R"]*r
true += ["W"]*w
count = 0
for num in range(N):
  if true[num] != c[num]:
    count += 1
print(int(count/2))

Submission status: Accepted

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

r_count = C.count("R")
ans = r_count - C[:r_count].count("R")

print(ans)

Submission status: Accepted

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''D'''
n = r()
a = input()
whites = a.count('W')
if whites == 0 :
    print(0)
else:
    reds = n-whites
    s = 'R'*reds+'W'*whites
    change = 0
    for i in range(n):
        if a[i] != s[i]:
            change+=1
    print((change+1)//2)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
ans = 0
cntW, cntR = 0, 0
flg = True
for i in range(n-1, -1, -1):
    if c[i] == 'W':
        cntW += 1
    else:
        cntR += 1
    if flg:
        if c[i] == 'R':
            flg = False
    else:
        if c[i] == 'W':
            ans += 1
ans = min(ans, cntW, cntR)
cnt = 0
for i in range(n-1, -1+cntR, -1):
    if c[i] == 'R':
        cnt += 1
ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = input()
    i, j = 0, n-1
    ans = 0
    while i < j:
        if c[i] == "W" and c[j] == "R":
            ans += 1
            i += 1
            j -= 1
        if c[i] == "R":
            i += 1
        if c[j] == "W":
            j -= 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
import sys
import bisect
#input=sys.stdin.readline
#t=int(input())
t=1
for _ in range(t):
    n=int(input())
    #n,d=map(int,input().split())
    #l=list(map(int,input().split()))
    s=input()
    f=0
    dw=[]
    dr=[]
    for i in range(n):
        if s[i]=='W':
            dw.append(i)
        else:
            dr.append(i)
    ind=0 
    cnt=0
    #print(dr[-1::-1],dw)
    for i in range(len(dr)-1,-1,-1):
        if ind>=len(dw):
            break
        
        if dr[i]>dw[ind]:
            cnt+=1
            ind+=1
    print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math

# import copy
from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret


def solve():
    n = getN()
    S = getS()
    l = 0
    r = n - 1
    # print(n, S, l, r)
    ansl, ansr = 0,0
    while S[r] == "W":
        r -= 1
        if l == r:
            print(max(ansl, ansr))
            return
    while True:
        while True:
            if S[l] == "W":
                l += 1
                ansl += 1
                # print(ansl, l, r)
                while S[l] == "R":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    l += 1
                break
            l += 1
            if l == r:
                break
            # print(l, r?)
        if l == r:
            print(max(ansl, ansr))
            return

        while True:
            if S[r] == "R":
                r -= 1
                ansr += 1
                while S[r] == "W":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    r -= 1
                break
            r -= 1
            if r == l:
                break
            # print(l, r)
        if l == r:
            print(max(ansl, ansr))
            return
    # ans2 = rc - rcnt
    # print(min(ans1,ans2))


def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

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

Rcnt = C.count("R")
Ccnt = N - Rcnt
swapcnt = 0
for i in range(N):
    if i < Rcnt and C[i] == "W":
        swapcnt += 1

print(min(swapcnt, min(Rcnt, Ccnt)))

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
Wnum = c.count("W")
if Wnum == 0 or Wnum == N:
    print(0)
else:
    F = "R" * (N - Wnum) + "W" * Wnum
    t = 0
    for i in range(N):
        if c[i] != F[i]:
            t += 1
    print(t//2 + t%2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
c = input()
ans = 0
for i in range(c.count('W')):
    ans += c[-(i + 1)] != 'W'
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    c = str(input())
    c = list(c)
    
    i = -1
    j = N
    up = True
    ans = 0
    
    for n in range(N):
        if i == j:
            break
        if up:
            i += 1
            if c[i] == 'W':
                up = False
        else:
            j -= 1
            if c[j] == 'R':
                up = True
                ans += 1
            
    print(ans)
                
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import math


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N = I()
    C = S()
    rcount = 0
    ans = 0
    for c in C:
        if c == "R":
            rcount += 1

    for r in range(rcount, N):
        if C[r] =="R":
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    C = list(input())
    Rs = []
    for i in range(N):
        if C[i] == 'R':
            Rs.append(i)
    count = 0
    for i in range(N):
        if C[i] == 'W':
            if len(Rs) == 0:
                break
            j = Rs.pop()
            if i < j:
                C[i], C[j] = C[j], C[i]
                count += 1
    return count


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

Submission status: Accepted

Solution code:
def solve():
    from collections import Counter
    n = int(input())
    s = input()
    c = Counter(s)
    x = c["R"]
    cnt = 0
    for i in range(x):
        if s[i] == "W":
            cnt += 1
    print(cnt)

solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

#@njit
def main():
    N = int(input())
    C = input()
    l = 0
    r = N-1
    count = 0
    while l < N and C[l] == 'R':
        l += 1
    while 0 <= r and C[r] == 'W':
        r -= 1
    while l < r:
        C = C[:l] + C[r] + C[l+1:r] + C[l] + C[r+1:]
        count += 1
        while l < N and C[l] == 'R':
            l += 1
        while 0 <= r and C[r] == 'W':
            r -= 1

    print(count)
    

main()

Submission status: Accepted

Solution code:
n=int(input())
c=input()
rlen=c.count("R")
wlen=c.count("W")
r=0
w=0
if rlen==0:
    print(0)
else:
    for i in c:
        if i=="R":
            r+=1
        else:
            w+=1
        if w==rlen-r:
            print(w)

Submission status: Accepted

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

r = 0
w = 0
for i in range(n):
    if(s[i] == "R"): r += 1
    else: w += 1

ngr = 0
ngw = 0
for i in range(n):
    if(i < r and s[i] == 'W'): ngw += 1
    if(i >= r and s[i] == 'R'): ngr += 1

print(min(ngw, ngr) + abs(ngw-ngr))

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    C = input()[:-1]
    
    m = C.count("R")
    r = 0
    w = 0
    for i in range(m):
        if C[i] == "W":
            w += 1
    for i in range(m, N):
        if C[i] == "R":
            r += 1
    
    print(max(r, w))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input()
s = input()
pref = 0
total = s.count('R')
res = int(1e9)
for i in range(total):
    pref += s[i] == 'R'
    res = min(res, total - pref)
print(res if total != len(s) and total != 0 else 0)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from heapq import heapify, heappush, heappop
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map

    N = int(input())
    S = input()
    if 'W' not in S or 'R' not in S:
        print(0)
        exit()

    # RとWの位置
    locR = [-i for i in range(N) if S[i] == 'R']
    locW = [i for i in range(N) if S[i] == 'W']
    heapify(locR)
    heapify(locW)

    dS = deque(S)
    # print(N, dS)
    # print(locR, locW)
    count = 0
    while True:
        # print(locR, locW)
        # print(">?>", -locR[0], locW[0])
        if (-locR[0]) < locW[0]:
            break
        # 交換する
        right_most_R = heappop(locR)
        left_most_W = heappop(locW)
        heappush(locR, -left_most_W)
        heappush(locW, -right_most_R)
        count += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    rc = s.count('R')
    rcl = s[:rc].count('R')

    print(rc - rcl)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase


"""def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)"""

"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""



def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    s=list(input())
    ct=0
    i=0
    j=n-1
    while(i<=j):
        if(s[i]=='W'):
            while(i<j and s[j]=='W'):
                j-=1
            if(i==j):
                break
            ct+=1
            s[i],s[j]=s[j],s[i]
        i+=1
    for i in range(1,n):
        if(s[i-1]=='W' and s[i]=='R'):
            ct+=1
    print(ct)

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")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input().rstrip())
c = list(input().rstrip()[:])
if c.count('W')==0:
    print(0)
else:
    print(c[-1*c.count('W'):].count('R'))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w_arr = [0]
r_arr = [0]

w_acc = 0
r_acc = 0
for i in range(n):
    if s[i] == 'W':
        w_acc += 1
    else:
        r_acc += 1

    w_arr.append(w_acc)
    r_arr.append(r_acc)


min_r = 999999999999
for i in range(n+1):
    w_1, r_1 = w_arr[i], r_arr[i]
    w_2, r_2 = w_arr[n] - w_arr[i], r_arr[n] - r_arr[i]

    if w_1 >= r_2:
        r = w_1
    else:
        r = r_2

    if r < min_r:
        min_r = r

print(min_r)

Submission status: Accepted

Solution code:
N = int(input())
li =input()
Rcount = 0
Wcount = 0

for i in range(N):
    if li[i] == "W":
        Wcount += 1
    else:
        Rcount += 1
anscount = 0

if Wcount >= Rcount:
    for i in range(Rcount):
        if li[i] == "W":
            anscount += 1
else:
    lire = li[::-1]

    for i in range(Wcount):
        if lire[i] == "R":
            anscount += 1
print(anscount)

Submission status: Accepted

Solution code:
widx = []
ridx = []
n = int(input())
s = input()
for i, char in enumerate(s):
  if char == "W":
    widx.append(i)
  else:
    ridx.append(i)
count = 0
i = 0
j = len(ridx) - 1
while i < len(widx) and j >= 0 and widx[i] < ridx[j]:
  i += 1
  j -= 1
  count += 1
print(count)

Submission status: Accepted

Solution code:
import sys
def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

from collections import defaultdict 
import math 

n = int(input())
s = input()
w_cnt = 0
r_cnt = 0

for i in s:
	if i=='W':
		w_cnt+=1
	else:
		r_cnt+=1

if w_cnt ==0 or r_cnt == 0:
	print(0)
else:
	ans = 0
	for i in range(r_cnt):
		if s[i]=='W':
			ans+=1
	print(ans)

Submission status: Accepted

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

rcnt = C.count("R")
res = C[rcnt:].count("R")

print(res)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
r=c.count("R")#全R
r_1=c[:r].count("R")#左からr番目までのR
#print(r,r_1)
ans=r-r_1
print(ans)

Submission status: Accepted

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

r = c.count('R')
ng = sum(map(lambda i: 1 if (i < r) == (c[i] == 'W') else 0, range(n)))
ans = ng // 2 + ng % 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
r = c.count('R')
w = c.count('W')

wrong_w = c[:r].count('W')
wrong_r = c[r:].count('R')

print(max(wrong_r,wrong_w))

Submission status: Accepted

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

l = 0
r = n - 1
count = 0

while True:
    while c[l] == "R" and l < r:
        l += 1
    while c[r] == "W" and l < r:
        r -= 1

    if r == l:
        break

    c[r], c[l] = c[l], c[r]

    count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
wnum = 0
for i in range(N):
  if S[i] == "W":
    wnum += 1
right = 0
Flag = False
wright = 0
for i in range(N):
  #print(i,Flag,S[-1-i])
  if S[-1-i] == "R" and not Flag:
    right += 1
  elif S[-1-i] == "W":
      wright += 1
      Flag = True
  else:
    break
ans = 0
for i in range(wnum):
  if S[-1-i] != "W":
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase
# from collections import defaultdict as dd
# from collections import deque as dq
# import itertools as it
# from math import sqrt, log, log2
# from fractions import Fraction

def main():
    n = int(input())
    s = input()
    rc = s.count('R')
    sor = 'R'*rc + 'W'*(n - rc)

    ed = 0
    for i in range(n):
        if s[i]!=sor[i]: ed += 1
    
    print(ed//2)


    #x, y = map(int, input().split())
    #nums = list(map(int, input().split()))
        




































# 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
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

s = list(input())

l = 0
r = n-1

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# 全部の赤を左側に持っていきたい
N = int(input())
C = input()

Red_cnt = 0
for i in range(N):
    if C[i] == "R":
        Red_cnt += 1

ans = 0
for i in range(Red_cnt):
    if C[i] == "W":
        ans += 1
print(ans)

Submission status: Accepted

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

num_w = c.count("W")
num_r = N - num_w

comp = "R"*num_r + "W"*num_w
ans = 0
for i in range(num_r):
    if c[i] != comp[i]:
        ans += 1

ans = min(num_w, num_r, ans)
print(ans)

Submission status: Accepted

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

r=s.count('R')
ans=r

for i in range(r):
  if s[i]=='R':
    ans-=1
    
print(ans)

Submission status: Accepted

Solution code:
def do():
    if s.find("W") == -1:
        print(0)
        return
    c1 = s.count("W")
    c2 = s.count("R")
    c3 = 0
    l = 0
    r = len(s) - 1
    ll = len(s)
    while l < r and r >= 0 and l <= (ll-1):
        #print(l, r)
        if s[l] == "R":
            l += 1
            continue
        if s[r] == "W":
            r -= 1
            continue
        #print("c3+", l, r)
        c3 += 1
        l += 1
        r -= 1
    #print(c1,c2,c3)
    print(min(c1,c2,c3))


n = int(input())
s = input()
do()

Submission status: Accepted

Solution code:
n=int(input())
s=list(input().strip())
i=0;j=n-1
an=0
while i<j:
  if s[i]=='W' and s[j]=='R':
    s[i]='R';s[j]='W'
    i+=1;j-=1
    an+=1;continue
  if s[i]=='R':
    i+=1
  if s[j]=='W':
    j-=1
print(an)

Submission status: Accepted

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

left = 0
right = N -1

ans = 0
while left < right:
    if C[left] == 'W' and C[right] == 'R':
        left += 1
        right -= 1
        ans += 1
    elif C[left] == 'W':
        right -= 1
    elif C[left] == 'R':
        left += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    R = S[::-1]
    cnt = 0
    i = 0
    r = N-1
    while i<r:
        if(S[i]=='W'):
            while i<r:
                if S[r]=='R':
                    cnt += 1
                    r -= 1 
                    break
                r -= 1
        i+=1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X=int(input())
t=input()
s=list(t)
co=0
re=0
a=s.count("R")
for i in range(X):
  if a==co:
    break
  elif s[i]=="W":
    re+=1
    co+=1
  else:
    co+=1
    
print(re)