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())
S = list(input())
count = 0
T = sorted(list(S))
T.sort(reverse=True)

for i in range(N):
  if S[i] == T[i]:
    count += 1
    
print(int(count/2))

Submission status: Wrong Answer

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

#色を揃えるパターン
#全部赤
ans1 = 0
for i in range(len(c)):
    if c[-(i+1)] == "R":
        break
dum = c[0:(n-i)]
ans1 = min(dum.count("R"),dum.count("W"))
ans2 = 0
right = 0
for i in range(len(c)):
    if i >= (n-right-1):
        break
    if c[i] == "W":
        for j in range(right,len(c)):
            if c[-(j+1)] == "R":
                right = j+1
                ans2 += 1
                break
print(min(ans1,ans2))

Submission status: Wrong Answer

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

while c.count('WR')>=1:
  w = c.index('W')
  r = c.index('WR')+1
  cl = list(c)
  cl[w] = 'R'
  cl[r] = 'W'
  c = ''.join(cl)
  cnt += 1

wr = min(c.count('W'),c.count('R'))
cnt = min(cnt,wr)
print(cnt)

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)
if r<w:
    print(0)
    exit(0)
c = C[w:r+1]
center = c.count('R')-1
wc = c.count('W')
rc = c.count('R')
if wc==rc:
    print(c[0:center+1].count('W'))
elif wc>rc:
    print(c[0:center+1].count('W'))
elif wc<rc:
    print(c[center:-1].count('R'))

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)
if r<w:
    print(0)
    exit(0)
c = C[w:r+1]
center = c.count('R')-1
wc = c.count('W')
rc = c.count('R')
if wc==rc:
    print(c[0:center+1].count('W'))
elif wc>rc:
    print(c[0:center+1].count('W'))
elif wc<rc:
    print(c[center+1:-1].count('R'))

Submission status: Wrong Answer

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

while c.count('WR')>=1:
  w = c.index('W')
  r = c.index('WR')+1
  cl = list(c)
  cl[w] = 'R'
  cl[r] = 'W'
  c = ''.join(cl)
  cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = input()
W = 0
R = 0

for x in c:
  if x=='R':
    R += 1
    
ans = R
for x in c:
  if c=='W':
    W += 1
  else:
    R -= 1
    
  ans = min(ans,max(R,W))
  
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')-1
wc = c.count('W')
rc = c.count('R')
if wc==rc:
    print(c[0:center+1].count('W'))
elif wc>rc:
    print(c[0:center+1].count('W'))
else:
    print(c[center+1:-1].count('R'))

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"
    for j in range (i):
        if s[j]=="W" :
            fw=j 
            break

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

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
C=input()
C=C.replace('R','0')
C=C.replace('W','1')

s2 = []
for s in C:
    s2.append(int(s))
s2_sort = sorted(s2)
cnt=0
for index,i in enumerate(s2):
    if s2 == s2_sort:
        exit()
    if i == 1:
        i2=0
        for j in reversed(s2):
            i2+=1
            if j == 0:
                print(len(s2)-i2)
                temp=s2[len(s2)-i2]
                s2[len(s2)-i2]=s2[i]
                s2[i]=temp
                cnt+=1
                break
print(cnt)

Submission status: Wrong Answer

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
for i in range(len(red_index_list)-1):
    red_index = red_index_list[i]
    if i==0:
        space = red_index
    else:
        space += red_index - red_index_list[i-1] - 1

    n_right_red = len(red_index_list[i+1:])
    diff = n_right_red - space
    if diff >= 0:
        toku += 1
    else:
        break

