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: Wrong Answer

Solution code:
n = int(input())
l = []
for i in input():
    l.append(i)
right = 1
left = 0
ans = 0
for i in l:
    if l[-right] == "R":
        l[-right] == "W"
        for j in l:
            try:
                if l[left] == "W":
                   l[left] = "R"
                   ans += 1
                   break
                else:
                  left += 1
            except:
                left += 1
    right += 1
    if left + right == n:
        break
print(ans)

Submission status: Wrong Answer

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

prev = S[0]
cur = 'R'
count = 0
wNum = 0
changedPoint = 0
for i in range(N-1, -1, -1):
    cur = S[i]
    if cur == 'R':
        for j in range(wNum, N - i):
            if S[j] == 'W':
                wNum = j + 1
                changedPoint = j
                break
        if wNum != 0:
            count += 1

    prev = cur
    if changedPoint >= i:
        break

print(count)

Submission status: Wrong Answer

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

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    white = [0] * (N + 1)
    red = [0] * (N + 1)
    for n in range(N):
        white[n + 1] = white[n] + 1 if S[n] == 'W' else white[n]
        red[n + 1] = red[n] + 1 if S[n] == 'R' else red[n]

    ret = MAX
    for left in range(N + 1):
        w = white[left] - white[0]
        r = red[N] - red[left]
        tmp = w + r
        if w == 1 and r >= 1: tmp -= 1
        ret = min(tmp, ret)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

'''
Author: andyli
Time: 2020-08-02 19:55:50
'''

import os
from io import BytesIO, IOBase
import sys


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

    def calc(s):
        p = 0
        while p < len(s) and s[p] == 'R': p += 1
        _ans = s.count('R')-p
        p = len(s)-1
        while p >= 0 and s[p] == 'W': p -= 1
        _ans = min(_ans, s.count('W')-(len(s)-p-1))
        return _ans

    ans = calc(s)
    # print(ans)
    p1 = 0
    while p1 < n and s[p1] == 'W': p1 += 1
    p2 = n-1
    while p2 >= 0 and s[p2] == 'R': p2 -= 1

    cnt = min(p1, n-p2-1)
    # print(p1,p2,cnt)
    s2 = 'R'*cnt + 'W' * max(0, p1-cnt) + s[p1:p2+1] + 'R' * max(0,n-p2-1-cnt) + 'W'*cnt
    print(s2)
    # calc(s2) + min()
    ans = min(ans, calc(s2) + cnt)
    print(ans)
    return


# region fastio
BUFSIZE = 1048576


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

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


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda: self.buffer.read().decode('ascii')
        self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip('\r\n')

# endregion


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
word=input()
line=[]
for i in range(n):
    if(word[0]=='R'):
        line.append(1)
    else:
        line.append(0)
    word=word[1:]
x=0
y=n-1
ans=0
while(1):
    while(1):
        print(22222222)
        if(line[x]==0):
            break
        if(x>=y):
            break
        x+=1
    while(1):
        print(3333333333)
        if(line[y]==1):
            break
        if(x>=y):
            break
        y-=1
    if(x>=y):
        break
    tmp=line[x]
    line[x]=line[y]
    line[y]=tmp
    print(ans)
    ans=ans+1
print(ans)

Submission status: Wrong Answer

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

ans = 0

l = 0
r = n-1

lsl = 0
lsr = 0

while l < n:
  lc = c[l]
  if lc == 'W':
    while 0 <= r:
      rc = c[r]
      if rc == 'R' and l < r:
        ans += 1
        lsl = l
        lsr = r
        r -= 1
        break
      r -= 1
  l += 1
#print('mid : ', ans)
if (lsr - lsl) > 1:
  for i in range(lsl+1, lsr-1):
    if c[i] == 'W':
      ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input())
a = 0
B = []
for i in range(N):
    if A[i] == 'W':
        a += 1
    B.append(a)
if a >= N - a:
    ans = B[N - a - 1]
else:
    ans = B[N - a - 1] - a
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
white=0
red=0
W=list(str(input()))
if "W" not in W or "R" not in W:
  print("0")
  exit()
