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:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

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

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

n = I()
c = RS()

# print(c)

r = c.count('R')
# print(r)

ans = 0
for i in range(r):
    if c[i]=='W':
        ans += 1
    else:
        pass
print(ans)

Submission status: Accepted

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

i = 0
j = N-1
count = 0
while i < j:
    if c[i] == "W":
        if c[j] == "R":
            i += 1
            j -= 1
            count += 1
        else:
            j -= 1
    else:
        i += 1

print(count)

Submission status: Accepted

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

    w = c.count("W")

    ans = c[N - w: N].count("R")
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0

W_left = 0
R_right = 0
for i in range(n):
    if arr[i] == 'R':
        R_right += 1

ans = [max(W_left,R_right)]
for i in range(n):
    if arr[i] == 'W':
        W_left += 1
    elif arr[i] == 'R':
        R_right -= 1
    ans.append(max(W_left,R_right))
print(min(ans))

Submission status: Accepted

Solution code:
N = input()                                                                                         
c = input()                                                                                         
                                                                                                    
N = int(N)                                                                                          
                                                                                                    
n_r = c.count('R')                                                                                  
print(c[n_r:].count('R'))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    
    r = S.count("R")
    ans = S[r:].count("R")
    print(ans)
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
N = int(input())
c = input()
C = []
for c_i in c:
    C.append(c_i)
count = 0
left = N-1
for i in range(N):
    if i >= left:
        print(count)
        break
    if C[i] == 'W':
        for j in reversed(range(left+1)):
            if i == 1:
                if j == i:
                    C[i] = 'R'
                    left = i
                    break
            else:
                if j == i-1:
                    left = i
                    break
            if C[j] == 'R':
                C[i] = 'R'
                C[j] = 'W'
                left = j
                count += 1
                break

Submission status: Accepted

Solution code:
N=int(input())
c=input()
t=c.count('R')
L=[[0,t]]
for i in range(N):
    if c[i]=='W':
        L.append([L[i][0]+1,L[i][1]])
    else:
        L.append([L[i][0],L[i][1]-1])
print(min([max(L[i]) for i in range(N+1)]))

Submission status: Accepted

Solution code:
import sys

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

N = int(readline())
C = read().rstrip().decode()

l, r = 0, N - 1
cost = 0
while l < r:
    if C[l] == 'R':
        l += 1
    elif C[r] == 'W':
        r -= 1
    else:
        # R...W
        cost += 1
        l += 1
        r -= 1

print(cost)

Submission status: Accepted

Solution code:
from sys import exit

# import copy
# import numpy as np
# from collections import deque

(N,) = map(int, input().split())

C = list(input())


na = C.count("R")

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    c = input()
    r,w = 0,0
    for i in range(n):
        if c[i]=="R":
            r += 1
        else:
            w += 1
    m = 0
    for i in range(r):
        if c[i] == "W":
            m += 1
    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
s=input()
r=s.count("R")
w=0
c=r
for i in range(N):
    if s[i]=="R":
        r-=1
    else:
        w+=1
    if c>max(r,w): c=max(r,w)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rs = s.count("R")
rs_ = s[:rs].count("R")
ans = min(rs, n-rs, rs-rs_)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
line = input()
r_count = 0
for i in line:
  if i == 'R':
    r_count += 1
w_count = 0
for i in range(r_count):
  if line[i] == 'W':
    w_count += 1
print(w_count)

Submission status: Accepted

Solution code:
N=int(input())
C=input()
R=0
for i in C:
    if i=="R":
        R+=1
D=C[:R]
count=0
for i in D:
    if i=="W":
        count+=1
print(count)

Submission status: Accepted

Solution code:
N=int(input())
C=input()
R=0
W=0
for i in C:
    if i=="R":
        R+=1
    else:
        W+=1
D=C[:R]
count=0
for i in D:
    if i=="W":
        count+=1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count("W")
