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())
mozi = input()
mozi2 = list(mozi)
a = len(mozi2)
num_w = mozi2.count("W"); num_r = mozi2.count("R")
list_w2 = mozi2[(a-num_w+1):]; list_w1 = mozi2[:(a-num_w+1)]
list_r2 = mozi2[(a-num_r+1):]; list_r1 = mozi2[:(a-num_r+1)]
if num_w >= num_r:
    if "W" in list_w2:
        print(list_w1.count("W"))
    else:
        print(num_w)
else:
    if "R" in list_r1:
        print(list_r2.count("R"))
    else:
        print(num_r)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
h = (n + 1) // 2
print(min(c[:h].count('W'), c[h:].count('R')))

Submission status: Wrong Answer

Solution code:
import collections
def main():
    N = int(input())
    S = list(input())
    if len(set(S))==1:
        print(0)
        return
    for i in range(N):
        # R is fine
        if S[i] == 'W':
            break
    S = S[i:]
    # print(S)
    cnt = 0
    left = 0
    right = len(S)-1
    while left<right:
        if S[left] == 'W':
            cnt +=1
            left += 1
            right -=1
        else:
            # S[left] == 'R'
            left += 1
            if S[left] == 'W':
                right -= 1
    print(cnt)
    return 0
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
C=input()
from collections import Counter
count=Counter(C)
#print(count)
countR=count["R"]
countW=count["W"]
missR=0
missW=0
for i in range(N):
    now=C[i]
    if now=="W" and i<N-countW:
        missW+=1
    if now=="R" and i>=N-countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)

ans=min(missW, missR)+abs(missR-missW)

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
    r = c.count("R")
    tmp = 0
    for i, ci in enumerate(c):
        if ci == "W":
            w += 1
        else:
            r -= 1
            tmp += 1
        if w >= r:
            print(i + 1 - tmp)
            exit()

    print(w)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in range(n):
    if s[i] == "R":
        red.append(i)
    else:
        white.append(i)

roop_min_num = min(len(red), len(white))

res = 0
for i in range(roop_min_num):
    if white[i] < red[-i]:
        res += 1
print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=str(input())
Wh=0
Red=0
for i in range(len(c)):
    if c[i] == 'W':
        Wh+=1
    else:
        Red+=1
s=min(Wh,Red)
if Wh==Red and n<=4:
    print(min(Wh,Red))
elif Wh==Red and n>3:
    print(s-1)
else: 
    print(min(Wh,Red))

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
na = len(c)
red = 0
whi = 0

cou_start = 0
cou = 0
for i in reversed(range(0,na)):
    if c[i] == "W":
        whi += 1
        if cou_start == 1:
            cou += 1
    else:
        red += 1
        
    #print(red,whi,cou)
    
    if red >= 2 and whi >= 1:
        cou_start=1
        

pri = min(whi,red)
if cou != 0:
	pri = min(cou,pri)
  
print(pri)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

J=0
ans=0
for i in range(N):
    if C[i]=="W":
        for j in range(J,N):
            if i+j>N:break
            if C[N-1-j]=="R":
                ans+=1
                #print(i,N-1-j)
                J=j+1
                break

    if i+J>N:break
    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import groupby
def input(): return sys.stdin.readline().rstrip()
 
def main():
    n=int(input())
    c=list(input())
    ans=0
    white=0
    for key, value in groupby(c):
        if key=='W':
            white=len(list(value))
        else:
            ans+=min(white,len(list(value)))
    print(ans)   
 
 
 
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
if s.count('R')==0 or s.count('W')==0:
    print(0)
