Problem:


Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3



Sample input:
4
WWRR


Sample output:
2

Submission status: Accepted

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

l = 0
r = N-1
ans = 0
while(l < r):
    while(l < N and c[l] == 'R'):
        l += 1
    while(r >= 0 and c[r] == 'W'):
        r -= 1
    if l > r or l >= N or r < 0:
        break
    else:
        # print(l, r)
        if l-1 == r:
            ans += 1
            break
        else:
            l += 1
            r -= 1
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
r_count=c.count("R")
w_count=c.count("W")
temp=list(c[:n-w_count])
print(temp.count("W"))

Submission status: Accepted

Solution code:
N = int (input())
c = str(input())
count=0
#print(c.count("R"), c.count("W"))
for i in range(c.count("R")):
    if c[i] == "W":
        count += 1
print(count)

Submission status: Accepted

Solution code:
N=int(input())
stone= list(input())
white_number=sum(x=='W' for x in stone)
stone.reverse()
ans=0
for i in range(white_number):
    if stone[i] != 'W':
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
def count_change(num):
    return max(num[0][0], num[1][1])

N = int(input())
C = input()
W = 0
R = 0
for c in C:
    if c=="W":
        W += 1
    else:
        R += 1

num = [[0,0],[W,R]]
min_change = count_change(num)
for i in range(N):
    if C[i]=="W":
        num[0][0] += 1
        num[1][0] -= 1
    else:
        num[0][1] += 1
        num[1][1] -= 1
    change = count_change(num)
    if change < min_change:
        min_change = change
        
print(min_change)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r_c = 0
ch = 0
for i in S:
  if i == "R":
    r_c += 1
for i in range(r_c):
  if S[i] == "W":
    ch += 1
print(ch)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
srcount = s.count('R')
if srcount == 0:
  print(0)
  exit()
t = s[:srcount]
twcount = t.count('W')
print(twcount)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
s = S.count("W")
no = N - s
ans = 0
for i in range(no):
    if S[i]=="W":
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys,math
from collections import deque,defaultdict
import operator as op
from functools import reduce

I=sys.stdin.readline

def ii():
	return int(I().strip())
def li():
	return list(map(int,I().strip().split()))
def mi():
	return map(int,I().strip().split())
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom


def main():
	n=ii()
	s=I().strip()
	i=0
	j=n-1
	cnt=0
	while i<j:
		if s[i]=="R":
			i+=1
		if s[j]=="W":
			j-=1
		if s[i]=="W" and s[j]=="R":
			cnt+=1
			i+=1
			j-=1
	print(cnt)






		










		


		










		






if __name__ == '__main__':
	main()

Submission status: Accepted

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

L = 0
R = n - 1
swaps = 0
while L < R:
    if c[L] == 'W' and c[R] == 'W':
        R -= 1
    elif c[L] == 'W' and c[R] == 'R':
        swaps += 1
        c[L], c[R] = c[R], c[L]
    elif c[L] == 'R' and c[R] == 'W':
        R -= 1
        L += 1
    elif c[L] == 'R' and c[R] == 'R':
        L += 1

print(swaps)

Submission status: Accepted

Solution code:
N = int(input())
C = str(input())
x=C.count('R')
ans=0
for i in range(x):
    if C[i]=='W':
        ans=ans+1
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
from collections import Counter

n = ii()
s = input()

d = Counter(s)
num = d['R']
ans = num
wcnt = 0
rcnt = 0
for i in s:
#    print(i)
    if i == 'W':
        wcnt += 1
    else:
        if num - 1 >= wcnt or wcnt == 0:
            ans -= 1
            num -= wcnt+1
            wcnt = 0
        if num == 0:
            break
print(ans)

Submission status: Accepted

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

w = S.count('W')
r = N - w
bad_w = S[:r].count('W')
bad_r = S[r:].count('R')

if w == 0 or r == 0:
    print(0)
else:
    print(min(bad_r, bad_w))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    c = list(input())
    print(c[:c.count('R')].count('W'))

main()

Submission status: Accepted

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

w = s.count('W')

s1 =list(sorted(s))
if 'W' in s:
    ans2 = s[:s1.index('W')].count('W')
else:
    ans2 = n
print(min(ans2, w, n-w))

Submission status: Accepted

Solution code:
import sys
import math
import time
sys.setrecursionlimit(int(1e6))
#dprint = print
def dprint(*args):
   pass

N = list(map(int, input().split()))[0]
s = list(input())

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

