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:
import sys
import heapq

def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    C = list(input())

    Wcount=0
    Rcount =0
    rr =C.count("R")
    ww =C.count("W")


    for j in range(rr):
        if C[j]=="W":
            Rcount+=1

    for i in range(ww):
        if C[-1-i]=="R":
            Wcount+=1

    print(min(Rcount,Wcount))






if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = n
w_to_r = 0
r_to_w = s.count('R')

ans = min(ans, max(w_to_r, r_to_w))

for i in range(n):
    if s[i] == 'W':
        w_to_r += 1        
    else:
        r_to_w -= 1
    ans = min(ans, max(w_to_r, r_to_w))

print(ans)

Submission status: Accepted

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

num_r = moji.count('R')
num_w = moji.count('W')

ext_moji = moji[:num_r]
print(ext_moji.count("W"))

Submission status: Accepted

Solution code:
#import numpy as np
#import collections
#import math

#f = open('./input_D.txt')
#
#N=int(f.readline())
#c = str(f.readline())

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

c_list = []
for i in range(N):
    if c[i] == "W":
        c_list.append(0)
    else:
        c_list.append(1)

n_R = sum(c_list)
c_left = c_list[:n_R]
c_right = c_list[n_R:]

W_left = len(c_left)-sum(c_left)
R_right = sum(c_right)

ans = max(W_left,R_right)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s=input()
a=s.count('R')
l=0
for i in range(a):
    if s[i]=='R':
        l+=1
print(a-l)

Submission status: Accepted

Solution code:
import math

#import sympy

#n,d=map(int,input().split())
n=int(input())
c=input()
rall=c.count("R")
rnum=c[0:rall].count("W")
wnum=c[rall:-1].count("R")

print(max(rnum,wnum))

Submission status: Accepted

Solution code:
n = int(input())
C = list(input())
c = 0
pi = 0
pj = n-1
while True:
    for i in range(pi, n):
        if C[i] == 'W':
            break
    else:
        break
    for j in range(pj, 0, -1):
        if C[j] == 'R':
            break
    else:
        break
    if i < j:
        C[i] = 'R'
        C[j] = 'W'
        c += 1
    else:
        break
    pi = i
    pj = j
print(c)

Submission status: Accepted

Solution code:
N = int(input())
l = input()
arr = []
for i in range(N):
    arr.append(l[i])

c = 0
left = 0
right = N-1

T = False
for i in arr:
    if i == 'R':
        T = True
if T == False:
    print(c)
else:
    T = False
    for i in arr:
        if i == 'W':
            T = True
    if T == False:
        print(c)
    else:
        while True:
            while True:
                if arr[left] == 'R':
                    left += 1
                else:
                    break
            while True:
                if arr[right] == 'W':
                    right -= 1
                else:
                    break
            arr[left] = 'R'
            arr[right] = 'W'
            if left >= right:
                break
            else:
                c += 1
        print(c)

Submission status: Accepted

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

al = list(a)
alr = list(reversed(al))

from collections import Counter
count = Counter(al)
r = count["R"]
w = count["W"]

if r*w ==0:
    print(0)
    exit()

rf = al.index("R")
wf = al.index("W")

rfr =alr.index("R")
wfr =alr.index("W")

#白に変える,最初の白より左の赤は除外
ans1 = r -wf
#赤に変える,最後の赤より右の白は除外
ans2 = w -rfr

#入れ替える,最初から赤の個数番目のうち赤を除外
alrr = al[:r]
countr = Counter(alrr)
rr = countr["R"]
ans3 = r - rr

print(min(ans1,ans2,ans3))

Submission status: Accepted

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

Rcnt = C.count('R')
Wcnt = C.count('W')
if len(C) == Rcnt or len(C) == Wcnt:
    print('0')
    exit()

print(Rcnt - C[:Rcnt].count('R'))

Submission status: Accepted

Solution code:
N = int(input())
A = list(input())
A = [1 if i == "W" else 0 for i in A]
W = sum(A)
R = N - W

