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: Time Limit Exceeded

Solution code:
n = int(input())
c = list(input())
if 'W' not in c or 'R' not in c:
    print(0)
else:
    l = 0
    r = n-1
    cnt = 0
    while l < r:
        for i in range(r+1):
            if c[i] == 'W':
                l = i
                break
        for i in range(r, l-1, -1):
            if c[i] == 'R':
                r = i
                break
        c[l], c[r] = c[r], c[l]
        cnt += 1
    print(cnt-1)
"""
p = -1
q = -1
cnt = 0

for i in range(n-1, -1, -1):
    if c[i] == 'R' and q == -1:
        p = i
        break
for i in range(p-1, -1, -1):
    if c[i] == 'R' and p != -1 and q == -1:
        q = i
    elif c[i] == 'W':
        break
if p == -1:
    print(0)
elif q == -1:
    for i in range(p):
        if c[i] == 'W':
            cnt += 1
    print(cnt)
else:
    for i in range(n):
        if c[i] == 'W' and i < p:
            cnt += 1
    print(cnt-(q-p+1))
"""

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

def main_chk():
    N=int(input())
    C=input()

    chk = N - 1
    cnt = 0

    for i in range(N):
        if C[i] == 'W':
            for x in range(N-i):
                if C[chk-x] == 'R':
                    chk = chk - x
                    break
            if chk <= i:
                break
            else:
                cnt = cnt + 1
                chk = chk - 1

    print(cnt)

main_chk()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = list(input())
sc = list(reversed(c))
ans = 0
flg = True

r = 0
l = 0

if 'W' not in set(c) or 'R' not in set(c):
    flg = False


while(flg):


    for i in range(l, n):
        if not flg:
            break

        if c[i] == 'W':
            for j in range(r, n):
                if sc[j] == 'R':
                    if len(c) >= i+j+2:
                        c[i] = 'R'
                        c[-1*j-1] = 'W'
                        ans += 1
                        sc = list(reversed(c))
                        break
                    else:
                        flg = False
                        break

                else:
                    r += 1
        else:
            l += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
Sorted = sorted(C)
num = 0
index = 0
for i in range(N):
  if C[i] == "W":
    for j in range(index, N-i-1):
      if C[N-j-1] == "R":
        C[i] = "R"
        C[N-j-1] = "W"
        num += 1
        index = j
        break
  if C == Sorted:
    break
print(num)

Submission status: Time Limit Exceeded

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

ans = 0

while "WR" in arr:
    left = arr.index("W")
    right = len(arr) - arr[::-1].index("R") - 1
    arr = arr[:left] + "R" + arr[left+1:right] + "W" + arr[right+1:]
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

while len(disastar_list) > 0:
    count = count + 1
    if(len(disastar_list) == 1):
        c_list[disastar_list[0]] = 'R'
        last_one = True
    else:
        tmp = c_list[disastar_list[len(disastar_list) - 1] + 1]
        c_list[disastar_list[len(disastar_list) - 1] + 1] = c_list[disastar_list[0]]
        c_list[disastar_list[0]] = tmp

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


print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
st = input()
from collections import deque
st = deque(st)

ans = 0
while len(st) > 1:
    if st[0] == 'W' and st[-1] == 'R':
        ans += 1
        st.popleft()
        st.pop()
    elif st[0] =='R' and st[-1] == 'R':
        st.popleft()
    elif st[0] == 'R' and st[-1] == 'W':
        st.popleft()
    else:
        if  not 'R' in st:
            break
        else:
            ans += 1
            st.popleft()
            st.reverse()
            st.remove('R')
            st.reverse()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
import copy
D = []
flag = 0
ans = 0

for p in range(N):
    if C[p] == "R":
        D += ["0"]
    else:
        D += ["1"]



def change(x,y):
    global ans
    hoge = copy.deepcopy(D[x])
    D[x] = D[y]
    D[y] = hoge
    ans += 1

def color(x):
    global ans
    ans += 1
    if D[x] == "0":
        D[x] = "1"
    elif D[x] == "1":
        D[x] = "0"

