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()
l = 0
r = n-1
ans = 0
while l<r:
    if c[l] == "R": l += 1
    if c[r] == "W": r -= 1
    
    if c[l]=="W" and c[r]=="R":
        l += 1
        r -= 1
        ans += 1
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
C = input()
idx_l = 0
idx_r = len(C) - 1
cnt = 0
while True:
    if idx_l >= idx_r:
        break
    if C[idx_l] == 'R':
        idx_l += 1
        continue
    if C[idx_r] == 'W':
        idx_r -= 1
        continue
    cnt += 1
    idx_l += 1
    idx_r -= 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
le=S.count('R')
P=S[le:]
print(P.count('R'))

Submission status: Accepted

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

l = 0
r = N-1
ans = 0

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

print(ans)

Submission status: Accepted

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

A=[]
for i in range(n):
    if C[i]=="R":
        A.append(i)
count=len(A)

ans=0
for i in A:
    if i>=count:
        ans +=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n = inn()
c = ins()
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:
        break
    cnt += 1
    l += 1
    r -= 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
cs = input().rstrip()

L, R = 0, N-1
ans = 0
while True:
    while L < N and cs[L] != 'W':
        L += 1
    while R >= 0 and cs[R] != 'R':
        R -= 1
#    print('L:', L, '/ R:', R)
    if L < R:
        ans += 1
    else:
        break
    L += 1
    R -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
c=input()
r=c.count("R")
w=n-r
ans=0
for i,j in zip(c,"R"*r+"W"*w):
    if i!=j:
        ans+=1
