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:
import sys
import numpy as np

def a():
    N = int(input())
    c = list(str(input()))
    l = N-1
    s = 0
    print(c)

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


    for i in range(int(N)):        
        if c[i] == 'W':
            for j in range(l,i,-1):
                if c[j] == 'R':
                    l = j-1
                    s = s+1
                    break
        if l < i:
            break
    print(s)
if __name__ == "__main__":
    a()

Submission status: Wrong Answer

Solution code:
input()
s = input()

def find_cost(s):
    counter, i, j = 0, 0, len(s)-1
    while i < j:
        while s[i] != 'W' and i < j:
                i += 1
        while s[j] != 'R' and i < j:
                j -= 1
        s = list(s)
        s[i], s[j] = s[j], s[i]
        s = "".join(s)
        counter += 1
        i += 1
        j -= 1

    for i in range(0, len(s) - 2):
        if s[i] == 'W' and s[i + 1] == 'R':
            counter += 1
    print(counter)
    
find_cost(s)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import math
N = int(input())
C = input()
#N, D=map(int,input().split())
#C = []
#for i in range(N):
#    C.append(list(map(int,input().split())))
w = -1
r = -1
for i in range(N):
    if C[i] == 'W':
        w = i
        break
for i in reversed(range(N)):
    if C[i] == 'R':
        r = i
        break
if w == -1 or r == -1:
    print(0)
    exit(0)

c = C[w:r+1]
center = c.count('R')
if w==r:
    print(c[0:center+1].count('W'))
elif w>r:
    print(c[0:center+1].count('W'))
else:
    print(c[center:-1].count('R'))

Submission status: Wrong Answer

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

if n % 2 == 0:
  half = int(n / 2)
  odd =0
else:
  half = int((n-1) / 2)
  odd = 1
pre_w_num = 0
for i in range(half):
  if c[i] == "W":
    pre_w_num = pre_w_num + 1
post_r_num = 0
for j in range(half):
  if c[-1 *(j+1)] == "R":
    post_r_num = post_r_num +1
if pre_w_num <= post_r_num:
  ans = pre_w_num
else:
  ans = post_r_num
if odd == 1:
  if c[half] == 'W':
    ans = ans +1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
lists = list(c)
cnt = 0
while 1:
    if lists[cnt] == "W":
        cnt += 1
        if cnt == len(lists):
            break
    else:
        break
number = 0
for i in range(len(lists) - cnt):
    if lists[i + cnt] == "W":
        number += 1
print(number)

Submission status: Wrong Answer

Solution code:
i=int(input())
s=list(input())
fw,fr=0,0
count=0

if s[0]=="W" and s[i-1]=="R":
    count+=1
    s[0]="R"
    s[i-1]="W"
if s[i-1]=="R":
    count+=1
    s[i-1]="W"
for j in range (i):
    if s[j]=="W" :
        fw=j 
        break
    
print (s)
for j in range (fw,i):
    if s[j]=="R":
        count+=1
print(count)

Submission status: Wrong Answer

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

n = int(input())
n2 = n//2
c = input()

if c.count("R") == n:
    print(0)
else:
    cnt = c.count("W", 0, n2)
    print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(input())
b=0
c=0
d=0
for i in range(-1,-(n+1),-1):
    if a[i]=="R":
        c=i+n
        break
for i in range(c,n):
    if a[i]=="W":
        d+=1
for i in range(len(a)):
    if a[i]=="W":
        b=i
        break
del a[:b]

print(min(d,a.count("R")))

Submission status: Wrong Answer

Solution code:
N = int(input())
C = list(input())
CC = C[::-1]
RC = C.count('W')
WC = C.count('R')
ans = min(WC, RC)
XC = 0
if ans==0:
    print(0)
else:
    while ans > XC:
        l = C.index('W')
        r = CC.index('R')
        if l > N-1-r:
            ans = min(XC, ans)
            break
        C[l],C[N-1-r] = C[N-1-r],C[l]
        CC[r],CC[N-l-1] = CC[N-l-1],CC[r]
        XC += 1