else:
    a= s.count('W')
    w=s.index('W')
    b= s[w:].count('R')
    if b==0:
        print(0)
    else:
        r = s[w:].index('R')
        if r+1<b:
            b-=1
        print(min(b,a))

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()
    sublist = []
    keep = 0
    ans = 0
    if s.count("WR") == 0:
        print(ans)
        exit()
    for i in range(n-1):
        if s[i] == "R":
            if s[i + 1] == "W":
                sublist.append(s[keep: i + 1])
                keep = i + 1
    sublist.append(s[keep:])
    for st in sublist:
        tmp = st.replace("WR", "P")
        subans = min(tmp.count("W"), tmp.count("R"))
        ans += subans + 1
    print(ans)
        
                
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
if N%2 == 0:
    S1 = S[0:N//2]
    S2 = S[N//2:N]
    R = S1.count("W")
    W = S2.count("R")
    print(min(R,W))
else:
    S1 = S[0:N//2]
    S2 = S[N//2:N]
    R = S1.count("W")
    W = S2.count("R")
    if S[N//2] == "W":
        print(min(R,W)+1)
    else:
        print(min(R,W))

Submission status: Wrong Answer

Solution code:
def main():
    N = input()
    CS = [c for c in input().strip()]

    wtotal = 0
    rtotal = 0
    for c in CS:
        if c == "W":
            wtotal += 1
        elif c == "R":
            rtotal += 1
        else:
            raise Exception()

    upper_bound = min(wtotal, rtotal)

    left = -1
    right = len(CS)

    op = 0
    w = 0
    r = 0
    while True:
        if w > r:
            right -= 1
            if CS[right] == "R":
                r += 1
        else:
            left += 1
            if CS[left] == "W":
                w += 1

        if w > 0 and r > 0:
            op += 1
            w -= 1
            r -= 1

        if right == left or right == 0 or left == len(CS) - 1:
            break

    change = op + w + r
    print(min(upper_bound, change))


main()

Submission status: Wrong Answer

Solution code:
n = int(input())
line = list(input())
if 'W' not in line or 'R' not in line:
    print(0)
else:
    ans = min(line.count('W'),line.count('R'))
    for i in range(n):
        if line[i] == 'R':
            w = line[:i].count('W')
            r = line[i+1:].count('R')
            if ans <= min(ans,abs(w-r)+min(w,r)):
                ans = min(ans,abs(w-r)+min(w,r))
                break
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

c = list(input())

w_count = c.count("W")
r_count = c.count("R")

right_center = c[w_count:]
right_w_count = right_center.count("W")

if w_count >= r_count:
    print(w_count)
else:
    print(w_count - right_w_count)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = str(input())
# print(list(c))

import collections
a = collections.Counter(c)
t = a["W"]
# print(a)
m = min(a["W"],a["R"])
# print(m)

co = 0

for i,j in enumerate(c):
    if co >=m or i>=t:
        break
    else:

        if j == "W":
            co +=1
        # print(i,j)
        
# print(m,co)
print(min(m,co))

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
tmp = 0
ans = 0
count = 0
if(N % 2 == 0):
    tmp = N - (N//2) + 1
else:
    tmp = N - (N//2)
for i in range(N):
    if(c[i] == "R"):
        count = i
for i in range(min(tmp,count)):
    if(c[i] == "W"):
        ans += 1
print(str(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    cs = list(input())
    left_w = 0
    right_w = 0
    left_r = 0
    right_r = 0
    ans = 0
    for i in range(n):
        if i < n//2:
            if cs[i] == "W":
                left_w += 1
            else:
                left_r += 1
        else:
            if cs[i] == "R":
                right_r += 1
            else:
                right_w += 1
    if left_w == 0:
        print(0)
        exit()
    if right_r >= left_w:
        ans += left_w
        left_r += left_w
        right_r -= left_w
        right_w += left_w
        left_w = 0
    ans += right_r
    right_w += right_r
    right_r = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
i = 0
j = n - 1
while i < n and s[i] == 'R':
	i += 1
while j >= 0 and s[j] == 'W':
	j -= 1
t = s[i:j + 1]
out = 0
i = 0
j = len(t) - 1
while j - i > 0:
	if t[i] == 'R' and t[j] == 'W':
		i += 1
		j -= 1
	elif t[i] == 'W' and t[j] == 'R':
		out += 1
		i += 1
		j -= 1
	elif t[i] == 'R' and t[j] == 'R':
		out += 1
		j -= 1
	else:
		out += 1
		i += 1
	#print(i, j)
print(out)

Submission status: Wrong Answer

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

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

left_r = s[:k//2].count('R')
left_w = (k//2) - left_r

right_r = s[k//2:].count('R')
right_w = (k//2) - right_r

res = min(left_w, right_r)

if right_r and not left_w and (k % 2 and s[k//2] == 'W'):res+=1

print(res)

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
        
        return max(x-1, w1)
print(solve())

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(input())
arr = [1 if i == "R" else 0 for i in arr]
rarr = list(reversed(arr))
a = sum(arr)
if a == 0 or a == n:
    ans = 0
else:
    widx = arr.index(0)
    ridx = n - rarr.index(1)
    arr = arr[widx:ridx]
    rsum = sum(arr)
    ws = arr[:-rsum].count(0)
    rs = arr[-rsum:].count(1)
    ans = max(ws,rs)

print(ans)

Submission status: Wrong Answer

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

rw_after=sorted(rw)
print(rw_after)

count=0
for i in range(N):
    if rw[i] != rw_after[i]:
        count+=1
print(int(count/2))

Submission status: Wrong Answer

Solution code:
n = int(input())

c = list(input())

w_count = c.count("W")
r_count = c.count("R")

right_center = c[w_count:]
right_w_count = right_center.count("W")

print(w_count - right_w_count)

Submission status: Wrong Answer

Solution code:
N = int(input())
input_list = list(input())
cnt_R = []
cnt_W = []

for i in range(N // 2):
    if input_list[i] == 'W':
        cnt_W.append(i)

for i in range(N // 2, N):
    if input_list[i] == 'R':
        cnt_R.append(i)
if len(cnt_W) == 0 or len(cnt_R) == 0:
    cnt = 0
elif len(cnt_W) <= len(cnt_R):
    cnt = len(cnt_R)
else:
    cnt = len(cnt_W)

print(cnt)

Submission status: Wrong Answer

Solution code:
import collections
n=int(input())
cnt=0
lst=list(str(input()))
y=collections.Counter(lst[:(n//2)])
Ry=y["R"]
Wy=y["W"]
z=collections.Counter(lst[(n//2):])
Rz=z["R"]
Wz=z["W"]
cnt+=min(Rz, Wy)
print(cnt)

Submission status: Wrong Answer

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

w_idx = -1
r_idx = -1

ans = 0

while left <= N // 2 - 1 and right >= (N + 1) // 2:
    if w_idx == -1 and c[left] == "W":
        w_idx = left
    if w_idx == -1 and c[left] == "R":
        left += 1

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s1 = input()
s2 = s1[::-1]
i1 = 0
i2 = 0
ans = 0
while i1 < n:
  if s1[i1]=='W':
    while i2 < n:
      if s2[i2]=='R':
        ans += 1
        i2 += 1
        break
      elif i1+i2 < n-2:
        break
      else:
        i2 += 1
  i1 += 1

print(ans)

Submission status: Wrong Answer

Solution code:
# D Alter Altar

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

num_R = C.count("R")

space = 0
for c in C:
  if c == "W":
    space += 1
  elif c == "R":
    break

if num_R <= space:
  print(num_R)
else:
  print(num_R - 1)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n=int(input())
cli = list(input())

cli_count = Counter(cli)

white = cli_count['W']
red = cli_count['R']

count = 0
for c in cli:
    if c == 'W':
        count += 1
        red -= 1
    else:
        red -= 1

    if red == 0:
        break

print(count)

Submission status: Wrong Answer

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

if len(c) % 2 == 0:
    w_count = c[:len(c)//2].count("W")
    r_count = c[len(c)//2:].count("R")
else:
    w_count = c[:len(c)//2 + 1].count("W")
    r_count = c[len(c)//2:].count("R")
    
if (w_count == 0)|(r_count==0):
    print(0)
else:
    print(w_count) if w_count < r_count else print(r_count)

Submission status: Wrong Answer

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


l = 0
r = N - 1

cnt = 0
while l < r:
    while l < r and C[l] == 'R':
        l += 1

    while l < r and C[r] == 'W':
        r -= 1

    cnt += 1
    l += 1
    r -= 1


d = Counter(C)
min_ = min(d['W'], d['R'])
print(min(min_, cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input()
a = A.count('W')
b = A.count('R')
c = 'R'*a+'W'*b
n = 0
m = 0
for i in range(N):
  if A[i] != c[i]:
    if A[i] == 'W':
      n+=1
    if A[i] == 'R':
      m+=1
e = min(n,m)+abs(n-m)
print(min(a,b,e))

Submission status: Wrong Answer

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n=k()
s=X()
p=[0]+[1 if i=="W" else 0 for i in s]
w = list(itertools.accumulate(p))

for i in range(1,n):
    if p[i]==0:
        cnt=i
        break

if w[cnt-1]==w[-1]:
    print(w[-1])
else:
    print(w[-1]-w[cnt-1])

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
p,q = 0,n-1
x = 0
while(p<q):
    while(c[p] == 'R' and p < n-1):
        p += 1
    while(c[q] == 'W' and q > 0):
        q -= 1
    if(p < n-1 and q > 0):
        p += 1
        q -= 1
        x += 1
    else:
        break
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
c = list(input())
wcount = 0
hand = 0
rcount = c.count("R")
if rcount == 0:
    print(0)
    exit()
if rcount == n:
    print(0)
    exit()
rlist = deque()
for i in range(n):
    if c[i] == "R":
        rlist.append(i)
for i in range(n):
    if c[i] == "W":
        wcount += 1
    else:
        if wcount <= rcount:
            rcount -=1
            hand += wcount
            for j in range(wcount):
                c[rlist.pop()] = "W"
            rcount -= wcount
            wcount =0
            if rcount == 0:
                break
        else:
            hand += rcount+1
            break
print(hand)

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
S=input()
#N=1000000
#S='WR'*500000
cnt_R = 0
cnt_R_list = []
for i in range(N-1,-1,-1):
  if S[i] == 'R':
    cnt_R += 1
  else:
    cnt_R_list.append(cnt_R)
cnt_R_list.reverse()
for i in range(len(cnt_R_list)):
  if i+1 == cnt_R_list[i]:
    print(i+1)
    sys.exit()
if cnt_R_list == []:
  print(0)
elif cnt_R_list[0]==0:
  print(0)
else:
  print(len(cnt_R_list))

Submission status: Wrong Answer

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


half = int(n/2)
def get_ans(half):
    w_num = c_array.count('W')
    r_num = n-w_num
    wl_num = c_array[:half].count('W')
    rr_num = c_array[half:].count('R')

    if w_num == 0 or r_num == 0:
        ans = 0
    else:
        ans = min(wl_num, rr_num) + abs(wl_num-rr_num)
    return ans

if n % 2 == 0:
    ans = get_ans(half)
else:
    ans = min(get_ans(half), get_ans(half+1))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=str(input())
start=0
end=N-1
count=0
flag=0
while flag==0 and start<N:
    if S[start]=="W":
        for i in range(end,start,-1):
            if i==start+1:
                flag=1
            if S[i]=="R":
                end=i-1
                start+=1
                break
        count+=1
    else:
        start+=1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = list(input())
pre = c[0:N//2]
suf = c[N//2: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:
n = int(input())
arr = list(input())
arr = [1 if i == "R" else 0 for i in arr]
rarr = list(reversed(arr))
a = sum(arr)
if a == 0 or a == n:
    ans = 0
else:
    widx = arr.index(0)
    ridx = n - rarr.index(1)
    arr = arr[widx:ridx]
    rsum = sum(arr)
    ws = arr[:-rsum].count(0)
    rs = arr[rsum:].count(1)
    ans = max(ws,rs)

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n=int(input())
cnt=0
lst=list(str(input()))
y=collections.Counter(lst[:(n//2)])
Ry=y["R"]
Wy=y["W"]
z=collections.Counter(lst[(n//2):])
Rz=z["R"]
Wz=z["W"]
if Rz>Ry:
  cnt+=min(Rz, Wy)
print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
S= list(input())
W_count=S.count('W')
R_count=S.count('R')
if((W_count==0) or (R_count==0)):
    print(0)
else:
    if(W_count==R_count):
        print(R_count-S[:len(S)//2].count('R'))
    else:
        if(len(S)%2==0):
            print(S[:len(S)//2].count('R')-S[:len(S)//2].count('W'))
        else:
            print(S[:len(S)//2+1].count('R')-S[:len(S)//2+1].count('W'))

Submission status: Wrong Answer

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

from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


N = int(input())
s = list(input())

num_w = 0
for i in range(N):
    if s[i] == 'W':
        num_w += 1
num_r = N - num_w
# print(num_w, num_r)


def count_r_after(delimiter):
    count = 0
    if delimiter == 0:
        return count
    for i in range(delimiter, N):
        if s[i] == 'R':
            count += 1
    return count


# print(count_r_after(num_r-1))

ans = min(num_w, num_r, count_r_after(num_r-1))
print(ans)

Submission status: Wrong Answer

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

N = int(input())
stones = input()


def count_WR(stones):
    return stones.count('WR')


def solve(stones):
    if count_WR(stones) == 0:
        return 0

    count = 0

    # 左端のWをRにする
    if stones[0] == 'W':
        stones = 'R' + stones[1:]
        count += 1

    count += count_WR(stones)

    return count


print(solve(stones))

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
r = 0
w = 0
l = 0
ans = 0
for i in range(n):
    if c[i] == "W":
        w += 1
    else:
        r += 1
        if i < n / 2:
            l += 1
if r == 0 or w == 0:
    print(0)
    exit()
elif w < r:
    ans += r - w
    r = w

ans += r - l
if ans <= w:
    print(ans)
else:
    print(w)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
s = rr()
cnt = 0
i = 0
j = n-1
while j >= i:
    while i <= n-1 and s[i] == 'R':
        i += 1
    while j >= 0 and s[j] == 'W':
        j -= 1
    if i >= n-1 or j <= 0:
        break
    cnt += 1
    i += 1
    j -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N = int(input())
S = list(input())
W = [0]*N
R = [0]*N
ans = N

for i in range(N):
    if S[i] == "W":
        W[i] = 1
    else:
        R[i] = 1
R.reverse()
R = list(accumulate(R))
W = list(accumulate(W))
if R[0] == 0 or W[N-1] == 0:
    ans = 0
else:
    sakai = R[N-1]
    ans = max(W[sakai-1], R[N-sakai-1])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
c = list(input())
wcount = 0
hand = 0
rcount = c.count("R")-1
if rcount+1 == 0:
    print(0)
    exit()
if rcount + 1 == n:
    print(0)
    exit()
rlist = deque()
for i in range(n):
    if c[i] == "R":
        rlist.append(i)
for i in range(n):
    if c[i] == "W":
        wcount += 1
    else:
        if wcount <= rcount:
            hand += wcount
            for j in range(wcount):
                c[rlist.pop()] = "W"
            rcount -= wcount
            wcount =0
            if rcount == 0:
                break
        else:
            hand += rcount+1
            break
print(hand)

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'

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

    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:
n = int(input())
stones = list(input())
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
goal = 'R' * w_num + 'W' * r_num

index = 0
poplist = list()
for i in stones:
    if i == goal[index]:
        poplist.append(index)
    index += 1
for i in sorted(poplist, reverse=True):
    stones.pop(i)
w_num = 0
r_num = 0
for i in stones:
    if i == 'W':
        w_num += 1
    else:
        r_num += 1
print(min(w_num, r_num))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    C = input()
    i = 0
    j = N - 1
    ANS = 0
    if C.find('W') == -1 or C.find('R') == -1:
        print(0)
        return
    while 1 == 1:
        i = C.find('W')
        j = C.rfind('R')
        if i >= j:
            break
        if i + 1 == j:
            ANS += 1
            break
        C = C[i+1:j]
        ANS += 1
    print(ANS)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [_ for _ in input()]

l, r = 0, n-1
if not('R' in s):
    print(0)
    exit()

if not('W' in s):
    print('0')
    exit()

ans = 0
while 1:
    while s[l] == 'R':
        l += 1

    while s[r] == 'W' and l <= r:
        r -= 1

    s[l] = 'r'
    s[r] = 'w'
    l += 1
    r -= 1

    # print(s)
    if l > r:
        if l == r:
            ans += 1
        break
    # print(l, r)
    ans += 1
print(ans)
# print(s)

Submission status: Wrong Answer

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

ans = 0
w = 0
r = 0
for i in range(N // 2):
    if C[i] == "W":
        if r > 0:
            r -= 1
            ans += 1
        else:
            w += 1
    j = N - i - 1
    if C[j] == "R":
        if w > 0:
            w -= 1
            ans += 1
        else:
            r += 1
ans += w
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
if s.count('R')==0 or s.count('W')==0:
    print(0)
else:
    w=s.index('W')
    i=w
    while(i<len(s) and s[i]=='W'):
        i+=1
    a= i-w+1
    if i==len(s):
        print(0)
    else:
        b=s[i:].count('R')
        if a<b:
            b-=1
        print(min(s.count("W"),b))

Submission status: Wrong Answer

Solution code:
# Problem D - Alter Altar
# input
N = int(input())
c_list = list(input())

# initialization
red_count = 0
white_count = 0
swap_count = 0

# count
for c in c_list:
    if c=="R":
        red_count += 1
    else:
        white_count += 1

# swap count
w_i = 0
r_i = N-1
while r_i>w_i:
    if c_list[r_i]=="R":
        # white search
        while r_i>w_i and w_i<=N-1:
            if c_list[w_i]=="W":
                break
            w_i += 1
        # swap
        tmp = c_list[r_i]
        c_list[r_i] = c_list[w_i]
        c_list[w_i] = tmp
        swap_count += 1
    else:
        r_i -= 1

# output
ans = min(red_count, white_count, swap_count)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
s = rr()
cnt = 0
i = 0
j = n-1
while j >= i:
    while i <= n-1 and s[i] == 'R':
        i += 1
    while j >= 0 and s[j] == 'W':
        j -= 1
    if i > n-1 or j < 0:
        break
    cnt += 1
    i += 1
    j -= 1
print(cnt)

Submission status: Wrong Answer

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

cnt1 = 0
for v in c[: n // 2 + 1]:
    if v == "W":
        cnt1 += 1
print(cnt1)

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('R')
        leftmostR = a.rfind('R')
        wnum2 = a[:leftmostR+1].count('W')
        ri = a.find('WR')
        wnum3 = a[ri+2:].count('R')
        r = min(wnum, wnum2,wnum3)
        print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N=int(input())
    c=input()
    #x...WR
    #o...RR, RW, WW
    #Wが来たらそれ以降はW
    #R(0個以上)+W(0個以上)
    
    i=0
    for _ in range(max(c.count('R'),c.count('W'))):
        if 'WR' in c:
            x=c.index('WR')  
            y=N-c[::-1].index('R')-1
            #print(x,y)
            c=c[:x]+'R'+c[x+1:y]+'W'+c[y+1:]
            #print(c)
            i+=1
        else:
            break
            
    print(min(c.count('R'),c.count('W'),i))
    

if __name__ == '__main__':
    main()