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: Runtime Error

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
initial_space = red_index_list[0]
if initial_space == 0:
    toku += 1
else:
    while True:
        right_red_inde = red_index_list.pop(-1)
        if len(red_index_list)==1:
            print(n_red - toku)
            exit()

        initial_space -= 1
        if (initial_space == 0):
            toku += 1
            break

for i in range(1, n_red):
    space = red_index_list[i] - red_index_list[i-1] - 1
    if (space == 0):
        toku += 1
    else:
        while True:
            right_red_inde = red_index_list.pop(-1)
            if len(red_index_list)==1:
                print(n_red - toku)
                exit()

            space -= 1
            if (space == 0):
                toku += 1
                break

print(n_red-toku)

Submission status: Runtime Error

Solution code:
def line_check(line, count):
    # print(line)
    try:
        first_w = line.index("W")
    except:
        return count
    next_r_from_last = len(v_a) - list(reversed(v_a)).index("R") - 1
    if next_r_from_last > first_w:
        line[first_w], line[next_r_from_last] = line[next_r_from_last], line[first_w]
        count += 1
        return line_check(line, count)
    else :
        return count



_ = input()
v = input()
v_a = [str(i) for i in v]
# v_a = ["W","R","W","R","W"]
a = line_check(v_a, 0)
print(a)

Submission status: Runtime Error

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

head = 0
tail = -1
count = 0

while head < N:
    if c[head] == "W":
        while c[tail] == "W":
            tail -= 1
        if head >= N + tail:
            print(count)
            exit()
        tail -= 1
        count += 1
    head += 1
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
c = list(map(int, input().split()))
left = 0
cnt = 0
right = len(c) - 1
while left < n and right >=0 and left < right:
	while left < n and c[left] != 'W':
		left += 1
	while right >= 0 and c[right] != 'R':
		right -= 1
	if left< n and right >= 0 and left<right:
		c[left], c[right] = c[right], c[left]
		cnt += 1
		left += 1
		right -=1 
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = int(input())
c = input()
s = Counter(c)
if s['W'] == 0:
  print('0')
elif c.index('R') == 0 and c.index('W') == s['R']:
  print('0')
elif s['R'] > c.index('R'):
  print(s['R'] - (c.index('R')+1) + c.index('R'))
else:
  print(s['R'])

Submission status: Runtime Error

Solution code:
 N = int(input().strip())
S = input().strip()
if S.count('W') == 0 or S.count('R') == 0:
    print(0)
