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())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r == -1:
    print(0)
    exit()
if input_str.find("R")==-1:
    print(0)
    exit()
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:first_r+index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr+1].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

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

if n_red == 0:
    print(0)
    exit()

toku = 0
for i in range(1, n_red):
    current_index = red_index_list[i]
    before_index = red_index_list[i-1]
    if current_index != 0:
        space = current_index - before_index - 1
    else:
        space = current_index
    if (space == 0):
        toku += 1
    else:
        while True:
            if current_index == red_index_list[-1]:
                print(n_red - toku)
                exit()

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

print(n_red-toku)

Submission status: Runtime Error

Solution code:
_ = input()
s = input()

def rec(s,ans):
    l = s[:len(s)//2]
    r = s[len(s)//2:]
    rw = r.count('W')
    rr = r.count('R')
    lw = l.count('W')
    lr = l.count('R')
    if rr:
        if lw > rr:
            return ans + rr
        else:
            return ans + lw 
    else:
        rec(l)

print(rec(s,0))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def change(c, white, red, i, j):
    temp = c[i]
    c[i] = c[j]
    c[j] = temp
    return c

def reload(c):
    white = []
    red = []
    for i in range(len(c)):
        if c[i] == 'W':
            white.append(i)
        elif c[i] == 'R':
            red.append(i)
    return [white, red]

N = int(input())
c = list(str(input()))
white, red = reload(c)
i = 0
while True:
    if min(white) < max(red):
        c = change(c, white, red, min(white), max(red))
        i += 1
        white, red = reload(c)
        input()
    else:
        break
print(i)

Submission status: Runtime Error

Solution code:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    C = input()

    counter = dict()
    for c in C:
        counter[c] = counter.get(c, 0) + 1
    r = counter[R] 

    _counter = dict()
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter[R]

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

Submission status: Runtime Error

Solution code:
def func_rfindr(str):
    return str.rfind('R')

def func_lfindw(str):
    return str.find('W')

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    print(C)
    print(ans)
    ans = ans + 1
    posrr = func_rfindr(C)
    poslw = func_lfindw(C)

print(ans)

Submission status: Runtime Error

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

if N % 2 == 1:
  c.append('W')
l = c[0:N//2]

#print([i for i, x in enumerate(l) if x == 'R'])
R = [i for i, x in enumerate(l) if x == 'R']
Rl = R[-1]
#print([i for i, x in enumerate(l) if x == 'W'])
W = [i for i, x in enumerate(l) if x == 'W']
Wf = W[0]

if c.count('W') == 0 or c.count('R') == 0:
  count = 0
elif Rl < Wf:
  count = 0
else:
  count = l.count('W')

print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r == -1:
    print(0)
    exit()
if input_str.find("R")==-1:
    print(0)
    exit()
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:first_r+index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

Solution code:
def change(c, white, red, i, j):
    temp = c[i]
    c[i] = c[j]
    c[j] = temp
    return c

def reload(c):
    white = []
    red = []
    for i in range(len(c)):
        if c[i] == 'W':
            white.append(i)
        elif c[i] == 'R':
            red.append(i)
    return [white, red]

N = int(input())
c = list(str(input()))
white, red = reload(c)
i = 0
while True:
    if min(white) < max(red):
        c = change(c, white, red, min(white), max(red))
        i += 1
        white, red = reload(c)
        input()
    else:
        break
print(i)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
N = int(input())
c = list(str(input()))
print(c)
l = N-1
s = 0
for i in range(int(N)):        
    if c[i] == 'W':
        for j in range(l,i,-1):
            if c[j] == 'R':
                l = j-1
                s = s+1
                break
    if l < i:
        break
print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = input()
A = list(A)
ret = 0
p = 0

while p < len(A):
  if A[p] == 'W' and A[p + 1] == 'R':
    if p == 0:
      ret += 1
      A[p] = 'R'
    elif p == len(A) - 2:
      ret += 1
      A[p + 1] = 'W'
    else:
      if A[p - 1] == 'R' :
        ret += 1
        A[p] = 'R'
      elif A[p - 1] == 'W' and A[p + 2] == 'W':
        ret += 1
        A[p + 1] = 'W'
      elif A[p - 1] == 'W' and A[p + 2] == 'R':
        q, r = p - 1, p + 2
        i = 0
        while True:
          if q == -1 or A[q] == 'R' :
            break
          if r == len(A) or A[r] == 'W':
            break
          i += 1
          q -= 1
          r += 1
        ret += i + 1
  p += 1
print(ret)

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

if n_red == 0:
    print(0)
    exit()

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

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

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

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

print(n_red-toku)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
cnt = 0
for i in range(n):
    print(c)
    r = c[i:].rfind('R') + i
    if c[i] == 'W' and i < r and r != -1:
        l = list(c)
        l[i] = 'R'
        l[r] = 'W'
        cnt += 1
        c = ''.join(l)

print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
c = list(input())
flag = False
ans = 0
for i in range(n-1):
    if c[i] == "W" and c[i+1] == "R":
        flag = True
        break
if not flag:
    print(0)
    exit()
for i in range(n//2):
    if c[i] == "R" and c[n-i-1] == "W":
        continue
    else:
        if c[i] == "W" and c[n-i-1] == "R":
            c[i] = "R"
            c[n-i-1] = "W"
            ans += 1
        elif c[i] == "W" and c[n-i-1] == "W":
            for j in range(n-i,i+1,-1):
                if c[j] == "R":
                    c[i] = "R"
                    c[j] = "W"
                    break
            ans += 1
        elif c[i] == "R" and c[n-i-1] == "R":
            for j in range(i+1, n-i-1):
                if c[j] == "W":
                    c[n-i-1] = "W"
                    c[j] = "R"  
                    break
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
C=input()
C=C.replace('R','0')
C=C.replace('W','1')

s2 = []
for s in C:
    s2.append(int(s))

s2_sort = sorted(s2)
cnt=0
i2=0
for index,i in enumerate(s2):
    if s2 == s2_sort:
        sys.exit()
    if i == 1:
        for j in reversed(s2):
            i2+=1
            if j == 0:
                temp=s2[len(s2)-i2]
                s2[len(s2)-i2]=s2[i]
                s2[i]=temp
                cnt+=1
                break
print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
l=input()
wn=0
rn=0
for i in l:
    if i=="R":
        rn+=1
    if i=="W":
        wn+=1
lastr=0
lastww=0
for x in reversed(l):
    if x=="R":
        lastr+=1
        l=l.strip("R")
    else:
        break
    for x in reversed(l):
        if x=="W":
            lastww+=1
        else:
            break
if lastr>=1:
    if wn<=lastr:
        print(wn)
        
    else:
        print(lastr+(wn-lastww-1))
lastrr=0
lastw=0
if lastr==0:
    for x in reversed(l):
        if x=="W":
            lastw+=1
            l=l.rstrip("W")
        else:
            break
    for x in reversed(l):
        if x=="R":
            lastrr+=1
        else:
            break
    wnoko=wn-lastw
    if wnoko<=lastrr:
        print(wnoko)
    elif wnoko==lastrr+1:

Submission status: Runtime Error

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

index = 0
if N % 2 == 0:
    index = N / 2
else:
    index = N // 2

print(c[index:].count("R"))

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r == -1:
    print(0)
    exit()
if input_str.find("R")==-1:
    print(0)
    exit()
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
if white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n = int(input())
c = input()
l=list(c)

# r=0
# while True:
#   c="".join(l)
#   lw=c.find("W")
#   lr=c.find("R")
#   rw=c.rfind("W")
#   rr=c.rfind("R")
#   if lw==-1:
#     break
#   if lw<rr:
#     l=list(c)
#     l[lw]='R'
#     l[rr]='W'
#     r=r+1
#   else:
#     break
# # print(r)

def rindex(l, v):
    return len(l) - l[::-1].index(v) - 1
r=0
if c.find("W")!=-1:
  ll=len(c)
  for i in range(ll):
    lw=l.index('W')
    rr=rindex(l,'R')
    #print(lw,rr)
    if lw<rr:
      l[lw]='R'
      l[rr]='W'
      r=r+1
    else:
      break
print(r)

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r == -1:
    print(0)
    exit()
if input_str.find("R"):
    print(0)
    exit()
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
if white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break
print(white)

Submission status: Runtime Error

Solution code:
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1


ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Submission status: Runtime Error

Solution code:
N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    while True:
        w=int(w_index[ans])
        r=int(r_index[ans])
        if w-r==1:
             break
        ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r == -1:
    print(0)
    exit()
if input_str.find("R").count("W"):
    print(0)
    exit()
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

Solution code:
from bisect import bisect_right as br

n = int(input())
c = list(input())
cFixed = [x for x in c]
cFixed.sort()
for i in range(n):
	if cFixed[i] == "R":
		lastR = i
ans = 0
"""for i in range(n):
	if c[i] != cFixed[i]:
		j = br(c,"R", lo = lastR + 1)-1
		#print(j,i,c[j],c[i],cFixed[i])
		c[i] = c[j]
		c[j] = "W"
		ans += 1


print(ans)"""

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

print(ans)

Submission status: Runtime Error

Solution code:
c=[i for i in s]
reverse_c = c[::-1]
#print(c)
#print(reverse_c)

R_count=c.count("R")
if R_count == 0 or R_count == N:
    print(0)
    exit()

flg=0
ans=0
while flg == 0:
    for i in range(N):
        left_W=c.index("W")#１番左の白のindex
        right_R=N-1-reverse_c.index("R")#１番右の赤のindex
        #print(left_W)
        #print(right_R)
        #入れ替え処理
        c[left_W]="R"
        c[right_R]="W"
        #print(c)

        cnt=0
        for j in range(R_count):
            if c[j] == "R":
                cnt+=1
            else:
                break
        ans+=1
        if cnt == R_count:
            print(ans)
            exit()

Submission status: Runtime Error

Solution code:
N = int(input())
s = input() #s_1 s_2を分割して取得し、sに値を入れる
count_white = 0
white_flag = 0
white_renzoku = 0

count_red = 0
red_flag = 0
red_renzoku = 0
for i in range(N):
  if s[i]=='W':
    count_white = count_white+1
    red_flag = 1
  elif s[i]=='R':
    count_red = count_red+1
  if red_flag == 0:
      red_renzoku = red_renzoku+1
  if s[N-1-i]=='R':
    white_flag = 1
  elif s[N-1-i]=='W':
    if white_flag==0:
      white_renzoku = white_renzoku+1
white_irekae = count_white-white_renzoku
red_irekae = count_red-red_renzoku

if white_irekae==red_irekae:
  print(white_irekae-1)
elif white_irekae >= red_irekae:
  print(white_ireka)
else:
  print(red_irekae)

Submission status: Runtime Error

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

for i in range(n):
  if c[i]=='W':
    #print('when W in',i)
    s=c[i:]
    num=s.find('R')
    if num<0:
      break
    num+=i
    #print('when num ',num)
    t=c[num:]
    num_ch=t.find('W')
    if num_ch<0:
      num_ch=n-num   
    #print(num_ch)
    num_ch+=num
    
    #print('when num_ch',num_ch)
    if num-i-1<=num_ch-num:
      #print('間が小さいので塗り替える')
      for j in range(i,num):
        #print(j)
        c=c[:j]+'R'+c[j+1:]
        count+=1
    else:
      #print('間が大きいので交換する')
      for j in range(num,num_ch):
        #print(j)
        c=c[:j]+'W'+c[j+1:]
        c[i+j-num]='R'
        count+=1
#print(c)
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-1-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

Solution code:
import sys

def check(c):
    for j in range(len(c)-1, -1 , -1):
        if c[j] == "R":
            return j

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

c_set = set(c)
# 全てRは0
if len(c_set) == 1 and c_set == {'R'}:
    print(0)
    sys.exit()

cnt = 0
for i in range(N):
    first_W = c.index("W")
    last_R = check(c)
    
    if first_W < last_R:
        c[first_W] = 'R'
        c[last_R] = 'W'
        cnt += 1
        continue
# print(c)
print(cnt)

Submission status: Runtime Error

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
 
 
def main():
    N,S = Input.split()
    print(N)
    N=int(N)
    l=0
    r=len(S)-1
    ans=0
    while(1):
        for i in range(l,len(S)):
            if S[i]=='W':
                l=i
                break
        for j in range(r,0,-1):
            if S[j]=='R':
                r=j
                break
        
        if i<j:
            ans+=1
            l=i+1
            r=j-1
        else:
            break
    print(ans)

 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
IN = iter(Input.split('\n')).__next__
def input():
    return IN()

###########コーディング
import sys
n=int(input())
text=input()
#wRwのindexを配列
wrw=0
flag_r=False
if text[0]=='R':
    flag_r=True
i=0
cnt=0
ch=True
ans=0
cnt_r=0
wkita = False
saisyo_r=0
for i in range(len(text)):
    if text[i]=='R':
        cnt_r+=1
    if ch:
        if  text[i]=='W':
            cnt+=1
            wkita=True
        elif wkita:
            ch=False
        else:
            saisyo_r+=1


if cnt_r==len(text):
    print(0)
    sys.exit()
if saisyo_r==cnt_r:
    print(0)
    sys.exit()
if text[0]=='R':
    print(cnt_r-1)
    sys.exit()
if cnt>=cnt_r:
    print(cnt_r)
    sys.exit()
ans+=cnt
cnt_r-=cnt+saisyo_r+1
if cnt_r>=0:
    ans+=cnt_r
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
def do(arr):
  mid = int(len(arr)/2)
  
  if len(arr) == 2:
    if arr[0] == 'W' and arr[1] =='R':
      return 1
    else:
      return 0 # is ok 
  if len(arr) == 1:
      return 0 # is ok 
    
    
  a1 = arr[0:mid]
  a2 = arr[mid:]
  c1 = do(arr[0:mid])
  c2 = do(arr[mid:])
  if c1==0 and c2==0:
    if a1[-1] == 'W' and a2[0] == 'R':
      if a1[-2] == 'W' and a2[1] == 'R':
      	return 2
      else:
      	return 1
    else:
      return 0
      
  else:
    if a1[-1] == 'W' and a2[0] == 'R':
      if a1[-2] == 'W' and a2[1] == 'R':
        return c1 + c2 + 2
      else:
      	return c1 + c2 + 1

    else:
      return c1 + c2

    
    
    
    

print(do(s))

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

if n_red == 0:
    print(0)
    exit()

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

        red_index_list.pop(-1)
        if red_index_list[0]==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:
            if red_index_list[i] == red_index_list[-1]:
                print(n_red - toku)
                exit()

            red_index_list.pop(-1)
            if red_index_list[i] == 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:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    _C = input()
    C = deque(_C)

    count = 0
    while True:
        i = len(C) -1
        while C[i] != R:
            i -= 1
        j = 0
        while C[j] != W:
            j +=1
            if j >= len(C):
                break
        if  i < j:
            break
        C[i], C[j] = C[j], C[i]
        count += 1
    
    print(count)
if __name__ == '__main__':
    main()

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

if n_red == 0:
    print(0)
    exit()

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

        red_index_list.pop(-1)
        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:
            if red_index_list[i] == red_index_list[-1]:
                print(n_red - toku)
                exit()

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

print(n_red-toku)

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:
        if red_index_list[0]==red_index_list[-1]:
            print(n_red - toku)
            exit()

        red_index_list.pop(-1)
        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:
            if red_index_list[i] == red_index_list[-1]:
                print(n_red - toku)
                exit()

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

print(n_red-toku)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
l = 0
j = 0
a = [0]*n
b = [0]*n
for i in range(n):
  if(c[i]=='W'):
    a[l] =i+1
    l +=1
l = 0
for i in range(n-1,0,-1):
  if(c[i]=='R'):
    b[j] = i+1
    j +=1
j = 0
while(a[l] < b[l]):
      a[l]=0
      b[l]=0
      l+=1
      j+=1
if(a[l+1]==0):
  while(b[l+1]>0):
    j +=1
else:
  while(a[l+1]>0):
    j +=1
print(j)

Submission status: Runtime Error

Solution code:
n = int(input())
input_str = input()
first_r = input_str.find("R")
r_num = input_str[first_r:].count("R")
if first_r > r_num:
    print(r_num)
    exit()
count = 0
for i in range(n):
    if input_str[-(i+1)] == "R":
        count += 1
    if count == first_r:
        index_lr = n-2-i
        break
first_w = input_str[first_r:].find("W")
Lr = input_str[first_r:index_lr].rfind("R")
count_r = input_str[first_w+first_r:].count("R")
count_w = input_str[first_r:first_r+Lr].count("W")
if count_r-first_r > count_w:
    print(count_w+first_r)
else:
    print(count_r)

Submission status: Runtime Error

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


def count_WR(stones):
    return stones.count('WR')


def solve(stones, count):
    if count_WR(stones) == 0:
        return count

    index_R = stones.rindex('R')
    index_W = stones.index('W')

    # 「もっとも右にあるR」と「もっとも左にあるW」を交換する
    new_stones = (stones[:index_W] + 'R' + stones[index_W + 1:index_R] + 'W' +
                  stones[index_R + 1:])

    count += 1
    return solve(new_stones, count)


if __name__ == "__main__":
    N = int(input())
    stones = input()

    count = 0

    result = solve(stones, count)
    print(result)

Submission status: Runtime Error

Solution code:
from collections import deque

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

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
print(count)

Submission status: Runtime Error

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

n = int(input())
#b = list(map(int, input().split()))
#n, m = map(int, input().split())
C = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
#readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

C = list(C)

d = {}
tot = 0

for i,c in enumerate(C):
    if c == 'R':
        tot += 1
    d[i+1] = tot

ans = tot-d[tot]
print(ans)

# left = 0
# left2mid = False
# mid = 0
# right = 0
# right2mid = False

# lp = 0
# rp = -1

# while lp < len(C)+rp:
#     if not left2mid and C[lp] == 'W':
#         left += 1
#     elif not left2mid and C[lp] == 'R':
#         left2mid = True
#     elif left2mid and C[lp] == 'R':
#         mid += 1
#     lp += 1

#     if not right2mid and C[rp] == 'R':
#         right += 1
#     elif not right2mid and C[rp] == 'W':
#         right2mid = True
#     elif right2mid and C[lp] == 'R':
#         mid += 1
#     rp -= 1
# print(left, mid, right)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(input())
# A = list("RRRRR")
import numpy as np
A = np.array(A)
W = set(np.where(A=="W")[0])
R = set(np.where(A=="R")[0])
n = 0
# print(W,R)
if (W!=set())&(R!=set()):
    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)
else:print(0)

Submission status: Runtime Error

Solution code:
count = int(input())
num = list(input())
indexes = [i for i, x in enumerate(num) if x == "W"]
if len(indexes) == 0:
  print(0)
else:
  change = 0
  if (len(num) - 1) != indexes[-1] and (len(num) - 1) != indexes[-2]:
    for i in range(len(indexes)):
      for j in range(len(num) - indexes[-1] - 1):
        if num[indexes[i] + j + 1] == "R":
          change += 1
        elif num[indexes[i] + j + 1] == "W":
          break
  elif (len(num) - 1) != indexes[-1]:
    change += 1
    for i in range(len(indexes)):
      for j in range(len(num) - indexes[-1] - 1):
        if num[indexes[i] + j + 1] == "R":
          change += 1
        elif num[indexes[i] + j + 1] == "W":
          break
  else:
    for k in range(len(indexes) - 1):
      if num[indexes[k] + 1] == "R":
        change += 1
  print(change)

Submission status: Runtime Error

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

head = 0
tail = -1
count = 0

while head < N and tail+N >= 0:
    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())
s = list(input())

def do(arr):
  mid = int(len(arr)/2)
  
  if len(arr) == 2:
    if arr[0] == 'W' and arr[1] =='R':
      return 1
    else:
      return 0 # is ok 
    
  a1 = arr[0:mid]
  a2 = arr[mid:]
  c1 = do(arr[0:mid])
  c2 = do(arr[mid:])
  if c1==0 and c2==0:
    if a1[-1] == 'W' and a2[0] == 'R':
      return 1
    else:
      return 0
      
  else:
    if a1[-1] == 'W' and a2[0] == 'R':
      return c1 + c2 + 1
    else:
      return c1 + c2

    
    
    
    

print(do(s))