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:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

def main():
	n=II()
	s=input().rstrip().decode()

	w=[0]*(n+1)
	r=[0]*(n+1)

	for i,ss in enumerate(s):
		if ss=="W":
			w[i+1]=w[i]+1
		else:
			w[i+1]=w[i]

	for i,ss in enumerate(reversed(s)):
		if ss=="R":
			r[i+1]=r[i]+1
		else:
			r[i+1]=r[i]

	#print(w,r)

	ans=0
	for i,j in zip(w[1:],reversed(r[1:])):
		ans=max(ans,min(i,j))
	print(ans)




if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = list(input())
    r = 0
    w = 0
    for i in range(n):
        if c[i] == "R":
            r += 1
        else:
            w += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import
#import math
#import numpy as np
N = int(input())
#= input()
#= map(int, input().split())
c = list(map(str, input()))
#= [input(), input()]
#= [list(map(int, input().split())) for _ in range(N)]
#= [int(input()) for _ in range(N)]
#= {i:[] for i in range(N)}

whitec = c.count("W")

res = 0

for i in range(N - whitec, N):
    if c[i] == "R":
        res += 1

print(res)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline

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

red = deque()
white = deque()

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

s = 0

while len(white) > 0 and len(red) > 0:
    w = white.popleft()
    r = red.pop()
    if w < r:
        s += 1
    else:
        break

print(s)

Submission status: Accepted

Solution code:
n=int(input())

c=input()

w=[]
r=[]

for i,color in enumerate(c):
    if color=="W":
        w.append(i)
    else:
        r.append(i)
# print(w)
# print(r)
w=w[::-1]

ans=0

def finish(w,r):
    if w==[] or r==[]:
        return True
    elif w.pop()>r.pop():
        return True
    return False
    

while not finish(w,r):
    ans+=1

print(ans)

Submission status: Accepted

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

j = N-1
count = 0
for i in range(N):
    if RW[i]=='W':
        while i<j:
            if RW[j]=='R':
                count += 1
                j -= 1
                break
            j -= 1

print(count)

Submission status: Accepted

Solution code:
import collections

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

r_n = collections.Counter(c)['R']

ans = r_n
for i in range(r_n):
    if c[i] == 'R':
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
a=int(input())
v=input()
b=v.count("R")
c=0
for i in v[:b]:
    if i!="R":
        c+=1
print(c)

Submission status: Accepted

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

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

# print(a, b)

# min_num = 10**18
# for i in range(N+1):
#     print(C[:i], C[i:])
#     print((a[i]-a[0]) - (b[N]-b[i]))
#     if a[i]-a[0] - b[N]-b[i] == 0:
#         min_num = min(a[i]-a[0], min_num)

# print(min_num)

def solve(x):
    r = (b[N]-b[x]) - (a[x]-a[0])
    # print(r)
    return r >= 0

# 右側のRの個数 - 左側のWの個数 >= 0 の最大
ok, ng = 0, N+1
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    # print(ok, ng, mid)
    if solve(mid):
        ok = mid
    else:
        ng = mid

# print(a, b)
# print(ok)

print(a[ok])

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
color=input()

c = Counter(color)
r = c['R']

count = 0
for i in range(N):
  if r == 0:break
  if color[i]=='W':
    count += 1
    r -= 1
  else:
    r -= 1

print(count)

Submission status: Accepted

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

R_list = list()
W_list = list()

for i in range(N):
    if c_list[i] == "R":
        R_list.append(i)
    else:
        W_list.append(i)
        
if len(W_list) == 0 or len(R_list) == 0:
    print(0)
else:
    #print(R_list)
    #print(len(R_list))
    #Rを左に揃えればよい
    ans = len(R_list) - bisect.bisect_left(R_list,len(R_list))
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
C = list(input())
cr = 0
cw = 0
for c in C:
    if c == 'R':
        cr += 1
    else:
        cw += 1

if cw == 0:
    print(0)
else:
    ans = 0
    l = 0
    for r in range(0, cw):
        if C[n-1-r] == 'R':
            while C[l] != 'W':
                l += 1
            l += 1
            ans += 1
    print(min(ans, cr))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
left = 0
right = n-1
ans = 0
while left!=right:
    if left+1 == right:
      if s[left]=="W" and s[right]=="R":
        ans += 1
        print(ans)
        exit()
      else:
        print(ans)
        exit()
    if s[left]!="W":
        left += 1
    else:
        if s[right]!="R":
            right -= 1
        else:
            right -= 1
            left += 1
            ans += 1
  #  print(left,right,ans)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