while flag == 0:

    for r in range(N):
        if int("".join(D),2) == 2**r-1:
            flag = 1

    if flag == 0:
        for q in range(N):
            if D[q] == "1":
                for s in range(N):
                    if D[N-s-1] =="0" :
                        if q < N-s-1:
                            change(q,N-s-1)
                        break
            else:
                continue
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = input()
C = C.lstrip('R').rstrip('W')

ans = 0
for i in range(10**6):
    if len(C) > 0:
        l = C.find('R')
        r = C[::-1].find('W')
        n = min(l, r)
        C = C[n:-n]
        C = C.lstrip('R').rstrip('W')
        ans += n
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def checkStn(stnData,numStn):
    ftn = False
    for i in range(numStn - 1):
        if stnData[i] == "W" and stnData[i+1] == "R":
            ftn = True
            break
    return ftn

def exchangeStn(stnData,numStn):
    cnt = 0
    for i in range(numStn):
        for j in range(numStn - 1 ,-1, -1):
            if i < j:
                if stnData[i] == "W" and stnData[j] == "R":
                    stnData[i] = "R"
                    stnData[j] = "W"
                    cnt += 1
                    break
    return cnt


numStn = int(input())
stnData = list(input())

ftn = checkStn(stnData,numStn)
cnt = 0

while ftn == True:
    ftn = checkStn(stnData,numStn)
    cnt += exchangeStn(stnData,numStn)

print(cnt)

Submission status: Time Limit Exceeded

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

w = s.count("W")
r = s.count("R")

a = 0b0
b = 0b0
cnt = 0

for t in s:
    if t == "W":
        b = b << 1
    else:
        b = b << 1
        b += 1

for i in range(r):
    a = a << 1
    a += 1
for i in range(w):
    a = a << 1


def count_ones_by_bin(num):
    bin_num = bin(num)[2:]
    count = 0
    for i in bin_num:
        count += int(i)
    return count


cnt = count_ones_by_bin(a ^ b)

cnt = cnt // 2

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
N = int(input())
Cn = list(input())
Cc = copy.copy(Cn)
if (Cn[0] == "R"):
    Rflg = True
else:
    Rflg = False
ans = 0
K = N-1    
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:
#         print("replace")
        ans += 1
        Cc[i] = "W"
#         print(Cc)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(input())
c = input()
list_c = [None] * N

for i in range(len(c)):
  list_c[i] = c[i]

def main():
  count = 0

  while True:
    if (''.join(list_c).find("WR") == -1):
      return(count)

    next = False
    for i in range(len(c)-1, 0, -1):
      if (list_c[i] == "R"):

        for j in range(0, len(c)):
          if (list_c[j] == "W"):
            list_c[i] = "W"
            list_c[j] = "R"
            count += 1
            next = True
            break
        if (next == True):
          break
      if (next == True):
        next = False
        continue

print(main())

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

J=0
ans=0
for i in range(N):
    if C[i]=="W":
        for j in range(J,N):
            if i+j>=N:break
            if C[N-1-j]=="R":
                ans+=1
                #print(i,N-1-j)
                J=j+1
                break

    if i+J+1>N:break
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

def main_chk():
    N=int(input())
    C=input()

    chk = N - 1
    cnt = 0

    for i in range(N):
        if C[i] == 'W':
            for x in range(N):
                if C[chk-x] == 'R':
                    chk = chk - x
                    break
            if chk <= i:
                break
            else:
                cnt = cnt + 1
                chk = chk - 1

    print(cnt)

main_chk()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
Sorted = sorted(C)
num = 0
for i in range(N):
  if C[i] == "W":
    for j in range(N-i-1):
      if C[N-j-1] == "R":
        C[i] = "R"
        C[N-j-1] = "W"
        num += 1
        break
  if C == Sorted:
    break
print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
import copy
D = []
flag = 0
ans = 0

for p in range(N):
    if C[p] == "R":
        D += ["0"]
    else:
        D += ["1"]



def change(x,y):
    global ans
    hoge = copy.deepcopy(D[x])
    D[x] = D[y]
    D[y] = hoge
    ans += 1

def color(x):
    global ans
    ans += 1
    if D[x] == "0":
        D[x] = "1"
    elif D[x] == "1":
        D[x] = "0"