print(sum(A[:R]))

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()))
n,=I()
s=list(input())
i=0
j=n-1
ans=0
while i<j:
	if s[i]=='R' and s[j]=='W':
		i+=1
		j-=1
	elif s[i]=='R':
		i+=1
	elif s[j]=='W':
		j-=1
	else:
		s[i]='R'
		s[j]='W'
		ans+=1
		i+=1
		j-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


from collections import deque

def main():
    N=input()
    C = input()
    W = C.count('W')
    R = C.count('R')
    RR = C[:R].count('W')
    WW = C[-W:].count('R')
    print(min(R, W, WW, RR))




main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
count=0
for i in range(n):
  if s[i]=='R':
    count+=1
for i in range(count):
  if s[i]=='R':
    count-=1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = input()
wc = c.count("W")
rc = c.count("R")

print(min(rc - c[:rc].count("R"),wc,rc))

Submission status: Accepted

Solution code:
##import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n=int(input())
c=list(input())
d=Counter(c)
w=d["w"]
r=d["R"]
res_w=res_r=0
for i in range(r):
    if c[i]=="W":
        res_w+=1
for i in range(r,n):
    if c[i] == "R":
        res_r+=1
print(min(res_r,res_w))

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
c=input()
r=deque()
lr=0
for i in range(n):
    if c[i]=="R":
        lr+=1
        r.append(i)
ans=0
for i in range(n):
    if lr==0:
        break
    if c[i]=="W":
        if i<r[-1]:
            ans+=1
            r.pop()
            lr-=1
        else:
            break

print(ans)

Submission status: Accepted

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

l = c[:c.count("R")]
print(l.count("W"))

Submission status: Accepted

Solution code:

import collections
from functools import lru_cache


def read():
    return input().strip()


def readInt():
    return int(input().strip())


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


def solve(N, arr):
    l = 0
    r = N-1

    ans = 0

    while l < r:
        while l < r and arr[l] != "W":
            l += 1

        while l < r and arr[r] != "R":
            r -= 1

        if l < r:
            ans += 1
            l += 1
            r -= 1

    return ans


N = readInt()
arr = [ch for ch in read()]

print(solve(N, arr))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
c = str(input())

w_pos = 0
r_pos = N - 1
w_flag = False
r_flag = False
ans = 0
for i in range(N):
    if w_pos >= r_pos:
        break
    if c[w_pos] == "W":
        w_flag = True
    else:
        w_flag = False
        w_pos += 1
    if c[r_pos] == "R":
        r_flag = True
    else:
        r_flag = False
        r_pos -= 1
    if w_flag and r_flag:
        ans += 1
        w_pos += 1
        r_pos -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
count = 0
i = 0
j = N-1
while i < j:
    while c[i] == "R" and i <= N-2:
        i += 1
    while c[j] == "W" and j >= 1:
        j -= 1
    if i < j:
        count += 1
    i += 1
    j -= 1
    if i >= j or i >= N or j <= 0:
        break
print(count)

Submission status: Accepted

Solution code:

from collections import Counter

n = int(input())

c = list(input())

C = Counter(c)

d = ['R']*C['R']+['W']*C['W']

ans = 0



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

