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:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]
#CW = [list(map(int, list(input()))) for _ in range(H)]


N = int(input())
c = list(input())
ans = 0
l = 0
r = N-1

while True:
  if(l>=r):
    break
  if(c[l]=="R"):
    l+=1
    continue
  elif(c[r]=="W"):
    r-= 1
    continue
  else:
    l+=1
    r-=1
    ans+=1
    continue

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
input()
c = input()
R = c.count("R")
W = c.count("W")
c2 = "R"*R+"W"*W
r = sum(i!=j and i =="W" for i, j in zip(c,c2))
w = sum(i!=j and i =="R" for i, j in zip(c,c2))
print(min(r,w))

Submission status: Accepted

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

c = 0
i = 0
j = n - 1

while True:
   while cs[i] != 'W' and i < j:
      i += 1
   while cs[j] != 'R' and i < j:
      j -= 1
   if i < j:
      cs[i], cs[j] = cs[j], cs[i]
      c += 1
   else:
      break

print(c)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Cs = list(input())
from collections import deque
Q = deque()
for i, c in enumerate(Cs):
    if c=='R':
        Q.append(i)

ans = 0
for i in range(N):
    c = Cs[i]
    if c=='W':
        if not Q:
            break
        r = Q.pop()
        if r>i:
            ans += 1
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
n = int(input())
#n, d = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

rc = 0
for i in range(n):
    if s[i] == "R":
        rc += 1

wc = 0
for i in range(rc):
    if s[i] == "W":
        wc += 1

print(wc)

Submission status: Accepted

Solution code:
n = int(input())
lc = input()
lc = lc.lstrip("R").rstrip("W")

ans = 0
lin = 0
rin = len(lc)-1
flg = False
while lin < rin:
    while True:
        if lc[lin] == "W":
            break
        lin += 1
    while True:
        if lc[rin] == "R":
            break
        rin -= 1
    if lin > rin:
        break
    lin += 1
    rin -= 1
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
a=int(input())
b=list(input())
c=[0]*a
for i in range(a):
  if b[i]=="R":
    c[i]=1
d=sum(c)
print(d-sum(c[:d]))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
left=0
right=len(s)-1
ans=0
while left<right:
    if s[left]!="W":
        left+=1
    if s[right]!="R":
        right-=1
    if s[left]=="W" and s[right]=="R":
        ans+=1
        left+=1
        right-=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l,r=0,n-1
ans=0
while l<r:
  cnt=0
  while r>=0 and s[r]=='W':r-=1
  while l<n and s[l]=='R':l+=1
  if l>=r:break
  while s[r]=='R':
    cnt+=1
    r-=1
  cm=cnt
  while cnt:
    if s[l]=='W':cnt-=1
    l+=1
    if l>r:break
  ans+=cm-cnt
print(ans)

Submission status: Accepted

Solution code:
num = int(input())
strs = input()
allRs = 0
notAlignedRs = 0
for i in range(num):
    if strs[i] == 'R':
        allRs += 1
for i in range(allRs):
    if strs[i] == 'W':
        notAlignedRs += 1
print(notAlignedRs)

Submission status: Accepted

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

w = 0
w_left = [None]*(N+1)
w_left[0] = 0
for i in range(N):
    if c[i] == 'W': w += 1
    w_left[i+1] = w

r = 0
r_right = [None]*(N+1)
r_right[N] = 0
for i in reversed(range(N)):
    if c[i] == 'R': r += 1
    r_right[i] = r

def cost(i):
    return max(w_left[i], r_right[i])

best = min(cost(i) for i in range(N+1))
print(best)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())
cnt_r = l.count('R')
cnt = 0
for i in range(cnt_r,n):
    if l[i] == 'R':
        cnt += 1
print(cnt)

Submission status: Accepted

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

right = N - 1
left = 0
ans = 0
flag = False