print(ans)

Submission status: Wrong Answer

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

if c.count("W") == 0 or c.count("R") == 0:
  print(0)
else:
  print(c.count("W"))

Submission status: Wrong Answer

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

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

if w < r:
    print(C[0:w].count("W"))
else:
    print(C[w:len(C)].count("R"))

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
count = 0
left_f = 0
right_f = N-1
while 1:
    if c[left_f] == 'R' and c[right_f] == 'W':
        left_f += 1
        right_f -= 1
    elif c[left_f] == 'W' and c[right_f] == 'W':
        right_f -= 1
    elif c[left_f] == 'R' and c[right_f] == 'R':
        left_f += 1
    else:
        count += 1
        left_f += 1
        right_f -= 1
    if left_f == right_f or left_f + 1 == right_f or left_f == right_f + 1:
        break
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    for i in range(min(len(r_index),len(w_index))):
        w=w_index[ans]
        r=r_index[ans]
        if w-r==1:
             ans=ans-1
             break
        ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import math
N = int(input())
C = input()
#N, D=map(int,input().split())
#C = []
#for i in range(N):
#    C.append(list(map(int,input().split())))
w = -1
r = -1
for i in range(N):
    if C[i] == 'W':
        w = i
        break
for i in reversed(range(N)):
    if C[i] == 'R':
        r = i
        break
if w == -1 or r == -1:
    print(0)
    exit(0)

c = C[w:r+1]
center = c.count('R')
if w==r:
    print(c[0:center+1].count('W'))
else:
    print(c[0:center+1].count('W'))

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
data = input()
num = len(data)
list_data = list(data)
R_num = list_data.count('R')
W_num = num-R_num

if(R_num==0 or W_num==0):
  print('0')
  sys.exit()
  
count = 0

while(not (list_data.count('R')==0 or list_data.count('W')==0)):
  
  list_R = [i for i, x in enumerate(list_data) if x == 'R']
  last_R = list_R[-1]
  first_W = list_data.index('W')
  if last_R-first_W==1:
    print(count)
    sys.exit()
  else:
    list_data[last_R]='W'
    list_data[first_W]='R'
    count +=1

Submission status: Wrong Answer

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

if n % 2 == 0:
  half = int(n / 2)
  odd =0
else:
  half = int((n-1) / 2)
  odd = 1
pre_w_num = 0
for i in range(half):
  if c[i] == "W":
    pre_w_num = pre_w_num + 1
post_r_num = 0
for j in range(half):
  if c[-1 *(j+1)] == "R":
    post_r_num = post_r_num +1
if pre_w_num <= post_r_num:
  ans = pre_w_num
else:
  ans = post_r_num
if odd == 1:
  if c[half] == 'W':
    ans = ams +1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input() #s_1 s_2を分割して取得し、sに値を入れる
count_white = 0
white_flag = 0
white_renzoku = 0

count_red = 0
red_flag = 0
red_renzoku = 0
for i in range(int(N/2)):
  if s[i]=='W':
    count_white = count_white+1
  if s[N-1-i]=='R':
    count_red = count_red+1
white_irekae = count_white
red_irekae = count_red

if white_irekae >= red_irekae:
  print(white_irekae)
else:
  print(red_irekae)

Submission status: Wrong Answer

Solution code:
N=int(input())
c=list(input())
R=c.count('R')
W=c.count('W')
k=0
for i in range(N):
    k+=1
    if c[i]=='R':
        R-=1
        k-=1
        if R==0 or W==0:
            break
    else:
        if R<=W:
            R-=1
            if R==0 or W==0:
                break
        else:
            k+=W
            W=0
            if R==0 or W==0:
                break
r=c.count('R')
w=c.count('W')
if r==N or w==N:
    k=0
print(k)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(input())
c=a.count("W")
b=a[:n-c]
d=b.count("W")
print(d)
print(b)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import copy 
n=int(input())
c=input()
cl=copy.deepcopy(c)
cl=list(cl)
d=Counter(c)
ans=0