print(ans//2)

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
n = c.count("R")
m = c[:n]
print(n-m.count("R"))

Submission status: Accepted

Solution code:
import math
def main():

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

    red = 0
    white = 0
    for i in range(N):
        if c[i] == "R": red += 1
        else: white += 1

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

    return count



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

Submission status: Accepted

Solution code:
n = int(input())
A = input()
#print(len(A))
A = str(0) + A
count = 0
for i in range(1,n+1):
    if A[i]=='R': count+=1
#print(count)
ans = 0
for j in range(count+1,n+1):
    if A[j]=='R': ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = input()
r, ans = 0, 0
for i in c:
    if i == 'R':
        r += 1

for i in range(r, N):
    if c[i] == 'R':
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
def resolve():
  N = int(input())
  C = [0 if x == "W" else 1 for x in list(input())]
  red_seacher_index = N - 1
  white_seacher_index = 0

  counter = 0
  while white_seacher_index <= red_seacher_index:
    if C[white_seacher_index] == 0:
      # 赤を Tail 側から探して見つけたら入れ替える
      while white_seacher_index <= red_seacher_index:
        if C[red_seacher_index] == 1:
          C[white_seacher_index] = 1
          C[red_seacher_index] = 0
          counter += 1
          break
        red_seacher_index -= 1
    white_seacher_index += 1
  print(counter)

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    c = SI()
    r = [0] * N
    w = [0] * N
    for i in range(N):
        r[i] = r[i-1] + (c[i] == 'R')
        w[i] = w[i-1] + (c[i] == 'W')

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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

if "W" not in c or "R" not in c:
  print("0")
  sys.exit()

red = c.count("R")
stay_red = c.count("R", 0, red)
print(red - stay_red)

Submission status: Accepted

Solution code:
N = int(input())

C = input()

r = C.count('R')
w = C.count('W')

if len(C) == r or len(C) == w or C[:r].count('R') == r:
    print(0)
else:
    print(C[:r].count('W'))

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    s = list(input())

    i = 0
    j = n
    ans = 0

    while i < n:
        if s[i] == "W":
            while j > i:
                j -= 1
                if s[j] == "R":
                    s[i], s[j] = s[j], s[i]
                    ans += 1
                    break
        i += 1


    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    n=int(input())
    c=list(input())
    count=INFI
    #allW
    count=c.count("R")

    #allR
    temp=c.count("W")
    count=min(count,temp)

    #RRR...WWW
    top=0
    bot=n-1
    temp=0
    while top<bot:
        while top < bot:
            if c[top] == "R":
                top+=1
            else:
                temp += 1
                break
        while top<bot:
            if c[bot]=="W":
                bot-=1
            else:
                temp += 1
                break

        top+=1
        bot-=1
#    print(temp)
    temp=temp//2
    count=min(count,temp)
    print(count)



if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw and dr:
    w = dw.popleft()
    r = dr.popleft()
    if w < r:
        ans += 1

    else:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys 
from collections import Counter
 
N = int(sys.stdin.readline().strip())
C = sys.stdin.readline().strip()

counter = Counter(C)
if len(counter) == 1:
    print(0)
    sys.exit()

limit = counter["R"]
ok = 0
for i in range(limit):
    if C[i] == "R":
        ok += 1

print(limit - ok)

Submission status: Accepted

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

rcount =0
wcount =0
for i in range(N):
    if c[i]=="R":
        rcount+=1
    else:
        wcount+=1

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n = int(input())
c = list(input())

r_cnt = c.count('R')
w_cnt = c.count('W')

ans = min(r_cnt, w_cnt)

from heapq import heappush, heappop

r_heapq = []
w_heapq = []

for i in range(1, n + 1):
    if c[i - 1] == "R":
        heappush(r_heapq, -i)
    else:
        heappush(w_heapq, i)
cnt = 0
while w_heapq and r_heapq:
    if heappop(w_heapq) < -heappop(r_heapq):
        cnt += 1
ans = min(ans, cnt)
print(max(ans, 0))

Submission status: Accepted

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

red=0
for i in c:
    if i=="R":
        red+=1
count = 0
for i in range(red):
    if c[i]=="W":
        count+=1
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
s = list(input())
if s.count("W") == 0:
    print(0)
    exit()
print(s[:s.count('R')].count("W"))

Submission status: Accepted

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

count = 0
i = 0
j = 1

while n>i+j:
    if c[i] == 'W' and c[-j] == 'R':
        count += 1
        i += 1
        j += 1
    elif c[i] == 'R':
        i += 1
    elif c[-j] == "W":
        j += 1

        
print(count)

Submission status: Accepted

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


sum_r, sum_w = [0], [0]
s1, s2 = 0, 0
for v in c:
    if v == 'R':
        s1 += 1
    else:
        s2 += 1
    sum_r.append(s1)
    sum_w.append(s2)

res = float('inf')
for i in range(n+1):
    res = min(res, max(sum_w[i], s1 - sum_r[i]))

print(res)

Submission status: Accepted

Solution code:
##a = int(stdin.readline().rstrip())
##b, c = [int(x) for x in stdin.readline().rstrip().split()]
##s = stdin.readline().rstrip()
##a = list(set(b))  重複を削除
##a = list(map(int,[int(x) for x in stdin.readline().rstrip().split()])) a[0, 1, 2, ...]
##a = [[0] * 2 for _ in range(n)]  a[0,0]
##v = [s[i: i+2] for i in range(0, len(s), 2)]
##  s="12345678" を v=["12","34","56","78"] にするイメージ
##s = "".join(list)リストを文字列に変換[""]は区切り文字
##a.sort(key=itemgetter(1))リストをソートできるよ(from operator import itemgetterが必要)
##sort_p = sorted(p) 昇順ソート

# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy
# stdin = open("sample.txt")


MOD = 1000000007
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): # 入力されたintを桁ごとに分解，リストで出力
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): # 最小公倍数
    return reduce(math.gcd, numbers)