else:
    count = 0
    
    SS = S[:len(S)//2]
    A = sorted(S)[:len(S)//2]
    
    for ss,a in zip(SS,A):
        if ss != a:
            count+=1
    print(count)

Submission status: Runtime Error

Solution code:
def countNuriNuri(s):
    if (s[0] == "W" and s[3] == "R"):
        return 2
    else:
        return 1

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

sum = 0
index = 1
big4 = []
big4.append("N")
big4.append(c[0])
big4.append(c[1])
if (len(c) > 2):
    big4.append(c[2])
else:
    big4.append("N")
while(index < len(c)):
    if (big4[1] == "W" and big4[2] == "R"):
        sum += countNuriNuri(big4)
        index += 2
        big4[0] = big4[2]
        if (index >= len(c)):
            break
        else:
            big4[1] = c[index - 1]
            big4[2] = c[index]
            big4[3] = c[index + 1]
    else:
        index += 1
        big4[0] = big4[1]
        big4[1] = big4[2]
        big4[2] = big4[3]
        if (index >= len(c)):
            break
        else:
            big4[3] = c[index + 1]

print(sum)

Submission status: Runtime Error

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

count = 0
disastar_list = []
for i in range(0, N - 1):
    if c_list[i] == 'W' and c_list[i + 1] == 'R':
        disastar_list.append(i)
        break

for i in reversed(range(0, N - 1)):
    if c_list[i] == 'R' and c_list[i - 1] == 'W':
        if (disastar_list[0] != i - 1):
            disastar_list.append(i)
            break

while len(disastar_list) > 0:
    count = count + 1
    if(len(disastar_list) == 1):
        c_list[disastar_list[0]] = 'R'
    else:
        c_list[disastar_list[1]] = 'W'
        c_list[disastar_list[0]] = 'R'

    disastar_list = []
    for i in range(0, N -1):
        if c_list[i] == 'W' and c_list[i + 1] == 'R':
            disastar_list.append(i)
            break

    for i in reversed(range(0,N)):
        if c_list[i] == 'R' and c_list[i - 1] == 'W':
            if i-1 > -1 and disastar_list[0] != i-1:
                disastar_list.append(i)
            else:
                break


print(count)

Submission status: Runtime Error

Solution code:
def function(N, c):
    c = c.lstrip('R')
    c = c.rstrip('W')
    num_R = c.count('R')
    num_W = c.count('W')

    if (num_R == 0) or (num_W == 0):
        return 0
    else:
        if num_R < num_W:
            num = 0
            for i in range(int(N/2)):
                if c[i] == 'R':
                    num += 1
            if num_R == 1:
                if c[0] == 'R':
                    return 0
                else:
                    return 1
            else:
                return num_R-num
        else:
            num = 0
            for i in range(int(N/2)):
                if c[N-1-i] == 'W':
                    num += 1
            if num_W == 1:
                if c[-1] == 'W':
                    return 0
                else:
                    return 1
            else:
                return num_W-num


if __name__ == '__main__':
    N = int(input())
    c = input()
    print(function(N, c))

Submission status: Runtime Error

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

l_tmp = 1
r_tmp = -1
# カウント
count = 0
# デバック
# print("    ", s)
# RRRRWW みたいにRを左におしやりたい
for i in range(n):
    if l_tmp - r_tmp - 1 >= n:
        break

    if s[l_tmp - 1] == "R":
        l_tmp += 1
        # print("cont", s)
        continue

    if s[l_tmp - 1] == "W":
        if s[r_tmp] == "R":
            # 交換処理
            s[l_tmp - 1] = "R"
            s[r_tmp] = "W"
            #
            l_tmp += 1
            r_tmp -= 1
            # カウント
            count += 1
            # デバック
            # print("chan", s)
            continue

        if s[r_tmp] == "W":
            # print("-------")
            # print(l_tmp, r_tmp)
            # print(s[l_tmp], s[r_tmp])
            # print("-------")
            # TODO: ここが更新処理で決めつけられない
            while s[r_tmp] == "W":
                r_tmp -= 1
                # print(s)

            # もしなければ更新処理
            if l_tmp - r_tmp - 1 == n and s[r_tmp] == "W":
                # 更新処理
                s[l_tmp - 1] = "R"
                #
                l_tmp += 1
                # カウント
                count += 1
                # デバック
                # print("chan", s)
                continue
            # あれば交換処理

print(count)

Submission status: Runtime Error

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
initial_space = red_index_list[0]
if initial_space != 0:
    while True:
        right_red_inde = red_index_list.pop(-1)
        if len(red_index_list)==1:
            print(n_red - toku)
            exit()

        initial_space -= 1
        if (initial_space == 0):
            toku += 1
            break

for i in range(1, n_red-1):
    space = red_index_list[i] - red_index_list[i-1]
    if (space == 0):
        continue
    while True:
        right_red_inde = red_index_list.pop(-1)
        if len(red_index_list)==1:
            print(n_red - toku)
            exit()

        space -= 1
        if (space == 0):
            toku += 1
            break

print(n_red)

Submission status: Runtime Error

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
from bisect import bisect_left
from collections import deque
#======================================================#
def main():
    N = II()
    cc = list(IS())
    cc_rev = cc[::-1]
    cnt = 0
    indexs = []
    for i, c in enumerate(cc):
        if c == 'W':
            indexs.append(i)
    indexs = deque(indexs)
    if cc.count('W') == 0:
        print(0)
        return 0
    for i, c in enumerate(cc_rev):
        if c == 'R':
            idx = indexs.popleft()
            if i >= idx:
                cc[idx] = 'R'
                cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(input())
# A = list("WRWWRWRRW")
import numpy as np
A = np.array(A)
W = set(np.where(A=="W")[0])
R = set(np.where(A=="R")[0])
n = 0
while (min(W)<max(R)):
    w,r = min(W),max(R)
    W.remove(w)
    R.remove(r)
    W.add(r)
    R.add(w)
    A[w],A[r] = A[r],A[w]
    n +=1
    # print(W,R)
    # if n == len(A):break
# print(A)
print(n)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()
N = I()
c = S()
ans = 0
if set(c)==1:
    print(0)
    exit()
temp1,temp2 = 0,N-1
while 1:
    if c[temp1]=='R':
        temp1 += 1
        continue
    if c[temp2]=='W':
        temp2 -= 1
        continue
    if temp1>=temp2:
        break
    ans += 1
    temp1 += 1
    temp2 -= 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

c_list = [s for s in input()]
cr = list(reversed(c_list))
back = N - 1


def check(forward):
    global back
    for i in range(back, -1, -1):
        if back < forward:
            return -1
        if c_list[i] == "R":
            back -= 1
            return back + 1


count = 0
for i, c in enumerate(c_list):
    if c_list[i] == "W":
        temp = check(i + 1)
        if temp != -1:
            c_list[i] = "R"
            c_list[temp] = "W"
            count += 1
        else:
            break
print(count)

Submission status: Runtime Error

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
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 S(): return input()
def LS(): return input().split()


def rle(s):
    t = s[0]
    cnt = 1
    res = []
    for i in range(1, len(s)):
        if t == s[i]:
            cnt += 1
        else:
            res.append([t, cnt])
            t = s[i]
            cnt = 1
    res.append([t, cnt])
    return res


INF = float('inf')


n = I()
c = S()
if len(set(list(c))) < 2:
    print(0)
    exit()

# R と W のまとまり
rw = rle(c)
# print(rw)
ans = 0
for i, elem in enumerate(rw):
    if i < n - 1 and elem[0] == 'W' and rw[i+1][0] == 'R':
        ans += min(elem[1], rw[i+1][1])
print(ans)

Submission status: Runtime Error

Solution code:
Cc = copy.copy(Cn)
if (Cn[0] == "R"):
    Rflg = True
else:
    Rflg = False
ans = 0
K = N-1    
allw = False
for i in range(N):
    if Rflg and Cc[i] == "R":
        continue
    else:
        Rflg = True
    if Cc[i] == "W":
        for j in range(K, i, -1):
            if Cc[j] == "R":
#                 print("change")
                ans += 1
                Cc[i], Cc[j] = Cc[j], Cc[i]
                K = j
#                 print(Cc)
                break
        else:
            allw = True

    else:
#         print("replace")
        ans += 1
        Cc[i] = "W"
#         print(Cc)
    if allw:
        break

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
 
N = int(input())
C = input()

ans = 0
i = -1
j = N
while( True ):
  
  while( i<j ):
    i += 1
    if( C[i] == 'W' ):
      break
  
  while( j>i ):
    j -= 1
    if( C[j] == 'R' ):
      break
  
  if ( i<j ):
    ans += 1
  else:
    break

print(ans)

Submission status: Runtime Error

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

ans = 0
w = 0
r = 0
u = 0
p = 0
for i in range(n):

    while c[u] == 'R':
        #print(79)
        u += 1
        if u >= n-1:
            p = 1
            ans += r
            break
        if u >= n-i-1:
            p = 1
            ans += r
            break
    if p == 1:
        break

    if c[n-i-1] == 'R':
        r += 1
        u += 1

        #print(0)
    else:
        ans += r
        r = 0
print(ans)

Submission status: Runtime Error

Solution code:
a=int(input())
b=input()
c=int(a/2)
d=0
e=a-1
i=0
while True:
    if (b[i]=='W'):
        for j in range(0,e+1)[::-1]:
            if b[j]=='R':
                b[i]='R'
                b[j]='W'
                d=d+1
                e=j-1
                break
    i=i+1
    if i>e: break
print(d)

Submission status: Runtime Error

Solution code:
n=int(input())
c=list(input())
t=0
i=0
j=n-1
while c[i]=='R':
    i += 1
    if i>n-1:
        print(0)
        exit()
while c[j]=='W':
    j -= 1
    if j<0:
        print(0)
if i<j:
    i += 1
    j -= 1
    t += 1
while i<j:
    while c[i]=='R':
        i += 1
    while c[j]=='W':
        j -= 1
    i += 1
    j -= 1
    t += 1
print(t)

Submission status: Runtime Error

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

count = 0

i = 1
j = len(C) - 2

while i <= j:
    while C[i - 1] == 'R':
        i += 1
    while C[j + 1] == 'W':
        j -= 1
    C[i - 1], C[j + 1] = C[j + 1], C[i - 1]
    count += 1

print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
cl = list(c)
wi = 0
wnum=0
ri = n-1
rnum=0
br=0
bw=0
for i in range(n):
    while(cl[wi] == "R" and bw==0):
        wi+=1
        if(wi >= n-1):
            bw=1
            break
    if(cl[wi] == "W"):
        wnum+=1
    wi+=1
    if(wi >= n-1):
        wi=0
        bw=1
    while(cl[ri] == "W" and br==0):
        ri-=1
        if(ri <= 0):
            br=1
            break
    if(cl[ri] == "R"):
        rnum+=1
    ri-=1
    if(ri <= 0):
        ri=n-1
        br=1
    if(ri < wi):
        res1 = i+1
    if(br*bw==1):
        break
print(min(res1, rnum, wnum))

Submission status: Runtime Error

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


flag = 0
for i in range(n-1): 
    if(s[i] == "R" and s[i+1] == "W"):
        w_left = i
        r_right = i+1
        flag = 1
        break


#print(w_left)
#print(r_right)
if(flag == 1):
    w_len = len([i for i in s[r_right:] if i == "R"])
    r_len = len([i for i in s[:w_left] if i == "W"])
    print(max(w_len,r_len))
else:
    for i in range(n):
        if(s[i] == "W" and s[i+1] == "R"):
            flag = 2
            pnt = i
            if((pnt+1)*2 < n):
                print(pnt+1)
            else:
                print(n-pnt-1)

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

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
cl = list(c)
wi = 0
wnum=0
ri = n-1
rnum=0
br=0
bw=0
for i in range(n):
    while(cl[wi] == "R" and bw==0):
        wi+=1
        if(wi >= n-1):
            bw=1
            break
    if(cl[wi] == "W"):
        wnum+=1
    wi+=1
    if(wi >= n-1):
        wi=0
        bw=1
    while(cl[ri] == "W" and br==0):
        ri-=1
        if(ri <= 0):
            br=1
            break
    if(cl[ri] == "R"):
        rnum+=1
    ri-=1
    if(ri <= 0):
        ri=n-1
        br=1
    #print(wi, ri)
    if(ri < wi):
        res1 = i+1
    if(br*bw==1):
        break
print(min(res1, rnum, wnum))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
w=s.index('W')
s=s[w:]
a= s.count('W')
b=s[len(s)-a:].count('W')
print(a-b)

Submission status: Runtime Error

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

'''
Author: andyli
Time: 2020-08-02 19:55:50
'''

import os
from io import BytesIO, IOBase
import sys


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

    while n != 0 and s[-1] == 'W': 
    def calc(s):
        p = 0
        while p < len(s) and s[p] == 'R': p += 1
        _ans = s.count('R')-p
        p = len(s)-1
        while p >= 0 and s[p] == 'W': p -= 1
        _ans = min(_ans, s.count('W')-(len(s)-p-1))
        return _ans

    ans = calc(s)
    # print(ans)
    p1 = 0
    while p1 < n and s[p1] == 'W': p1 += 1
    p2 = n-1
    while p2 >= 0 and s[p2] == 'R': p2 -= 1

    cnt = min(p1, n-p2-1)
    # print(p1,p2,cnt)
    s2 = 'R'*cnt + 'W' * max(0, p1-cnt) + s[p1:p2+1] + 'R' * max(0,n-p2-1-cnt) + 'W'*cnt
    print(s2)
    # calc(s2) + min()
    ans = min(ans, calc(s2) + cnt)
    print(ans)
    return


# region fastio
BUFSIZE = 1048576


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)
def input(): return sys.stdin.readline().rstrip('\r\n')

# endregion


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = list(s)
#n = len(l)
i = 0
j = n-1
cnt = 0
while(i < j):
    if l[i] == "W" and l[j] == "R":
        l[j] = 'W'
        l[i] = 'R'
        i = i+1
        j = j-1
        cnt = cnt+1
        continue

    if l[i] == "R":
        i = i+1

    if l[j] == "W":
        j = j-1

f = 0
while(len(l) > 0):
    if l[-1] == "W":
        l.pop()

    if l[-1] == "R":
        break

k = "".join(l)
print(k.count("W")+cnt)

Submission status: Runtime Error

Solution code:
#ABC174  D
n = int(input())
s = input() 
c = [str(i) for i in s]
count = c.count('R')
ans = 0
if count == 0 or count ==n:
  print(ans)
else:
  idx_w = c.index('W')
  print(count,idx_w)
  if idx_w == 0:
    idx_r = c.index('R')
    if count >idx_r:
      ans = count -1
    else:
      ans = count
  else:
    count -= idx_w
    c1 = c[idx_w:]
    idx_r = c1.index('R')
    if count >idx_r:
      ans = count -1
    else:
      ans = count   
print(ans)

Submission status: Runtime Error

Solution code:
import datetime
import string
import re
import math

N=int(input())
input = input()
str = []
for i in range(N):
    str.append(input[i])

count = 0
begin = 0
end = len(str) - 1
while True:
    if begin < len(str):
        while str[begin] == 'W':
            begin += 1
            if begin == len(str):
                break

    if end >= 0:
        while str[end] == 'R':
            end -= 1
            if end == -1:
                break

    if begin < end:
        str[begin] = 'R'
        str[end] = 'W'
        begin += 1
        end -= 1
        count += 1
    elif begin == len(str) and end != -1:
        if str[end + 1] == 'R':
            str[end] = 'W'
            end -= 1
            count += 1
        else:
            end -= 1
    elif begin != len(str) and end == -1:
        if str[begin - 1] == 'W':
            str[begin] = 'R'
            begin += 1
            count += 1
        else:
            begin += 1
    else:
        print(count)
        break

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
rl = [ i for i,_ in enumerate(c) if _ == 'R' ]

cnt = 0
loc = 0
rid = -1
while True:
	if c[loc] == 'W':
		rloc = rl[rid]
		rid -= 1
		if rloc < loc:
			break
		else:
			cnt += 1
	loc += 1
	if loc == n:
		break
	#print(c,cnt)
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
cl = list(c)
wi = 0
wnum=0
ri = n-1
rnum=0
for i in range(n):
    br=0
    bw=0
    while(cl[wi] == "R" and br==0):
        wi+=1
        if(wi >= n-1):
            br=1
            break
    if(cl[wi] == "W"):
        wnum+=1
    wi+=1
    if(wi >= n-1):
        wi=0
        br=1
    while(cl[ri] == "W" and bw==0):
        ri-=1
        if(ri <= 0):
            bw=1
            break
    if(cl[ri] == "R"):
        rnum+=1
    ri-=1
    if(ri <= 0):
        ri=n-1
        bw=1
    #print(wi, ri)
    if(ri < wi):
        res1 = i+1
    if(br*bw==1):
        break
print(min(res1, rnum, wnum))

Submission status: Runtime Error

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

ans = 0

color = [C[0]]
c = [0]
for i in range(len(C)):
    if C[i] == color[-1]:
        c[-1] += 1

    else:
        color.append(C[i])
        c.append(1)

for i in range(len(color)):
    if color[i] == 'R':
        continue
    ans += min(c[i], c[i+1])
    if i + 3 <= len(color):
        c[i + 2] += max(0, c[i+1] - c[i])


print(ans)

Submission status: Runtime Error

Solution code:
import sys
N=int(input())
cstr=input()
clist=['']*N
for i in range(N):
    clist[i]=cstr[i]
rindexlist=[i for i,x in enumerate(clist) if x=='R']
windexlist=[i for i,x in enumerate(clist) if x=='W']
rindexlist.sort(reverse=True)
lenw=len(windexlist)
ans=0
#print(lenw)
if rindexlist==[] or windexlist==[]:
    print(0)
    sys.exit()
for i in range(lenw):
    #print(i)
    if i<=len(rindexlist):
        if windexlist[i]<rindexlist[i]:
            ans+=1
if lenw<len(rindexlist):
    ans+=len(rindexlist)-lenw
print(ans)

Submission status: Runtime Error

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

if (("R" not in c) or ("W" not in c)):
    print(0)
    exit(0)

R_R = 0
L_R = 0
for i in range(N - 1, -1, -1):

    if (c[i] == "R"):
        R_R += 1
for i in range(N):

    if (c[i] == "W"):
        break
    L_R += 1

Ans1 = R_R - L_R

L_W = 0
R_W = 0
for i in range(N):
    if (c[i] == "W"):
        L_W += 1
for i in range(N - 1, -1, -1):
    if (c[i] == "R"):
        break
    R_W += 1

Ans2 = L_W - R_W


i = 0
j = N - 1

count = 0

while (True):
    while (c[i] != "W"):
        i = i + 1

    while (c[j] != "R"):
        j = j + 1

    count += 1
    i = i + 1
    j = j - 1
    if (i > j or i == j or i >= N or j < 0):
        break

Ans3 = count

print(min(Ans1, Ans2, Ans3))

Submission status: Runtime Error

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

ans = 0
sorted = N - 1
for i in range(N):
  if cs[i] == "W":
    j = i
    for j in range(sorted, i, -1):
      if cs[j] == "R":
        cs[i], cs[j] = cs[j], cs[i]
        ans += 1
        sorted = j
        break
  if (i + 1) == j:
    break
print(str(ans))

Submission status: Runtime Error

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
s = input().strip()
if s.count('W') in [0,n]:
    print(0)
    exit()
ans = n
w,r = 0,s.count('R')
for i in range(n):
    if s[i] == 'W':
        w += 1
    elif s[i] == 'R':
        r -= 1
    ans = max(ans,min(w,r))
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
s = input().strip()
l,r = 0,n-1
ct = 0
if s.count('W') in [0,n]:
    print(ct)
    exit()
while l < r:
    while s[l] == 'R':
        if l >= r:
            break
        l += 1
    while s[r] == 'W':
        if l >= r:
            break
        r -= 1
    ct += 1
    l += 1
    r -= 1
print(min(ct,s.count('R'),s.count('W')))

Submission status: Runtime Error

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

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Submission status: Runtime Error

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    n=int(readline())
    ss=readline().rstrip()
    j=len(ss)
    ans=0
    if j==1:
        print(0)
        return
    for i in range(len(ss)):
        if ss[i]=="W":
            j-=1
            if j >= 0:
                while ss[j]!="R":
                    j-=1
            if j > i:
                ans+=1
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Runtime Error

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

c = Counter(S)
S_target = 'R' * c['R'] + 'W' * c['W']

ans = 0
for a, b in zip(S, S_target):
    if b == 'W':
        break
    if a != b:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
TMP_C = [x for x in input()]
C = []
for i in range(N - 1, -1, -1):
    C.append(TMP_C[i])
# print(C)

cnt = 0
for i in range(N - 1, -1, -1):
    if C[i] == "W":
        j = C.index("R")
        if j < i:
            C[i], C[j] = C[j], C[i]
            cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
import sys
N=int(input())
cstr=input()
clist=['']*N
for i in range(N):
    clist[i]=cstr[i]
rindexlist=[i for i,x in enumerate(clist) if x=='R']
windexlist=[i for i,x in enumerate(clist) if x=='W']
rindexlist.sort(reverse=True)
lenw=len(windexlist)
ans=0
#print(lenw)
if rindexlist==[]:
    print(0)
    sys.exit()
for i in range(lenw):
    #print(i)
    if i<=len(rindexlist):
        if windexlist[i]<rindexlist[i]:
            ans+=1
if lenw<len(rindexlist):
    if windexlist==[]:
        ans=0
    else:
        ans+=len(rindexlist)-lenw
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
C = list(input())
ans = 0
r = 0
l = N - 1
while r+1 < l or r < N - 1:
    if C[r] == 'W':
        while True:
            if C[l] == 'R':
                ans += 1
                l -= 1
                break
            else:
                l -= 1
                if r+1 == l:
                    break
    r += 1
print(ans)

Submission status: Runtime Error

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

ans = 0
sorted = N - 1
for i in range(N):
  if cs[i] == "W":
    for j in range(sorted, i, -1):
      if cs[j] == "R":
        cs[i], cs[j] = cs[j], cs[i]
        ans += 1
        sorted = j
        break
  if (i + 1) == j:
    break
print(str(ans))

Submission status: Runtime Error

Solution code:
from collections import deque
N=int(input())
C=list(input())

qw=deque(list())
qr=deque(list())

ans=0
for i in range(N//2):
    if C[i]=='W':
        qw.append(i)
    if C[-(i+1)]=='R':
        qr.append(-(i+1))
    if len(qw)>0 and len(qr)>0:
        ans+=1
        l=qw.popleft()
        r=qr.popleft()
        C[l],C[r]=C[r],C[l]

if len(qw)>0:
    l=qw.popleft()
    if C[l+1]=='R':
        C[l+1]='W'
        ans+=1
elif len(qr)>0:
    r=qr.popleft()
    if C[r-1]=='W':
        C[r-1]='R'
        ans+=1

if (C[N//2-1]==C[N//2]==C[N//2+1]) or (C[N//2]=='R' and C[N//2-1]=='R' and C[N//2+1]=='W') or (C[N//2]=='W' and C[N//2-1]=='R' and C[N//2+1]=='R'):
    print(ans)
else:
    print(ans+1)

Submission status: Runtime Error

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    n=int(readline())
    ss=readline().rstrip()
    j=len(ss)
    ans=0
    for i in range(len(ss)):
        if ss[i]=="W":
            j-=1
            if j >= 0:
                while ss[j]!="R":
                    j-=1
            if j > i:
                ans+=1
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Runtime Error

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n = int(input())
s = input()
def f(str):
    if str == '':
        return 0
    else:
        if str[0] == 'R':
            return f(str[1:])
        elif str[-1] == 'W':
            return f(str[:-1])
        else:
            ret = f(str[1:-1]) + 1
            ret = min(f(str[:-1]) + 1, ret)
            ret = min(f(str[1:]) + 1, ret)
            return ret

print(f(s))

Submission status: Runtime Error

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

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>=N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<=0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Submission status: Runtime Error

Solution code:

if w_count >= r_count:
    print(w_count)
else:
    print(w_count - right_w_count)