x=0
M=N
for i in range(N):
  if c[i]=="W":
    for j in range(N):
      if i==M-1-j:
        M=i
        break
      elif c[M-1-j]=="R":
        x+=1
        c[M-1-j]="W"
        M=M-j
        break
  if i==M:
    break
print(x)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

r = s.count("R")
print(min(s[:r].count("W"), s[r:].count("R")))

Submission status: Accepted

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
n = ini()
c = input()
rc = c.count('R')
lc = c[:rc].count('W')
print(lc)

Submission status: Accepted

Solution code:
n=int(input())
cs=input()
ss=sorted(cs)
wr=0
for x,y in zip(cs,ss):
    if x=='W' and y=='R':
        wr+=1
print(wr)

Submission status: Accepted

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

n=int(input())
c=input().rstrip()

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:
        break
    ans+=1
    l+=1
    r-=1

temp=0
for i in c:
    if i=='R':
        temp+=1
temp=min(temp,n-temp)
ans=min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq
#import numpy as np
import bisect
import sys
#import sympy
sys.setrecursionlimit(10**6)


def bfs(s, n, node):
    # 頂点が探索済みかどうかのチェック配列
    check = [False for _ in range(n)]
    check[s] = True
    # 次見る頂点を格納するキュー
    queue = deque([s])
    visited_num = 1

    # 問題固有の配列
    #color_check=[[True for _ in range(l)] for _ in range(n)]
    color = [-1 for _ in range(n)]
    color[s] = 0

    while visited_num < n:
        # グラフが全連結ではない場合
        if len(queue) == 0:
            # 空配列を返す
            # 現状までの計算済み配列を返してもいいと思う
            return color

        now_vertex = queue.popleft()
        # インデックスがない場合は
        for next_vertex in node[now_vertex]:

            if check[next_vertex] == True:
                continue

            queue.append(next_vertex)
            check[next_vertex] = True

            # 問題固有の計算
            color[next_vertex] = color[now_vertex]+1
            visited_num += 1

    return color


mod = 10**9+7
# n=int(input())
# n,m=list(map(int,input().split()))
# a=list(map(int,input().split()))
def ceil(x, y): return (x+y-1)//y
def input_list(): return list(map(int, input().split()))


n = int(input())
s = input()
w = s.count("W")
#print(s[w*-1:])
print(w-s[w*-1:].count("W"))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
rcount=0
for i in s:
	if(i=='R'):
		rcount+=1
ans=0
for i in range(rcount,n):
	if(s[i]=='R'):
		ans+=1
print(ans)

Submission status: Accepted

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

    w = C.count("W")
    r_right = C[-w:].count("R")

    print(min(n-w,w,r_right))
main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import deque

def main():
    n = int(input())
    c = input()

    cnt = 0
    que = deque(c)

    while que:
        r = que.pop()
        if r == 'R':
            while que:
                l = que.popleft()
                if l == 'W':
                    cnt += 1
                    break

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

n=0
for i in range(N-1):
    if (S[i]=='W' and S[i+1]=='R'):
        n+=1

if n==0 :
    ans = 0
else:
    ans=0
    p=N-1
    for i in range(N):
        if S[i]=='W':
            found = 0
            while p>i and found == 0:
                # print("i=",i," S[i]=",S[i]," p=",p," S[p]=",S[p])
                if S[p]=='R':
                    ans+=1
                    found = 1 

                p-=1


print(ans)

Submission status: Accepted

Solution code:
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N = ii()
    c = list(input())
    r = c.count('R')
    if r == 0:
        print(0)
        return
    print(r-c[:r].count('R'))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
cnt=0
res=10**30
rcum=[0]*(N+1)
wcum=[0]*(N+1)
for i in range(N):
    rcum[i+1]=rcum[i]+(c[i]=='R')
    wcum[i+1]=wcum[i]+(c[i]=='W')
for i in range(N+1):
    a=wcum[i]+(rcum[N]-rcum[i])
    res=min(res,a)
for i in range(N+1):
    b=max(wcum[i],(rcum[N]-rcum[i]))
    res=min(res,b)
    

print(res)

Submission status: Accepted

Solution code:
def solved():
    n = int(input())
    colors = input()
    red = colors.count('R')
    white = n - red

    wrong_red = 0
    for i in range(red):
        if colors[i] != 'R':
            wrong_red += 1
    wrong_white = 0
    for i in range(white):
        if colors[red + i] != 'W':
            wrong_white += 1
    assert wrong_white == wrong_red
    print(wrong_red)


if __name__ == '__main__':
    solved()

Submission status: Accepted