if d["R"]==0 or d["W"]==0:
  print(0)
  exit()
swap=0

if d["R"]>=n//2:
  r=n//2
else:
  r=d["R"]
for i in range(r):
  #indexes = [j for j, x in enumerate(cl) if x == "R"]
  #print(cl)
  if cl[i]=="W":
    #cl[indexes[-1]]="W"
    #cl[i]="R"
    swap+=1

#print(cl)
#print(cl[cl.index("W"):])
#print(swap)
Rindexes = [j for j, x in enumerate(cl) if x == "R"]
Windexes = [j for j, x in enumerate(cl) if x == "W"]
#print(swap,Windexes)
#print(Windexes[swap-1]+1)

if swap == n//2:
  print(swap)
  exit()

d=Counter(cl[Windexes[swap-1]+1:])
ans=d["R"]
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import collections as c
n = int(input())
s = list(input())
mr = s.count("R")
mw = s.count("W")
cnt = 0

r = 0
w = 0

for i in s:
    if w>=mr-r:
        print(cnt)
        break
    if i == "R":
        r += 1
    else:
        w += 1
    cnt += 1

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
count = 0
ws = 0
rs = 0
for i in range(n):
  if 'W' == c[i]:
    ws += 1
  else:
    rs += 1
if ws > rs:
  print(rs)
