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 deque
n = int(input())
c = input()
d = deque()
a = 0; b = 0
for i in range(n):
    d.append(c[i])
for _ in range(len(d)):
    if d[0] == "R":
        d.popleft()
    else:
        break
for _ in range(len(d)):
    if d[-1] == "W":
        d.pop()
    else:
        break
for i in range(len(d)):
    if d[i] == "R": a += 1
for i in range(a):
    if d[i] == "W": b += 1
print(b)

Submission status: Accepted

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

Submission status: Accepted

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



rn=c.count("R")
print(c[:rn].count("W"))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    c = input()

    r = N - 1
    l = 0
    r_ch, l_ch = True, True
    ans = 0
    while True:
        if l_ch:
            if c[l] == "W":
                l_ch = False
            l += 1
        if r_ch:
            if c[r] == "R":
                r_ch = False
            r -= 1
        if l_ch is False and r_ch is False:
            ans += 1
            r_ch, l_ch = True, True
        if l-1 >= r+1:
            break

    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
cli = list(input())

cli_count = Counter(cli)

white = cli_count['W']
red = cli_count['R']

count = 0
for c in cli:
    if red == 0:
        break

    if c == 'W':
        count += 1
        red -= 1
    else:
        red -= 1

print(count)

Submission status: Accepted

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(r, n):
    if s[i] == "R":
        q += 1

print(min(p, q))

Submission status: Accepted

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

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


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


n = I()
s = list(S())

w = 0
r = n - 1
ans = 0
while True:
    for i in range(w, n):
        if s[i] == "W":
            w = i
            break
    else:
        w = -1
    for i in range(r, 0, -1):
        if s[i] == "R":
            r = i
            break
        r += 1
    else:
        r = -1
    if w == -1 or r == -1:
        print(ans)
        exit()
    else:
        if r < w:
            print(ans)
            exit()
        ans += 1
        s[w] = "R"
        s[r] = "W"
        w += 1
        r -= 1

Submission status: Accepted

Solution code:
N=int(input())
c=list(map(str,input()))
#print(c)
W=0;R=0;w=0;r=0;p=0
for i in range(N):
  if c[i]=="W":
    W=W+1
R=len(c)-W
#print(R,W)
for i in range(R):
  if c[i]=="W":
    p=p+1
if W==0 or R==0:
  print(0)
else:
  print(p)

Submission status: Accepted

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

l, r = 0, n-1
if not('R' in s):
    print(0)
    exit()

if not('W' in s):
    print('0')
    exit()

ans = 0
while 1:
    while s[l] == "R":
        l += 1
    while s[r] == "W" and l != r:
        r -= 1

    if l != r:
        s[l], s[r] = s[r], s[l]
        ans += 1
        # print(s)
    if l == r:
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

l = 0
r = n-1

cnt = 0

while r > l:
    if c[l] == "R":
        l = l + 1
    else:
        while r > l:
            if c[r] == "W":
                r = r - 1
            else:
                cnt = cnt + 1
                r = r - 1
                l = l + 1
                break

print(cnt)

Submission status: Accepted

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

N = int(readline().strip())
C = readline().strip()
c = []
for i in range(len(C)):
  c.append(C[i])

ans = 0
s = 0
e = len(c)-1
d = True
while s < e:
  si = s
  ei = e
  for i in range(si, ei):
    if c[i] == "R":
      s += 1
    else:
      break
  for j in range(ei, si, -1):
    if c[j] == "W":
      e -= 1
    else:
      break
  if s < e:
    c[s] = "R"
    c[e] = "W"
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
r_num = c.count("R")
ref = c[:r_num]
w_num = ref.count("R")
print(r_num - w_num)

Submission status: Accepted

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

r_count = 0
for i in range(N):
    if(c[i] == "R"):
        r_count += 1
w = 0
for i in range(r_count):
    if(c[i] == "W"):
        w += 1

print(w)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]

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

ans = 0
r = 0
#Rの総数を数える
for i in range(n):
    if c[i] == 'R':
        r+=1

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