Solution code:


def main():
    n = int(input())
    C = input()
    w_count = C.count("W")
    if w_count == 0:
        print(0)
        return

    right_ws = C[-w_count:].count("W")
    diff = w_count - right_ws
    print(diff)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
    return int(input())
def readInts():
    return list(map(int, input().split()))
def readChar():
    return input()
def readChars():
    return input().split()

n = readInt()
c = list(input())
ans = 0
i = len(c)-1
j = 0
while 1:
    rflag = 0
    for i in range(i,-1,-1):
        if c[i]=="R":
            rflag=1
            break

    wflag = 0
    for j in range(j,len(c)):
        if c[j]=="W":
            wflag=1
            break
    if wflag and wflag and j<i:
        c[i]="W"
        c[j]="R"
        ans+=1
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 0
d = {"RW": 0, "WR": 0}
for i in range(N):
    if S[i] == "R":
        d["RW"] += 1
ans = d["RW"]
for i in range(N):
    if S[i] == "R":
        d["RW"] -= 1
    else:
        d["WR"] += 1
    ans = min(ans, max(d["RW"], d["WR"]))
print(ans)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    c = input()
    r = c.count('R')
    w = c.count('W')
    ropt = c[:r].count('R')
    wopt = c[r:].count('W')

    rnot = r - ropt
    wnot = w - wopt

    return min(rnot,wnot)
    
 
if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

r=0
w=0

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

delete=0

for j in range(r):
    if c[j]=="W":
        delete+=1

if r>=delete:
    print(delete)
else:
    print(delete + (r-delete))

Submission status: Accepted

Solution code:
N = int(input())
C = input()
nw = C.count('W')
nr = N - nw

if nw == N or nr == N:
    print(0)
else:
    a = C[:nr]
    b = C[nr:]
    nw = a.count('W')
    nr = b.count('R')
    n = min(nw, nr)
    m = max(nw, nr) - n
    print(n + m)

Submission status: Accepted

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

r = []
for i in range(n):
    if c[i] == 'R':
        r.append(i)

count = 0
for i in range(len(r)):
    if r[i] != i:
        r.insert(i, i)
        r.pop()
        count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
w = []
r = []
for i, ci in enumerate(c):
    if ci == 'W':
        w.append(i)
    if ci == 'R':
        r.append(i)
# print(w, r)
answer = 0
lenw = len(w)
lenr = len(r)
while answer < min(lenr, lenw) and w[answer] < r[-answer-1]:
    # print(w[answer], r[-answer])
    answer += 1
print(answer)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    N = int(input())
    C = list(input().strip())
    l, r = 0, N-1
    ans = 0
    if 'R' not in C or 'W' not in C:
        print(0)
        return True
    while l < r:
        while C[l] == 'R':
            l += 1
        while C[r] == 'W':
            r -= 1
        if l < r:
            ans += 1
            l += 1
            r -= 1
            
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count('W')
r=s.count('R')
ans=min(w,r)
ans=min(ans,r-s[:r].count('R'))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
w = deque([])
r = deque([])
s = input()
for i in range(n):
  if s[i] =="W":
    w.append(i)
  else:
    r.append(i)

ans = 0
if len(r)==0 or len(w)==0:
  print(0)
  quit()
  
while r[-1]>w[0]:
  rtemp = r.pop()
  wtemp = w.popleft()
  r.appendleft(wtemp)
  w.append(rtemp)
  ans += 1
  #print(r)
  #print(w)
  #print(" ")
print(ans)

Submission status: Accepted

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

R = 0
W = 0


for i in range(n):
    if c[i] == "R":
        R += 1
W = n - R
RR = 0
WW = 0
i = 0
while i<R:
    if c[i] == "W":
        WW +=1 
    i += 1
print(WW)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
ans = 0
r = False
w = False
for i in c:
    if i == 'W':
        w = True
    else:
        r = True
    if r == True and w == True and i == 'R':
        ans += 1
c.reverse()
r = False
w = False
ans2 = 0
for i in c:
    if i == 'W':
        w = True
    else:
        r = True
    if r == True and w == True and i == 'W':
        ans2 += 1
cnt = c.count('W')
ans3 = 0
for i in range(N):
    if c[i] == 'R' and cnt > 0:
        ans3 += 1
        cnt -= 1
    if c[i] == 'W':
        cnt -= 1
    #print(ans3,cnt)
print(min(ans,ans2,ans3))

Submission status: Accepted

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

W_cnt=c.count("W")
R_cnt=c.count("R")