for i in range(N//2):
  if W[i]=="W":
    white+=1
for i in range(N//2):
  if W[N-1-i]=="R":
    red+=1
Z=min(white,red)
rest=abs(white-red)
Z=Z+rest
if (N/2)%1!=0:
  if W[N//2+1]=="W":
    Z+=1
print(Z)

Submission status: Wrong Answer

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

ans = 0
s = c.count('R')
e = n // 2
f = c.count('WR')

if f == 0:
    ans = 0

elif n % 2 == 0:
    sr = c.count('W',0,e)
    sw = c.count('R',e,n)

    if sr >= sw:
        ans = sr
    else:
        ans = sw

else:
    sr = c.count('W',0,e)
    sw = c.count('R',e+1,n)

    if sr >= sw:
        ans = sr
    else:
        ans = sw
    
print(ans)

Submission status: Wrong Answer

Solution code:
K=int(input())

def solve(K):
    from collections import defaultdict
    if K==1:
        return 1
    elif K%2==0 or K%5==0:
        return -1
    
    elif K%7==0:
        K=K//7
        app = defaultdict(lambda: False)
        broken=False
        for n in range(1,K+1):
            if n==1:
                amari = 1
            else:
                amari = (amari + pow(10,n-1,K) )%K
                
            if amari==0:
                broken=True
                break
            if app[amari]:
                broken=True
                break
            else:
                app[amari]=True
            
        if broken:
            return n
        else:
            return -1
        
        
    else:        
        app = defaultdict(lambda: False)
        broken=False
        for n in range(1,K+1):
            if n==1:
                amari = 7%K
            else:
                amari = (amari + 7*pow(10,n-1,K) )%K
            #print(n,amari)
            if amari==0:
                broken=True
                break
            if app[amari]:
                broken=True
                break
            else:
                app[amari]=True
        if broken:
            return n
        else:
            return -1
        

print(solve(K))

Submission status: Wrong Answer

Solution code:
import collections

def solve():
    wr = collections.Counter(c)
    
    w = wr["W"]
    r = wr["R"]
    left  = collections.Counter(c[0:r])
    right = collections.Counter(c[r:N])
#    print(left)
#    print(right)
#    print(right["R"])
    cnt = 0
    tmp = 0
    is_change = False
    for i in range(N):
        if not is_change and c[i] == "W":
            cnt += 1
            is_change = True
        elif is_change and c[i] == "W":
            cnt += 1
            r += 1
            is_change = True
        if c[i] == "R":
            tmp += 1
            if tmp == r:
                break
    print(cnt)

if __name__ == "__main__":
    N = int(input())
    C = input()
    c = [str(l) for l in C]
#    print(c)
    solve()

Submission status: Wrong Answer

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


r = 0
w = 0
for x in c:
    if x == 'R':
        r += 1
    else:
        w += 1

ans = 0
for x in c:
    r -= 1
    if x =='W':
        ans += 1
    if r == 0:
        break
    

print(ans)

Submission status: Wrong Answer

Solution code:
import re

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

def set_c(a):
    a = re.sub('^[R]*W', 'W', a)
    a = re.sub('R[W]*$', 'R', a)
    return a

c = set_c(c)
l1 = len(c)//2
l2 = (len(c)+1)//2

print(max(c[:l1].count('W'),c[l2:].count('R')))

Submission status: Wrong Answer

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

count = 0
while(True):
    #print(S)
    HeadW = S.find('W')
    preTailR = S[::-1].find('R')
    if(preTailR != -1):
        TailR = len(S) - preTailR
    else:
        TailR = -1

    if(HeadW == -1 or TailR == -1):
        break

    elif(HeadW == TailR):
        break

    elif(HeadW != -1 and TailR != -1):
        S = S[HeadW+2:TailR]#S[:HeadW] + 'R' + S[HeadW+2:]
        #S = S[:TailR]#S[:TailR] + 'W' + S[TailR+2:]
        count += 1
    
    elif(HeadW == -1):
        count += S[HeadW:].count('R')
        break

    elif(TailR == -1):
        count += S[:TailR].count('W')
        break


print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

C = input()

mid = N//2

Wcount = C.count("W", 0, mid)
print(Wcount)

Submission status: Wrong Answer

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

count_W = 0
for i in range(N//2):
    if c[i] == 'W':
        count_W += 1
        
count_R = 0
for i in range(N//2):
    if c[-1-i] == 'R':
        count_R += 1

print(min(count_R, count_W))

Submission status: Wrong Answer

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


def main():
    N = int(input())
    c = input()
    cnt = collections.Counter(c)
    ans = 200000
    ans2 = 200000

    W_cnt = cnt['W']
    R_cnt = cnt['R']

    if W_cnt == R_cnt:
        cnt2 = collections.Counter(c[:R_cnt])
        ans2 = cnt2['W']
    elif c[0] == 'R':
        R_cnt -= 1
    ans = min(W_cnt, R_cnt)
    print(min(ans, ans2))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    x = c.count('R')
    if x == n or x == 0: return 0

    r1 = 0
    for i in range(n):
        if c[i] == 'R':
            r1 += 1
        else:
            break
    c = c[r1:n]
    x -= r1
    if 'R' not in c:
        return 0
    else:
        w1 = 0
        for i in range(len(c)):
            if c[i] == 'W':
                w1 += 1
            else:
                break
        if w1 <= x-1:
            return x-1
        else:
            return x
print(solve())

Submission status: Wrong Answer

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

N = int(input())
word = input()
count = 0
res = 0
if word[0] == 'R':
    is_red = True
else:
    is_red = False

count_white = 0
count_red = 0
for i in range(N):
    if i < N // 2 and word[i] == 'W':
        count_white += 1
    elif i >= N // 2 and word[i] == 'R':
        count_red += 1

res = min(count_white, count_red)
print(res)

Submission status: Wrong Answer

Solution code:
def function(N, c):
    num_R = c.count('R')
    num_W = c.count('W')
    if (num_R == 0) or (num_W == 0):
        return 0
    else:
        if num_R >= num_W:
            num = 0
            for i in range(int(N/2)):
                if c[i] == 'R':
                    num += 1
            if num_R == 1:
                if c[0] == 'R':
                    return 0
                else:
                    return 1
            else:
                return num_R-num
        else:
            num = 0
            for i in range(int(N/2)):
                if c[N-1-i] == 'W':
                    num += 1
            if num_W == 1:
                if c[-1] == 'W':
                    return 0
                else:
                    return 1
            else:
                return num_W-num


if __name__ == '__main__':
    N = int(input())
    c = input()
    print(function(N, c))

Submission status: Wrong Answer

Solution code:
N = int(input())
C = input()
R = C.count("R")
C = C[:len(C)//2]
W2 = C.count("W")
print(min(R, N - R) if W2*2 > N else min(min(R, N - R),W2))

Submission status: Wrong Answer

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

l = 0
r = len(S) - 1

W = 'W'
R = 'R'

cnt = 0
while l < r:
    if S[l] == R:
        l += 1
        continue
    else:
        while l < r:
            if S[r] == W:
                r -= 1
                continue
            else:
                cnt += 1
                l += 1
                r -= 1

print(cnt)

Submission status: Wrong Answer

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

count = 0
while(True):
    #print(S)
    HeadW = S.find('W')
    preTailR = S[::-1].find('R')
    if(preTailR != -1):
        TailR = len(S) - preTailR
    else:
        TailR = -1

    if(HeadW == -1 or TailR == -1):
        break

    elif(HeadW == TailR):
        break

    elif(HeadW != -1 and TailR != -1):
        S = S[HeadW+2:]#S[:HeadW] + 'R' + S[HeadW+2:]
        S = S[:TailR]#S[:TailR] + 'W' + S[TailR+2:]
        count += 1
    
    elif(HeadW == -1):
        count += S[HeadW:].count('R')
        break

    elif(TailR == -1):
        count += S[:TailR].count('W')
        break


print(count)

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
c=input()
if c.count("R")==0 or c.count("W")==0:
    print(0)
else:
    W_count=0
    R_count=0
    for i in range(N-c[::-1].index("W")-1):
        if c[i]=="W":
            W_count+=1
    for i in range(N-c.index("R")-1):
        if c[N-i-1]=="R":
            R_count+=1
    print(min(c.count("R"),c.count("W"),max(W_count,R_count)))

Submission status: Wrong Answer

Solution code:
def main(N, S):
    ans = 0
    cw = S.count("W")
    cr = S.count("R")
    cht = min(cw, cr)
    right = N-1
    for left in range(N//2):
        if S[left] == "W" and S[right] == "R":
            S[left], S[right] = S[right], S[left]
            ans += 1
        while N//2 <= right and S[left] == "W" and S[right] != "R":
            right -= 1

        if N//2 > right or ans == cht:
            break
    add = 0
    T = S[:]
    for i in range(N-1):
        if S[i] == "W" and S[i+1] == "R":
            S[i+1] = "W"
            add += 1
    add2 = 0
    for i in range(N-1)[::-1]:
        if T[i] == "W" and T[i+1] == "R":
            T[i] = "R"
            add2 += 1
    return ans + min(add, add2)


def main2(N, S):
    cw = S.count("W")
    cr = S.count("R")
    cht = min(cw, cr)
    return cht


if __name__ == '__main__':
    N = int(input())
    S = [s for s in input()]
    ans = min(main2(N, S), main(N, S))
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
def try_count(c, color):
    try:
        return c.count(color)
    except:
        return 0
all_r = try_count(c, 'R')
all_w = try_count(c, 'W')
left_w = try_count(c[:all_w], 'W')
print(min(all_r,all_w,left_w))

Submission status: Wrong Answer

Solution code:
n=int(input())
c=list(input())
w=[i for i, x in enumerate(c) if x == 'W']
r=[i for i, x in enumerate(c) if x == 'R']
wc=len(w)
rc=len(r)
wt=list(range(wc,wc+rc))
tand = set(w) & set(wt)
print(wc-len(tand))

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
C = deque(input())

ans = 0
while len(C)>1:
  a,b = C.popleft(), C.popleft()
  if b=="R" and a=="W":
    C.appendleft(b)
    ans += 1
  C.appendleft(b)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

def resolve():
    N=int(input())
    C=input()

    target = 'WR'
    index = -1
    cnt=0
    
    Rf=C[:N//2].count('R')
    Rr=C[N//2+1:].count('R')
    Wf=C[:N//2].count('W')
    Wr=C[N//2+1:].count('W')

    if(N%2==0):
        Centor=""
    else:
        Centor=C[N//2:N//2+1]

    cnt+=max(Wf,Rr)   

    if(C.count('R')==0 or C.count('W')==0):
        cnt=0

    print(cnt)
 
resolve()

Submission status: Wrong Answer

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

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

R_R = 0
L_R = 0
for i in range(N - 1, -1, -1):

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

    if (c[i] == "W"):
        break
    L_R += 1

Ans1 = R_R - L_R

L_W = 0
R_W = 0
for i in range(N):
    if (c[i] == "W"):
        L_W += 1
for i in range(N - 1, -1, -1):
    if (c[i] == "R"):
        break
    R_W += 1

Ans2 = L_W - R_W


i = 0
j = N - 1

count = 0

while (True):
    while (c[i] != "W"):
        i = i + 1

    while (c[j] != "R"):
        j = j - 1
    count += 1
    i = i + 1
    j = j - 1
    if (i > j or i == j or i >= N or j < 0):
        break

Ans3 = count

print(min(Ans1, Ans2, Ans3))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
cnt = 0
for i in s:
    if i == "R":
        cnt += 1
    else:
        break

print(s.count("R")-cnt)

Submission status: Wrong Answer

Solution code:
import sys
import os
import itertools
import numpy as np
from collections import deque

def main():
    if os.getenv("ENV")=='LOCAL':
        sys.stdin = open(os.path.dirname(__file__)+'/input.txt', 'r')

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

    R_l=c.find('R')
    R_n=c.count('R')

    ans=0

    if c.find('WR')==-1:
        print(0)
    else:
        R_info=[]
        p=0
        for i in range(N):
            if c[i]=='R':
                R_info.append(p)
                p=0
            else:
                p+=1

        # print(R_info)
        for i,n in enumerate(R_info):
            # print(i)
            # print(R_n)
            if n==0:
                R_n-=1
            elif n>=1 and R_n>=n:
                R_n-=n+1
                ans+=n
                # print(ans)
            else:
                ans+=R_n
                # print(ans)
                break
        print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
c = list(input())
pre = c[0:N//2+1]
suf = c[N//2+1:N]
ans = 0

pre_cnt = pre.count('W')
suf_cnt = suf.count('R')

ans = min(pre_cnt,suf_cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    c = list(input())

    w = 0
    tw = 0
    r = c.count("R")

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

    lastw = 0
    tmp = 0
    for i in range(n):
        if c[i] == "W":
            r -= 1
            lastw = i
        else:
            r -= 1
            tmp += 1
        if r == 0:
            break

    print(max(0, lastw + 1 - tmp))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
C = input()
w_cnt = 0
r_cnt = 0
prev_r = False
ans = 0
for c in C:
    if c == 'W' and not prev_r:
        w_cnt += 1
    if c == 'R':
        r_cnt += 1
        prev_r = True
    if c == 'W' and prev_r:
        ans += min(r_cnt, w_cnt)
        r_cnt = 0
        w_cnt = 1
        prev_r = False
if C[-1] == 'R':
    ans += min(r_cnt, w_cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
C = list(input())
count = 0
N = int(N/2)
for i in range(N):
    if (C.count('W') == 0 or C.count('R') == 0):
        print(0)
        break
    if (C[i] == 'W'):
        count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(input())
if N%2==0:
  x = a[0:int(N/2)]
  y = a[int(N/2):N]
else:
  x = a[0:int(N/2)]
  y = a[int(N/2)+1:N]
count_ri = x.count('W')
count_le = y.count('R')
if a.count('W') ==0 or a.count('R')==0:
  print(0)
elif x.count('R')==0 and y.count('W')==0:
  print(0)
elif count_ri==count_le:
  print(count_ri)
elif count_ri>count_le:
  print(count_le)
elif count_ri<count_le:
  print(count_ri)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
altars = []
transformations = 0

for altar in c:
    altars.append(altar)

rightmost_red_index = len(altars) - 1

for i in range(len(altars) // 2):
    if altars[i] == "W":
        for j in range(rightmost_red_index, len(altars) // 2 - 1, -1):
            if altars[j] == "R":
                altars[j] = "W"
                altars[i] = "R"
                rightmost_red_index = j
                transformations += 1
                break

if len(altars) % 2 == 1:
    first_white_index = - 1
    for i in range(len(altars)):
        if altars[i] == "W":
            first_white_index = i + 1
            break
    if first_white_index != -1:
        for j in range(first_white_index, len(altars)):
            if altars[j] == "R":
                transformations += 1

print(transformations)

Submission status: Wrong Answer

Solution code:
import sys
import copy
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
INF = 1000000000000

n = I()
s = list(S())

i = 0
c = 0
while(s[i] == 'W' and s[n-i-1] == 'R' and i < n//2):
    s[i] = 'R'
    s[n-i-1] = 'W'
    i += 1
    c += 1

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

print(c)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=input()
c=int(a/2)
d=0
e=a-1
i=0
while True:
    if (b[i]=='W'):
        for j in range(0,e+1)[::-1]:
            if b[j]=='R':
                d=d+1
                e=j-1
                break
    i=i+1
    if i>e: break
print(d)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
l=[0]
for i in range(n):
    l.append(l[i]+(s[i]=="R"))
ans=n
print(l)
for i in range(n+1):
    ans=min(ans,abs(l[i]-i)+abs(l[-1]-l[i]))
    #print(ans,i)
    if i==l[-1]:
        ans=min(ans,abs(l[i]-i))
        #print(ans)
print(ans)

Submission status: Wrong Answer

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

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

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

def check(a, n, ct):
    L = Counter(a[:n//2])
    R = Counter(a[n//2:])
    
    ct += min(L["W"], R["R"])

    # print(a, L, R, n//2, L["W"], R["R"])
    if L["W"] > R["R"]:
        ct = check(a[:n//2], n // 2, ct)

    return ct

print(check(c, N, 0))

Submission status: Wrong Answer

Solution code:
# D Alter Altar

n = int(input())
C = input()

num_R = C.count("R")

# 全部R or Wの場合
if len(C) == num_R or num_R == 0:
  print(0)
  exit()

# 一色に染めるときの回数で少ない方
num_all = min(len(C) - num_R, num_R)

#左端にRを固める作戦の回数
space = 0
for c in C:
  if c == "W":
    space += 1
  elif c == "R":
    break
if num_R <= space:
  num_left = num_R
else:
  num_left = num_R - 1
  
print(min(num_left, num_all))

Submission status: Wrong Answer

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
#======================================================#
def main():
    N = II()
    cc = list(IS())
    cc_rev = cc[::-1]
    cnt = 0
    if cc.count('W') == 0:
        print(0)
        return 0
    for i, c in enumerate(cc_rev):
        if c == 'R':
            idx = cc.index('W')
            if i >= idx:
                cc[idx] = 'R'
                cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    N = int(input())
    c = input()
    cnt = collections.Counter(c)
    ans = 200000
    ans2 = 200000


    W_cnt = cnt['W']
    R_cnt = cnt['R']

    if W_cnt == R_cnt:
        n = N//2
        cnt2 = collections.Counter(c[:n])
        ans2 = cnt2['W']
    elif c[0] == 'R':
        R_cnt -= 1
    ans = min(W_cnt, R_cnt)
    print(min(ans, ans2))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    a = input()

    if not 'WR' in a:
        print(0)
    else:
        rightmostW = a.find('RW')
        wnum = a[rightmostW+1:].count('W')
        print(wnum)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
C = input()
flag = False
cnt = 0
for i in range(n):
    if not flag and C[i] == 'W':
        flag = True
    if flag:
        if C[i] == 'R':
            cnt += 1
        elif C[i] == 'W':
            if i + 1 < n:
                if C[i+1] == 'R':
                    pass
                else:
                    cnt = max(cnt-1,0)
            else:
                cnt = max(cnt-1,0)
r = C.count('R')
nya = 0
for i in range(r,n):
    if C[i] == 'R':
        nya += 1
a = 0
for i in range(n-1):
    if C[i] == 'W' and C[i+1] == 'R':
        a += 1
print(min(max(a,cnt), nya))

Submission status: Wrong Answer

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

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

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

def check(a, n, ct):
    L = Counter(a[:n//2])
    R = Counter(a[n//2:])
    
    ct += min(L["W"], R["R"])

    # print(a, L, R, n//2, L["W"], R["R"])
    if n // 2 != 1 and L["W"] > R["R"]:
        ct = check(a[:n//2], n // 2, ct)

    return ct

A = Counter(c)
print(check(c, N, 0))

Submission status: Wrong Answer

Solution code:
import math
import sys

def main():
    N=int(input())
    C=list(input())
    res=0
    for i in range(len(C)):
        if i>=1:
            if C[i]=="R" and C[i-1]=="W":
                res+=1
    print(res)


if __name__=="__main__":
    main()

Submission status: Wrong Answer

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
    return len(l_1)

print(get_res(l_1))

Submission status: Wrong Answer

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

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

left, right = 0, len(c) - 1
cnt = 0

while left < right:
    if left > 0 and c[left-1] == c[left+1] == 'R' and c[left] == 'W':
        c[left] = 'R'
        left += 1

    elif right < len(c) - 1 and c[right-1] == c[right+1] == 'W' and c[right] == 'R':
        c[right] = 'W'
        right -= 1

    elif c[left] is 'W' and c[right] is 'R':
        c[left] = 'R'
        c[right] = 'W'

    elif c[left] is 'W':
        right -= 1
        continue
    
    elif c[right] is 'R':
        left += 1
        continue

    else:
        left += 1
        right -= 1
        continue

    left += 1
    right -= 1

    cnt += 1


print(cnt)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right as br

n = int(input())
c = list(input())
cFixed = [x for x in c]
cFixed.sort()
for i in range(n):
	if cFixed[i] == "R":
		lastR = i
ans = 0
for i in range(n):
	if c[i] != cFixed[i]:
		j = br(c,"R", lo = lastR + 1)-1
		#print(j,i,c[j],c[i],cFixed[i])
		c[i] = c[j]
		c[j] = "W"
		ans += 1


print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input())
a = 0
B = []
for i in range(N):
    if A[i] == 'W':
        a += 1
    B.append(a)
if a >= N - a:
    ans = min(N - a, B[N - a])
else:
    ans = min(a, B[a])
print(ans)