while(l < r):
    dprint('l,r=', l, r)
    dprint('s=', s)
    while(l < r):
        if (s[l]=='W'):
            break
        l = l + 1
        dprint('l=', l)

    while(l < r):
        if (s[r]=='R'):
            break
        r = r - 1
        dprint('r=', r)

    if (l < r):
        s[l] = 'R'
        s[r] = 'W'
        count = count + 1
    
print(count)

Submission status: Accepted

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

num=c.count('R')
if num == 0 or len(c) == num:
  print(0)
  exit()

count=0

for i in range(num):
  if c[i] == 'W':
    count+=1

print(count)

Submission status: Accepted

Solution code:
def d1(x):
  if x == "R":
    return 0
  else:
    return 1

N = int(input())
C = list(map(d1,input()))
w = sum(C)
r = N - w

if sum(C[:r]) < w:
  print(sum(C[:r]))
else:
  print(w)

Submission status: Accepted

Solution code:
n=int(input())
c=input()
w_num=c.count("W")
r_num=c.count("R")
now_r_num=0
for i in range(r_num):
    if c[i]=="R":
        now_r_num+=1
print(r_num-now_r_num)

Submission status: Accepted

Solution code:
from sys import stdin


def readline_from_stdin() -> str:
    return stdin.readline().rstrip


if __name__ == '__main__':
    _ = int(input())
    # R = 0
    # W = 1
    C = [0 if c == "R" else 1 for c in input()]
    # print(C)

    L = 0
    R = len(C)-1

    count = 0
    while True:
        # print(L,R,C)
        # 次の白まで飛ぶ
        while C[L] == 0:
            if L == len(C)-1:
                print(count)
                exit()
            L += 1
        # 次の赤まで飛ぶ
        while C[R] == 1:
            if R == 0:
                print(count)
                exit()
            R -= 1
        # 真ん中まで見たなら終わり
        if R-L < 1:
            print(count)
            exit()

        # 入れ替え
        C[L] = 0
        C[R] = 1
        L += 1
        R -= 1
        count += 1
        # print("!!!")
        # 入れ替え後、終わり？
        if L == len(C)-1:
            print(count)
            exit()
        if R == 0:
            print(count)
            exit()

Submission status: Accepted

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


def main():
    N = int(input())
    c = list(input())
    w = c.count("R")
    hidari = c[:w]
    ans = 0
    for i in hidari:
        if i == "W":
            ans += 1
    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(input())
    C = input()
    red = C.count('R')
    red2 = C[:red].count('R')
    print(red-red2)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
c=list(input())
countR = c.count('R')
countW2 = 0
countR2 = 0
for i in range(countR):
    if c[i]=="W":
        countW2 += 1