print(n_red-toku)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
c = list(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
        c[left_f] = 'R'
        c[right_f] = 'W'
        left_f += 1
        right_f -= 1
    if left_f == right_f or left_f + 1 == right_f or left_f == right_f + 1:
        break
index = N - 1
while 1:
    if c[index] == 'W':
        index -= 1
        if index == 0:
            count += N
            break
    else:
        index += 1
        count += c[:index].count('W')
        break
print(count)

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) and(pre_w_num != 0):
  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:
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()
    r, w = 0, 0
    if N % 2 != 0: N+=1
    for n in range(N // 2):
        if C[n] == 'W':
            w += 1
    C = C[::-1]
    for n in range(N // 2):
        if C[n] == 'R':
            r += 1
    cnt = collections.Counter(C)
    print(min(r, w, cnt['R']))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
#print(c)


num=0
for i in reversed(range(N)):
    if c[i]=='R' :
        #print('Y')
        for u in (range(num,i)):
            if c[u]=='W':
                c[u]='y'
                c[i]='W'
                num+=1
                break
            break
                
            
#print(c)
print(num)

Submission status: Wrong Answer

Solution code:
k = int(input())

m = 7%k
ans = 1
jud = True
p = 70%k
for i in range(k):
    m = (m + p)%k
    ans += 1
    if m == 0:
        break
    p = p*10%k
    if p == 0:
        jud = False
        break
if jud:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
#print(c)


num=0
for i in reversed(range(N)):
    if c[i]=='R' :
        print('Y')
        for u in (range(num,i)):
            if c[u]=='W':
                c[u]='y'
                c[i]='W'
                num+=1
                break
            break
                
            
#print(c)
print(num)

Submission status: Wrong Answer

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

r = c.count("R")
cnt = 0
for i, j in enumerate(c):
    if i + 1 == r:
        break
    if j == "W":
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
for i, red_index in enumerate(red_index_list):
    if i==0:
        space = red_index
    else:
        space = red_index - red_index_list[i-1] - 1

    n_right_red = len(red_index_list[i+1:])
    diff = n_right_red - space
    if diff >= 0:
        toku += 1
        del red_index_list[:-space]

        if red_index_list[-1] == red_index:
            break
    else:
        break

print(n_red-toku)

Submission status: Wrong Answer

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

r_num = c.count("R")

for i in range(n):
    if c[i] == "W":
        break

print(max(c[i:].count("R")-c[:r_num].count("R"), 0))

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:
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()
    r, w = 0, 0
    if N % 2 != 0: N+=1
    for n in range(N // 2):
        if C[n] == 'W':
            w += 1
    C = C[::-1]
    for n in range(N // 2):
        if C[n] == 'R':
            r += 1
    print(min(r, w))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(input())
x = a.count('W')
ans = 0

for i in range(x):
    if a[i] == 'W':
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
#print(c)


num=0
for i in reversed(range(N)):
    if c[i]=='R' :
        for u in (range(num,i)):
            if c[u]=='W':
                c[u]='y'
                c[i]='W'
                num+=1
                break
        break
                
            
#print(c)
print(num)

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] == 'R':
    ans = ans +1
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n//2):
    if a[i] == 'W':
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
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
flag=0
while(count<=N//2):
  
  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:
    break
    
  else:
    list_data[last_R]='W'
    list_data[first_W]='R'
    count +=1
    
print(count+1)

Submission status: Wrong Answer

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
for i, red_index in enumerate(red_index_list):
    if i==0:
        space = red_index
    else:
        space = red_index - red_index_list[i-1] - 1

    n_right_red = len(red_index_list[i+1:])
    diff = n_right_red - space
    if diff >= 0:
        toku += 1
        for _ in range(n_right_red):
            red_index_list.pop(-1)

        if red_index_list[-1] == red_index:
            break
    else:
        break

print(n_red-toku)

Submission status: Wrong Answer

Solution code:


N = int(input())
red_index_list = []
n_red = 0
c_list = list(input())
for i in range(N):
    if (c_list[i] == 'R'):
        red_index_list.append(i)
        n_red += 1

toku = 0
for i, red_index in enumerate(red_index_list):
    if i==0:
        space = red_index
    else:
        space = red_index - red_index_list[i-1] - 1

    n_right_red = len(red_index_list[i+1:])
    diff = n_right_red - space
    if diff >= 0:
        toku += 1
        for _ in range(diff):
            red_index_list.pop(-1)

        if red_index_list[-1] == red_index:
            break
    else:
        break

print(n_red-toku)

Submission status: Wrong Answer

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# import numpy as np


mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

try:
    # sys.setrecursionlimit(int(pow(10,6)))
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("../output.txt", "w")
except:
    pass


# @lru_cache(None)


n=L()[0]
s=list(input())
r=s.count("R")
i=0
j=n-1
count=0
while(i<r):
    while(i<r and s[i]=="R"):
        i+=1
    while(j>=r and s[i]=="W"):
        j-=1
    try:
        s[i],s[j]=s[j],s[i]
    except:
        continue
    count+=1
    i+=1
    j-=1
print(count)

Submission status: Wrong Answer

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

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

sys.setrecursionlimit(10000000)

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

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

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

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


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

    N = ii()
    C = iss()
    r, w = 0, 0

    for n in range(N // 2):
        if C[n] == 'W':
            w += 1
    C = C[::-1]
    for n in range(N // 2):
        if C[n] == 'R':
            r += 1
    print(min(r, w))


if __name__ == '__main__':
    main()

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] == 'R':
    ans = ans +1
print(ans-1)

Submission status: Wrong Answer

Solution code:
import sys
import math

n = 8
c = list('WRWRRWWR')
ans = 0
left, right = 0, n-1

while(left < right):
    while(c[left] == 'R'):
        left += 1
        if left >= n:
            break
    while(c[right] == 'W'):
        right -= 1
        if right < 0:
            break
    if (left < right):
        c[left], c[right] = c[right], c[left]
        ans += 1
    else:
        break

print(ans)

Submission status: Wrong Answer

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

def change(string):
  result = 0
  while len(string) > 0:
    while "W" in string:
      if string.pop(0) == "W":
        break
    while("R" in string):
      if string.pop(-1) == "R":
        break
    result += 1
  return result


if string.count("R") == 0 or string.count("R") == len(string):
  print(0)
elif string.index("W") not in range(0, string.count("R")):
  print(0)
else:
  print(change(string))

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:
  print(c[half])
  if c[half] == 'R':
    ans = ans +1
print(ans-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:
  print(c[half])
  if c[half] == 'R':
    ans = ans +1
print(ans+1)

Submission status: Wrong Answer

Solution code:
k=int(input())
n=0
a=0
ss=0
if k%7==0:
  print(1)
elif k%2==0:
  print(-1)
elif k%5==0:
  print(-1)
else:
  while a==0 and n<=1000000:
    n+=1
    ss=10*(ss%k)+1
    if ss%k==0:
      a=1
  if a==1:
    print(n)
  else:
    print(-1)

Submission status: Wrong Answer

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

left = N - 1

count = 0

for right in range(N):
    if right == left:
        break

    if C[right] == 'W':
        while C[left] == 'W' and right <= left:
            left -= 1
        if C[left] == 'W':
            break
        count += 1
        left -= 1
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
stone=str(input())
count=0
for i in range(n-1):
    if  stone[i]=='W' and stone[i+1]=='R':
        count=count+1
        m=0
        while m<=i and stone[i-m]=='W':
            if stone[i-m-1]=='W':
                count=count+1
                m=m+1
            else:
                m=m+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 in c[fs:ls]:
        if i == 'W':
            break
        fs += 1
    for i  in c[fs:ls:-1]:
        if i == 'R':
            break
        ls -= 1
    if fs >= ls:
        break
    ans += 1
    fs += 1
    ls -= 1
    #print(c[fs:ls] + 1)
    if (ls - fs) < 1:
        break
print(ans)

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':
        if c[left_f:right_f].count('W') == len(c[left_f:right_f]):
            count += len(c[left_f:right_f])
            break
        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 = input()
ans = 0
fs = 0
ls = N - 1
for _ in range(N):
    for i in c[fs:ls]:
        if i == 'W':
            break
        fs += 1
    for i  in c[fs:ls:-1]:
        if i == 'R':
            break
        ls -= 1
    if fs >= ls:
        break
    ans += 1
    fs += 1
    ls -= 1
    #print(c[fs:ls] + 1)
    if (ls - fs) <= 1:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
stone=str(input())
count=0
for i in range(n-1):
    if  stone[i]=='W' and stone[i+1]=='R':
        count=count+1
        m=1
        while m<=i and stone[i-m]=='W':
            if stone[i-m-1]=='W':
                count=count+1
                m=m+1
            else:
                m=m+1
print(count)

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')-1
wc = c.count('W')
rc = c.count('R')
if wc==rc:
    print(c[0:center+1].count('W'))
elif wc>rc:
    print(c[0:center+1].count('W'))
else:
    print(c[center:-1].count('R'))

Submission status: Wrong Answer

Solution code:
import math
N= int(input())
c=list(str(input()))
#N=4
#c=['W', 'W', 'R', 'R']
#c.reverse()
print(c)


num=0
for i in reversed(range(N)):
    if c[i]=='R' :
        for u in (range(num,i)):
            if c[u]=='W':
                c[u]='y'
                c[i]='W'
                num+=1
                break
            
print(c)
print(num)

Submission status: Wrong Answer

Solution code:
# D Alter Altar

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

# Rの数
num_R = C.count("R")

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

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

# 動かす必要のないWの数
_C = C[::-1]
safe_W = 0
for _c in _C:
  if _c == "W":
    safe_W += 1
  else:
    break

# 右端のRの数
right_R = 0
sec_W = 0
frag = ""
for _c in _C:
  if _C == "R":
    right_R += 1
    frag = "right"
  elif frag == "right" and _C == "W":
    sec_W += 1
    frag = "sec"
  elif frag == "sec" and _C == "R":
    break
  else:
    break
  
#左端にRを固める作戦の回数
space = 0
for c in C:
  if c == "W":
    space += 1
  elif c == "R":
    break
if num_R <= space:
  num_left = min(len(C) - num_R - safe_W, num_R)
else:
  num_left = min(len(C) - num_R - safe_W, num_R - 1)
  if space >= right_R:
    num_left = min(num_left, len(C) - num_R - sec_W)

print(min(num_left, num_all))

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(rcnt):
        if c[i] == 'R':
            cnt += 1
    
    print(cnt)

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:
  print(c[half])
  if c[half] == 'R':
    ans = ans +1
print(ans)

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[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
    
for j in range (fw,i):
    if s[j]=="R":
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    c = input()
    # RRRRWWWWみたいな形にすれば良い
    # 色の塗替えよりも色の交換の方が2つの石を動かせるので良いはず
    # 一番右のRと一番左のWを入れ替え続けるのが最善手
    check_target = c
    for i in range(N):
        if "WR" not in check_target:
            print(i)
            break
        red_index = check_target.rfind("R")
        white_index = check_target.find("W")
        check_target = "R" + c[white_index+1:red_index] + "W"

resolve()

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()
if cl[n//2]=="W":
  d=Counter(cl[Windexes[swap]:])
else:
  d=Counter(cl[Windexes[swap]-1:])
ans=d["R"]
print(ans)

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()
if cl[n//2]=="W":
  d=Counter(cl[Windexes[swap]:])
else:
  d=Counter(cl[Windexes[swap-1]+1:])
ans=d["R"]
print(ans)

Submission status: Wrong Answer

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

a = []
    #print(c[1])
#print(c[0][1])
#for i in range()
#print(len(c[0]))
l = len(c[0])
for i in range(l):
    a.append(c[0][i])
#print(a)
#print(l//2)
l1 = l//2
cnt = 0
for i in range(l1, l):
    #num = a.Count('R')
    if a[i] == 'R':
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = str(input())
# c = [1 if i == 'R' else 0 for i in input()]

cnt = 0
while 'WR' in c:
    c = c.replace('WR', 'RR', 1)
    idx = c.rfind('R')
    c = c[:idx]
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = str(input())
# c = [1 if i == 'R' else 0 for i in input()]

cnt = 0
while 'WR' in c:
    c = c.replace('WR', 'RR', 1)
    idx = c.rfind('R')
    c = c[:idx] + 'W' + c[idx+1:]
    cnt += 1
print(cnt)

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)