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_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:
  print(stones)
  print(target_idx_list)
  target_idx = target_idx_list[0]
  
  if (target_idx == 0) or stones[target_idx - 1] == 'R':
    
    target_idx_list.remove(target_idx)
    n_process += 1
    
    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)
        n_process += 1
        break
      
      elif stones[swap_idx + 1] == 'W':
        target_idx_list.remove(another_idx)
        n_process += 1
        break
    
  else:
    target_idx_list.remove(target_idx)
    n_process += 1
    
    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
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(math.ceil(N/2)):
        if c[i]=="W":
            W_count+=1
        else:
            R_count+=1
    print(min(c.count("R"),c.count("W"),max(W_count,R_count)))

Submission status: Wrong Answer

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

i = 0
j = N-1
count = 0 
frag = True
Frag_f = True
Frag_b = True
while (frag):
    if i >= j:
        break
    if Frag_f == True:
        if C[i] == "R":
            i = i + 1
        else:
            Frag_f = False
    
    if Frag_b == True:
        if C[j] == "w":
            j = j - 1
        else:
            Frag_b = False
    
    if Frag_b == False and Frag_f == False:
        count += 1
        Frag_f = True
        Frag_b = True
        i = i+1
        j = j-1
print(count)

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(n)
            if n>=1 and R_n-i-1>=n:
                R_n-=n+1
                ans+=1
                # print(ans)
            else:
                ans+=R_n
                # print(ans)
                break
        print(ans)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#D - Alter Altar
N = int(input())
c = input()
W_r = 0
r = 0

if c[0]=="W" and c[1]=="W":
    W_r += 1
else:
    pass

for i in range(1,N):
    if c[i-1]=="W" and c[i]=="R":
        W_r += 1
        r += 1
    else:
        pass

if r == 0:
    W_r = 0

print(W_r)

Submission status: Wrong Answer

Solution code:
import collections
n=int(input())
cnt=0
lst=list(str(input()))
x=collections.Counter(lst)
R=x["R"]
W=x["W"]
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, n//2)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = list(input())
ans = 0
if c.count('W') == n or c.count('W') == 0:
    ans =0 
else:
    if n % 2 == 0:
        a = n //2
        c_for = c[:a]
        c_back = c[a:]
        count_for = 0
        count_back = 0
        for i in range(a):
            if c_for[i] == 'W':
                count_for += 1
            if c_back[-i-1] == 'R':
                count_back += 1
            if count_for > 0 and count_back > 0:
                count_for -= 1
                count_back -= 1
                ans +=1
        ans += count_for  
        ans += count_back
        
    else:
        a = n//2
        a = n //2
        c_for = c[:a]
        c_back = c[a+1:]
        count_for = 0
        count_back = 0
        for i in range(a):
            if c_for[i] == 'W':
                count_for += 1
            if c_back[-i-1] == 'R':
                count_back += 1
            if count_for > 0 and count_back > 0:
                count_for -= 1
                count_back -= 1
                ans +=1
        ans += count_for  
        ans += count_back
        if count_for >0 and c[a] == 'R':
            ans += 1
        if count_back>0 and c[a] == 'W':
            ans +=1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
white=0
red=0
W=list(str(input()))
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
if white==0 or red==0:
  print("0")
  exit()
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()
C = C[:len(C)//2]
print(C.count("W"))

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
cnt2 = 0
for v in c[n // 2 - 1 :]:
    if v == "R":
        cnt2 += 1
print(min(cnt1, cnt2))

Submission status: Wrong Answer

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

def getLeftR(c, currentIndex):
    for i in range(currentIndex+1, N):
        if c[i] == 'W':
            return i
    return -1

def swap(c, i, j):
    c[i], c[j] = c[j], c[i]
    return c

for i in range(N-1):
    if c[i] == 'W' and c[i+1] == 'R':
        leftWindex = getLeftR(c, i+1)
        if leftWindex > 0:
            c = swap(c, leftWindex, i+1)
            count += 1
        else:
            break
print(count)

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
for i in range(n):
  indexes = [j for j, x in enumerate(cl) if x == "R"]
  if cl[i]=="W":
    cl[indexes[-1]]="W"
    cl[i]="R"
    swap+=1
  else:
    break
  if swap>=n//2:
    break


d=Counter(cl[cl.index("W"):])
ans+=swap+d["R"]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
altar = input().rstrip()
alter = 0
for i in range(len(altar)-1):
  if altar[i]=='W' and altar[i+1]=='R':	alter+=1
print(alter)

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)
l = len(c)//2

print(min(c[:l].count('W'),c[l:].count('R')))

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<countW:
        missW+=1
    if now=="R" and i>=N-countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)