def combination(m,n): # mCn
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): # nの素因数分解の結果を辞書で出力 fuct(24) → {2: 3, 3: 1}
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r = S.count("R")
    Sr = S[0:r]
    w_Sr = Sr.count("W")
    print(w_Sr)
    
main()

Submission status: Accepted

Solution code:
N = int(input())

stones = list(input())

left = 0
right = N - 1

count = 0

while left < right:
    if stones[left] == 'R':
        left += 1
        continue
    while left < right:
        if stones[right] == 'R':
            stones[left], stones[right] = stones[right], stones[left]
            left += 1
            right -= 1
            count += 1
            break
        right -= 1

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
## Library
 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 
## input
 
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))
 
N=int(input())
S = input()

if S.count("R")==0 or S.count("W")==0:
    print(0)
    sys.exit()

tmp = S.count("R")
mae = S[0:tmp]
tmp2 = mae.count("R")

print(tmp-tmp2)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
#n, d = map(int, input().split())
#a, b, c = input()

print(c.count("W", 0, c.count("R")))

Submission status: Accepted

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

r = 0
for i in range(n):
    if c[i] == 'R':
        r += 1

ans = 0
for i in range(r, n):
    if c[i] == 'R':
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
"""
    こんにちは。これはクシャルです
    
    Author : thekushalghosh (クシャル)
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
    
def solve():
    n = inp()
    s = insr()
    q = s.count("R")
    print(q - s[:q].count("R"))
    
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
        sys.stdout.close()
    
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def counter(a):
    q = [0] * max(a)
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)

def counter_elements(a):
    q = dict()
    for i in range(len(a)):
        if a[i] not in q:
            q[a[i]] = 0
        q[a[i]] = q[a[i]] + 1
    return(q)

def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97],1
    return(q)
 
def factors(n):
    q = []
    for i in range(1,int(n ** 0.5) + 1):
        if n % i == 0: q.append(i); q.append(n // i)
    return(list(sorted(list(set(q)))))
 
def prime_factors(n):
    q = []
    while n % 2 == 0: q.append(2); n = n // 2
    for i in range(3,int(n ** 0.5) + 1,2):
        while n % i == 0: q.append(i); n = n // i
    if n > 2: q.append(n)
    return(list(sorted(q)))

def transpose(a):
    n,m = len(a),len(a[0])
    b = [[0] * n for i in range(m)]
    for i in range(m): 
        for j in range(n): 
            b[i][j] = a[j][i]
    return(b)

#-----------------------------------------------------------------------#
        
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
main()

Submission status: Accepted

Solution code:
#from functools import lru_cache
import sys

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

ls = reader()
n = int(ls[0][0])
wr =ls[1][0]
nr = len(list(filter(lambda a:a=='R',wr)))
nleftr = len(list(filter(lambda a:a=='R',wr[:nr])))
print(nr-nleftr)

Submission status: Accepted

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

cnt_r = 0
for c in C:
  if c == "R":
    cnt_r += 1

cnt_r_in_lefternmost_cnt_r = 0
for c in C[:(cnt_r)]:
  if c == "R":
    cnt_r_in_lefternmost_cnt_r += 1

print(cnt_r - cnt_r_in_lefternmost_cnt_r)

Submission status: Accepted

Solution code:
N=int(input())

stones=input()

variation={"R":0,"W":0}
for i in range(N):
    variation[stones[i]]+=1

num=0
for i in range(variation["R"]):
    if stones[i]=="R":
        num+=1

ans=variation["R"]-num

print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/agc008/submissions/15248942
import sys
import math

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
c = input()

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

ans = min(R,W)
ans = min(ans,left.count('W'))

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    C = input().rstrip()

    l = 0
    r = N - 1
    ans = 0
    while True:
        if C[l] == "W" and C[r] == "R":
            ans += 1
            l += 1
            r -= 1
        elif C[l] == "R" and C[r] == "R":
            l += 1
        elif C[l] == "W" and C[r] == "W":
            r -= 1
        else:
            l += 1
            r -= 1

        if not l < r:
            break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    N=int(sys.stdin.readline())
    C=[1 if c=='R' else 0 for c in sys.stdin.readline().strip()]
    R=C.count(1)
    print(C[:R].count(0))

main()

Submission status: Accepted

Solution code:
n = int(input())
c = list(input())
print(c[c.count("R"):].count("R"))

Submission status: Accepted

Solution code:
N = int(input())
c = list(input())
from collections import Counter
dic = Counter(c)
left = c[:dic["R"]]
print(left.count("W"))

Submission status: Accepted

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

Submission status: Accepted

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

while left < right:
    if c[left] == 'W':
        flag = True
        while flag:
            if left >= right:
                flag = False
            if c[right] == 'R':
                c[right] = 'W'
                c[left] = 'R'
                counter += 1
                right -= 1
                flag = False
            else:
                right -= 1
    left += 1

print(counter)

Submission status: Accepted

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

l = 0
r = n - 1

ans = 0

while True:
  while c[l] == 'R' and l < r:
    l += 1
  while c[r] == 'W' and l < r:
    r -= 1
  if l < r:
    ans += 1
    l += 1
    r -= 1
  else:
    print(ans)
    break

Submission status: Accepted

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


def solve(cs):
    num_w = cs.count("W")
    num_r = cs.count("R")
    ref = "R" * num_r + "W" * num_w
    num_mismatches = sum(c != d for c, d in zip(cs, ref))
    res = num_mismatches // 2
    return res


def main():
    n = int(input())
    cs = input()
    res = solve(cs)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = [0]*N
W = [0]*N
for i in range(N):
    if S[i]=="R":
        R[i]+=1
    else:
        W[i]+=1

sumR = sum(R)

ans = sumR-sum(R[:sumR])

print(ans)

Submission status: Accepted

Solution code:
n=int(input()) 
s=input()
a=[]
r,w=0,0
ans=float("inf")
for i in range(n):
    if s[i]=="R":
        r+=1
    else:
        w+=1
    a.append([r,w])
if r==n or w==n:
    print(0)
    exit()
for i in range(n):
    r1,w1=a[i][0],a[i][1]
    r2,w2=r-r1,w-w1
    t=min(w1,r2)
    tmp=t+max(w1-t,0)+max(r2-t,0)
    if tmp<ans:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
C = input()
c_c = Counter(C)
w = c_c['W']
r = c_c['R']
ans = 0
for i in range(r):
    if C[i] == 'W':
        ans += 1
print(ans)

Submission status: Accepted

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

W = [0 for _ in range(N)]
R = [0 for _ in range(N)]

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

if r == 0 or r == N:
    print(0)
    exit(0)

for i, ch in enumerate(C):
    R[i] = r
    if ch == "W":
        w += 1
    else:
        r -= 1
    W[i] = w

ans = N
for i in range(N-1):
    ans = min(ans, max(W[i], R[i+1]))
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
#input = stdin.buffer.readline
setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

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

N = int(input())
S = input()

R_cnt = S.count("R")
W_cnt = S.count("W")

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

answer = 0
for s in S:
    if R_cnt == 0:
        print(answer)
        exit()
    if s == "W":
        answer += 1
    R_cnt -= 1