while True:
  if c[right]=='W':
    if (right>=1)and(right>left+1):
      right -= 1
      continue
    else:
      break
    
  else:
    while True:
      if c[left]=='R':
        if (left<N)and(left<right-1):
          left += 1
          continue
        else:
          flag = True
          break
        
      else:
        ans += 1
        left += 1
        right -= 1
        break
    if flag:
      break
      
print(ans)

Submission status: Accepted

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

from collections import Counter

c=Counter(s)

r=c['R']

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


print(cnt)

Submission status: Accepted

Solution code:
#from bisect import bisect_left as bl                #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br               #c++ upperbound br(array,element)
#from __future__ import print_function, division    #while using python2


def modinv(n,p):
    return pow(n,p-2,p)

def main():
    #sys.stdin = open('input.txt', 'r')
    #sys.stdout = open('output.txt', 'w')

    n = int(input())
    s = input()
    c1 = s.count('W')
    c2 = s.count('R')

    c3 = s[:c2].count('W')
    print(min(c1, c3))

#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            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()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    C = kwargs["C"]
    left = 0
    right = C.count("R")
    res = max([left, right])

    for i in range(n):
        if C[i] == "W":
            left += 1
        if C[i] == "R":
            right -= 1
        tmp = max([left, right])
        res = min(res, tmp)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]
    cin = input().split()[0]
    kwargs["C"] = [i for i in cin]

    cout = main(kwargs)
    print(cout)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    C = list(input())
    #print(C)    
    print('{:}'.format(C[:C.count('R')].count('W')))
    #print('R :{:}'.format(C.count('R')))

    return 0

if __name__ == "__main__":
    main()

Submission status: Accepted

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

# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = input_int()
    c_list = input()

    red_cnt = c_list.count("R")
    white_cnt = n - red_cnt

    pre = "R"
    is_out = False
    for c in c_list:
        if c != pre:
            is_out = True
            break
        pre = c

    if is_out:
        print(min(red_cnt, white_cnt, c_list[:red_cnt].count("W")))
    else:
        print(0)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug  2 17:58:28 2020