else:
  for i in range(n//2 + 1):
    if 'W' == c[i]:
      count += 1
  print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
ans = 0
fs = 0
ls = N - 1
for _ in range(N):
    for i, ci in enumerate(c[fs:ls]):
        if ci == 'W':
            break
        fs += 1
    for i , ci in enumerate(c[fs:ls:-1]):
        if ci == 'R':
            break
        ls -= 1
    if fs >= ls:
        break
    ans += 1
    fs += 1
    ls -= 1
    #print(c[fs:ls] + 1)
    if len(c[fs:ls + 1]) <= 1:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np
# sympy as syp(素因数分解とか)
Mod = 1000000007
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1] 
for i in range(2, 10**5 + 1):
    fact.append((fact[-1] * i) % Mod)
    inv.append((-inv[Mod % i] * (Mod // i)) % Mod)
    factinv.append((factinv[-1] * inv[-1]) % Mod)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % p
    
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res
 
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)
def pow_k(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 main():  #startline-------------------------------------------
    n = int(input())
    s = input()
    print(min(s[:n//2].count("W"),s[n//2:].count("R")))
        
                
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

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

for i in range(n):
  if c[i]=='W':
    #print('-----when W in',i)
    s=c[i:]
    num=s.find('R')
    if num<0:
      break
    num+=i
    #print('when num ',num)
    t=c[num:]
    num_ch=t.find('W')
    if num_ch<0:
      num_ch=n-num   
    #print('',num_ch)
    num_ch+=num
    
    #print('when num_ch',num_ch)
    #print('間',num-i)
    #print('赤連続数',num_ch-num)
    if num-i<=num_ch-num:
      #print('間が小さいので塗り替える')
      for j in range(i,num):
        #print(j)
        c=c[:j]+'R'+c[j+1:]
        count+=1
        
        
    else:
      #print('間が大きいので交換する')
      for j in range(num,num_ch):
        #print(j)
        c=c[:j]+'W'+c[j+1:]
        c=c[:i+j-num]+'R'+c[i+j-num+1:]
        count+=1

print(count)

Submission status: Wrong Answer

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

counter = 0
k = 0

i = 0
j = n-1

while i <= j:
	if c[i] == 'W' and c[j] == 'R':
		counter += 1
		i += 1
		j -= 1
	else:
		j -= 1
print(counter)

Submission status: Wrong Answer

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

# from numba import njit

# input = stdin.readline

# @njit
def solve(n,s):
  flag = False
  whiteCount = 0
  for i in range(n-1,-1,-1):
    if s[i] == "R":
      flag = True
    if s[i] == "W" and flag:
      whiteCount += 1
  res = 0
  for i in range(n):
    if s[n-i-1] == "W" and i+1 > whiteCount:
      res += 1
  
  return res



def main():
  N = int(input())
  s = input()
  print(solve(N,s))
  return

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
N = int(input())
tmpN = N
C = input()
if N % 2 == 1:
  N -= 1
  N = int(N / 2)
  result = C.count('R',N)
  if C[N+1] == 'R' and C[N] == 'R':
    result += 1
else:
  N = int(N / 2)
  result = C.count('R',N)

if C.count('R') == tmpN:
  print('0')
else:
  print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
red = []
white = []
for i in range(n):
  if s[i] == 'W':
    white.append(i)
for i in range(n - 1, -1, -1):
  if s[i] == 'R':
    red.append(i)
#print(white, red)
if min(len(red), len(white)) == 0:
  print(0)
  exit()

for i in range(min(len(red), len(white))):
  if white[i] >= red[i]:
    print(i)
    exit()

Submission status: Wrong Answer

Solution code:
import sys


def resolve(in_):
    n = int(next(in_))
    c = next(in_).strip()

    c = c.lstrip('R')
    c = c.rstrip('W')
    if c.count('R') == 0 or c.count('W') == 0:
        return 0
    # print(f'{c=}')

    a, b = c[:len(c) // 2], c[len(c) // 2 + 1:]
    # while len(a) and len(b) and  a.count('W') == 0:
        # print(f'{a=}')
        # print(f'{b=}')
        # a, b = b[:len(b) // 2 + 1], b[len(b) // 2 + 1:]
    # print(f'{a=}')
    # print(f'{b=}')

    wc = a.count('W')
    rc = b.count('R')
    # print(f'{wc=} {rc=}')

    return max(wc, rc)


def main():
    answer = resolve(sys.stdin)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    c = input()
    w_l_cnt = c.find('R')
    w_cnt = c.count('W')
    r_cnt = c.count('R')
    if w_l_cnt == 0:
        print(0)
    elif r_cnt == 0:
        print(w_l_cnt)
    elif w_cnt - w_l_cnt < r_cnt -1:
        print(w_cnt)
    elif w_l_cnt >= r_cnt:
        print(w_l_cnt)
    elif w_l_cnt < r_cnt:
        print(r_cnt - 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

N=int(input())
c=list(input())
wcnt=0
rcnt=0
wcng=0
rcng=0
both=0
ans=0


for i in range(len(c)):
    if c[i]=="R" and wcnt==0:
        pass
    elif c[i]=="R" and wcnt!=0:
        rcnt+=1
    elif c[i]=="W" and rcnt!=0:
        if wcnt == rcnt:
            both+=wcnt
        elif wcnt > rcnt:
            rcng+=rcnt
        else:
            wcng+=wcnt
        wcnt=0
        rcnt=0
        wcnt+=1
    elif c[i]=="W" and rcnt==0:
        wcnt+=1
if rcnt != 0:
    if wcnt == rcnt:
        both+=wcnt
    elif wcnt > rcnt:
        rcng+=rcnt
    else:
        wcng+=wcnt

if abs(wcng-rcng)>=both:
    ans=max(wcng,rcng)
else:
    ans=max(wcng,rcng)+math.ceil((both-abs(wcng-rcng))/2)
print(ans)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
C = input()
wLen = len(C.replace('R', ''))
rLen = len(C.replace('W', ''))
if wLen == 0 or rLen == 0:
  print(0)

else:
  tmp = C
  tmpRes = min(wLen, rLen)
  for i in range(tmpRes):
    tmp = re.sub('^R*', '', tmp)
    tmp = re.sub('W*$', '', tmp)
    tmpLen = len(tmp) - max(len(re.sub('^W*', '', tmp)), len(re.sub('R*$', '', tmp)))
    tmp = tmp[int(tmpLen): int(len(tmp) - tmpLen)]
    if len(tmp) == 0:
      tmpRes = i + 1
      break
  print(tmpRes)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np
# sympy as syp(素因数分解とか)
Mod = 1000000007
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1] 
for i in range(2, 10**5 + 1):
    fact.append((fact[-1] * i) % Mod)
    inv.append((-inv[Mod % i] * (Mod // i)) % Mod)
    factinv.append((factinv[-1] * inv[-1]) % Mod)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % p
    
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res
 
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)
def pow_k(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 main():  #startline-------------------------------------------
    n = int(input())
    s = input()
    print(min(s[:n//2].count("W"),s[n//2+1:].count("R")))
        
                
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

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

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

print(w, r)

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

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
C = input()
red_count = C.count('R')
white_count = C.count('W')
left_base = min(red_count, white_count)
right_base = max(red_count, white_count)
left_not_red_count = copy.deepcopy(C)[:left_base].count('W')
left_not_white_count = copy.deepcopy(C)[:right_base].count('R')
right_not_red_count = copy.deepcopy(C)[left_base:].count('W')
right_not_white_count = copy.deepcopy(C)[right_base:].count('R')

# 右基準か左基準か決める
# →どちらが、差分が少ないか
# 揃える
# 斑点が必要な場合は斑点する

asd = ''
for i in range(white_count):
    asd +='W'
for sdd in range(red_count):
    asd +='R'
if(left_not_red_count > right_not_red_count - 1):
    # 右揃え
    print(right_not_red_count)

else:
    # 左揃え
    print(left_not_red_count + 1)

Submission status: Wrong Answer

Solution code:
N=int(input())
C = input()
def find_left_W(p):
  np = C[:p+1].rfind('R')
  count = len(C[:p+1]) - np-1
  return(np,count)

def find_right_R(p):
  count=0
  while p<len(C):
    if C[p]=='R':
      count+=1
      p += 1
    else:
      break
  return(p,count)
count = 0
while True:
  p = C.find('WR')
  if p==-1:
    break
  else:
    l_p,l_c = find_left_W(p)
    r_p,r_c = find_right_R(p+1)
    count+=min(l_c,r_c)
    C = C[:l_p+1]+C[r_p:]

print(count)

Submission status: Wrong Answer

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

# 最も右にあるR < 最も左にあるW

if len(set(c)) == 1:
    print(0)
    exit()

# 右からみて初めてRの位置
idx_R = n - 1
i = n - 1
while True:
    if c[i] == 'R':
        idx_R = i
        break
    i -= 1

# 左から見て初めてのWの位置
idx_W = 0
i = 0
while True:
    if c[i] == 'W':
        idx_W = i
        break
    i += 1

if idx_R < idx_W:
    print(0)
    exit()

res = 0
if idx_R > idx_W:
    for i in range(n):
        if c[i] == 'R':
            res += 1
    print(min(res, n - res))
    exit()

# しゃくとり
left = 0
right = n - 1
cnt = 0
while left < right:
    while c[left] == 'R': # W まで進める
        left += 1
    while c[right] == 'W': # R まで進める
        right -= 1
    cnt += 1
    c[left] = 'R'
    c[right] = 'W'
    left += 1
    right -= 1

print(cnt)

Submission status: Wrong Answer

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

wi = c.find('W')
ri = c.rfind('R')

if wi == -1 or ri == -1:
    print(0)
else:
    ans = 0
    while wi < ri:
        wi = c[wi:].find('W') + wi + 1
        ri = c[:ri].rfind('R')
        ans += 1

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import copy
N = int(input())
imp = list(str(input()))
tprev = copy.copy(imp)
t = ''
count = 0
def swap(sprev):
    s = sprev
    for i in range(len(s)):
        if s[i] == 'W':
            st = i
            break
    for j in range(len(s)-1,0,-1):
        if s[j] == 'R':
            en = j
            break
    if i < j:
        s[i] = 'R'
        s[j] = 'W'
        return s
    else:
        return s
    
tx = ''.join(t)
tprevx = ''.join(tprev)

while tx != tprevx:
    ref = copy.copy(tprev)
    t = swap(tprev)
    count += 1
    tx = ''.join(t)
    tprevx = ''.join(ref)
    #print(tx,tprevx)
    # if tx == tprevx:
    #     print(count)
    #     sys.exit()
        
    

print(count-1)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
str = input()
A = list(str)

X = math.floor(N/2)
count = 0
for i in range(0,X+1):
    if A[i] == "R":
        count = count +1
    else:
        pass
print (X-count)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(input())
num1 = l.count('R')
num2 = 0
i = 0
A = True
for i in range(num1):
    if l[i] != 'R':
        A = False
if num1 == 0 or A == True:
    print(0)
else:
    while l[i] != 'R':
        num2 += 1
        i += 1
    if num2 <= num1 - 1:
        print(num1 - 1)
    else:
        print(num1)

Submission status: Wrong Answer

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

def search(cs):
    todo = []
    for i, c in enumerate(cs):
        if c=="R":
            if (i-1)!=-1:
                if cs[i-1] =="W":
                    todo.append(i)
    return todo

def replace(sentence, num, word):
    return sentence[:num]+word+sentence[num+1:]

todo = search(cs)
if len(todo)==0:
    print(0)
else:
    count = 0
    while len(todo)!=0:
        arg = todo[-1]
        m = re.match(r".*W.*", cs[:arg])
        cs = replace(cs, arg, "W")
        count+=1
        if not m is None:
            tmp = replace(cs, m.start(), "R")
            renew_todo = search(tmp)
            if len(renew_todo)<len(todo):
                renew_todo = todo
                todo = renew_todo
                cs = tmp
        else:
            todo = search(cs)
    print(count)

Submission status: Wrong Answer

Solution code:

n_stone = int(input())
stones = list(input())

target_idx_list = []
n_process = 0

for stone_idx in range(n_stone - 1):
  if (stones[stone_idx] == 'W') and (stones[stone_idx + 1] == 'R'):
    target_idx_list.append(stone_idx)
    
while len(target_idx_list) > 0:
  target_idx = target_idx_list[0]
  
  n_process += 1
  
  if ((target_idx == 0) or stones[target_idx - 1] == 'R') and (len(target_idx_list) >= 2):
    target_idx_list.remove(target_idx)
    
    swap_flg = False
    for another_idx in target_idx_list:
      swap_idx = another_idx + 1
      if swap_idx == (n_stone - 1):
        target_idx_list.remove(another_idx)
        swap_flg = True
        break
      
      elif stones[swap_idx + 1] == 'W':
        stones[target_idx] = 'R'
        stones[swap_idx] = 'W'
        target_idx_list.remove(another_idx)
        swap_flg = True
        break

    if not swap_flg:
      target_idx_list.remove(another_idx)
      stones[swap_idx] = 'W'
      target_idx_list.append(swap_idx)
      target_idx_list.append(another_idx)
		
  else:
    target_idx_list.remove(target_idx)
    
    return_idx = target_idx + 1
    stones[return_idx] = 'W'
    
    if (return_idx != n_stone - 1) and stones[return_idx + 1] == 'R':
      target_idx_list.append(return_idx)
    
print(n_process)

Submission status: Wrong Answer

Solution code:
import math

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

    target = 'WR'
    index = -1
    cnt=0
    
    test=N//2

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


    Rf=C1.count('R')
    Rr=C2.count('R')
    Wf=C1.count('W')
    Wr=C2.count('W')


    cnt+=max(min(Wr,Rf),min(Wf,Rr))

    if(Rr==0 and Centor=='R'):
        cnt+=1
    if(Wr==0 and Centor=='W'):
        cnt+=1

    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()
wc = c.count('W')
rc = c.count('R')
print(min(c[:rc+1].count('W'), c[wc:].count('R'), n - rc, n-wc))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
red = []
white = []
for i in range(n):
  if s[i] == 'W':
    white.append(i)
for i in range(n - 1, -1, -1):
  if s[i] == 'R':
    red.append(i)
print(white, red)
if min(len(red), len(white)) == 0:
  print(0)
  exit()

for i in range(min(len(red), len(white))):
  if white[i] >= red[i]:
    print(i)
    exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
d = [0] * N
for i in range(N):
    if c[i] == 'R':
        d[i] = 1
count = 0
v = [0] * N
k = 1
l = -1
for i in range(0, N):
    if d[i] == 0:
        v[i] = k
        k += 1
for j in reversed(range(0, N)):
    if d[j] == 1:
        v[j] = l
        l -= 1
for i in range(0, N):
    if v[i] > 0:
        count += 1
    if v[i] < 0:
        if i == 0:
            for i in range(1,N):
                if d[i] == 1:
                    break
                if i == N-1:
                    print(0)
        elif v[i-1] >= -v[i]:
            break
print(count)

Submission status: Wrong Answer

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

ans = 0

color = [C[0]]
c = [0]
for i in range(len(C)):
    if C[i] == color[-1]:
        c[-1] += 1

    else:
        color.append(C[i])
        c.append(1)

idx_w = 0
idx_r = len(c) - 1
while True:
    if color[idx_w] != 'W':
        idx_w += 1
    if color[idx_r] != 'R':
        idx_r -= 1

    if idx_w == len(c) or idx_r == 0:
        break
    # print(idx_w)

    ans += min(c[idx_r], c[idx_w])
    if c[idx_w] < c[idx_r]:
        c[idx_r] -= c[idx_w]
        c[idx_w] = 0
        idx_w +=1
    else:
        c[idx_w] -= c[idx_r]
        c[idx_r] = 0
        idx_r -=1

    if idx_w + 1 >= idx_r or idx_w == len(c) or idx_r == 0:
        break

for i in c:
    ans += i

print(ans)

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
        cnt = 0
        for i in range(len(c)):
            if c[i] == 'W':
                w1 += 1
            elif c[i] == 'R':
                cnt += 1
            if cnt == -(-x//2):
                break

        return max(cnt, w1)
print(solve())

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.rfind('WR')
        if rightmostW == -1:
            print(0)
        else:
            wnum = a[:rightmostW].count('W')
            rnum = a[rightmostW:].count('R')
            #print(wnum-rnum)
            print(max(wnum , rnum))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
n = int(input())
c = input()
s = Counter(c)
if s['W'] == 0 or s['R'] == 0 or c.index('R') == 0 and c.index('W') == s['R']:
  print('0')
  sys.exit()
r = s['R']
las_pos=0
count = 0
for i, v in enumerate(c):
  if v == 'R':
    if i - las_pos <= r:
      r = r - (i - las_pos + 1)
      count += (i - las_pos)
      las_pos = i
    else:
      count += r
      break
  if r == 0:
    break
print(count)

Submission status: Wrong Answer

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

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

rl = N - wl
    
l = 0
if N % 2 == 0:
  for i in range(N//2):
    if S[i] == "W":
      l += 1
else:
  for i in range(N//2+1):
    if S[i] == "W":
      l += 1

print(min([wl, rl, l, rl-(wl-l)]))

Submission status: Wrong Answer

Solution code:
a = int(input())
b = input()
b.count('R')
c = b.find('R')
d =b[c+1:].count('R')
if b.count('W') == 0:
    print(0)
elif b.find('R') < b.find('W'):
    print(0)
elif (a - b.find('R'))==b.count('R') and b.count('R') == b.count('W'):
    print(b.find('R'))
else:
    print(c + (d-c))

Submission status: Wrong Answer

Solution code:
N=int(input())
c=input()
mid=N//2
if N%2==0:
  rl=c[mid:].count('R')
  wf=c[:mid].count('W')
else:
  rl=c[mid:].count('R')
  wf=c[:mid+1].count('W')
  
print(min(rl,wf))

Submission status: Wrong Answer

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

if n % 2 == 0:
  half = int(n / 2)
else:
  half = int((n-1) / 2)
pre_w_num = 0
for i in range(half):
  if c[i] == "W":
    pre_w_num = pre_w_num + 1
post_r_num = 0
for j in range(half):
  if c[-1 *(j+1)] == "R":
    post_r_num = post_r_num +1
if pre_w_num <= post_r_num:
  ans = pre_w_num
else:
  ans = post_r_num
print(ans)