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()

x =[0] * N

ww = 0
rr = 0
for i, cc in enumerate(c):
    if cc == "R":
        x[i] = 1
        rr += 1
    else:
        ww += 1

cmd = 0
for i in range(rr):
    if x[i] == 0:
        cmd += 1

cmd2 = 0
for j in range(rr+1, len(x)):
    if x[j] == 1:
        cmd2 += 1


print(max(cmd, cmd2))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = [i for i, achr in enumerate(input()) if achr=="R"]
    most_right_r = len(c)
    if most_right_r==n:
        print("0")
        exit()

    count = 0
    pre_r_pos = -1
    for i, r_pos in enumerate(c):
        diff_r_pos = r_pos - pre_r_pos - 1
        if diff_r_pos and i<most_right_r:
            if diff_r_pos < most_right_r - i:
                most_right_r -= diff_r_pos
                count+=diff_r_pos
                #print(i, r_pos, diff_r_pos, most_right_r, count)
            else:
                count+=most_right_r - i - 1
                most_right_r -= most_right_r - i - 1
                count+=1
                #print(i, r_pos, diff_r_pos, most_right_r, count)
                break
        pre_r_pos = r_pos

    print(count)

if __name__=="__main__":
    main()

Submission status: Accepted

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

a=list(a)

t=0
s=0
for i in range(0,n):
    if a[i]=="R":
        t=t+1
        
for i in range(0,t):
    if a[i]!="R":
        s=s+1
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import deque
sys.setrecursionlimit(10**6)
MOD = 10**9+7 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = readline().decode().rstrip()
W_cnt = S.count('W')
R_cnt = S.count('R')
ans = 0
W_ind = []
R_ind = []
for i in range(N):
    if S[i] == 'W':
        W_ind.append(i)
    else:
        R_ind.append(i)
for w in W_ind:
    if len(R_ind) == 0:
        break
    r = R_ind.pop()
    if r < w:
        break
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D
from functools import reduce as R
import heapq as H

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()
stones = S()
i, j = 0, len(stones)-1
ans = 0

if 'WR' not in stones:
  print('0')
else:
  stones = list(stones)
  while i < j:
    while stones[i] == 'R':
      i += 1
    while stones[j] == 'W':
      j -= 1
    if i < j:
      stones[i] = 'R'
      stones[j] = 'W'
      i += 1
      j -= 1
      ans += 1
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
strings = "R"*c.count("R") + "W"*c.count("W")
ans = 0
for a,b in zip(strings,c):
    if a != b: ans += 1