print(countW2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
c=list(input())
ans=0
import collections

n=c.count("R")

for i in range(n):
    if c[i]!="R":
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[2]:


import collections


# In[26]:


N = int(input())
clist = input()


# In[27]:


c = collections.Counter(clist)
cr = c["R"]
# mylist = [x for x in clist]
# mylist_r = list(reversed(mylist))
# ans = 0
# while mylist[:cr].count("R") != cr:
#     W_i = mylist.index("W")
#     R_i = mylist_r.index("R")
#     mylist[W_i] = "R"
#     mylist[N-R_i-1] = "W"
#     mylist_r[N-W_i-1] = "R"
#     mylist_r[R_i] = "W"
#     ans += 1
# print(ans)
ans = clist[:cr].count("W")
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
if len(set(C)) == 1:
  print(0)
else:
  R = C.count("R")
  W = C.count("W")
  print(max(C[:R].count("W"), C[R:].count("R")))

Submission status: Accepted

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

cnt = 0
r_num = s.count('R')
w_num = n - r_num

# 要はRRRRWWWWWの形にする
# r_num以内にあるRは動かさなくて良い
# r_numより外にあるRを動かす必要がある

for i in range(n):
    if i > r_num - 1 and s[i] == 'R':
        cnt += 1
print(cnt)

Submission status: Accepted

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

left = 0
right = len(c)-1
count = 0

while left < right:
    while c[left] != 'W':
        left += 1
        if left >= right:
            print(count)
            exit()
    while c[right] != 'R':
        right -= 1
        if left >= right:
            print(count)
            exit()
        
    count += 1
    left += 1
    right -= 1

print(count)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
c = 0
l=0
r=len(S)-1
while r > l:
    while l < r and S[l] == 'R':
        l += 1
    while r > l and S[r] == 'W':
        r -= 1
    if r > l:
        S[l] = 'R'
        S[r] = 'W'
        c += 1
print(c)

Submission status: Accepted

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

left = 0
right = N-1
c = 0

while True:
  if right <= left:
    break
  if seq[left] == "R":
    left += 1
    continue
  if seq[right] == "W":
    right -= 1
    continue
  left += 1
  right -= 1
  c += 1

print(c)

Submission status: Accepted

Solution code:
import collections


N= int(input())
s = input()
counter = collections.Counter({'R':0, 'W':0})
counter.update(s)
print(s[:counter['R']].count('W'))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r_count=s.count('R')
ans='R'*r_count+'W'*(n-r_count)
cnt=0
for i in range(n):
    if ans[i]!=s[i]:
        cnt+=1
print(cnt//2)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def set_w(c, s):
    # sをWの位置にセット
    while s < len(c):
        if c[s] == 'W':
            return s
        else:
            s += 1
    return s

def main():
    n = int(input())
    c = list(input())
    s = 0
    e = len(c) - 1
    ans = 0

    # sを最初のWにセット
    s = set_w(c, s)

    while e > s:
        if c[e] == 'R':
            c[s] = 'R'
            c[e] = 'W'
            s = set_w(c, s)
            ans += 1
        e -= 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

left = 0
right = N - 1

ans = 0
while left < right:
    if C[left] == "W":
        if C[right] == "R":
            ans += 1
            left += 1
            right -= 1
        else:
            right -= 1
    else:
        left += 1


print(ans)

Submission status: Accepted

Solution code:
import sys

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

left = [-1, False]  # 一番左の W　を記録
right = [len(c), False]  # 一番右の R を記録
count = 0

for i in range(len(c)):
    if not left[1]:
        left[0] += 1
        if c[left[0]] == 'W':
            left[1] = True

    if not right[1]:
        right[0] -= 1
        if c[right[0]] == 'R':
            right[1] = True

    if left[1] and right[1]:
        if left[0] > right[0]:
            break
        count += 1
        left[1] = False
        right[1] = False

    # if c[i] == 'W':
    #     left = i
    # if c[len(c) - 1 - i] == 'R':
    #     right = len(c) - 1 - i
    #
    # if (left is not None) and (right is not None) and (left < right):
    #     # print(f'left{left}, right {right}')
    #     count += 1
    #     left = None
    #     right = None

print(count)

Submission status: Accepted

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

r = c.count('R')
w = n - r

l = ['R'] * r + ['W'] * w

count = 0
for i in range(n):
    if c[i] != l[i]:
        count += 1
        
print(int(count / 2))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
# left should record the first w
# right should record the last r
for i in range(n):
    if s[i] == 'W':
        left = i
        break
else:
    print(0)
    exit()
for i in range(n-1, -1, -1):
    if s[i] == 'R':
        right = i
        break
else:
    print(0)
    exit()
ans = 0
while left < right and left < n and right >= 0:
    ans += 1
    # perform the swap
    left += 1
    right -= 1
    while left < n and s[left] != 'W':
        left += 1
    while right >= 0 and s[right] != 'R':
        right -= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
x=[]
y=[]

for i in range(n):
  if s[i]=='R':
    x+=[i]
  elif s[i]=='W':
    y+=[i]
    
ans=0


while True:
    if len(y)==0:
      print(ans)
      break
    elif len(x)==0:
      print(ans)
      break
    elif x[-1]>y[0]:
      x.pop()
      y.pop(0)
      ans+=1
      continue
    
    else:
      print(ans)
      break

Submission status: Accepted

Solution code:
import copy

# a = get_int()
def get_int():
    return int(input())

# a = get_string()
def get_string():
    return input()

# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]

# a_list = get_string_list():
def get_string_list():
    return input().split()

# a, b = get_int_multi()
def get_int_multi():  # a, b = get_int_multi()
    return map(int, input().split())

# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))

# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    n = get_int()
    c_list = get_string_char_list()

    count = 0
    ret = 0
    for i in range(0, n):
        if c_list[i] == "R":
            count += 1

    for i in range(0, count):
        if c_list[i] == "W":
           ret += 1

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
stone = list(input())
r = 0
counter = 0
for i in range(n):
  if stone[i] == 'R':
    r += 1
for i in range(r):
  if stone[i] == 'W':
    counter += 1
print(counter)

Submission status: Accepted

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

r = c.count("R")
w = c.count("W")
a = "R"*r+"W"*w

ans = 0
for i in range(N):
    if(c[i] != a[i]):
        if(c[i] == "W" and a[i] == "R"):
            ans += 1
print(ans)

Submission status: Accepted

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

N=int(input().rstrip('\n'))
S=list(input().rstrip('\n'))
score = [S.count('R'),S.count('W')]
sta = 'R'
ws = 0
rs = S.count('R')
sto = 0
for i in range(N):
  if S[i] != sta:
    if sta=='W':
      ws += sto
      sto = 1
      score.append(max(rs,ws))
      sta ='R'
    else:
      rs -= sto
      sto = 1
      score.append(max(rs,ws))
      sta ='W'
  else:
    sto += 1
print(min(score))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力

n = int(input())
c = input()
# c = list(map(int, input().split()))


r_max = 0
w_min = n - 1

cnt = 0

while r_max < w_min:
    if c[r_max] == "R":
        r_max += 1
        continue
    elif c[w_min] == "W":
        w_min -= 1
        continue
    else:
        cnt += 1
        r_max += 1
        w_min -= 1


print("{}".format(cnt))

Submission status: Accepted

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

head = 0
tail = n-1
cnt = 0

while head < tail:
  if c[tail] == "R":
    while c[head] == "R" and head != tail:
      head += 1
    if head != tail:
      head += 1
      cnt += 1
  tail -= 1
  
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50



def run():
    N = int(input())
    C = list(input())
    ans1 = C.count('W')
    ans2 = C.count('R')
    ans = min(ans1, ans2)
    if not ans:
        print(ans)
        return

    ans3 = 0
    l = 0
    r = N-1
    while l<r:
        if C[l] == 'W' and C[r] == 'R':
            ans3 += 1
            l += 1
            r -= 1
            continue
        elif C[l] == 'R':
            l+=1
        elif C[r] == 'W':
            r-=1
    print(min(ans, ans3))

if __name__ == "__main__":
    run()

Submission status: Accepted

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


def main():
  N = int(input())
  C = input()[:-1]

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

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

  print(ans)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
n = int(input())
c = input()
cnt = 0
l, r = -1, n + 2

while True:
    l = c[l + 1 :].find("W") + l + 1
    r = c[:r].rfind("R")
    if l == -1 or r == -1 or r <= l:
        break
    else:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())
r=0
for i in l:
    if i=="R":
        r+=1
cr=0
cw=0
if r==0:
    print("0")
else:
    for i in range(len(l)):
        if l[i]=="R":
            cr+=1
        else:
            cw+=1
        if cw== r-cr:
            break
    print(cw)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    #rn = s.count("R")
    #wn = s.count("W")
    #red = 0
    #white = 0
    l = 0
    r = n-1
    ans = 0
    while(True):
        if(l >= n or r < 0):
            break
        if(s[l] == "R"):
            l += 1
            continue
        else:
            while(s[r] == "W"):
                if(r < 0):
                    print(0)
                    return
                r -= 1
            if(l >= r):
                break
            l += 1
            r -= 1
            ans += 1
            if(l >= r):
                break
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq


N = int(input())
C = list(input())
W = [i for i, c in enumerate(C) if c == 'W']
R = [-i for i, c in enumerate(C) if c == 'R']
if not W or not R:
    print(0)
else:
    heapq.heapify(W)
    heapq.heapify(R)
    res = 0
    while W[0] < -R[0]:
        i = heapq.heappop(W)
        j = heapq.heappop(R)
        heapq.heappush(W, -j)
        heapq.heappush(R, -i)
        res += 1
    print(res)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
cr = c.count('R')
cw = c.count('W')
if cr == 0 or cw == 0:
    print(0)
    exit()
print(c[:cr].count('W'))

Submission status: Accepted

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

r = C.count('R')
ans = 0
for c in C:
    if r == 0:
        break
    if c == 'W':
        ans += 1
        r -= 1
    else:
        r -= 1

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b


# 入れ替えだけ考えればいいのかな？
N = int(input())
C = read()[:-1]
cntR = C.count('R')
print(C[:cntR].count('W'))

Submission status: Accepted

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

if 'R' in c:
  rc = c.count('R')
  change = c[:rc].count('W')
  right = 0
  for i in range(N):
    if c[N-1-i] == 'R':
      right = N-i
      break
  paint = c[:right].count('W')
  ans = min(change, paint)
else:
  ans = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
T = deque(list(input()))
W = deque([])
R = deque([])
i = 1
while T:
  x = T.popleft()
  if x=="W":
    W.append(i)
  else:
    R.append(i)
  i += 1
len_w = len(W)
len_r = len(R)
if len_w==0 or len_r==0:
    print(0)
    exit()
else:
    c = 0
    while R[-1]>W[0]:
        x = W.popleft()
        y = R.pop()
        W.append(y)
        R.appendleft(x)
        c += 1
    print(min(c,len_w,len_r))

Submission status: Accepted

Solution code:
import sys


def _s(): return sys.stdin.readline().strip()


def main():
    _ = _s()
    s = _s()

    rc = 0
    wc = s.count("R")
    m = wc
    for c in s:
        if c == "R":
            wc -= 1
        else:
            rc += 1
        m = min(m, max(wc, rc))
    return m


if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    C = LS()

    j = N-1
    cnt = 0
    # print(C)
    for i in range(N):
        if i >= j: break
        ci = C[i]
        if ci == 'W':
            # change
            # print(i, j)
            for k in range(j, i, -1):
                j = k-1
                ck = C[k]
                if ck == 'R':
                    # print(i, j)
                    # C[i], C[k] = ck, ci
                    cnt += 1
                    break

    # print(C)
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    C = input()
    i = 0
    j = N - 1
    ans = 0
    while i < j:
        if C[i] == 'W' and C[j] == 'W':
            j -= 1
        elif C[i] == 'W' and C[j] == 'R':
            # tmp = C[i]
            # C[j] = C[i]
            # C[i] = tmp
            i += 1
            j -= 1
            ans += 1
        elif C[i] == 'R' and C[j] == 'R':
            i += 1
        elif C[i] == 'R' and C[j] == 'W':
            i += 1
            j -= 1
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    #t=int(input())
    t=1
    for _ in range(t):
        n=int(input())
        st=input()
        #n=len(st)
        red=0
        white=0
        for i in range(n):
            if st[i]=='R':
                red+=1
            else:
                white+=1
        ans=0
        for i in range(red):
            if st[i]=="W":
                ans+=1
        print(ans)








BUFSIZE = 8192


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)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threadin.Thread(target=main).start()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
l = 0
r = n - 1
cnt = 0
while l < r:
    if s[l] == "R":
        l += 1
    elif s[r] == "W":
        r -= 1
    else:
        cnt += 1
        l += 1
        r -= 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