for i in range(n):
    if c[i] == 'W':
        r -= 1
        ans+=1
    else:
        r -= 1
    if (r == 0):
        break
print(ans)

Submission status: Accepted

Solution code:
def redc(arr):
    res=0
    for i in range(len(arr)):
        if arr[i]=='R':
            res+=1
    return res
        
n=int(input())
arr=list(input())
m=redc(arr)
ans=m-redc(arr[0:m])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l ==[]:
  print(0)
elif l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))

Submission status: Accepted

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

rs = clist.count("R")
ws = clist.count("W")

sort_rs = rs - clist[0:rs].count("R")
ans = rs
if ws < ans:
    ans = ws

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

Submission status: Accepted

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

R_num=c.count('R')
c_ext=c[:R_num]
W_num=c_ext.count('W')
print(W_num)

Submission status: Accepted

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

    ans = 0
    lp = 0
    rp = N-1
    try:
        while lp < rp:
            while lp < N and c[lp] == "R":
                lp += 1
            while rp > -1 and c[rp] == "W":
                rp -= 1
            if lp < rp:
                ans += 1
                lp += 1
                rp -= 1
    except Exception as err:
        print(err)
        print(c, lp, rp)
    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=0
r=0
for i in s:
    if i=='W':
        w+=1
    else:
        r+=1
if r==0 or w==0:
    print(0)
else:
    t='R'*r
    t=t+'W'*w
    #print(t)
    ans=0
    for i in range(len(s)):
        if s[i]!=t[i]:
            ans+=1
    ans=ans//2
    print(ans)

Submission status: Accepted

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

num_w = c.count('W')

ans = 'R' * (N-num_w) + 'W' * num_w
cnt = 0
for i in range(N-num_w):
    if c[i] != ans[i]:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

l = list(input())

begin = 0
end = n-1

cnt = 0
while(True):
    
    if(end==-1):
        break
    if(begin==n):
        break
    for i in range(begin,end+1):
        w_place = i
        if(l[w_place]=='W'):
            begin=w_place
            break
        if(i==end):
            begin=end
    for j in range(end,begin-1,-1):
        r_place = j
        if(l[r_place]=='R'):
            end=r_place
            break
        if(j==begin):
            end=begin
    if(begin>=end):
        break
    else:
        cnt += 1
        l[begin] = 'R'
        l[end] = 'W'
    begin += 1
    end -= 1

print(cnt)

Submission status: Accepted

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

ans = 0
rightNum = n-1
flag = 0
for i in range(n):
    if c[i] == "W":
        for j in range(n):
            if i >= rightNum-j:
                flag = 1
                break
            if c[rightNum-j] == "R":
                c[rightNum-j] = "W"
                c[i] = "R"
                rightNum -= j+1
                ans += 1
                break
    if flag == 1:
        break
print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
s = sr()
ans = inf
a = [0 for i in range(n)]
a1 = [0]
for i,c in enumerate(s):
    if c == 'W':
        a[i]=1
for num in a:
    a1.append(a1[-1]+num)
def sumArea(l, r):
    return a1[r]-a1[l]

for k in range(n+1):
    numW0k = sumArea(0,k)
    numRkn = n-k-sumArea(k,n)
    pre = 0
    if numW0k >= numRkn:
        pre = numRkn+(numW0k-numRkn)
    else:
        pre = numW0k+(numRkn-numW0k)
    ans = min(ans, pre)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
cnt=[0,0]
a=input()
for i in range(len(a)):
  if a[i]=='R':
    cnt[0]+=1
  else:
    cnt[1]+=1   
cnt_=[0,0]
for j in range(cnt[0]):
  if a[j]=='R':
    cnt_[0]+=1   
print(cnt[0]-cnt_[0])

Submission status: Accepted

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


cnt = 0
for i in range(n):
  w_idx = c.find("W")
  r_idx = c.rfind("R")
  if w_idx < r_idx and w_idx != -1:
    c = c[w_idx+1:r_idx]
    cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
p,q = 0,n-1
x = 0
while(1):
    while(c[p] == 'R' and p < n-1):
        p += 1
    while(c[q] == 'W' and q > 0):
        q -= 1
    if(p < n-1 and q > 0 and p < q):
        p += 1
        q -= 1
        x += 1
    else:
        break