if X!=0:
    ans=min(missW, missR)+abs(missR-missW)
else:
    ans=0
print(ans)

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()
    C = iss()
    cnt = collections.Counter(C)
    if cnt['R'] == 0 or cnt['W'] == 0:
        print(0)
        exit()

    w, b, ret = 0, 0, 0
    for n in range(N):
        if C[n] == 'W':
            w += 1
        if C[n] == 'R':
            if w == 0:
                continue
            elif w == 1:
                w = 0
                ret += 1
                b += 1
            elif b > 0:
                b = 0
                continue
            else:
                ret += 1
                w -= 1
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
c = list(input())
left = 0
cnt = 0
right = len(c) - 1
while left < n and right >=0 and left < right:
    while left < n and c[left] != 'W':
        left += 1
    while right >= 0 and c[right] != 'R':
        right -= 1
    if left < n and right >= 0:
        c[left], c[right] = c[right], c[left]
        cnt += 1
        left += 1
        right -= 1
print(cnt)

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:
            print(b-1)
        else:
            print(b)

Submission status: Wrong Answer

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


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


def readint():
    return int(input())


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


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


def main():
    _ = readint()
    c = readstr()
    ans = 0
    wc = 0
    rc = 0
    for s in c:
        if s == 'W':
            if rc:
                rc = 0
                wc = 0
            wc += 1
        elif s == 'R':
            rc += 1
            if wc:
                wc -= 1
                ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import math
n = int(input())
color = list(input())

i = 0
j = n-1
count = 0

while(i<j):
  found1 = 0
  found2 = 0
  for k in range(n-1-i):
    if color[i+k] == 'W':
      i = i+k+1
      found1 = 1
      break
  for k in range(j):
    if color[j-k] == 'R':
      j = j-k-1
      found2 = 1
      break

  if found1*found2 == 0:
    break
  else:
    count += 1
      

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
c=str(input())
cnt = 0
for i in range(N):
    if c[i] == "R":
        break
    else:
        cnt += 1
x = c.count("R")
#print(x)
if cnt == 0:
    print(0)
elif cnt >= x:
    print(x)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