r=n-w
ans1 = r - s[:r].count("R")
ans2 = w - s[-w:].count("W")
print(min(ans1,ans2))

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    S = input()
    cnt_R = S.count("R")

    cnt = 0
    for i in range(cnt_R):
        if S[i] == "R":
            cnt += 1
    print(cnt_R - cnt)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

N = int(input())
c = input()[:-1]
R = c.count('R')
acc = [0]

for ci in c:
    acc.append(acc[-1]+(1 if ci=='R' else 0))

ans = 10**18

for i in range(N+1):
    if i>=R:
        ans = min(ans, i-R+acc[N]-acc[i])
    else:
        ans = min(ans, R-i+i-acc[i])
    
print(ans)

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:
# 前提条件
# ・最終的な石の状態は赤い石は左に、白い石は右に集まった状態
# ・色変えは行う必要はなく、入れ替えだけ行う
# ・入れ替え前後で赤い石と白い石の総数は変わらない
# ・最終的には赤い石の総数が左に並び、白い石の総数が右に並ぶ
# ・一度の操作で、左側に並んだ赤い石の総数から右にある赤い石は一つずつしか減らせない
# ・最小回数は、左側に並んだ赤い石の総数より右にある赤い石の数、もしくは左側に並んだ赤い石の総数のなかにある白い石の数

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

r_cnt = c.count("R")

min_cnt = 0
for i in range(r_cnt):
    if c[i] == "W":
        min_cnt += 1
    
print(min_cnt)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n = input()
s = input()
print(s[:s.count('R')].count('W'))

Submission status: Accepted

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

P = c.count('R') 
R = P - c[:P].count('R')
print(R)

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(s)
ans = 0

if "R" in count.keys():
    if count["R"] != n:
        r_count = collections.Counter(s[:count["R"]])
        if r_count["R"] == 0:
            ans = count["R"]
        else:
            ans = r_count["W"]

print(ans)

Submission status: Accepted

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

R=c.count("R")

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

Submission status: Accepted

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

Submission status: Accepted

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

Rcount = 0
Wcount = 0
for i in range(len(c)):
    if c[i] == 'R':
        Rcount += 1
for i in range(Rcount):
    if c[i] == 'W':
        Wcount += 1

print(Wcount)

Submission status: Accepted

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

  if c[N-1-i]=="R":
    R+=[i]
    pr+=1

for i in range(200000):
    if min(len(W),len(R))==i:
        result=i
        break
    if W[i]+R[i]>=N:
        result=i
        break
print(result)
result=i

Submission status: Accepted

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

cnt =  0
R = C.count('R')

for i in range(R):
    if C[i] == 'R':
        cnt += 1