@author: Paras
"""

n = int(input())
s = input()

i = 0
j = n-1
count = 0
while i<j:
    while i<n and s[i]=='R':
        i+=1
    while j>=0 and s[j]=='W':
        j-=1
    if i>=j:
        break
    count+=1
    i+=1
    j-=1
    
print(count)

Submission status: Accepted

Solution code:
import heapq
import sys
N = int(input())
K = list(input())
W = []
R = []
for i in range(N):
    if K[i] == "W":
        W.append(i)
    else:
        R.append(-i)

if len(W) == 0 or len(R) == 0:
    print(0)
    sys.exit()
heapq.heapify(W)
heapq.heapify(R)
c = 0
while(True):
    a = heapq.heappop(W)
    b = heapq.heappop(R)
    b *= -1
    if a > b:
        break
    heapq.heappush(W, b)
    heapq.heappush(R, -a)
    c += 1
print(c)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
## coding: UTF-8
from collections import deque

N = int(input())
c = input()
l = deque()
for i in range(N):
    l.append(c[i])
#print(l)


t = 0
#前処理(t = 0)
def shori():
    stat = True
    while stat:
        if(len(l) > 0):
            if(l[0] == 'R'):
                l.popleft()
            else:
                stat = False
        else:
            stat = False

    stat = True
    while stat:
        if(len(l) > 0):
            if(l[-1] == 'W'):
                l.pop()
            else:
                stat = False
        else:
            stat = False

    #print('t=', t, l)

shori()

while len(l) > 0:
    l.popleft()
    l.pop()
    t += 1
    shori()

print(t)

Submission status: Accepted

Solution code:
# N, D = [int(i) for i in input().split()]
N = int(input())
C = list(input())
l = 0
r = N - 1
count = 0
while l < r:
  if C[l] == 'W' and C[r] == 'R':
    C[l], C[r] = C[r], C[l]
    l += 1
    r -= 1
    count += 1
  else:
    if C[l] == 'R':
      l += 1
    if C[r] == 'W':
      r -= 1
print(count)

Submission status: Accepted

Solution code:
N=int(input())

c=input()

C=[a for a in c]

number=0
answer=0
red=C.count('R')

if red==0 or red==N:
    print(0)
else:
    words=C[red:]
    print(words.count('R'))

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
ramount = c.count('R')
ans = c[: ramount].count('W')
print(ans)

Submission status: Accepted

Solution code:
#d
n = int(input())
s=input()
r=s.count("R")
w=s.count("W")    
print(s[0:r].count("W"))

Submission status: Accepted

Solution code:
"""According to a fortune-teller, a white stone placed to the immediate left of a red stone 
WR leads to nok
"""
n = int(raw_input())
s = list(raw_input())
import collections


def f(s):
	cc = collections.Counter(s)
	if cc['R'] == 0 or cc['W'] == 0:
		return 0
	i = 0
	j = len(s) - 1
	#while(i < len(s) and s[i] == 'R'): i +=1
	#while(j>=0 and s[j] == 'W'): j -=1
	
	c = 0
	while(i < j):
		if s[i] == 'W' and s[j] == 'R':
			s[i],s[j] = s[j],s[i]
			i += 1
			j -= 1
			c += 1
		while(i < j and s[i] == 'R'): i+=1
		while(i < j and s[j] == 'W'): j-=1
	return min(c, cc['R'], cc['W'])

print f(s)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=0
for i in range(n):
  if s[i]=="R":
    r+=1
w=n-r
ans=0
for i in range(r):
  if s[i]=="W":
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
C=list(input())
D=sorted(C)
P=0
for i in range(N):
  if C[i]!=D[i]:
    P+=1
print(P>>1)

Submission status: Accepted

Solution code:
n=int(input(""))
lis=input("")
rt=0
for i in lis:
    if(i=="R"):
        rt+=1
ht=n-rt
r=0
h=0

s=max(h,rt)

for i in lis:
    if(i=="R"):
        r+=1
    else:
        h+=1
    on=rt-r
    m=max(on,h)
    if(s>m):
        s=m

print(s)

Submission status: Accepted

Solution code:
n=int(input())

s=input()
import heapq
windex=[]
rindex=[]

for i,ss in enumerate(s):
  if ss=="W":
    heapq.heappush(windex,i)
  else:
    heapq.heappush(rindex,i*(-1))
 
cnt=0
while 1:
  if len(windex)==0 or len(rindex)==0:
    print(cnt)
    exit()
  else:
    tmpw=windex[0]
    tmpr=rindex[0]*(-1)
    
    if tmpw>tmpr:
      print(cnt)
      exit()
    else:
      cnt+=1
      heapq.heappop(windex)
      heapq.heappop(rindex)

Submission status: Accepted

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

Submission status: Accepted

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

count=0;
lp=0
rp=N-1
swap=0
for i in range(N):
    while c[lp]!='W' and lp<rp:
        lp+=1
    while c[rp]!='R' and lp<rp:
        rp-=1
    if lp<rp:
        swap+=1
        lp+=1
        rp-=1
    else:
        break
print(swap)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
r = c.count("R")
ans = 0
for i in range(r):
    if c[i] == "W":
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
from collections import deque
queue=deque(input())
#print(queue)
#C=[0]*n
s=0
while len(queue)>1:
    a=queue.popleft()
    b=queue.pop()
    if a=='W' and b=='R':
        s+=1
        #print(s)
    elif a=='W' and b=='W':
        while queue and b=='W':
            b=queue.pop()
        if b=='R':
            s+=1
    elif a=='R' and b=='R':
        while queue and a=='R':
            a=queue.popleft()
        if a=='W':
            s+=1                
print(s)

Submission status: Accepted

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

l=[]
for c in cs:
    l.append(1 if c=='R' else 0)

iW=0
iR=N-1
ans=0
while True:
    #iW
    while l[iW]!=0:
        iW+=1
        if iW > N-1:
            break
    #iR
    while l[iR]!=1:
        iR-=1
        if iR<0:
            break
    if iW<iR:
        l[iW],l[iR]=l[iR],l[iW]
        ans+=1
    else:
        break
print(ans)

Submission status: Accepted

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

n=int(input())
s=list(input().strip())
count = 0
cw = s.count('W')
cr = n-cw

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

print(count)

Submission status: Accepted

Solution code:
n = input()
s = input()
L = 0
r = len(s)

cnt = 0
while True:
    L = s.find('W')
    if L == -1:
        break
    r = s.rfind('R')
    if r == -1:
        break

    if (L > r):
        break

    s = s[L + 1:r].lstrip('R').rstrip('W')

    cnt += 1

if L != -1 and L < r:
    cnt += L + 1

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
a = C.count("R")
b = C[:a].count("R")
print(a-b)

Submission status: Accepted

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

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

r_cnt = 0
w_cnt = 0
for i in c:
    if i =='R':
        r_cnt += 1
    else:
        w_cnt += 1

r_cnt2 = 0
for i in c[:-w_cnt-1:-1]:
    if i == 'R':
        r_cnt2 += 1
w_cnt2 = 0
for i in c[:r_cnt]:
    if i == 'W':
        w_cnt2 += 1

ans = min(w_cnt2, r_cnt2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
red = 0
for i in c:
    if i  == "R": red += 1
ans = 0
for i in c[red:]:
    if i == "R": ans += 1
print(ans)

Submission status: Accepted

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

c = [ "R" ] + c + [ "W" ]
l = 1
r = n
ans = 0
while l < r:
    while c[ l ] == "R":
        l += 1
    while c[ r ] == "W":
        r -= 1
    if l < r:
        c[ l ] = "R"
        c[ r ] = "W"
        ans += 1

print( ans )

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
C = input()
W = []
R = []
for i in range(N):
    if C[i] == 'W':
        W.append(i)
    else:
        R.append(i)
R.reverse()
ans = 0
for i in range(min(len(W), len(R))):
    if W[i] > R[i]:
        break
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    c=input().strip("\n")
    cntR=0
    cntW=0
    for i in range(N):
        if c[i]=="R":
            cntR+=1
        else:
            cntW+=1
    ans=N
    
    ans1=cntR
    for i in range(cntR):
        if c[i]=="R":
            ans1-=1
    ans=min(ans,ans1)
    
    print(ans)
    
    
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = input()
r = c.count('R')
cc = c[:r]
print(cc.count('W'))

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    c = list(input())
    r_cnt = 0
    for i in range(n):
        if c[i] == "R":
            r_cnt+=1
    w_cnt = n-r_cnt
    change = 0
    for i in range(r_cnt):
        if c[i] != "R":
            change+=1
    ans = change
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return [LIST() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]


INF = float("inf")
MOD = 10**9 + 7


def main():
    N = INT()
    C = input()

    count = 0
    head = 0
    tail = N-1
    while head < tail:
        # 頭からWを探す。Rである限り進む
        while head < N and C[head] == "R":
            head += 1
        # 後ろからRを探す。Wである限り戻る
        while 0 <= tail and C[tail] == "W":
            tail -= 1
        # 順序が逆転したら終わり
        if head > tail:
            break
        # 入れ替える操作
        count += 1
        head += 1
        tail -= 1
    print(count)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

a = k.count("R")
b = k[:a].count("W")
print(b)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
f = 0
ans = 0
if 'W' not in c:
    f = 1

r_count = c.count("R")
for i in range(r_count):
    if c[i] == "R":
        ans -= 1

if f == 1:
    print("0")
else:
    print(ans+r_count)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    c=input()
    cc=sorted(c)
    count = 0
    for i in range(n):
        if c[i] != cc[i]:
            count += 1
    print(count//2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    _ = int(rl())
    c = rl().rstrip()
    
    counter = Counter(c)
    red = counter['R']
    white = counter['W']
    
    red -= c[:red].count('R')
    white -= c[-white:].count('W')
    print(min(red, white))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import deque
def resolve():
    N = int(input())
    C = input()
    deq = deque(C)
    count = 0
    while deq:
        if deq[-1]=='W':
            deq.pop()
        if len(deq)==0:
            break

        if deq[0]=='R':
            deq.popleft()
        if len(deq)==0:
            break
        
        if deq[0]=="W" and deq[-1]=='R':
            deq.pop()
            deq.popleft()
            count +=1
    print(count)
resolve()

Submission status: Accepted

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

count = 0
if "W" in c and "R" in c:
  left_w = c.find("W")
  right_r = c.rfind("R")
  while left_w < right_r:
    c = c[:left_w] + "R" + c[left_w+1:right_r] + "W" + c[right_r+1:]
    count += 1
    left_w = c.find("W")
    right_r = c.rfind("R")
print(count)

Submission status: Accepted

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

rCount=0
for i in range(N):
  if c[i]=='R':
    rCount+=1
wCount=0

for i in range(rCount):
  if c[i]=='W':
    wCount+=1
print(wCount)

Submission status: Accepted

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

count_r = 0
for i in range(n):
  if s[i] == "R":
    count_r += 1

ans = 0
for i in range(count_r):
  if s[i] == "W":
    ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = input()
    l = 0
    r = n - 1
    cnt = 0
    while True:
        ok = True
        while c[l] != "W":
            l += 1
            if l == r:
                ok = False
                break
        if not ok:
            break
        while c[r] != "R":
            r -= 1
            if l == r:
                ok = False
                break
        if not ok:
            break
        cnt += 1
        l += 1
        r -= 1
        if r <= l:
            break
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

d = list(c)

i = 0
j = N - 1
result = 0
while True:
    while i < N and d[i] != 'W':
        i += 1
    while j > 0 and d[j] != 'R':
        j -= 1
    if i == N or j == -1 or i >= j:
        break
    d[i] = 'R'
    d[j] = 'W'
    result += 1
print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = [int(i) for i in readline().split()]
s = input()


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

ans = 1<<30
for i in range(n+1):
    """
    W..WR..R (Wがi個)
    不純なWがw個
    不純なRがr個
    """
    v = max(w,r)
    ans = min(ans,v)
    
    if i!=n:
        if s[i] == "W":
            w += 1
        else:
            r -= 1
    
    #print(r,w)

print(ans)

Submission status: Accepted

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

ans = 0
l = 0
r = N-1
while l < r:
    if c[l] == "W":
        for i in range(r,l,-1):
            if c[i] == "R":
                ans += 1
                r = i-1
                break
        else:
            l = N
            break
    l += 1

print(ans)

Submission status: Accepted

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

if not 'R' in s:
    print(0)
    quit()

wn = [0] * n
if s[0] == 'W':
    wn[0] = 1
for i in range(1, n):
    wn[i] = wn[i-1]
    if s[i] == 'W':
        wn[i] += 1
rn = [0] * n
if s[-1] == 'R':
    rn[-1] = 1
for i in range(n-2, -1, -1):
    rn[i] = rn[i+1]
    if s[i] == 'R':
        rn[i] += 1
rn.append(0)
min_val = n
for i in range(n):
    m = max(wn[i], rn[i+1])
    if min_val > m:
        min_val = m
print(min_val)

Submission status: Accepted

Solution code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# def
def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

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

def int_list():
    return list(map(int,input().split()))

def print_space(l):
    return print(" ".join([str(x) for x in l]))

# import
import numpy as np



# main code
N = int(input())
c = list(input())

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

cnt2 = 0
for i in range(0,cnt):
    if c[i] == "R":
        cnt2 += 1

print(cnt - cnt2)

Submission status: Accepted

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

r = A.count("R")

ans = A[:r].count("W")

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=list(input())
white=s.count("W")
red=s.count("R")
llist=[]
wl=0
wr=white
bl=0
br=red
swap=0
for i in range(len(s)):
    if(s[i]=="W"):
        wr-=1
        if(wl<=br):
            llist.append(br)
        else:
            llist.append(wl)
        wl+=1
    else:
        br-=1
        if(wl<=br):
            llist.append(br)
        else:
            llist.append(wl)
        bl+=1
print(min(llist))

Submission status: Accepted

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

ans = 0
lp = 0
rp = n-1
while True:
    if lp >= rp:
        break
    while lp < n and c[lp] != 'W':
        lp += 1
    while rp >= 0 and c[rp] != 'R':
        rp -= 1
    if lp >= rp:
        break
    lp += 1
    rp -= 1
    ans += 1
print(ans)

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

Solution code:
import math

n = int(input())
cL = input()

ans = 0
s = 0
e = n - 1
while True:
    if s >= e:
        break
    if cL[s] == "R":
        s += 1
        continue
    else:
        if cL[e] == "W":
            e -= 1
            continue
        else:
            ans += 1
            s += 1
            e -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r_count = S.count('R')
first_w = S.find('W')

result = min(S[first_w:].count('R'), S[:r_count].count('W'))

print(result)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()
c = LS2()

w = [0]*N
r = [0]*N
for i in range(N):
    if i == 0:
        if c[i] == 'W':
            w[i] = 1
        else:
            r[i] = 1
    else:
        if c[i] == 'W':
            w[i] = w[i-1]+1
            r[i] = r[i-1]
        else:
            w[i] = w[i-1]
            r[i] = r[i-1]+1
a = w[-1]
b = r[-1]
if b == 0:
    print(0)
    exit()
for i in range(N):
    if w[i] == b-r[i]:
        print(w[i])
        exit()

Submission status: Accepted

Solution code:
def main():
    import collections

    N = int(input())
    stones = input()

    N_red = stones.count('R')
    N_whi = N - N_red

    ans = stones[:N_red].count('W')

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
c=input()
i,j=0,n-1
ans=0
while True:
    if c[i]=="W":
        while True:
            if c[j]=="R":
                ans+=1
                j-=1
                break
            j-=1
            if i>=j:
                break
    i+=1
    if i>=j: break
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()

    s = in_s()

    red = deque()
    white = deque()

    for i in range(N):
        if s[i] == 'R':
            red.append(i)
        else:
            white.append(i)

    if len(red) == 0:
        print(0)
        exit()
    if len(white) == 0:
        print(0)
        exit()

    cnt = 0
    while True:
        if len(red) == 0 or len(white) == 0:
            break
        if white[0] > red[-1]:
            break

        white.popleft()
        red.pop()
        cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
arr = list(input())
head = 0
tail = n-1
count = 0
while True:
    while arr[head]=='R' and head<tail:
        head+=1
    while arr[tail]=='W' and head<tail:
        tail-=1
    if arr[head]=='W' and arr[tail]=='R':
        count+=1
    head+=1
    tail-=1
    if head>=tail:
        break
print(count)

Submission status: Accepted

Solution code:
n=int(input())

s=list(input())

count_w = 0

w_list = []

r_list = []
for i in range(n):
    if s[i] == 'W':
        count_w += 1
        w_list.append(i)
    else:
        r_list.append(i)
w_list.reverse()

chage_count = 0
while len(w_list) > 0 and len(r_list) > 0 and r_list[-1] > w_list[-1]:
    r_list.pop()
    w_list.pop()
    chage_count += 1

print(min(chage_count, count_w))

Submission status: Accepted

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

cnt = 0
C_sort = sorted(C)
for i in range(N):
    if C[i] != C_sort[i]:
        cnt += 1

print(cnt // 2)

Submission status: Accepted

Solution code:

n = int(input())
l = list(input()) #str
#print(n, l)
l_r = sorted(l)
r = l.count('R')
w = l.count('W')
#print(r,w)
#print(l_r)

dif = 0
for i in range(n):
    if l[i] != l_r[i]:
        dif += 1
#print(dif)
print(dif //2 + dif%2)