wcnt=0
rcnt=0
for n in range(R_cnt):
    if c[n]=="W":
        wcnt+=1
for n in range(R_cnt+1,N):
    if c[n]=="R":
        rcnt+=1
print(max(wcnt,rcnt))

Submission status: Accepted

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

l, r = 0, N - 1
cnt = 0

while l < r:
    L, R = c[l], c[r]
    if L == 'W' and R == 'R':
        c[l], c[r] = 'R', 'W'
        l += 1
        r -= 1
        cnt += 1
    elif L == 'W' and R == 'W':
        r -= 1
    elif R == 'R' and R == 'R':
        l += 1
    else:
        l += 1
        r -= 1

print(cnt)

Submission status: Accepted

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

red = m.count('R')

con = 0;

for i in range(red):
    if m[i] == 'W':
        con += 1
print(con);

Submission status: Accepted

Solution code:
N = int(input())
stones = str(input())

mae = 0
ushiro = N - 1

swap = 0

while mae < ushiro:
    if stones[mae] == "W":
        while mae < ushiro:
            if stones[ushiro] == "R":
                swap += 1
                ushiro -= 1
                break
            else:
                ushiro -= 1
                
        else:
            break
        mae += 1
        
    else:
        mae += 1
        
print(swap)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
c = instr()
ans = 0

t = {"R":0, "W":0}

for i in range(n):
    t[c[i]] += 1

num_r = t["R"]

for i in range(num_r):
    if c[i] == "W":
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
r = c.count("R")
cc = c[0:r]
print(cc.count("W"))

Submission status: Accepted

Solution code:
N = int(input())
stones = list(input().rstrip())
r = stones.count("R")

print(stones[r:].count("R"))

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()
C = input()

# 石を二つ選ぶ
# 1こ選んで色を変える

# 最小回数を求める
# 効率がいい方法を考える

# Rを左に寄せ、Wを右に寄せる

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

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

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
C = input()
p = Counter(C)["R"]
if p == 0 or p == N:
  print(0)
  exit()
r = 0
w = 0

for i,c in enumerate(C):
  if i < p:
    if c == "W":
      w += 1
  else:
    if c == "R":
      r += 1
print(max(r,w))

Submission status: Accepted

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

Wcnt = S.count('W')
Rcnt = S.count("R")

C = "R" * Rcnt + "W" * Wcnt

diff = 0
for i in range(len(S)):
    if C[i] != S[i]:
        diff += 1

ans = diff // 2
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    C = input()
    l = 0
    r = len(C)-1
    cnt = 0
    while l < r:
        while l < len(C) and C[l] == 'R':
            l += 1
        while r >= 0 and C[r] == 'W':
            r -= 1

        if l >= r:
            break

        if C[l] == 'W' and C[r] == 'R':
            # C[l], C[r] = C[r], C[l]
            cnt += 1
            l += 1
            r -= 1
    
    print(cnt)
    
resolve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    C = S()

    wi, ri = 0, N-1
    ans = 0

    while ri >= 0 and wi < N:
        while wi < N and C[wi] != "W":
            wi += 1
        while ri >= 0 and C[ri] != "R":
            ri -= 1
        if wi >= ri:
            break
        if wi < N and ri >= 0:
            C[wi], C[ri] = C[ri], C[wi]
        wi += 1
        ri -= 1
        ans += 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

r = 0
w = 0
for c in C:
    if c == 'R':
        r += 1
    else:
        w += 1

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

ans = 0
if r < w:
    for rx in range(r):
        if C[rx] != 'R':
            ans += 1
else:
    for wx in range(w):
        if C[N-1-wx] != 'W':
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
C = deque(list(input()))

ans = 0
while C:
    while C and C[0]=='R':
        _ = C.popleft()
    while C and C[-1]=='W':
        _ = C.pop()
    if C:
        ans += 1
        _ = C.popleft(), C.pop()

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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


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

if r_cnt == n:
    print(0)
else:
    print(r_cnt - r_cnt_2)

Submission status: Accepted

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

wCounter = 0
for i in range(n):
    if cA[i] == "W":
        wCounter+=1

flag = 0
count = 0
for i in range(n-1, -1, -1):
    if flag == 0 and cA[i] == "R":
        flag = 1
    if flag == 1 and i < n-wCounter and cA[i] == "W":
        count+=1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
i=c.count('R')
j=c.count('W',0,i)
print(j)

Submission status: Accepted

Solution code:
N=int(input())
X=input()
aka=0
siro=0
for i in range(N):
    if(X[i]=="R"):
        aka+=1
    else:
        siro+=1