print(x)

Submission status: Accepted

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

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

ans = 0
for i in range(n-w):
    if s[i] =='W':
       ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c_str = input()
R_cnt = c_str.count('R')
tmp_cnt = c_str[:R_cnt].count('R')
print(R_cnt - tmp_cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cR = s.count("R")
cW = n - cR
print(s[cR:].count("R") )

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = 0
right = n-1
for i in range(n):
    if s[i] == "W":
        while right >= i:
            if s[right] == "R":
                s[right] = "W"
                s[i] = "R"
                ans += 1
                right -= 1
                break
            else:
                right -= 1


print(ans)

Submission status: Accepted

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

nw = C.count('W')

ans = 0
for _ in range(N):
    if nw == 0:
        break
    tmp = C.pop()
    if tmp == 'W':
        nw -= 1
    else:
        ans += 1
        nw -= 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    C = input()
    
    ANS = 0
    while 1 == 1:
        i = C.find('W')
        j = C.rfind('R')
        if i == -1 or j == -1:
            break
        if i >= j:
            break
        if i + 1 == j:
            ANS += 1
            break
        C = C[i+1:j]
        ANS += 1
    print(ANS)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

r=c.count('R')
w=0
ans=r
for ch in c:
    if ch=='W':
        w+=1
    else:
        r-=1
    m=min(r,w)
    ans=min(r+w-m, ans)

print(ans)

Submission status: Accepted

Solution code:
#abc174 d

import sys
input = sys.stdin.readline

N=int(input())
c=input().rstrip()
R =[0]*(N+1)
W =[0]*(N+1)

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

ans = float('inf')

for i in range(N+1):
    tmp = min(W[i],Rall-R[i])+abs(W[i]-(Rall-R[i]))
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
if len(set(c))==1:
    print('0')
else:
    r=c.count('R')
    w=c.count('W')
    rc=c[:r].count('R')
    wc=c[:r].count('W')
    if rc==0:
        if r<w:
            print(r)
        else:
            print(w)
    else:
        if r-rc<wc:
            print(r-rc)
        else:
            print(wc)

Submission status: Accepted

Solution code:
# うまく行けば２こ一気に解消できるケースがある
# RRRRWWWWをつくれば良い，一番右のRと一番左のWを入れ替えていく
N = int(input())
C = input()
RL=[]
WL=[]
for i,c in enumerate(C):
    if c == 'R':
        RL.append(-i)
    else:
        WL.append(i)

import heapq
heapq.heapify(RL)
heapq.heapify(WL)
count = 0
if not RL or not WL:
    print(0)
    exit()

while True:
    r = heapq.heappop(RL)
    r = -r
    w = heapq.heappop(WL)
    if w>r:
        print(count)
        exit()
    heapq.heappush(RL,-w)
    heapq.heappush(WL,r)
    count+=1

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
cnt = c.count('R')
b = c[cnt:]
print(b.count('R'))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
w = S.count('W')
r = S.count('R')
A = 'R'*r+'W'*w
count = 0
for i in range(len(S)):
    if S[i]!=A[i]:
        count += 1

if count%2 ==0:
    print(count//2)
else:
    print(count//2 + 1)

Submission status: Accepted

Solution code:
import math
# import bisect
import sys
# from collections import Counter
input = sys.stdin.readline
def inp():
	return(int(input()))
def inlt():
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(s[:len(s)-1])
def invr():
	return(map(int,input().split()))
def print_fract(p,q):
	g = math.gcd(p,q)
	p//=g
	q//=g
	print(str(p)+"/"+str(q))
# list1, list2 = zip(*sorted(zip(list1, list2)))
# K = inp()
# if K%2 == 0 or K%5 == 0:
# 	print(-1)
# else:
# 	i = 7
# 	while True:
# 		if len(str(i)) == 999982:
# 			break
# 		if i%K == 0:
# 			print(len(str(i)))
# 			break
# 		i = (10*i) + 7
# 777777777777
N = inp()
S = insr()
print(S[:S.count("R")].count("W"))

Submission status: Accepted

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

# print(line)
bottom_w = N
top = 0
count = 0
while(bottom_w > top):
    for i in reversed(range(bottom_w)):
        if(line[i]=='R'):
            bottom_w = i
            break

    if(line[-1]=='W' and bottom_w == N):
        print(0)
        exit()
    if(bottom_w <= top):
        break
    for i in range(top,N):
        if(i == N-1):
            print(count)
            exit()
        if(line[i]=='W' and bottom_w > i):
            # print("line",i,line[i])
            # print("swap",i,bottom_w)
            count += 1
            line[bottom_w] = 'W'
            top = i+1
            break
print(count)

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
S = list(input())
W = [0]*N
R = [0]*N
ans = N

for i in range(N):
    if S[i] == "W":
        W[i] = 1
    else:
        R[i] = 1
R.reverse()
R = list(accumulate(R))
W = list(accumulate(W))
if R[N-1] == 0 or W[N-1] == 0:
    ans = 0
else:
    sakai = R[N-1]
    ans = max(W[sakai-1], R[N-sakai-1])
print(ans)

Submission status: Accepted

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

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

bad_w = 0
red = 0
ok = True
for i in range(N-1,-1,-1):
    if C[i] == 'R':
        red += 1
        ok = False
    if not ok and C[i] == 'W':
        bad_w += 1

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


print( min(bad_w, N-bad_w, red-good_red) )

Submission status: Accepted

Solution code:
c = []

n = int(input())
c = input()
counterR = 0
counterW = 0
flag = 0

for i in range(n):
    if(c[i] == 'R'):
        counterR += 1 # the number of 'R'

if(counterR == n): # if c[i] are all 'R'
    print('0')
    flag = 1

else:
    for i in range(counterR):
        if(c[i] == 'W'):
            counterW += 1

if(flag == 0):
    print(counterW)

Submission status: Accepted

Solution code:
from collections import Counter

# print('input >>')
N = int(input())
c = input()

counter = Counter(c)
R_cnt = counter['R']
W_cnt = counter['W']

ans = 0
for s in c[:R_cnt]:
    if s == 'W':
        ans += 1

# print('-----output-----')
print(ans)

Submission status: Accepted

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

r = N-1
l = 0
ans = 0

while C[r] == 'W':
    r -= 1
    if r < 0:
        print(0)
        exit()
while C[l] == 'R':
    l += 1
    if l >= N:
        print(0)
        exit()
while l < r:
    C[r], C[l] = C[l], C[r]
    ans += 1
    while C[r] == 'W':
        r -= 1
        if r < 0:
            break
    while C[l] == 'R':
        l += 1
        if l >= N:
            break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
md = c.count('R')
rc, wc = c[md:].count('R'), c[:md].count('W')
if rc==wc: print(rc)
elif rc > wc: print(rc+(rc-wc))
else: print(wc+wc-rc)

Submission status: Accepted

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

n = int(input())
s = list(input())
r = s.count('R')
w = s.count('W')

all_paint = min(r, w)
all_convert_r = r - s[0:r].count('R')
all_convert_w = w - s[n-w:n].count('S')
print(min(all_paint, all_convert_r, all_convert_w))

Submission status: Accepted

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

w_count = c.count('W')

move_count = c[-w_count:].count('R')
if w_count == 0:
    print(0)
else:
    print(move_count)

Submission status: Accepted

Solution code:
# Problem D - Alter Altar
# input
N = int(input())
c_list = list(input())

# initialization
red_count = 0
white_count = 0
swap_count = 0

# count
for c in c_list:
    if c=="R":
        red_count += 1
    else:
        white_count += 1

# swap count
w_i = 0
r_i = N-1
while r_i>w_i:
    if c_list[r_i]=="R":
        find_white = False
        # white search
        while r_i>w_i and w_i<=N-1:
            if c_list[w_i]=="W":
                find_white = True
                break
            w_i += 1
        # swap
        if find_white:
            tmp = c_list[r_i]
            c_list[r_i] = c_list[w_i]
            c_list[w_i] = tmp
            swap_count += 1
    else:
        r_i -= 1

# output
ans = min(red_count, white_count, swap_count)
print(ans)

Submission status: Accepted

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

cnt = 0

ans = "".join(sorted(c))
ans_ = list(ans)
ans_[-1] = "R"
ans_ = "".join(ans_)


cnt = 0
for i in range(n-1):
    if c[i] != ans[i]:
        cnt += 1
    
        
print(cnt//2 + cnt%2)

Submission status: Accepted

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

Nr = C.count("R")
ans = C[:Nr].count("W")
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
rh_list = list(input())
#Rの個数
r_n = rh_list.count('R')
#Rの個数までのlist
R_list=[]
for i in range(r_n):
  R_list.append(rh_list[i])
#listのｒ個数
R_r_n = R_list.count('R')
print(r_n-R_r_n)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
w = sum([True for c in C if c == "W"])
r = N - w

count = 0
for i, c in enumerate(C):
  if i < r and c == 'W':
    count += 1
print(count)

Submission status: Accepted

Solution code:
import sys

n = int(input())

c = input()

count = 0
check = 0
wList = []
rList = []

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

if len(wList) == 0 or len(rList) == 0:
    print(0)
    sys.exit()

rMax = rList[-1]
wMin = wList[0]

for i in range(len(rList)):
    if rList[-1] < wList[0]:
        break
    else:
        count += 1
        del rList[-1]
        del wList[0]

print(count)

Submission status: Accepted

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline

########################################

n = int(input())
s = input()
i = 0
j = n-1
ans2 = 0
while i < j:
	while s[i] == "R":
		i+=1
	while s[j] == "W":
		j-=1
	if i < j: 
		ans2+=1
		i+=1
		j-=1
print(ans2)

Submission status: Accepted

Solution code:

n = int(input())
c = list(input())
c = c[::-1]
w = c.count("W")
r = c.count("R")
ans = 0
for i in c:
    if w == 0 or r == 0:
        print(ans)
        break
    if i == "R":
        w -= 1
        r -= 1
        ans += 1
    else:
        w -= 1

Submission status: Accepted

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

a = 0
rlist = []
wlist = []
for i in range(n):
  if stones[i] == "W":
    wlist.append(i+1)
  elif stones[i] == "R":
    rlist.append(i+1)
rlist.sort(reverse=True)
r_numbers =  len(rlist)
w_numbers =  len(wlist)

if r_numbers == 0 or w_numbers == 0:
  print(0)
elif r_numbers>=w_numbers:
  count = 0
  while rlist[count] > wlist[count] and count < w_numbers-1:
    count += 1
  if rlist[count] > wlist[count]:
    print(count+1)
  else:
    print(count)
elif r_numbers<=w_numbers:
  count = 0
  while rlist[count] > wlist[count] and count < r_numbers-1:
    count += 1
  if rlist[count] > wlist[count]:
    print(count+1)
  else:
    print(count)

Submission status: Accepted

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

cnt = 0
r = c.count('R')

r_s = c[:r].count('R')

print(r - r_s)

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    S = input().rstrip('\n')
    S = [s for s in S]

    l = 0
    r = N-1
    ans = 0
    while True:
        if l >= r:
            break
        if S[l] == "W" and S[r] == "R":
            S[l], S[r] = S[r], S[l]
            ans += 1
            l += 1
            r -= 1
        elif S[l] == "W" and S[r] == "W":
            r -= 1
        elif S[l] == "R" and S[r] == "R":
            l += 1
        else:
            l += 1
            r -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    lw, rr = -1, -1
    for i, c in enumerate(C):
        if lw == -1 and c == "W":
            lw = i
        elif c == "R":
            rr = i
    if lw == -1 or rr == -1:
        print(0)
        return
    ans = 0
    while rr > lw:
        C[lw] = "R"
        C[rr] = "W"
        ans += 1
        while lw < N:
            lw += 1
            if C[lw] == "W":
                break
        while rr >= 0:
            rr -= 1
            if C[rr] == "R":
                break
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = input()
Best = ('R'*C.count('R'))+('W'*C.count('W'))
Count = 0
for T in range(0,N):
    if Best[T]!=C[T]:
        Count += 0.5
print(int(Count))

Submission status: Accepted

Solution code:
N = int(input())
c = str(input())
ans = 0
left = 0
right = N-1
while(left < right):
    if c[right] == 'W':
        # 右側で見ているのは白い石。影響がないのでその一つ左を見ることにする
        right -= 1
    elif c[right] == 'R':
        if left < right and c[left] == 'R':
            # 左側で見ているのは赤い石。影響がないのでその一つ右を見ることにする
            left += 1
        elif left < right and c[left] == 'W':
            # この時、左側で見ている白の石と右側で見ている赤の石を交換する。
            ans += 1
            # 左側で見ていた白は赤になり、影響がなくなったので、その一つ右をみる。
            left += 1
            # 右側で見ていた赤は白になり、影響がなくなったので、その一つ左をみる。
            right -= 1

print(ans)

Submission status: Accepted

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

w = []
r = []
for i in range(n):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)

w_len = len(w)
r_len = len(r)
if w_len == 0 or r_len == 0:
  print(0)
  exit(0)

if w_len < r_len:
  search = w_len
else:
  search = r_len

w_min = w[0]
r_max = r[r_len-1]

if r_max < w_min:
  print(0)
  exit(0)

count = 0
for i in range(search):
  tmp = w[i]
  w[i] = r[r_len-i-1]
  r[r_len-i-1] = tmp
  count += 1
  
  if w_len > count and (r_len-count-1) >= 0:
    w_min = w[count]
    r_max = r[r_len - count - 1]
  else:
    w_min = w[w_len-1]
    r_max = r[0]
    
  if r_max < w_min:
    print(count)
    exit(0)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input().strip())

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    rw = input()
    r_num = 0
    for i in rw:
        if i == 'R':
            r_num += 1
    w_num = n - r_num
    ans = 0
    for i in range(r_num):
        if rw[i] == 'W':
            ans += 1
    print(ans)
    return
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
a=S.count("W")
b=S.count("R")
print(min([S[:b].count("W"),a,b]))

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
wpos = 0
rpos = n-1
count = 0
while wpos < rpos :
  while c[wpos] == "R":
    wpos += 1
    if wpos > rpos: break
  while c[rpos] == "W":
    rpos -= 1
    if wpos > rpos: break
  if wpos < rpos :
    count += 1
    c[wpos],c[rpos]=c[rpos],c[wpos]
print(count)

Submission status: Accepted

Solution code:
import collections as col

N = int(input())
C = input()
mp = col.Counter(C)
ideal = ''
ideal += ('R' * mp['R'])
ideal += ('W' * mp['W'])

ans = 0
for i, c in zip(ideal,C):
    if i!=c:
        ans += 1
print(ans//2)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = readline().decode().rstrip()

w = s.count('W')
r = s.count('R')
if r == 0 or w == 0:
    print(0)
    exit()
i_r = s.index('R')
r2 = s[:r:].count('R')
print(r-r2)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Rcnt = S.count('R')
Wcnt = N-Rcnt
# Wのが少ないとき
if Wcnt < Rcnt:
    cnt = S[-Wcnt:].count('W')
    print(Wcnt - cnt)
else:
    cnt = S[:Rcnt].count('R')
    print(Rcnt-cnt)

Submission status: Accepted

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

count = 0
R_count = c_list.count("R")
for i in range(R_count):
    if c[i] == "W":
        count += 1
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cs = [0 for i in range(N)]
for i,color in enumerate(c):
    if color == "R":
        cs[i] = 1

ans = min(sum(cs), N - sum(cs))
tmp = sum(cs)


count = 0
for i in range(tmp):
    if cs[i]:
        count += 1

ans = min(ans, tmp - count)
print(ans)




"""
out 
WR



ok
RRRRRWWWWW 入れ替える
RRRRRR 全てRにする
WWWWWW 全てWにする


1100

1110111000

tmp = 6


"""