print(R - cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
"""
RWとなってなければおｋ
WRはだめ
"""


def main():
    import sys

    input = sys.stdin.readline

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

    white, red = 0, c.count('R')
    if red == 0:
        print(0)
        return
    ans = N
    for i in range(N):
        if c[i] == 'W':
            white += 1
        else:
            red -= 1
        res = max(white, red)
        ans = min(ans, res)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

rn = l.count('R')
wn = l.count('W')
ans = rn - (l[:rn].count('R'))
print(ans)

Submission status: Accepted

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

nr = s.count("R")
nw = s.count("W")

nwc = s[:nr].count("W")
nrc = s[nr:].count("R")

print(max(nwc,nrc))

Submission status: Accepted

Solution code:
l = []
n = int(input())
r = 0
for i in [1 if x == 'R' else 0 for x in list(input())]:
    r += i
    l.append(r)

c = r - l[r-1]
print(c)

Submission status: Accepted

Solution code:
N=int(input())
c=input()
S=[0 for i in range(N+1)]
for i in range(N):
    if c[i]=="R":
        S[i+1]=S[i]+1
    else:
        S[i+1]=S[i]
ans=N
for i in range(N+1):
    ans=min(ans,max(i-S[i],S[N]-S[i]))
print(ans)

Submission status: Accepted

Solution code:
# AFTER EDITORIAL


def solve(ls):
    n = len(ls)
    num_w = len([x for x in ls if x == "W"])
    num_r = n - num_w
    left_w = 0
    right_r = num_r
    result = num_r
    for i in range(n):
        if ls[i] == 'W':
            left_w += 1
        else:
            right_r -= 1
        result = min(result, max(left_w, right_r))
    return result


def main(istr, ostr):
    (n,) = list(map(int, istr.readline().strip().split()))
    s = istr.readline().strip()
    result = solve(s)
    print(result, file=ostr)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
c=input()
d=Counter(c)
w,r=Counter(c)["W"], Counter(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()
red = 0
white = 0
for i in range(N):
    if C[i] == "W":
        white +=1
    else:
        red += 1
c = 0
for i in range(red):
    if C[i] == "W":
        c +=1
print(c)

Submission status: Accepted

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

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

print(min([w[i]+r[i]-min(w[i],r[i]) for i in range(0,n+1)]))

Submission status: Accepted

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

if 'R' in c and 'W' in c:
  R = int(c.count('R'))
  c[0:R] = []
  print(c.count('R'))
else:
  print(0)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
import collections
p=collections.Counter(c)
last=["R"]*p["R"]+["W"]*p["W"]
ans=0
for i in range(n):
    if c[i]!=last[i]:
        ans+=1
print(ans//2)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Nw, Nr = 0, 0
for i in range(len(c)):
    Nw += 1 if c[i] == "W" else 0
    Nr += 1 if c[i] == "R" else 0

if Nw == 0 or Nr == 0:
    print(0)
    exit()

left_w = 0
right_r = 0
if c[0] == "W":
    left_w = 1
    right_r = Nr
else:
    left_w = 0
    right_r = Nr - 1

#仕切りの位置をiの横とすると
ans = max(left_w, right_r)

for i in range(len(c)):
    if i > 0:
        if c[i] == "W":
            left_w += 1
        else:
            right_r -= 1
    swap = max(left_w, right_r)
    ans = min(swap, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def Ra():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()

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


def dijkstra(edge,st):
    # edge=[[(v_to,dist_to_v),...],[],...]
    # initialize: def: d=dist(st,i), prev=[previous vertex in minimum path], q[]
    n=len(edge)
    d=[(0 if st==i else inf) for i in range(n)]
    q=[(j,i) for i,j in enumerate(d)]
    heapify(q)
    v=[False]*n
    # calc
    while q:
        dist,cur=heappop(q)
        if v[cur]:
            continue
        v[cur]=True
        for dst,dist in edge[cur]:
            alt=d[cur]+c[cur]
            if alt<d[dst]:
                d[dst]=alt
                heappush(q,(alt,dst))
    return d

class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
 
    def _root(self, x):
        if self.table[x] < 0:
            return x
        else:
            # 経路の圧縮
            self.table[x] = self._root(self.table[x])
            return self.table[x]
 
    def find(self, x, y):
        return self._root(x) == self._root(y)
 
    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
    
    def __str__(self):
        rt=[i if j<0 else j for i,j in enumerate(self.table)]
        return str(rt)

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo



show_flg=False
show_flg=True

ans=0


n=I()
s=input()
r,w=[],[]
for i in range(n):
    if s[i]=='R':
        r+=i,
    else:
        w+=i,
w=w[::-1]

ans=0
while w and r:
    c=w.pop()
    if c>r[-1]:
        break
    r.pop()
    ans+=1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = input()
Wonleft = 0
Ronright = l.count('R')
ans = Ronright
for i in range(N):
    if l[i] == 'W':
        Wonleft += 1
    else:
        Ronright -= 1
    t = max(Wonleft, Ronright)
    if ans > t:
        ans = t
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = 0
b = 0
for c in s:
    if c == "R": a += 1
ans = max(a,b)
for i in range(n):
    if s[i] == "R": a -= 1
    else: b += 1
    ans = min(ans, max(a, b))
print(ans)

Submission status: Accepted

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

cnt = 0

W = 0
R = c.count('R')

min_operation = max(W, R)

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

for i in range(n):
    if c[i] == 'W':
        W += 1
    else:
        R -= 1

    min_operation = min(min_operation, max(W, R))
    if min_operation == 0:
        print(0)
        exit()

print(min_operation)

Submission status: Accepted

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

ans = 0
r = N - 1
for l in range(N):
    if C[l] == 'R':
        continue
    r = max(l, r)
    while r > l and C[r] == 'W':
        r -= 1
    if r <= l:
        break
    C[l], C[r] = C[r], C[l]
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


N = int(input())
C = input()
C_cntr = Counter(C)

ans = 0
for i in range(C_cntr["R"]):
    if C[i] == "W":
        ans += 1

print(ans)

Submission status: Accepted

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

import sys
n = int(input())
word = input()
res = 0
num_r = 0

for charactor in word:
    if charactor == 'R':
        num_r += 1

num_w = 0
for i in range(num_r):
    if word[i] == 'W':
        num_w += 1

print(num_w)

Submission status: Accepted

Solution code:
n,s = int(input()),input()
print(s[:s.count("R")].count("W"))

Submission status: Accepted

Solution code:
n,s = int(input()),input()
print(min([s.count("W"),s.count("R"),s[:s.count("R")].count("W")]))

Submission status: Accepted

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

    left, right = 0, N - 1
    ans = 0
    while left < right:
        while left < right and C[left] == 'R':
            left += 1
        while right >= 0 and C[right] == 'W':
            right -= 1

        if left >= right:
            break
        left += 1
        right -= 1
        ans += 1
    return ans

print(d_alter_altar_2())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
c = list(input())
n = 0
k = N - 1
for i in range(N):
    if c[i] == 'W':
        for j in range(k, i, -1):
            if c[j] == 'R':
                n += 1
                c[i] = 'R'
                c[j] = 'W'
                k = j - 1
                break
            if j == i + 1:
                print(n)
                sys.exit()
print(n)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
a = 0
b = 0
for i in range(n):
    if c[i] == 'R':
        a+=1
ans = max(a, b)
for i in range(n):
    if c[i] == 'R':
        a-=1
    else:
        b+=1
    now = max(a, b)
    ans = min(ans, now)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=list(input())
 
rj=c.count('R')
wj=0
mini=rj
for i in range(n):
  if c[i]=='R':
    rj-=1
  else:
    wj+=1
  mini=min(mini,max(rj,wj))
  
print(mini)

Submission status: Accepted

Solution code:
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)
print(r_count - sum(a_list[0:r_count]))

Submission status: Accepted

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

Submission status: Accepted

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

a, b = 0, 0

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

ans = max(a, b)

for i in range(n):
    if c[i] == 'R':
        a -= 1
    else:
        b += 1
    now = max(a, b)
    ans = min(now, ans)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
lists = list(c)
cnt = 0

for i in range(len(lists)):
    if lists[i] == "R":
        cnt += 1
change = 0
for i in range(cnt):
    if lists[i] == "W":
        change += 1
print(change)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# from typing import *

import collections


# def solve(N: str, c: List[str]) -> int:
def solve(c):
    counts = collections.Counter(c)
    if counts['R'] == 0 or counts['W'] == 0:
        return 0

    inversions = 0
    for i in range(counts['R']):
        if c[i] != 'R':
            inversions += 1

    return inversions

# generated by online-judge-template-generator v4.4.0 (https://github.com/kmyk/online-judge-template-generator)
def main():
    _ = int(input().rstrip())
    c = input().rstrip()
    a = solve(c)
    print(a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

l_w = [0 for i in range(n+1)]
r_r = [0 for i in range(n+1)]

for i in range(n):
    l_w[i+1] = l_w[i]
    if c[i] == 'W':
        l_w[i+1] += 1
for i in range(n, 0, -1):
    r_r[i-1] = r_r[i]
    if c[i-1] == 'R':
        r_r[i-1] += 1

ans = int(1e9)

for i in range(n+1):
    ans = min(ans, max(l_w[i], r_r[i]))

print(ans)

Submission status: Accepted

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

n = input()
s = input()

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

Submission status: Accepted

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

S=0
T=N-1

t=0
while S<T:
  if C[S]=='R' and C[T]=='R':
    S+=1
  elif C[S]=='R' and C[T]=='W':
    S+=1
    T-=1
  elif C[S]=='W' and C[T]=='R':
    S+=1
    T-=1
    t+=1
  else:#C[S]=='W' and C[T]=='W'
    T-=1

print (t)

Submission status: Accepted

Solution code:
n = int(input())
ls = list(input())
r = ls.count('R')
w = ls.count('W')
if r == 0 or w == 0:
    print(0)
else:
    r_in_half = ls[:r].count('R')
    ans = r - r_in_half
    print(ans)

Submission status: Accepted

Solution code:
import sys

n=int(input())
c=input()
r=c.count("R")
w=c.count("W")
mi=min(r,w)
l_w=0
l_r=0
ans=float('inf')
if w==n:
  print(0)
  sys.exit()


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

  r_r=r-l_r
  if l_w<=r_r:
    now=l_w+(r_r-l_w)
  else:
    now=r_r+(l_w-r_r)

  ans=min(ans,now)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

c=list(input())


#c[1]=c[2]
sta=1
ans=0
for i in range(N):
    if(-sta+N<i):
        break
    if(c[i]=='W'):
        for j in range(sta,N):
            if(-j+N<=i):
                print(ans)
                exit()
                break
            if(c[-j]=='R'):
                temp=c[-j]
                c[-j]=c[i]
                c[i]=temp
                sta=j
                ans=ans+1
                break


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
C = list(input())
l, r, c = 0, n-1, 0
while l < r:
    if C[l] != 'W': l += 1
    elif C[r] != 'R': r -= 1
    else: l += 1; r -= 1; c += 1
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
RL, WL = [0 for _ in range(N+1)], [0 for _ in range(N+1)]

for i in range(N):
  RL[i+1] = RL[i] + (S[i] == 'R')
  WL[i+1] = WL[i] + (S[i] == 'W')
mcnt = int(1e13) + 13
for i in range(N, 0, -1):
  if S[i-1] == 'R':
    mcnt = min(mcnt, RL[N] - RL[i] + max(0, WL[i-1] - RL[N] + RL[i]))
if RL[N] == 0:
  mcnt = 0
print(mcnt)

Submission status: Accepted

Solution code:
n = int(input())
c = input()
w = 0
r = c.count('R')
ans = r
for i in c:
    if i == 'W': w += 1
    else: r -= 1
    ans = min(ans, max(w, r))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_ = int(input())
C = input()
print(C[C.count('R'):].count('R'))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

N = I()
c = _S()

R = c.count('R')
ans = 0

# Rを左詰めすれば良い。左から探索してWであれば入れ替える
# for i in range(R):
#     if c[i]=='W':
#         ans += 1
ans = c[:R].count('W')
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    C = list(input().strip("\n"))

    count = 0
    rightBound = N - 1
    for i in range(N):
        if C[i] == "W":
            while rightBound > i:
                if C[rightBound] == "R":
                    C[i] = "R"
                    C[rightBound] = "W"
                    count += 1
                    break
                else:
                    rightBound -= 1
            else: break
    print(count)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n = input()
s = input()
print(s[:s.count('R')].count('W'))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

n = int(input())
s = input()
r = s.count('R')
print(s[:r].count('W'))