print(ans//2)

Submission status: Accepted

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

l = 0
r = n - 1

ans = 0
while l < r:
    while c[l] != "W" and l + 1 < n:
        l += 1

    while c[r] != "R" and r - 1 >= 0:
        r -= 1

    if l < r:
        ans += 1
        l += 1
        r -= 1

print(ans)

Submission status: Accepted

Solution code:
def f(c):
    return 1*(c=="R")

N=int(input())
C=list(map(f,input()))

R=C.count(1)
S=C[:R].count(0)
print(S)

Submission status: Accepted

Solution code:
N = int(input())
c = list(str(input()))
for i in range(N):
    if c[i] == "R":
        c[i] = 1
    else:
        c[i] = 0
a = sum(c)
d = c[:a]
e = sum(d)
print(a - e)

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n=I()
s=SI()
count=0
for i in range(n):
    if s[i]=="R":
        count+=1
for i in range(count):
    if s[i]=="R":
        count-=1
print(count)

Submission status: Accepted

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

cnt = c.count('R')
res = 0
for i in range(cnt):
    if c[i] == 'W':
        res += 1
print(res)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
white = c.count("W")
red = c.count("R")
if white == 0 or red == 0:
    print(0)
    exit()

ans = 0
count = 0
if white > red:
    for x in c:
        if x == "W":
            ans += 1
            count += 1
        else:
            count += 1
        if count == red:
            print(ans)
            exit()
else:
    for x in reversed(c):
        if x == "R":
            ans += 1
            count += 1
        else:
            count += 1
        if count == white:
            print(ans)
            exit()

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
rn = c.count("R")
rnl = c[:rn].count("R")
print(rn-rnl)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  s=S()

  l=0
  r=n-1

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

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from collections import deque

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

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

    count = 0
    while len(whites) > 0 and len(reds) > 0 and whites[0] < reds[-1]:
        reds.pop()
        whites.popleft()
        count += 1
    
    print(count)

solve()

Submission status: Accepted

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

head=0
end=n-1

ans=0
ret=False
while head<end:
    while c[head]!="W":
        head+=1
        if head==end:
            ret=True
            break
    
    if ret==True:
        break
    
    while c[end]!="R":
        end-=1
        if head==end:
            ret=True
            break
    
    if ret==True:
        break
    
    ans+=1
    head+=1
    end-=1

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N = INT()
A = list(input())

C = Counter(A)
ans = 0
for i in range(C['R']):
    if A[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

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

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

ans = 0
l = 0
r = n - 1
while True:
    while c[l] != 'W' and l <= n - 1:
        l += 1
    if l >= r:
        print(ans)
        exit()
    while c[r] != 'R' and r >= 0:
        r -= 1
    if l >= r:
        print(ans)
        exit()
    c[l] = 'R'
    c[r] = 'W'
    ans += 1

Submission status: Accepted

Solution code:
n = int(input())
# s = list(map(str,input().split()))
s = input()
count_R = 0
count_W = 0
for ss in s:
    if ss == "R":
        count_R += 1
    else:
        count_W += 1
t_countR = 0
for i in range(count_R):
    if s[i]=="W":
        t_countR += 1

print(t_countR)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
C = list(input())[:-1]
r = 0
w = 0
for s in C:
    if s == "R":
        r += 1
    elif s == "W":
        w += 1
ans = 0
if w == 0 or r == 0:
    print(0)
else:
    for i in range(r):
        if C[i] == "W":
            ans += 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
c=str(input())
cc=collections.Counter(c)

c2=c[:cc['R']]
cc2=collections.Counter(c2)


print(cc2['W'])

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = 0
r = n - 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
C=input()
L=0
R=n-1
ans=0
while True:
    while L<n:
        if C[L]=="W":
            break
        else:
            L+=1
    while R>=0:
        if C[R]=="R":
            break
        else:
            R-=1
    if L<R:
        ans+=1
        L+=1
        R-=1
    else:
        break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
r = c.count('R')

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = input()
C = input().rstrip()
dic = Counter(C)
l = dic["R"]
dic2 = Counter(C[:l])
l2 = dic2["R"]
print(l-l2)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
c = deque(input())
ans = 0
while c:
    a = c.popleft()
    if a == "R":
        continue
    else:
        if c:
            while c:
                b = c.pop()
                if b == "W":
                    continue
                else:
                    ans += 1
                    break
        else:
            break
print(ans)

Submission status: Accepted

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

ans = 0
l, r = 0, N - 1
while l < r:
    if A[l] == 'W' and A[r] == 'R':
        l += 1
        r -= 1
        ans += 1
    else:
        if A[l] == 'R':
            l += 1
        if A[r] == 'W':
            r -= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
result=0
d={"RW":0,"WR":0}
for i in range(n):
    if s[i]=="R":
        result+=1
        d["RW"]+=1
        
for i in range(n):
    if s[i]=="R":
        d["RW"]-=1        
    else:
        d["WR"]+=1
    maximum,minimum=max(d["RW"],d["WR"]),min(d["RW"],d["WR"])
    sub_result=maximum
    result=min(sub_result,result)
print(result)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
white = 0
white_ends = 0

for i in range(N):
  if c[i] == "W":
    white += 1
for i in range(N-white, N):
  if c[i] == "W":
    white_ends += 1
print(white-white_ends)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
from collections import Counter
c=Counter(s)
w=c['W']
r=c['R']
if w==0 or r==0:
    print (0)
else:
    z=0
    for i in range(r):
        if s[i]=='W':
            z+=1
    print (z)

Submission status: Accepted

Solution code:
n = int(input())
data = list(input())
w = 0
r = 0
for i in range(n):
    if data[i] == "W":
        w+=1
    else:
        r +=1
wW = 0
for i in range(r):
    if data[i] ==  "W":
        wW+=1
rW = 0
for i in range(r+1,n):
    if data[i] == "R":
        rW+=1
print(max(rW,wW))

Submission status: Accepted

Solution code:
import collections
n = int(input())
c = input()
c_ls = []
Ws = collections.deque()
for i in range(n):
    c_ls.append(c[i])
    if c[i] == "W":
        Ws.append(i)
ans = 0
for i in range(n-1,-1,-1):
    if c_ls[i] == "R" and len(Ws):
        w = Ws[0]
        if i > w:
            c_ls[i] = "W"
            c_ls[w] = "R"
            ans += 1
            Ws.popleft()
    elif c_ls[i] == "W" and len(Ws) == 0:
        ans += 1
#print(c_ls)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    C = input()
    p = C.count("R")
    C1 = C[p:]
    q = C1.count("R")
    print(q)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
C = input()
C = list(C)
left = 0
length = len(C) - 1
right = length


def checkLeft():
    global left

    while C[left] == "R" and left < length:
        left += 1


def checkRight():
    global right
    while C[right] == "W" and right > 0:
        right -= 1


checkLeft()
checkRight()
# print(left, right)
cnt = 0
while left < right:
    C[left] = "R"
    C[right] = "W"
    checkLeft()
    checkRight()
    cnt += 1
    # print(left, right)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
ans = 0
a = 0
b = n-1
while(a < b):
  while(a < n and c[a] == 'R'):
    a += 1
  while(b >= 0 and c[b] == 'W'):
    b -= 1
  if a < b:
    ans += 1
    a += 1
    b -= 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
C=input()
w=0
r=0
for c in C:
  if c=='W':
    w+=1
  else:
    r+=1
    
x=0
for c in C[:r]:
  if c=='W':
    x+=1
    
print(x)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
s = 0
t = N - 1

ans = 0
while True:
  while s < N:# Most-Left W
    if C[s] == 'R':
      s += 1
    else:
      break
  while t > -1:# Most-Right R
    if C[t] == 'W':
      t -= 1
    else:
      break
  if s < t:
    ans += 1
    s += 1
    t -= 1
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
import sys, math

if(__name__ == "__main__"):
    """
    N, D = map(int, input().strip().split() )
    D_sq = D**2
    xy_list = []
    for i in range(N):
        xy = list( map(int, input().strip().split() ) )
        xy_list.append(xy)
    """
    N = int( input().strip() )
    color_str = input().strip()
    colors = [ 0 ] * N
    #R -> 0, W->1
    for i in range(N):
        if(color_str[i] == "W"):
            colors[i] = 1
    left = 0
    right = N-1
    times = 0
    while(left < right):
        while(left < N-1 and colors[left]==0):
            left += 1
        while(right > -1 and colors[right]==1):
            right -= 1
        if(left < right):
            colors[left], colors[right] = colors[right], colors[left]
            times += 1
    print(times)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    c = input()
    r = deque()
    for i in range(n)[::-1]:
        if c[i] == "R":
            r.append(i)
    if len(r) in (0, n):
        print(0)
        return
    ans = 0
    for i in range(n):
        if c[i] == "W":
            while r:
                j = r.popleft()
                if j > i:
                    break
            else:
                print(ans)
                return
            ans += 1
    print(ans)
    return

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    C = input()
    r = C.count('R')
    print(C[:r].count('W'))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
x = S.count("W")
y = S[N - x:].count("W")
print(x - y)

Submission status: Accepted

Solution code:
import random


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


def lcm(a, b):
    return (a * b) / gcd(a, b)



#a = list(map(int, input().split()))
n=int(input())
s=input()
l=0
r=n-1
c=0
while(l<r):
    if s[l]=='R':
        l+=1
    if s[r]=='W':
        r-=1
    if s[l]=='W' and s[r]=='R':
        c+=1
        l+=1
        r-=1

print(c)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
ans = 0
j = n-1
for i in range(n):
    if i >= j:
        break
    if c[i] == 'R':
        continue
    while i < j and c[j] != 'R':
        j -= 1
    if i == j:
        break
    ans += 1
    j -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
countR = 0
for k in range(len(c)):
  if c[k] == 'R':
    countR += 1

ans = 0
for k in range(countR):
  if c[k] == 'W':
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = str(input())
l = 0
r = n-1
answer = 0
while l < r:
    if c[l] == "W" and c[r] == "R":
        answer += 1
        l += 1
        r -= 1
    elif c[l] == "W" and c[r] == "W":
        r -= 1
    elif c[l] == "R" and c[r] == "R":
        l += 1
    else:
        l += 1
        r -= 1
print(answer)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.readline
import time
import math
import itertools as it
def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))
start_time = time.perf_counter()
# ------------------------------