c=0
for i in range(aka):
    if(X[i]=="W"):
        c+=1
print(c)

Submission status: Accepted

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

ans = 0

if (len(c) > c.count('R')) and (c.count('R') != 0):
  r_c = c[:c.count('R')]
  ans = r_c.count('W')

print(ans)

Submission status: Accepted

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

al = 0
for c in C:
    if c == 'R':
        al += 1

so = 0
for c in C[:al]:
    if c == 'W':
        so += 1

print(so)

Submission status: Accepted

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N = int(input())
    c = input()
    countlist = [0]
    count = 0
    for i in c:
        if i == 'W':
            count += 1
            countlist.append(count)
        else:
            countlist.append(count)
    ans = 10**6
    for i in range(N+1):
        lnum = countlist[i]
        rnum = N - i - (countlist[-1] - countlist[i])
        difnum = min(lnum, rnum)
        lnum -= difnum
        rnum -= difnum
        tmp = difnum
        tmp += lnum + rnum
        if tmp < ans:
            ans = tmp
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:

import collections


def main():
    n = int(input())
    c = list(input())
    wcnt = c.count("W")
    rcnt = c.count("R")
    if wcnt == 0 or rcnt == 0:
        print(0)
        return
    ans = c[0:rcnt].count("W")
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = input()
d = sorted(c)
c_sorted = "".join(d)
r_count =c.count("R")
ans = 0
for i in range(r_count):
    if c[i] != c_sorted[i]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
cs = [i for i in input()]
n_R = 0
for c in cs:
    if c == 'R':
        n_R += 1
n_n_R = 0
for i in range(n_R):
    if cs[i] == 'R':
        n_n_R += 1
print(n_R - n_n_R)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
t=c.count('R')
s=len(c)
if t==s:
  print(0)
  exit()
u=c[t:]
v=u.count('R')
print(v)

Submission status: Accepted

Solution code:

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

cnt = S.count("R")
ans = S[cnt:].count("R")
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
c = input()
r,w,cnt = 0,0,0
s = 0
flag = False
for i in range(N):
    if c[i] is 'W':
        w += 1
    elif c[i] is 'R':
        r += 1
for i in range(r):
    if c[i] is 'R':
        cnt += 1
print(min(min(r,w),r-cnt))

Submission status: Accepted

Solution code:
n=int(input())
c=input()
wlist=[]
rlist=[]
cntw=0
cntr=0
for i in range(n):
  if c[i]=='R':
    rlist.append(i)
    cntr+=1
  else:
    wlist.append(i)
    cntw+=1
rlist.sort(reverse=True)
cnt=0
for j in range(min(cntw,cntr)):
  if rlist==[] or wlist==[]:
    break
  elif rlist[j]>wlist[j]:
    cnt+=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
C = input()
R_all = C.count('R')
R_left = C[:R_all].count('R')
print(R_all - R_left)

Submission status: Accepted

Solution code:
# 2020/08/02
# AtCoder Beginner Contest 174 - D

# Input
n = int(input())
c = input()

# Count R
rcnt = 0
for i in range(n):
    if c[i] == 'R':
        rcnt = rcnt + 1

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

# Output
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
w=0
for i in range(n):
    if c[i]=="W":
        w+=1
w1=0
t=100
s=0
for i in range(n+1):
    r=n-i-(w-w1)
    if abs(w1-r)==1 and t!=0:
        t=1
        s=w1
    if w1==r:
        t=0
        s=w1
    if i!=n:
        if c[i]=="W":
            w1+=1
print(s+t)

Submission status: Accepted

Solution code:
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7


w=[]
r=[]
n=int(data())
s=data()
for i in range(n):
    if s[i]=='R':
        r.append(i)
    else:
        w.append(i)
cnt=0
w=w[::-1]
while r and w:
    if r.pop()<w.pop():
        break
    cnt+=1
out(cnt)

Submission status: Accepted

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

cw=c.count('W')
cr=c.count('R')

ans_s='R'*cr+'W'*cw

cnt=0
for i in range(n):
  if c[i]!=ans_s[i]:
    cnt+=1

print((cnt+2-1)//2)

Submission status: Accepted

Solution code:

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

rcount = 0
wcount = 0
for i in range(N):
    if c[i] == 'W':
        wcount += 1
    else:
        rcount += 1

rst = 0
wst = 0
for i in range(N):
    if i <= rcount-1:
        if c[i] == 'W':
            wst += 1
    else:
        if c[i] == 'R':
            rst += 1
ans = max(wst, rst)
wst -= ans
rst -= ans

ans += wst
ans += rst

print(ans)