ans = 0
right = n-1
for i in range(n//2+1):
    if s[i] == "W":
        while right >= i:
            if s[right] == "R":
                s[right] = "W"
                s[i] = "R"
                ans += 1
                right -= 1
                break
            else:
                right -= 1


print(ans)

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()
w=[0]+[1 if i=="W" else 0 for i in s]
w = list(itertools.accumulate(w))

for i in range(n):
    if w[i]==w[i+1] and w[i]!=w[-1]:
        cnt=w[i]
        break

print(w[-1]-cnt)

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 = pre_cnt and suf_cnt

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
c=list(input())
print(c)
i=0
count1=0
count2=0
for i in c:
    if i=="R":
        count1+=1
for j in range(count1):
    if c[j]=="R":
        count2+=1
print(count1-count2)

Submission status: Wrong Answer

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

num_w = c.count('W')

ans = 'R' * (N-num_w) + 'W' * num_w
cnt = 0
for i in range(N//2):
    if c[i] != ans[i]:
        cnt+=1
print(cnt)

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<countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)
if X!=0:
    ans=min(missW, missR)+abs(missR-missW)
else:
    ans=0
print(ans)

Submission status: Wrong Answer

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


cnt = 0
for i in range(n):
  w_idx = c.find("W")
  r_idx = c.rfind("R")
  if w_idx < r_idx:
    c = c[w_idx+1:r_idx]
    cnt += 1

print(cnt)

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, count_r_after(num_r-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

c=input()
c=input()

tmp=collections.Counter(c)

count_r=0
count_w=0
ans=0
dis_r=0

for i in c:
    if dis_r == tmp["R"]:
        break
        
    elif i == "W":
        if count_r<=tmp["R"]-count_r:
            ans+=1
            dis_r+=1
        else:
            ans+=1
    else:
        count_r+=1
        dis_r+=1
print(ans)

Submission status: Wrong Answer

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

print(max(c[:n//2].count("W"), c[n//2:].count("R")))

Submission status: Wrong Answer

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

w = []
r = []
for i in range(n):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)

w_len = len(w)
r_len = len(r)
if w_len == 0 or r_len == 0:
  print(0)
  exit(0)

if w_len < r_len:
  search = w_len
else:
  search = r_len

w_min = w[0]
r_max = r[r_len-1]

count = 0
for i in range(search):
  tmp = w[i]
  w[i] = r[r_len-i-1]
  r[r_len-i-1] = tmp
  count += 1
  
  if w_len > count and (r_len-count-1) >= 0:
    w_min = w[count]
    r_max = r[r_len - count - 1]
  else:
    w_min = w[w_len-1]
    r_max = r[0]
    
  
  if r_max < w_min:
    print(count)
    exit(0)
print(count)

Submission status: Wrong Answer

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


rcnt = c.count('R')
wcnt = c.count('W')

ans = 0
if set(c) == {'R'} or set(c) == {'W'}:
    print(0)
else:
    cnt = 0
    for i in range(n//2):
        if c[i] == 'R':
            cnt += 1
    
    print(rcnt - cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(input())
cr = al.count('R')
cw = al.count('W')
if cr == 0 or cw == 0:
    print(0)
    exit()


def ceil(a, b):
    return -(-a // b)


ans = 0
cr = cr // 2 + 1
for a in al[::-1]:
    if a == 'W':
        cr -= 1
        ans += 1
    if cr == 0:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
mozi = input()
mozi2 = list(mozi)
a = len(mozi2)
num_w = mozi2.count("W")
list_w = mozi2[(a-num_w+1):]
list_r = mozi2[:(a-num_w+1)]
if "W" in list_w:
    print(list_r.count("W"))
else:
    print(num_w)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
c = list(input())
C = collections.Counter(c)
ans = 0

if C['W'] == C['R']:
    C_1 = collections.Counter(c[:n//2])
    ans = max(C_1['W'],C_1['R'])
else:
    ans = min(C['W'],C['R'])

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

    for _ in range(n):
        if c[0] == 'R':
            c = c[-1:0:-1]
            c = c[::-1]
            x -= 1
        else:
            break
    w1 = 0
    for i in range(len(c)):
        if c[i] == 'W':
            w1 += 1
        elif c[i] == 'A':
            continue
        else:
            break
    
    return max(x-1, w1)
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
c = input()
d = deque()
a = 0; b = 0
for i in range(n):
    d.append(c[i])
for _ in range(len(d)):
    if d[0] == "R":
        d.popleft()
    else:
        break
for _ in range(len(d)):
    if d[-1] == "W":
        d.pop()
    else:
        break
for i in range((len(d)+1)//2):
    if d[-i-1] == "R": a += 1
for i in range((len(d)+1)//2):
    if d[i] == "W": b += 1
print(min(a, b))

Submission status: Wrong Answer

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

ans = 0
W = -1
for i in range(N):
    if S[i] == "W":
        W = i
        break
if W == -1:
    print(ans)
    sys.exit()

R = N
for i in range(N-1,-1,-1):
    if S[i] == "R":
        R = i
        break
if R == N:
    print(ans)
    sys.exit()
ans += 1

while W < R:
    flagW = False
    flagR = False
    for i in range(W+1,R):
        if S[i] == "W":
            W = i
            flagW = True
            break
    else:
        print(ans)
        sys.exit()
    
    for j in range(R-1, W, -1):
        if S[j] == "R":
            R =j
            flagR = True
            break
    else:
        print(ans)
        sys.exit()
    if flagW and flagR:
        ans += 1
        continue
    else:
        print(ans)
        sys.exit()

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()

L = Counter(c[:N//2])
R = Counter(c[N//2:])

ct = min(L["W"], R["R"])
    
A = Counter(c)
print(min(A["W"], A["R"], ct))

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] == "W"):
        count += 1
if(count == N):
    print("0")
    exit()
for i in range(tmp):
    if(c[i] == "W"):
        ans += 1
print(str(ans))

Submission status: Wrong Answer

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

w = 0
r = 0
for c in C:
    if c == 'W':
        w += 1
    else:
        r += 1

left = 0
right = N-1
ans = 0
while left < right:
    if C[right] == 'R':
        while left <= right and C[left] == 'R':
            left += 1
            r -= 1
        if left <= right and C[left] == 'W':
            ans += 1
            right -= 1
            left += 1
            r -= 1
    else:
        right -= 1
        

print(ans + r)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(input())
# 白くするコスト
wc=[0]
# 赤くするコスト
rc=[0]

import itertools
a=['R']+a

if a[-1]=='R':
    a.append('W')
else:
    a.append('R')

cntr=0
cntw=0
flag1=True
for i in range(n+2):
    if a[i]=='W':
        cntr+=1
        if flag1:
            flag1=False
        else:
            if cntw!=0:
                wc.append(cntw)
                cntw = 0
    else:
        if flag1:

            continue
        else:
            cntw+=1
            if cntr!=0:
                rc.append(cntr)
                cntr = 0

wc=list(itertools.accumulate(wc))
rc=list(itertools.accumulate(rc))


ans=float('inf')
for i in range(len(wc)):
    ans=min(ans,wc[i]+rc[-1]-rc[i])
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 and right_w == 0:
        print(0)
        exit()
    if left_r == 0 and right_r == 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:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

n = inp()
s = input().strip()
l,r = 0,n-1
ct = 0
if s.count('W') in [0,n]:
    print(ct)
    exit()
while l < r:
    while s[l] == 'R':
        l += 1
        if l >= r:
            break
    while s[r] == 'W':
        r -= 1
        if l >= r:
            break
    ct += 1
    l += 1
    r -= 1
print(ct)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = input()
n = len(c)//2
ans = min(c.count('W',0,n), c.count('R',n,n*2))
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

n = int(input())
c = input()
cnt = 0
m = n

for i in range(n):
  if c[i:i+1] == 'W':
    
    for j in range(m):
      if c[-j-1:-j] =='R':
        d = copy.deepcopy(c)
        c = c.replace('W', 'R', 1)
        c = c[::-1]
        c = c.replace('R', 'W', 1)
        c = c[::-1]
        
        if c == d:
          break
        cnt += 1
        m = n - j
        break
        
        
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import Counter

class Levenshtein:

    def initArray(self,str1,str2):
        distance = []
        for i in range(len(str1)+1):
            distance.append([0]*(len(str2)+1))
            distance[i][0] = i
        for j in range(len(str2)+1):
            distance[0][j] = j
        return distance

    def editDist(self,str1,str2,distance):
        dist = [0]*3
        for i in range(1,len(str1)+1):
            for j in range(1,len(str2)+1):
                dist[0] = distance[i-1][j-1] if str1[i-1]==str2[j-1] else distance[i-1][j-1]+1
                dist[1] = distance[i][j-1]+1
                dist[2] = distance[i-1][j]+1
                distance[i][j]=min(dist)
        return distance[i][j]

    def __init__(self,str1,str2):
        self.str1 = str1
        self.str2 = str2
        Levenshtein.distance = self.initArray(str1,str2)
        Levenshtein.dist = self.editDist(str1,str2,Levenshtein.distance)

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

c = Counter(S)
S_target = 'R' * c['R'] + 'W' * c['W']
print(S_target)

leven = Levenshtein(S, S_target)
print(leven.dist // 2)

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'
    else:
        left += 1
        right -= 1
        continue

    left += 1
    right -= 1

    cnt += 1


print(cnt)

Submission status: Wrong Answer

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


half = int(n/2)

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)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
data = input()
intdata = []
ans = 0
for d in data:
  if d == "R":
    intdata.append(0)
  else:
    intdata.append(1)

sortdata = sorted(intdata)

if len(intdata) % 2 == 0:
  amari = 0
else:
  amari = 1

for i in range(int(len(intdata)/2) + amari):
  if intdata[i] != sortdata[i]:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
stones = list(input())
if not ('R' in stones and 'W' in stones):
    print('0')
    exit()
right = 0
left = N - 1
white_idx = -1
red_idx = -1
ans = 0
while right < left:
    while right < N and right < left:
        if stones[right] == 'W':
            white_idx = right
            break
        right += 1
    while 0 < left and right < left:
        if stones[left] == 'R':
            red_idx = left
            break
        left -= 1
    if 0 <= white_idx and 0 <= red_idx:
        ans += 1
    right += 1
    left -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = list(input())
flag = False
ans = 0
for i in range(n-1):
    if c[i] == "W" and c[i+1] == "R":
        flag = True
        break
if not flag:
    print(0)
    exit()
if c[0] == "W" and c[-1] == "R":
    ans += 1
    c[0], c[-1] = "R", "W"
    for i in range(n-1):
        if c[i] == "W" and c[i+1] == "R":
            ans += 1
    print(ans)
else:
    for i in range(n-1):
        if c[i] == "W" and c[i+1] == "R":
            ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
white=0
red=0
W=list(str(input()))
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
if white==0 or red==0:
  print("0")
  exit()
Z=min(white,red)
rest=abs(white-red)
Z=Z+rest
print(Z)

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<countW:
        missW+=1
    if now=="R" and i>countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)
if X!=0:
    ans=X+abs(missR-missW)
else:
    ans=0
print(ans)

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(N):
        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()

Submission status: Wrong Answer

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

# 色を変えるのは、１つの石のみで、遅い
# 入れ替える場合はは、赤と白が左と右にある場合２つの石に効果を上げられるのでよい
# まずは入れ替えを考え、これ以上入れ替えれない（効果が少ない時）色を変える
# 理想は、wwww,,,RRRRと並ぶこと

def is_ok(c):
  s = True
  # どこかにW->Rの変化がある場合、false
  for i, p in enumerate(c[:-1]):
    if p == 'W':
      if c[i+1] == 'R':
        s = False
        break
  return s

if is_ok(c):
  print(0)
  exit()

# W->RとなっているRの位置を全てチェックする
# pos = [False] * len(c)
#
# cnt = 0
# for i, p in enumerate(c[:-1]):
#   if p == 'W':
#     if c[i + 1] == 'R':
#       pos[i + 1] = True
#       cnt += 1
# print(pos)

# 数が偶数だと左右を入れ替える操作をN/2することでOK
# 数が奇数だと左右を入れ替えて操作をint(N/2)回、色をチェンジする操作を１回するだけでOK

# if cnt % 2 == 0:
#   print(cnt/2)
# else:
#   print(int(N/2)+1)

# WとRの数で、並べると最後の正解の並び方がわかり、それと最初との差分を取ることができるはず
Wn = len([x for x in c if x == 'W'])
Rn = len([x for x in c if x == 'R'])
# print(Wn, Rn)
c_r = 'R'*Wn+'W'*Rn
# print(c_r)
# print(c)

w_cnt = 0
for i in range(len(c)):
  if c_r[i] != c[i]:
    w_cnt += 1
# print(w_cnt)

# 間違いが偶数だとそのまま割り算で答え、奇数だと1を足した操作数
if w_cnt % 2 == 0:
  print(int(w_cnt/2))
else:
  print(int((w_cnt+1)/2))

Submission status: Wrong Answer

Solution code:
N=int(input())
c=str(input())
cnt = 0
for i in range(N):
    if c[i] == "R":
        break
    else:
        cnt += 1
x = c.count("R")
if cnt == 0:
    print(0)
elif cnt >= x:
    print(x)
else:
    print(x-1)

Submission status: Wrong Answer

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

print(c[:n//2].count("W"))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	C = inputStr()

	aida = []
	ai = 0
	yokei = 0
	flg = False
	for i,val in enumerate(C):
		ai += 1
		if val == "W":
			if flg == False:
				flg = True
		elif val == "R":
			if flg == False:
				aida.append(ai)
			else:
				yokei += 1
				aida.append(ai)

	if yokei == 0:
		print(0)
		sys.exit()

	ans = 0
	for i,val in enumerate(aida):
		if val <= N//2:
			continue
		ans += 1

	print(ans)
	#print(aida)
	#print(yokei)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
N=int(input())
c=input()
if N%2==0:
    print(max(c[:N//2].count('W'),c[N//2:].count('R')))
else:
    print((max(c[:(N-1)//2].count('W'),c[(N-1)//2:].count('R'))))