n = inp()
c = input()
cntR = 0
for i in range(n):
    if c[i] == 'R':
        cntR += 1
ans = cntR
for i in range(cntR):
    if c[i] == 'R':
        ans -= 1
print(ans)


# -----------------------------
end_time = time.perf_counter()
print('time:', end_time-start_time, file=sys.stderr)

Submission status: Accepted

Solution code:
import sys
def Ii():return int(sys.stdin.readline())
def Mi():return map(int,sys.stdin.readline().split())
def Li():return list(map(int,sys.stdin.readline().split()))

n = Ii()
c = sys.stdin.readline()
rct = c.count('R')
ans = rct - c.count('R',0,rct)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

_ = int(input())
s = input()
counter = Counter(s)
red = counter.get('R', 0)
white = counter.get('W', 0)

mismatches = 0
for i in range(red):
    if s[i] != 'R':
        mismatches += 1

print(mismatches)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = list(input())
fl = False
rig_w = -1
for i in range(n)[::-1]:
    if fl and s[i] == 'W':
        rig_w = i
        break
    elif s[i] == 'R':
        fl = True
res1 = 0

if rig_w != -1:
    res1 = s[:rig_w+1].count('W')

r_count = s.count('R')
res2 = s[:r_count].count('W')

print(min(res1,res2))

Submission status: Accepted

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

w = [i for i in range(N) if c[i]=='W']
r = [-i for i in range(N) if c[i]=='R']
heapq.heapify(w)
heapq.heapify(r)
ans = 0
for i in range(N):
    if w and r:
        a = heapq.heappop(w)
        b = -heapq.heappop(r)
        if a-b==1:
            break
        else:
            heapq.heappush(w,b)
            heapq.heappush(r,-a)
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(map(str,input()))

r1=c.count('R')