print(ans//2)

Submission status: Accepted

Solution code:
input()
c=input()
a=0
while 1:
	w=c.find('W')
	r=c.rfind('R')
	if w<0 or r<0 or r<w:break
	c=c[:w]+'R'+c[w+1:r]+'W'+c[r+1:]
	a+=1
print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque

rl = sys.stdin.buffer.readline
INF = sys.maxsize

X = int(rl())
SS = [c for c in rl().strip()]

W = ord(b'W')
R = ord(b'R')

WP = []
RP = []
for i in range(len(SS)):
  if SS[i] == W:
    WP.append(i)
  elif SS[i] == R:
    RP.append(i)

RP.reverse()

WP = deque(WP)
RP = deque(RP)

sc = 0
while len(WP) != 0 and len(RP) != 0:
  w = WP.popleft()
  r = RP.popleft()
  if w < r:
    sc += 1

print(sc)

Submission status: Accepted

Solution code:
n=int(input())
li = list(input())
i = 0
k = 1
counter = 0
done = 0
while True:
    # print(li)
    # print(counter)
    # print("i=", i, li[i])
    if done == 1:
        # print("done")
        break
    if i+k == n:
        # print("owari")
        break
    if li[i] == "W":
        # print("yes")
        if k+i >= n:
            # print("あいうえお")
            break
        while True:
            if li[-k] == "R":
                li[i] = "R"
                li[-k] = "W"
                counter += 1
                k += 1
                break
            k += 1
            if k+i == n:
                done = 1
                if li[-k] == "R":
                    li[-k] = "W"
                    counter += 1
                break
    i += 1
print(counter)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
left = 0
right = N - 1

w_idx = -1
r_idx = -1

ans = 0

while left < right:
    if w_idx == -1 and c[left] == "W":
        w_idx = left
    if w_idx == -1 and c[left] == "R":
        left += 1

    if r_idx == -1 and c[right] == "R":
        r_idx = right
    if r_idx == -1 and c[right] == "W":
        right -= 1

    if w_idx != -1 and r_idx != -1:
        ans += 1
        w_idx = -1
        r_idx = -1
        left += 1
        right -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
C2 = sorted(C[:])
r_n = C.count("R")

ans = 0
for i in range(r_n):
    if C[i] != C2[i]:
        ans += 1

print(ans)

Submission status: Accepted

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

r = st.count("R")

count = 0

for i in range(r):
    if st[i]=="W":
        count += 1

print(count)

Submission status: Accepted

Solution code:
# abc174_d.py

N = int(input())
S = input()
scnt = S.count("R")

print(S[:scnt].count("W"))

Submission status: Accepted

Solution code:
N = int(input())
stone = list(map(str,input()))
ind = [""]*2
ind[0] = list()
ind[1] = list()
R = 0
W = 0
for i in range(N):
    if(stone[i]=="W"):
        ind[0].append(i)
        W += 1
    else:
        ind[1].append(i)
        R += 1
newR = 0
for i in range(R):
    if(i != ind[1][i]):
        break
    newR+= 1


newind =list(reversed(ind[1]))
ans2 = 0

# print(W,R,newR)
# print(ind[1],newind)
if(newR != R):
    ans1 = min(W,R-newR)
else:
    ans1 = min(W,R)
if(ans1 != 0):
    for i in range(N//2):
        if(ind[0][i]>newind[i]):
            break
        ans2+=1
        # print(ans)
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
C = list(input())
import copy
D = []
flag = 0
ans = 0
keisan = 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"

zero = D.count("0")
ichi = D.count("1")

for p in range(zero):
    if D[p] == "1":
        ans += 1


"""
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):
                    keisan += 1
                    if D[N-s-1] =="0" :
                        if q < N-s-1:
                            change(q,N-s-1)
                        break
            else:
                continue
            break
"""
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter
import heapq
for _ in range(1):
    n=int(input())
    s=list(input())
    ptr=0
    while(ptr<n and s[ptr]=="R"):ptr+=1
    if ptr==n:
        print(0)
        exit(0)
    k=ptr
    ptr=n-1
    while(ptr>=0 and s[ptr]=="W"):
        ptr-=1
    if ptr==-1:
        print(0)
        exit(0)
    l=ptr
    ans=0
    while(k<l):
        if s[k]=="W" and s[l]=="R":
            s[k],s[l]=s[l],s[k]
            k+=1
            l-=1
            ans+=1

        elif s[k]=="R":
            k+=1
        elif s[l]=="W":
            l-=1
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
rn,wn=0,0
for i in range(len(S)):
    if S[i]=='R':
        rn+=1
    else:
        wn+=1
winr,rinw=0,0
for i in range(rn):
    if S[i]=='W':
        winr+=1
for i in range(rn,rn+wn):
    if S[i]=='R':
        rinw+=1
#print(rn,wn,rinw,winr)
print(max(winr,rinw))

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
num_r = c.count("R")
right=num_r
ans = 0
b_num_r = c[:num_r].count("R")
"""
for i in range(num_r):
    if c[i] == "W":
        while right != n-1 or c[right] != "W" 
"""
print(num_r - b_num_r)

Submission status: Accepted

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

rCnt = C.count('R')
tmp = C[0:rCnt]
print(tmp.count('W'))

Submission status: Accepted

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

i= 0
j = N - 1
cnt = 0
while i < j:
    if C[i] == 'W':
        while i < j:
            if C[j] == 'R':
                cnt += 1
                j -= 1
                break
            j -= 1
    i += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
c_ls = [str(i) for i in c]

num_r = c_ls.count('R')
num_sub_r = c_ls[:num_r].count('R')

print(num_r-num_sub_r)

Submission status: Accepted

Solution code:
#D

N=int(input())
c=input()
d=list(c)
w=0
ans=0
w_list=[0]*N
w_cnt=0
r_cnt=0

if c.count("W")==0 or c.count("W")==N:
    ans=0
else:
    for i,j in enumerate(d):
        if j=="W":
            w_cnt +=1
        else:
            w_list[i]=w_cnt
            r_cnt += 1
            
    #print(w_list)
    #print("r個数",r_cnt)

    for i in range(N-1,-1,-1):
        if d[i]=="R":
            r_cnt -= 1
            
            if w_cnt>0:
                ans += 1
                w_cnt -= 1 
            else:
                break
        else:
            w_cnt -= 1
            if w_cnt<=0:
                break
        #print(N-1-i, r_cnt, w_cnt)
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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


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


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

Submission status: Accepted

Solution code:
import sys
N = int(input())
c = input()
n = int(N/2)
cnt_R = c.count('R')
c_1 = c[:cnt_R]
cnt = c_1.count('W')
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l_1 = []

cnt = 0
for i in s[::-1]:
    if i =='R':
        cnt += 1
    else:
        l_1.append(cnt)

l_1.reverse()

def get_res(l_1):
    for i,j in enumerate(l_1):
        if i+1 == j:
            return j
        elif i+1 > j:
            return i
    return len(l_1)

print(get_res(l_1))

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
c = deque(list(input()))
cnt = 0
def left(c):
  l = c.popleft()
  return l
def right(c):
  r = c.pop()
  return r
while c:
  l = left(c)
  if l == 'R':
    continue
  else:
    while True:
      if not c:
        break
      r = right(c)
      if r == 'W':
        continue
      else:
        cnt += 1
        break
print(cnt)

Submission status: Accepted

Solution code:
import sys

n = int(input())
c = [0 if x=='W' else 1 for x in input()]
counter = 0

white_index = [i for i,ele in enumerate(c) if ele == 0]
red_index = [i for i in range(len(c)-1, -1, -1) if c[i] == 1]
    
    
for i in range(min(len(white_index), len(red_index))):
    if white_index[i] < red_index[i]:
        counter += 1
print(counter)

Submission status: Accepted

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

head = 0
tail = -1
count = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
c = [0 if s=='R' else 1 for s in input()]

l = 0
r = N - 1

ans = 0
while l < r:
    while l < N and c[l] == 0:
        l += 1
    while r > 0 and c[r] == 1:
        r -= 1
    if l < r:
        ans += 1
    l += 1
    r -= 1
print(ans)

Submission status: Accepted

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

# あかのインデックス
RR=[-1]
# しろのインデックス
WW=[]

for i in range(n):
    if a[i]=='R':
        RR.append(i)
    else:
        WW.append(i)
WW.append(float('inf'))

if a.count('R')==0 or a.count('R')==n:
    print(0)
    exit()
if a.count('W')==1 or a.count('R')==1:
    print(1)
    exit()

for i in range(len(RR)-1):
    if RR[-2-i]<WW[i+1]:
        ans=i+1
        break

# 白くするコスト
wc = [0]
# 赤くするコスト
rc = [0]

import itertools

a = ['R'] + a

if a[-1] == 'R':
    a.append('W')
else:
    a.append('R')

cntr = 0
cntw = 0
flag1 = True
for i in range(n + 2):
    if a[i] == 'W':
        cntr += 1
        if flag1:
            flag1 = False
        else:
            if cntw != 0:
                wc.append(cntw)
                cntw = 0
    else:
        if flag1:

            continue
        else:
            cntw += 1
            if cntr != 0:
                rc.append(cntr)
                cntr = 0

wc = list(itertools.accumulate(wc))
rc = list(itertools.accumulate(rc))


for i in range(len(wc)):
    ans = min(ans, rc[i] + wc[-1] - wc[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
_trigger = False
ans = [0 for i in range(2)]
_count = c.count('R')
for i in range(len(c)):
  if c[i] == 'W':
    _trigger = True
    if _count >= i + 1:
      ans[1] += 1
  else:
    if _trigger == True and i + 1 >= _count:
      ans[0] += 1
print (min(ans))

Submission status: Accepted

Solution code:
N = int(input())
c = input()
li = list(c)
count = li.count('R')
num = 0
for i in range(count):
    if li[i] == 'W':
        num += 1

print(num)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    c = input()
    num_of_w = 0
    for i in range(N):
        if c[i] == 'W':
            num_of_w += 1

    num_of_w2 = 0
    for j in range(N-1,N-1-num_of_w,-1):
        if c[j] == 'W':
            num_of_w2 += 1

    judge = num_of_w - num_of_w2
    if judge <= 0:
        return 0
    else:
        return judge

print(main())

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
w=c.count('W')
ans=0
for i in range(n-w):
    if c[i]=='W':
        ans += 1

print(ans)

Submission status: Accepted

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

cw = c.count("W")
cr = c.count("R")
cc = c[-cw:].count("W")
print(cw - cc)

Submission status: Accepted

Solution code:
n = int(input())
a = input()
b = list(a)
c = sorted(b)
d = 0
for i in range(n):
  if(c[i] != b[i]):
    d += 1
print(d//2)

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


mod=1000000007
def combinations_count_mod(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x,y: x*y%mod, range(n, n - r, -1), 1)
    denom = pow( reduce(lambda x,y: x*y%mod, range(1, r + 1), 1) , mod-2, mod)
    return numer * denom % mod


def solve(n, s):
    #
    cnt=0
    doneflag=0
    l=0
    r=n-1    

    #
    sameflag=0
    for x in s:
        sameflag *=( x=='R' )
    for x in s:
        sameflag *= ( x=='W')
    if sameflag!=0:
        return 0

    #
    w_cnt=0
    for i in range(n):
        if s[i]=='W':
            w_cnt+=1
            

    ##
    for r in range(0,n)[::-1]:
        #
        if s[r] == 'R':
            for l in range(l,n):
                if s[l]=='W':
                    cnt+=1
                    w_cnt-=1
                    l+=1
                    eprint('r,l, w_cnt ',end=':\n')
                    eprint(r,l, w_cnt)
                    break
        else: # s[r] == 'W'
            w_cnt-=1

        #
        if l>=r or w_cnt==0:
            eprint('r,l, w_cnt ',end=':\n')
            eprint(r,l, w_cnt)
            return cnt       
    return cnt
    
    

def main():
    n = int(input().strip())
    S = input().strip()
    print(solve(n,S))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
C_ = S()
C = [1 if c == 'R' else 0 for c in C_]
# print(C)
ans = sum(C)
ans = min(ans, N-ans)


ans_ = 0
i = N - 2
_C = copy.copy(C)
for _ in range(N-1):
    # print(i, 'C[i + 1]', C[i + 1])
    if (_C[i] == 0 and _C[i + 1] == 1):
        _C[i] = 1
        # print(C)
        ans_ += 1
    i -= 1
ans = min(ans, ans_)


# 入れ替えで達成を目指す
# 右にW=0をかためる
_C = copy.copy(C)

l = 0
r = N-1
ans_ = 0
while (l < r):
    while (_C[l] != 0):
        l += 1
        if (l >= r):
            break
    while (_C[r] != 1):
        r -= 1
        if (l >= r):
            break
    if (l >= r):
        break
    _C[l], _C[r] = _C[r], _C[l]
    # print(_C)
    ans_ += 1
ans = min(ans, ans_)


print(ans)

Submission status: Accepted

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

# 色の変更で対応する場合のステップ数
numW = c.count('W')
numR = c.count('R')

# スワップするときのステップ数
rindex = [i for i, x in enumerate(c) if x == 'R']
rindextobe = range(0, len(rindex))
swap = len(set(rindex) - set(rindextobe))

print(min(numW, numR, swap))

Submission status: Accepted

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
#sort = sorted(a)[::-1] 降順
n = int(input())
c = input()

wcount = c.count("W")
rcount = c.count("R")
check = 0

num = min(wcount, rcount)

if rcount < wcount:
    for i in range(rcount):
        if c[i] == "R":
            check += 1

else:
    for i in range(n - wcount, n):
        if c[i] == "W":
            check += 1

print(num - check)

Submission status: Accepted

Solution code:
N = int(input())
C = input()
# WとRの数が等しくなるところ？
white_ruisekiwa = [0]
for i in range(N):
    if C[i] == "W":
        white_ruisekiwa.append(white_ruisekiwa[-1]+1)
    else:
        white_ruisekiwa.append(white_ruisekiwa[-1])

red_ruisekiwa = [0]
for i in range(N-1, -1, -1):
    if C[i] == "R":
        red_ruisekiwa.append(red_ruisekiwa[-1]+1)
    else:
        red_ruisekiwa.append(red_ruisekiwa[-1])

# print("白の累積わ : ", white_ruisekiwa)
# print("赤の累積わ : ", red_ruisekiwa)

for i in range(N):
    if white_ruisekiwa[i] == red_ruisekiwa[N-i]:
        print(white_ruisekiwa[i])
        exit()
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
i, j = 0, N - 1
res = 0
while i < j:
    while i < j and S[i] == 'R':
        i += 1
    while i < j and S[j] == 'W':
        j -= 1
    if i < j and  S[i] == 'W' and S[j] == 'R':
        S[i], S[j] = S[j], S[i]
        i += 1
        j -= 1
        res += 1
        continue

print(res)

Submission status: Accepted

Solution code:
n = int(input())
c=str(input())
if c.count("W") >= c.count("R"):
    C_W =c[:-c.count("W")]
    print(C_W.count("W"))
else:
    C_R =c[c.count("R"):]
    print(C_R.count("R"))

Submission status: Accepted

Solution code:
N=int(input())
num=list(input())
num=[0 if i=="W" else 1 for i in num ]
count=0
first=0
for i in num:
    if i==1:count+=1
jk=0
for i in range(count):
    if num[i]==1:
        jk+=1
print(count-jk)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
n=0
if S.find("W")==-1 or S.find("R")==-1:
  print(0)
else:
  while S.find("W")<S.rfind("R") and S.find("W")>=0:
    S = S[S.find("W")+1:S.rfind("R")]
    n+=1
  print(n)

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count("R")
print(S[:a].count("W"))

Submission status: Accepted

Solution code:
N = int(input())
c = input()
li = list(c)
count = li.count('R')
num = 0

#for i in range(count):
    #if li[i] == 'W':
        #for j in range(len(li)-count):
            #if li[j*(-1)-1] == 'R':
                #li[i] = "R"
                #li[j*(-1)-1] = 'W'
                #num += 1

#for i in range(int(N/2)):
    #if li[i] == 'W' and li[j*(-1)-1] == 'R':
        #li[i] = 'R'
        #li[j*(-1)-1] = 'W'
        #num += 1

#for i in range(count):
    #if li[i] == 'W':
        #li[i] == 

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

print(num)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
from collections import  deque
cc = deque(c)
ans = 0
while len(cc)>1:
    #print(cc)
    c1 = cc.popleft()
    c2 = cc.pop()
    #print(cc,c1,c2,ans)
    if c1 == "R":
        cc.append(c2)
        continue
    else:
        if c2 == "R":
            ans += 1
            continue
        else:
            cc.appendleft(c1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
rr = []
ww = []
for i in range(N):
  if (c[i] == "R"):
    rr.append(i)
  if (c[i] == "W"):
    ww.append(i)

wl = len(ww)
rl = len(rr)
l = min([wl,rl])
ans = 0
for i in range(l):
  if (ww[i] < rr[-i-1]):
    ans += 1

print(ans)

Submission status: Accepted

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

x=s.count('R')

l=s[:x].count('W')
r=s[x:].count('R')

print(min(l,r))

Submission status: Accepted

Solution code:
N = int(input())
Cs= input()
str_ans = "R"*Cs.count("R") + "W"*Cs.count("W")


diff = 0
for s_ans, s_inp in zip(str_ans, Cs):
    if s_ans != s_inp:
        diff+= 1
print(diff//2)

Submission status: Accepted

Solution code:
nn = int(input())
ss = str(input())
stones = list(map(str, str(ss)))
#print(stones)


numW = sum(x == 'W' for x in stones)
numR = sum(x == 'R' for x in stones)
if numW == 0:
    print('0')
elif numR == 0:
    print('0')
else:
    addrW = [0]*numW
    addrR = [0]*numR
    countW = 0
    countR = 0

    for i in range(nn):
        if stones[i] == 'W':
            addrW[countW] = i
            countW += 1
        else:
            addrR[countR] = i
            countR +=1

    ind_leftmostW = 0
    ind_rightmostR = len(stones)

    ind = 0

    while ind < min(numR,numW):
        if addrW[ind] < addrR[numR-1-ind]:
    #        stones[addrW[ind]] = 'R'
    #        stones[addrR[numR-1-ind]] = 'W'
    #        print(stones)
            ind += 1
        else:
            break

    print(ind)

Submission status: Accepted

Solution code:
N = int(input())
mozi = input()
mozi2 = list(mozi)
num_w = mozi2.count("W"); num_r = mozi2.count("R")
list_w2 = mozi2[(N-num_w):]; list_w1 = mozi2[:(N-num_w)]
list_r2 = mozi2[num_r:]; list_r1 = mozi2[:num_r]
if num_w <= num_r:
    if "W" in list_w2:
        print(list_w1.count("W"))
    else:
        print(num_w)
else:
    if "R" in list_r1:
        print(list_r2.count("R"))
    else:
        print(num_r)

Submission status: Accepted

Solution code:
ans = 0
n = int(input())
c = input()
#print(c)
check  = c[0]
count = 0
flag=0
for i in c:
    if(i=="R"):
        count+=1
for i,k in enumerate(c):
#    print(check,k)
    if check=="W" and k=="R":
        ans+=1
        check = "W"
        if i<count:
            flag+=1
    else:
        check  = k
print(ans-flag)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
S = list(input())

s = 0
e = N - 1

cnt = 0
while s <= e:
    if S[s] == 'W':
        while (S[e] != 'R'):
            if s >= e:
                print(cnt)
                exit()
            e -= 1
        S[s], S[e] = S[e], S[s]
        cnt += 1
    s += 1

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
mozi = input()
a_list = []
for i in range(n):
    if mozi[i] == 'W':
        a_list.append(0)
    else:
        a_list.append(1)

r_count = sum(a_list)
w_count = n - r_count
start = r_count
count = 0
for i in range(r_count):
    if a_list[i] == 0:
        count += 1
        while True:
            if a_list[start] == 1:
                start += 1
                break
            else:
                start += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
c=0
d=0
for i in range(N):
    if S[i]=="R":
        c+=1

for j in range(c):
    if S[j]=="W":
        d+=1

for k in range(c,N):
    if S[k]=="R":
        d+=1

if d%2==0:
    print(int(d/2))
else:
    print(int((d+1)/2))

Submission status: Accepted

Solution code:
"""
AtCoder :: Beginner Contest 174 :: Alter Altar
https://atcoder.jp/contests/abc174/tasks/abc174_d
"""
import sys


def solve(altar):
    """Solve puzzle."""
    soln = 0
    left = 0
    right = len(altar) - 1
    while left < right:
        while left < len(altar) and altar[left] == 'R':
            left += 1
        while right >= 0 and altar[right] == 'W':
            right -= 1
        if left < right:
            altar[left], altar[right] = altar[right], altar[left]
            soln += 1
    return soln


def main():
    """Main program."""
    N = int(sys.stdin.readline())
    altar = [c for c in sys.stdin.readline().strip()]
    print(solve(altar))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input()
l =list(input())
cw = l.count('W')
cr = l.count('R')

if cr < cw:
  ans = l[:cr].count('W')
else:
  ans = l[cr:].count('R')
print(ans)

Submission status: Accepted

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

rcnt=s.count("R")
num=s[0:rcnt]
rnum=num.count("R")

print(rcnt-rnum)

Submission status: Accepted

Solution code:
n = int(input())
cs = input()
l = len(cs)

ans = 0

i = 0
j = 1
while i < l:
  if cs[i] == "W":
    while j <= l - i:
      if cs[-j] == "R":
        ans += 1
        j += 1
        break
      j += 1

  i += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = list(input().split())
str_list = []
for s in S:
  str_list.append(list(s))
a = str_list[0].count('W')
if a == 0:
  print(a)
  exit()
else:
  ans = str_list[0][-a:].count('R')
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = input()

wcnt = 0
for i in range(N):
    if S[i] == 'W':
        wcnt += 1

cnt = 0

for i in range(1,wcnt+1):
    if S[-i] == 'W':
        cnt += 1

print(wcnt-cnt)