while flag == 0:
    for q in range(N):
        if D[q] == "1":
            for s in range(N):
                if D[N-s-1] =="0" :
                    if q < N-s-1:
                        change(q,N-s-1)
                    break
        else:
            continue
        break

    for r in range(N):
        if int("".join(D),2) == 2**r-1:
            flag = 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy 
n=int(input())
c=input()
cl=copy.deepcopy(c)
cl=list(cl)
d=Counter(c)
ans=0


if d["R"]==0 or d["W"]==0:
  print(0)
  exit()
swap=0
for i in range(n//2):
  indexes = [j for j, x in enumerate(cl) if x == "R"]
  #print(cl)
  if cl[i]=="W":
    cl[indexes[-1]]="W"
    cl[i]="R"
    swap+=1

#print(cl)
#print(cl[cl.index("W"):])

d=Counter(cl[cl.index("W"):])
ans+=swap+d["R"]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c = input()
l =list(c)
L = len(l)//2
ans = 0

for i in range(n-1):
    for j in range(n-1,i,-1):
        if l[i]== 'W' and l[j] == 'R':
            l[i] = 'R'
            l[j] = 'W'
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
data=list(input())
if "W" not in data or "R" not in data:
    print(0)
else:
    s=0
    while True:
        for i in range(N):
            if data[i] =="W":
                first_w_index=i
                break
        for i in reversed(range(N)):
            if data[i] == "R":
                final_r_index=i
                break
        if first_w_index < final_r_index:
            data[first_w_index]="R"
            data[final_r_index]="W"
            s += 1
        else:
            print(s)
            break

Submission status: Time Limit Exceeded

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

white_index = 0
red_index = N
ans = 0
for i in range(N):
    red_index = c.rfind('R')
    white_index = c.find('W')
    if red_index < 0:
        break
    if white_index >= red_index or white_index < 0:
        break
    c = c[:red_index] + 'W' + c[red_index+1:]
    c = c[:white_index] + 'R' + c[white_index+1:]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
c = list(input())
count = 0
flg = False
r=999999
w=999999
for i in range(n):
        if c[i]=='W':
            w=i
            break
for i in range(n):
    if c[n-i-1]=='R':
        r=n-i-1
        break
if r<w:
    flg=True
    
while((r>=w and count<=n/2)):
    count += 1
    for i in range(n):
        if c[i]=='W':
            c[i]='R'
            break
    for i in range(n):
        if c[n-i-1]=='R':
            c[n-i-1]='W'
            break
    for i in range(n):
        if c[i]=='W':
            w=i
            break
    for i in range(n):
        if c[n-i-1]=='R':
            r=n-i-1
            break
        
if flg==True:
    print(0)
else:
    print(count)

Submission status: Time Limit Exceeded

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)

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

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



print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
c = input()
cnt = 0
r_cnt = c.count('R')
w_cnt = c.count('W')
ans = 'R' * r_cnt
if not w_cnt or not r_cnt:
  print(cnt)
else:
  while True:
    if c[:r_cnt] == ans:
      break
    else:
      w_index = c.index('W')
      r_index = N-c[::-1].index('R')-1
      c = c[:w_index] + 'R' + c[w_index+1:r_index] + 'W' + c[r_index+1:]
      cnt += 1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = list(input())
x = c.count("R")

end = []
for i in range(n+1):
    end.append("R"*i + "W"*(n-i))
#print(end)

newc = "".join(c)
ans = 0
j,k = -1,n
if newc in end:
    print(ans)
    exit()
while newc != end[x]:
    for i in range(j+1,n):
        if c[i] == "W":
            j = i
            c[i] = "R"
            break
    for i in reversed(range(k)):
        if c[i] == "R":
            k = i
            c[i] = "W"
            break
    newc = "".join(c)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = str(input())
cn = [False]*n
for i in range(n):
    if c[i]=="R":
        cn[i]= True


ans = 0
k=n-1
b=0

for i in range(n):
    if b==1:
        break

    if not(cn[i]):
        for j in range(k,0,-1):

            if cn[j]:
                k =j-1
                if i < j:
                    #cn[i]=True
                    #cn[j]=False
                    ans +=1
                    break
                elif i>=j:
                    b=1
                    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