b=list()
for i in range(r1):
    b.append(c[i])
r2=b.count('R')
print(r1-r2)

Submission status: Accepted

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

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

print(first[:r_num].count('W'))

Submission status: Accepted

Solution code:
N = int(input())
c = list(str(input()))
count_R = 0
for ci in c:
    if ci == 'R':
        count_R += 1
count_over_R = 0
for ci in c[count_R:]:
    if ci == 'R':
        count_over_R += 1
print(count_over_R)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
num_R = S.count('R')
num_R_in_pre_R = S[0:num_R].count('R')
print('{}'.format(num_R-num_R_in_pre_R))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
C = [1 if x == 'W' else 0 for x in input()]

rcount = C.count(0)
ans = rcount
W_acc = itertools.accumulate(C)
R_count = C.count(0)
for i, c in enumerate(W_acc, 1):
    ans = min(ans, abs(rcount - i) + c)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    cs = list(input())

    l = 0
    r = N - 1

    cnt = 0
    while l < r:
        while l <= N - 1 and cs[l] == 'R':
            l += 1

        while 0 <= r and cs[r] == 'W':
            r -= 1

        if l >= r:
            break

        cs[l], cs[r] = cs[r], cs[l]
        cnt += 1

    print(cnt)

main()

Submission status: Accepted

Solution code:
N=int(input())
C=input()
R=[]
W=[]
for i in range(N):
  c=C[i]
  if c=="R":
    R.append(i)
  else:
    W.append(i)
ans=0
for i in range(N):
  if i>=len(R) or i>=len(W):
    break
  if W[i]>R[-i-1]:
    break
  ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
white = 0
red = 0
white_ren = 0
red_ren = 0
for i in range(n):
    if s[i] == "W":
        white += 1
    else:
        red += 1
for i in range(red):
    if s[i] == "R":
        red -= 1
for i in range(white):
    if s[-i - 1] == "W":
        white -= 1
remain = white + red
ans1 = min(red, white)
remain -= 2 * ans1
print(remain + ans1)

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
"""
白、赤の順で並んでいる箇所

べき論を求めるのがよさそう。
赤石が全て左半分に来るようにすればよい。
赤石の数を求める⇒赤石はi番目より左になくてはいけない、と定義する。
i番目より右にある赤石の数を求める
"""
RCnt = C.count("R")
#赤石はRCnt-1番目(0-indexed)より左になくては行けない
RCnt2 = C[:RCnt].count("R")
print(RCnt-RCnt2)

Submission status: Accepted

Solution code:
N = int(input())
l = list(input())
count = 0
minus = 0
for i in l:
    if i == 'R':
        count += 1
for i in l[:count]:
    if i == 'R':
        minus += 1
print(count - minus)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
rcount=0
wcount=0
n=len(s)
ans1=0
ans2=0
for i in range(0,n):
    if(s[i]=='R'):
        rcount+=1
    else:
        wcount+=1
for i in range(0,rcount):
    if(s[i]!='R'):
        ans1+=1
for i in range(rcount,n):
    if(s[i]!='W'):
        ans2+=1
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
s=c.count("W")
d=c[:n-s].count("W")
print(d)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
ans, count = 0, 0
for i in range(n-1):
    if c[i] == 'W' and c[i+1] == 'R':
        ans += count+1
        count = 0
    elif c[i] == 'W' and c[i+1] == 'W':
        count += 1

c2 = ''.join(sorted(c))
count2 = 0

for i, j in zip(c, c2):
    if i != j:
        count2 += 1
print(min(ans, count2//2))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Red = [0]
White = [0] * (N + 1)
r = 0
w = 0
for s in S:
    if s == 'R':
        r += 1
    Red.append(r)
for i, s_ in enumerate(S[::-1]):
    if s_ == 'W':
        w += 1
    White[N-i-1] = w

ans = float('inf')
for i in range(N + 1):
    x = i - Red[i]
    y = N - i - White[i]
    m = min(x, y)
    M = max(x, y)
    ans = min(ans, M)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
from itertools import groupby
N = int(input())
S = input()

B = [len(list(x[1])) for x in groupby(S)]

q = deque(B)
if S[0] == 'R':
    q.popleft()
if S[-1] == 'W':
    q.pop()

ans = 0
while q:
    if len(q) == 1:
        break
    l, r = q.popleft(), q.pop()
    if l == r:
        if len(q) >= 2:
            q.popleft()
            q.pop()
        ans += l
    elif l < r:
        if len(q):
            q.popleft()
            q.append(r-l)
        ans += l
    else:
        if len(q):
            q.pop()
        q.appendleft(l-r)
        ans += r

print(ans)