c = list(input())
c_dict = Counter(c)
if len(c_dict) == 1:
    print(0)
else:
    print(c[:n - c_dict['W']].count('W'))

Submission status: Accepted

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

ans1=c.count("R")
ans2=c.count("W")

ans3=0
for i in range(ans1):
    if c[i]=="W":
        ans3+=1

print(min([ans1,ans2,ans3]))

Submission status: Accepted

Solution code:
n=int(input())

c=input()

wnum=0

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

cnt=0
for ci in c[:n-wnum]:
    if ci=="W":
        cnt+=1

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from collections import  Counter
def resolve():
    N,=pin()
    C=input()
    X=Counter(C)
    a=(min(X["R"],X["W"]))#すべての色を変えにしてしまったらいい時
    c=list(C)


    rightlimit_R=-1
    for i in range(1,N):
        if c[-i]=="R":
            rightlimit_R=N-i
            break
    cnt=0

    for i in range(N):

        if c[i]=="W":

            for j in range(rightlimit_R-1,0,-1):
                if c[j]=="R":

                    
                    rightlimit_R=j
                    break

            cnt+=1
        if rightlimit_R<=i:
            
            break


    print(min(a,cnt))
    
#%%submit!
resolve()

Submission status: Accepted

Solution code:
n=int(input())
c=input()
l=0
r=n-1
score=0
for i in range(n):
  if l>=r:
    break
  if c[l]=="W":
    if c[r]=="R":
      score=score+1
      l=l+1
      r=r-1
    else:
      r=r-1
  else:
    l=l+1
print(score)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w_cnt = 0
r_cnt = 0
r_lis = []
res=0
#RRRRRWWWWWにしたい
tmp = s.count("R")
for i in range(tmp):
    if(s[i]=="W"):
        res+=1

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    n = int(input())
    s = input()

    r_list = deque([])
    for i,j in enumerate(s):
        if j == 'R':
            r_list.append(i)

    c = 0
    for i,j in enumerate(s):
        if r_list: 
            if j == 'W':
                r_list.pop()
                c += 1
            else:
                r_list.popleft()
        else:
            break
    print(c)
        
main()