N = int(input())
Cn = list(input())
Cc = copy.copy(Cn)
if (Cn[0] == "R"):
    Rflg = True
else:
    Rflg = False
ans = 0
    
for i in range(N):
    if Rflg and Cc[i] == "R":
        continue
    else:
        Rflg = True
    if Cc[i] == "W":
        for j in range(N-1, i, -1):
            if Cc[j] == "R":
#                 print("change")
                ans += 1
                Cc[i], Cc[j] = Cc[j], Cc[i]
#                 print(Cc)
                break
    else:
#         print("replace")
        ans += 1
        Cc[i] = "W"
#         print(Cc)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
n = int(input())
stone = list(input())
w_num = stone.count("W")
r_num = stone.count("R")
if w_num == n or r_num == n:
    print(0)
    exit()

def w_serch(entry_point):
    for i in range(n):
        try:
            pointer_left = entry_point + i
            if stone[pointer_left] == "W":
                return pointer_left
        except:
            pass

def r_serch(entry_point):
    for i in range(n):
        try:
            pointer_right = entry_point - i
            if stone[pointer_right] == "R":
                return pointer_right
        except:
            pass

counter = 0
for i in range(n // 2):
    entry_point_left = 0
    entry_point_right = n-1
    pointer_l = w_serch(entry_point_left)
    pointer_r = r_serch(entry_point_right)

    if pointer_l > pointer_r:
        break
    else:
        stone[pointer_l] = "R"
        stone[pointer_r] = "W"
        entry_point_left = pointer_l
        entry_point_right = pointer_r
    #print(stone, pointer_l, pointer_r)
    counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    c=input()
    
    #x...WR
    #o...RR, RW, WW
    #Wが来たらそれ以降はW
    #R(0個以上)+W(0個以上)
    
    i=0
    for _ in range(N):
        if 'WR' in c:
            x=c.index('WR')  
            y=N-c[::-1].index('R')-1
            cl=list(c)
            cl[x]='R'
            cl[y]='W'
            c=''.join(cl)
            #c=c[:x]+'R'+c[x+1:y]+'W'+c[y+1:]
            #print(c)
            #print(x,y)
            i+=1
    #print(i) 
    print(min(c.count('R'),c.count('W'),i))
    
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = list(input())
sc = reversed(c)
ans = 0
flg = True

if 'W' not in set(c) or 'R' not in set(c):
    flg = False


while(flg):

    for i, r in enumerate(c):
        if not flg:
            break

        if r == 'W':
            for j, l in enumerate(sc):
                if l == 'R':
                    if len(c) >= i+j+2:
                        c[i] = 'R'
                        c[-1*j-1] = 'W'
                        ans += 1
                        sc = reversed(c)
                        break
                    else:
                        flg = False
                        break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import re

N = int(input())
pattern = input()

# pattern = 'WRWWRWRR'
pattern = list(pattern)

flg = False
cnt = 0

while (flg == False):

    reversed_pattern = list(reversed(pattern))

    try:
        first_w_idx = pattern.index('W')
        last_r_idx = len(pattern) - reversed_pattern.index('R') - 1
    except:
        flg = True
        break

    if first_w_idx > last_r_idx:
        flg = True
        break

    tmp = 'R'
    pattern[first_w_idx] = 'R'
    pattern[last_r_idx] = 'W'

    cnt += 1

    # print(first_w_idx, last_r_idx)


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

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

N = I()
c = list(input())

ans = 0
l = 0
r = N-1
while l <= r:
    flag = False
    for i in range(l,N):
        if c[i] == 'W':
            l = i
            flag = True
            break
    if not flag:
        l = N

    flag = False
    for i in range(r+1)[::-1]:
        if c[i] == 'R':
            r = i
            flag = True
            break
    if not flag:
        r = 0

    if r < l:
        break
    
    c[l] = 'R'
    c[r] = 'W'
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = input()
#from collections import Counter
#print(min([i for i in Counter(list(c)).values()]))

cnt = 0
loc = 0
while True:
	if c[loc] == 'W':
		rloc = c.rfind('R')
		if rloc < loc:
			break
		else:
			cnt += 1
			lc = list(c)
			lc[rloc] = 'W'
			c = ''.join(lc)
	loc += 1
	if loc == n:
		break
	#print(c,cnt)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N):
    # print(i+1,"回目")
    # print("exchange=",exchange)
    # print("c=",c)
    if N-i > exchange:
        if c[-(i+1)] =="R":
            # c = list(c)
            # c[exchange] = "R"
            # c[-(i+1)] = "W"
            # c = "".join(c)
            c = c[:exchange]+"R"+c[exchange+1:]
            # print("newc=",c)
            c = c[:-(i+1)]+"W"+c[N-i:]
            # print("newc=",c)
            ans += 1
            for k in range(exchange+1,N):
                if c[k] == "W":
                    exchange = k
                    break
            else:
                break
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = input()
C = [True] * n

for i in range(n):
    if c[i] == 'W':
        C[i] = False
r = C.count(True)

count = 0
while not all(C[:r]):
    a = C.index(False)
    C.reverse()
    b = n-1 - C.index(True)
    C.reverse()
    C[a], C[b] = True, False
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
RS=S[::-1]
#print(RS)
RSS=[]
F=0
W=0
R=0
for i in range(len(RS)):
    if F==0:
        if RS[i]=="R":
            RSS.append("R")
            R+=1
            F=1
    else:
        RSS.append(RS[i])
        if RS[i]=="R":
            R+=1
        else:
            W+=1

SS=RSS[::-1]
#print(SS,R,W)

LW=[]
LR=[]
for i in range(len(SS)):
    if SS[i]=="W":
        LW.append(i)
    else:
        LR.append(i)
LR=LR[::-1]

while len(LW)!=len(LR):
    if len(LW)<len(LR):
        LW.append(10**7)
    else:
        LW.append(-1)
LW.append(10**7)
LR.append(-1)
#print(LW,LR)
for i in range(len(LR)):
    if LW[i]>LR[i]:
        cnt=i
        break
print(min(cnt,W,R))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = str(input())
cn = [False]*n
for i in range(n):
    if c[i]=="R":
        cn[i]= True


ans = 0
k=n-1
b=0

for i in range(n):
    if b==1:
        break

    if cn[i]== False:
        for j in range(k,0,-1):

            if cn[j]==True:
                k =j-1
                if i < j:
                    #cn[i]=True
                    #cn[j]=False
                    ans +=1
                    break
                elif i>=j:
                    b=1
                    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(input())
import copy
D = []
flag = 0
ans = 0

for p in range(N):
    if C[p] == "R":
        D += ["0"]
    else:
        D += ["1"]



def change(x,y):
    global ans
    hoge = copy.deepcopy(D[x])
    D[x] = D[y]
    D[y] = hoge
    ans += 1

def color(x):
    global ans
    ans += 1
    if D[x] == "0":
        D[x] = "1"
    elif D[x] == "1":
        D[x] = "0"

while flag == 0:
    for q in range(N):
        if D[q] == "1":
            for s in range(N):
                if D[N-s-1] =="0" and q < N-s-1:
                    change(q,N-s-1)
                    break
                
    for r in range(N):
        if int("".join(D),2) == 2**r-1:
            flag = 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
start=0
end=N-1
count=0
flag=0
while flag==0 and start<N:
    if S[start]=="W":
        for i in range(end,start,-1):
            if i==start+1:
                if S[i]=="W":
                    count-=1
                flag=1
            if S[i]=="R":
                end=i-1
                start+=1
                break
        count+=1
    else:
        start+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
c = list(input())
count = 0
flg = False
r=999999
w=999999
for i in range(n):
        if c[i]=='W':
            w=i
            break
for i in range(n):
    if c[n-i-1]=='R':
        r=n-i-1
        break
if r<w:
    flg=True
    
while(r>=w):
    count += 1
    for i in range(n):
        if c[i]=='W':
            c[i]='R'
            break
    for i in range(n):
        if c[n-i-1]=='R':
            c[n-i-1]='W'
            break
    for i in range(n):
        if c[i]=='W':
            w=i
            break
    for i in range(n):
        if c[n-i-1]=='R':
            r=n-i-1
            break
        
if flg==True:
    print(0)
else:
    print(count)

Submission status: Time Limit Exceeded

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

C = [s for s in STR]
cnt_w = C.count('W')    # 最大入替回数

ans = 0
for i in range(cnt_w):
    idx = C.index('W')
    if idx < cnt_w:
        C[i] = 'R'
        target = STR.rfind('R')
        if target < idx:
            break
        C[target] = 'W'
        ans += 1
        STR = ''
        for j in C:
            STR += j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = input()
C = C.lstrip('R').rstrip('W')

i = 0
while len(C) > 0:
    l = C.index('R')
    r = C[::-1].index('W')
    n = min(l, r)
    C = C[n:-n]
    C = C.lstrip('R').rstrip('W')
    i += n
else:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

def main_chk():
    N=int(input())
    C=input()

    chk = N - 1
    cnt = 0

    for i in range(N):
        if C[i] == 'W':
            for x in reversed(range(0,chk+1)):
                if C[x] == 'R':
                    chk = x
                    break
            if chk <= i:
                break
            else:
                cnt = cnt + 1
                chk = chk - 1

    print(cnt)

main_chk()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = str(input())
cn = [False]*n
for i in range(n):
    if c[i]=="R":
        cn[i]= True


ans = 0
k=n-1
b=0

for i in range(n):
    if b==1:
        break

    if cn[i]== False:
        for j in range(k,0,-1):

            if cn[j]==True:
                k =j-1
                if i < j:
                    cn[i]=True
                    #cn[j]=False
                    ans +=1
                    break
                elif i>=j:
                    b=1
                    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
colors = input()
data = [int(c == 'R') for c in colors]

min_move = float('inf')

for i in range(N + 1):
    left = data[:i]
    lm = len(left) - sum(left)
    right = data[i:]
    rm = sum(right)
    move = min(lm, rm) + abs(lm - rm)
    min_move = min(min_move, move)

print(min_move)

Submission status: Time Limit Exceeded

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


def cut(c):
    num = 0
    for i in range(N):
        if c[i]!='R':
            break
        else:
            num+=1
    return c[num:]


def change(c):
    for i in range(len(c)-1, 0, -1):
        if c[i]=='R':
            return 'R' + c[1:i]+'W'+c[i+1:]
    
c = cut(c)
cnt = 0
while 'R' in c:
    c = change(c)
    c = cut(c)
    cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

end = []
for i in range(n+1):
    end.append("R"*i + "W"*(n-i))
#print(end)

newc = "".join(c)
ans = 0
j,k = -1,n
if newc in end:
    print(ans)
    exit()
while newc not in end:
    for i in range(j+1,n):
        if c[i] == "W":
            j = i
            c[i] = "R"
            break
    for i in reversed(range(k)):
        if c[i] == "R":
            k = i
            c[i] = "W"
            break
    newc = "".join(c)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    ws = []
    rs = []
    s = input()
    for i in range(n):
        if s[i] == 'W':
            ws.append(i)
        else:
            rs.append(i)

    if len(ws) == 0 or len(rs) == 0:
        print('0')
        return

    cnt = int(0)
    w_min = ws[0]
    r_max = rs[-1]
    while w_min < r_max:
        cnt += 1
        del ws[0]
        del rs[-1]

        if len(ws) == 0 or len(rs) == 0:
            break
        w_min = ws[0]
        r_max = rs[-1]
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

wi = c.find('W')
ri = c.rfind('R')

if wi == -1 or ri == -1:
    print(0)
else:
    ans = 0
    while wi < ri:
        s = list(c)
        s[wi] = 'R'
        s[ri] = 'W'
        c = ''.join(s)
        wi = c.find('W')
        ri = c.rfind('R')
        ans += 1

    print(ans)

Submission status: Time Limit Exceeded

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 > sorted:
    break
print(str(ans))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right as br

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


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

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

chk = N - 1
cnt = 0

for i in range(N):
    if C[i] == 'W':
        for x in reversed(range(0,chk+1)):
            if C[x] == 'R':
                chk = x
                break
        if chk <= i:
            break
        else:
            cnt = cnt + 1
            chk = chk - 1

print(cnt)