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: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

//.define
// #define FILE_IN_OUT
#define RET(_x)    return cout << (_x) << '\n', 0;
#define all(_obj)  (_obj).begin(), (_obj).end()
#define loop(_n)   for (int i = 0; i < (_n); ++i)
#define sz(_obj)   static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x)  std::cerr << (#_x) << " = " << (_x) << '\n' << std::flush;
#else
#define debug(_x)  {}
#endif
#define endl "\n"
template<typename P, typename Q>
P CeilDiv(P _dividend, Q _divider) {
    return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template<typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
    for (Tp current = _begin; current != _end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int n;
    string s;
    cin >> n >> s;
    int l = 0;
    int r = n - 1;
    int ans = 0;
    while (l < r) {
        while (l < n && s[l] == 'R') {
            ++l;
        }
        while (r >= 0 && s[r] == 'W') {
            --r;
        }
        if (l < r) {
            swap(s[l], s[r]);
            ++ans;
        }
    }
    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
result = max(a)
left = 1
right = result
med = left + (right - left) // 2
while left <= right:
    counter = sum([(x - 1) // med for x in a])
    if counter <= k:
        result = med
        right = med - 1
    else:
        left = med + 1
    med = left + (right - left) // 2
print(result)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = 0
r = n - 1
ans = 0
while l < r:
    while l < r && s[l] == 'R':
        l += 1
    while l < r && s[r] == 'W':
        r -= 1
    ans += 1
    l += 1
    r -= 1

print(ans)

Submission status: Runtime Error

Solution code:
read = sys.stdin.read
from collections import deque
def main():
    n = int(input())
    a = input()
    if not 'WR' in a:
        print(0)
        sys.exit()
    a = list(a)
    rnum = a.count('R')
    r = 0
    for i1 in range(rnum):
        r += a[i1] == 'W'
    for j1 in range(n - rnum):
        r += a[rnum + j1] == 'R'

    r = r // 2
    print(r)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
c = [0 if x=='W' else 1 for x in input() ]
cnt = 0
while 1 in c[c.index(0):]:
  c[-(c[::-1].index(1)+1)] = 0
  c[c.index(0)] = 1
  cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
from itertools import accumulate #list(accumulate(A))
import math

N, K = mi()
A = li()

if K == 0:
    print(max(A))
    exit()

left = 0
right = max(A)

def count(num):
    cnt = 0
    for i in range(N):
        cnt += math.ceil(A[i] / num)
    
    return cnt


while True:
    mid = (left + right) // 2
    tmp = count(mid)
    if tmp == K:
        break
    elif tmp > K:
        if left == mid:
            mid += 1
            break
        left = mid
    else:
        right = mid

if mid == 0:
    print(mid)
    exit()

right = mid
while True:
    tmp = count(mid)
    if tmp == K and tmp != count(mid-1):
        print(mid)
        break
    elif tmp == K:
        right = mid
    else:
        left = mid
    mid = (left + right) // 2

Submission status: Runtime Error

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: WRWRRRRRRRR
    print(c[center+1:-1].count('R'))

Submission status: Runtime Error

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 or 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: WRWRRRRRRRR
    print(c[center+1:-1].count('R'))

Submission status: Runtime Error

Solution code:
N = int(input())
C = input()
wLen = len(C.replace('R', ''))
rLen = len(C.replace('W', ''))
if wLen == 0 or rLen == 0:
  print(0)
else:
  C = C.replace('R', '0')
  C = C.replace('W', '1')
  tmp = int(C)
  res = min(wLen, rLen)
  for i in range(1, res):
    while tmp % 10 != 0:
      tmp /= 10
    if tmp == 0:
      res = i
      break
    if tmp == 10:
      res = i + 1
      break
    tmp = int(str(int(tmp))[1:-1])
  print(res)

Submission status: Runtime Error

Solution code:
n=int(input())
from collections import deque
q =deque(input())

ans = 0
while q:
    while q[0] == 'R':
        q.popleft()
    while q[-1] == 'W':
        q.pop()
    ans += 1
    q.popleft()
    q.pop()
print(ans)

Submission status: Runtime Error

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-1):
    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: Runtime Error

Solution code:
n = int(input())
c = input()
c = [char for char in c]

def op1(l, id1, id2):
    temp = l[id1]
    l[id1] = l[id2]
    l[id2] = temp
    return l

r_idx, w_idx = [], []
for j in range(n):
    if c[j] == "R":
        r_idx.append(j)
    elif c[j] == "W":
        w_idx.append(j)

ans = 0
for i in range(min(len(r_idx), len(w_idx))): # i is number of op1
    c_i = c[:]
    c_i = op1(c_i, w_idx[j], r_idx[-j - 1])
    ans[i] += 1
if "W" in "".join(c_i).rstrip("W"):
    ans[i] += "".join(c_i).rstrip("W").count("W")
print(min(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
c = [char for char in c]

def op1(l, id1, id2):
    temp = l[id1]
    l[id1] = l[id2]
    l[id2] = temp
    return l

r_idx, w_idx = [], []
for j in range(n):
    if c[j] == "R":
        r_idx.append(j)
    elif c[j] == "W":
        w_idx.append(j)

ans = 0
else:
    for i in range(min(len(r_idx), len(w_idx))): # i is number of op1
        c_i = c[:]
        c_i = op1(c_i, w_idx[j], r_idx[-j - 1])
        ans[i] += 1
    if "W" in "".join(c_i).rstrip("W"):
        ans[i] += "".join(c_i).rstrip("W").count("W")
    print(min(ans))

Submission status: Runtime Error

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
    else:
        index += 1
        break
count += c[:index].count('W')
print(count)

Submission status: Runtime Error

Solution code:
def func_rfindr(str):
    return str.rfind('R')

def func_lfindw(str, start):
    return str.find('W', start)

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    ans = ans + 1
    posrr = func_rfindr(C)
    poslw = func_lfindw(C, poslw)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
import math

n = int(sys.stdin.readline().rstrip("\n"))
c = list(sys.stdin.readline().rstrip("\n"))
ans = 0
left, right = 0, len(arr) - 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: Runtime Error

Solution code:
def func_rfindr(str, start):
    return str.rfind('R')

def func_lfindw(str, start):
    return str.find('W')

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    ans = ans + 1
    posrr = func_rfindr(C, posrr)
    poslw = func_lfindw(C, poslw)

print(ans)

Submission status: Runtime Error

Solution code:
def func_rfindr(str, start):
    return str.rfind('R')

def func_lfindw(str, start):
    return str.find('W', start)

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    ans = ans + 1
    posrr = func_rfindr(C, posrr)
    poslw = func_lfindw(C, poslw)

print(ans)

Submission status: Runtime Error

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]

    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[-1] == red_index:
            break
    else:
        break

print(n_red-toku)

Submission status: Runtime Error

Solution code:
def func_rfindr(str, start):
    return str.rfind('R', start)

def func_lfindw(str, start):
    return str.find('W', start)

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    ans = ans + 1
    posrr = func_rfindr(C, posrr)
    poslw = func_lfindw(C, poslw)

print(ans)

Submission status: Runtime Error

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

if n_red == 0:
    print(0)
    exit()

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]

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

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

print(n_red-toku)

Submission status: Runtime Error

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

if n_red == 0:
    print(0)
    exit()

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]

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

print(n_red-toku)

Submission status: Runtime Error

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

index = 0
if N % 2 == 0:
    index = N / 2
else:
    index = N // 2


lcount = c[0 : index + 1].count("W")
print(lcount)

Submission status: Runtime Error

Solution code:
import sys

def check(c):
    for j in range(len(c)-1, -1 , -1):
        if c[j] == "R":
            return j

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

c_set = set(c)
# 全てRは0
if len(c_set) == 1 and c_set == {'R'}:
    print(0)
    sys.exit()

cnt = 0
for i in range(N):
    try:
        first_W = c.index("W")
    except ValueError:
        print(cnt)
        sys.exit()

    last_R = check(c)
    # print(i,':',first_W,', ',last_R)
    
    if first_W < last_R:
        c.pop(first_W)
        c.pop(last_R-1)
        cnt += 1
        continue
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
C = list(input().strip())
count = 0
j = N - 1
for i in range(N):
  if C[i] == "W":
    if i < j:
      while True:
        if C[j] == "R":
          C[i], C[j] = C[j], C[i]
          count += 1
          break
        else:
          j -= 1
    else:
      break

print(count)

Submission status: Runtime Error

Solution code:
WHITE = 0
RED = 1
_ = input()
C = input()
s = []
for c in C:
  if c == "W":
    s.append(WHITE)
  else:
    s.append(RED)
    
def helper(s):
  if len(s) <= 1:
    return 0
  if s[0] == WHITE and s[-1] == RED:
    return 1 + helper(s[1:-1])
  if s[0] == RED:
    return helper(s[s.index(WHITE):])
  if s[-1] == WHITE:
    return helper(s[:-1])

print(helper(s))

Submission status: Runtime Error

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: Runtime Error

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
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 = (num_left, len(C) - num_R - sec_W)

print(min(num_left, num_all))

Submission status: Runtime Error

Solution code:
n= int(input())
s = input()
a = [str(c) for c in s]

z = [0, n-1]


def search(a,n,x,y):
  x1 = x
  y1 = y

  for i in range(n):
    x = x1 + i
    if a[x1 +i] == 'W':
      break
  for i in range(n):
    y = y1 -i
    if a[y1-i] == 'R':
      break
  return[x, y]

c = 0

while True:
  z = search(a, n, z[0],z[1])
  if z[0] > z[1]:
    break
  else:
    c +=1
    a[z[0]], a[z[1]] = a[z[1]], a[z[0]]
  
  
    
print(c)

Submission status: Runtime Error

Solution code:
ef 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: Runtime Error

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

basenum = len(s)-1
while s[-1] == "W":
    del s[-1]


num1 = 0
num2 = 0
for i in range(len(s)//2):
    if s[i]=="R":
        num1 += 1
        
    if s[basenum-i] == "R":
        num2 += 1

print(abs(num1-num2))

Submission status: Runtime Error

Solution code:
n=int(input())
l=[]
count=0
l.append(input().split())
for i in range(n):
  if(l[i]=='W' and l[i+1]=='R'):
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
r_cnt = 0
w_init_cnt = 0
is_init =True
for s in S:
    if s == 'R':
        is_init = False
        r_cnt += 1
    if is_init and s == 'W'
        w_init_cnt += 1
print(r_cnt-w_init_cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
C = list(input())
N1 = int(N/2)
C1 = C[0:N1]
C2 = C[N1:N]
count = 0
if ((C1.count('W') == 0 and C2.count('R') == 0) or
       (C2.count('W') == 0 and C1.count('R') == 0)):
    count = 0
elif (C1.count('W') == C2.count('R')):   #左右対象のとき
    count = C1.count('W')
elif(C1.count('R') == C2.count('W')):
    count = C.count('W')
elif(C1.count('W') == C2.count('W') and
       C2.count('R') == C1.count('R')):
    count = min(C.count('W'),C2.count('R'))
    count = min(count, C.count((R)))
else:
    if(C.count('W') > C.count('R')):
        count = C.count('R')
    else:
        count = C.count('W')
print(count)

Submission status: Runtime Error

Solution code:
def main():
    N=int(input())
    c=list(input())[::-1]
    
    #x...WR
    #o...RR, RW, WW
    #Wが来たらそれ以降はW
    #R(0個以上)+W(0個以上)
    
    res=0
    cR=c.count('R')
    cW=c.count('W')
    
    l,r=0,N-1
    while True:
        if c[l]=='W':
            if c[r]=='R':
                c[l],c[r]='R','W'
                res+=1
            else:
                r-=1
        else:
            l+=1
        
    print(min(cR,cW,res))
    
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
WHITE = 0
RED = 1
_ = input()
C = input()
s = []
for c in C:
  if c == "W":
    s.append(WHITE)
  else:
    s.append(RED)
    
def helper(s):
  if len(s) <= 1:
    return 0
  if s[0] == WHITE and s[-1] == RED:
    return 1 + helper(s[1:-1])
  if s[0] == RED:
    return helper(s[s.index(WHITE):])
  if s[-1] == WHITE:
    return helper(s[:s.rindex(RED)])

print(helper(s))

Submission status: Runtime Error

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

basenum = len(s)
while s[-1] == "W":
    del s[-1]


num1 = 0
num2 = 0
for i in range(len(s)//2):
    if s[i]=="R":
        num1 += 1
        
    if s[basenum-i] == "R":
        num2 += 1

print(abs(num1-num2))

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
if s not in "R":
    print(0)
    exit()
if s not in "W":
    print(0)
    exit(0)
cut=n//2
mae=[]
usiro=[]
for i in range(cut):
    mae.append(s[i])
for j in range(cut):
    usiro.append(s[-1-j])
cntm=0
cntu=0
for i in range(len(mae)):
    if mae[i]=="W":
        cntm+=1
for i in range(len(usiro)):
    if usiro[i]=="R":
        cntu+=1        
print(min(cntm,cntu))

Submission status: Runtime Error

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

index = 0
if N % 2 == 0:
    index = N / 2
else:
    index = N // 2


lcount = c[0 : index + 1].count("R")
rcount = c[index:].count("R")

if lcount < rcount:
    print(lcount)
else:
    print(rcount)

Submission status: Runtime Error

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

r_m = c.count('R')
w_m = c.count('W')
wr_m = c.count('WR')

print(min(r_m, w_m) - wr_m //2)
s = 0
if r_m >= w_m:
    for i in range(len(c) - 1):
    if c[i] == 'W' and c[i + 1] == 'R':
        c[i + 1] = 'W'
        s += 1

Submission status: Runtime Error

Solution code:
N = input()

import numpy as np

c_list = list(input())

import numpy as no

c_array = np.array(c_list)

if len(np.unique(c_array)) == 1:
  print(0)

else:
  w_ichi = np.where(c_array == 'W')
  r_ichi = np.where(c_array == 'R')
  
  w_min = np.min(w_ichi)
  r_max = np.max(r_ichi)
  
  n_c_array = carray[w_min:r_max+1]
  
  r = np.min(np.array([sum(n_c_array == 'R' ), sum(n_c_array == 'W') ])) -1
  
  print(r)

Submission status: Runtime Error

Solution code:
N=int(input())
data=list(input())
if "W" not in data or "R" not in data:
    print(0)
else:
    s=0
    while True:
        first_w_index=int(data.index("W"))
        data2=reversed(data)
        final_r_index=N-1-list(data2.index("R"))
        if first_w_index < final_r_index:
            data[first_w_index]="R"
            data[final_r_index]="W"
            s += 1
        else:
            print(s)
            break

Submission status: Runtime Error

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

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
C = readline().decode().rstrip()

w_idx = []
r_idx = []
for i, c in enumerate(C):
    if c == 'W':
        w_idx.append(i)
    else:
        r_idx.append(i)

m = r_idx[len(r_idx) // 2 - 1]

if (len(w_idx) == 0) | (len(r_idx) == 0):
    print(0)
    sys.exit()

cnt = 0
for w in w_idx:
    if w < m:
        cnt += 1

print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
c = deque(input())

cnt = 0
l = c.popleft()
r = c.pop()
while c:
    if l == 'R':
        l = c.popleft()
    if r == 'W':
        r = c.pop()
    if l == 'W' and r == 'R':
        cnt += 1
        l = c.popleft()
        if not c:
            break
        r = c.pop()

print(cnt)

Submission status: Runtime Error

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

left = 0
right = 0
Wcount = 0
Rcount = 0
for i in range(len(c)):
    while c[left] != 'W':
        left += 1
    while c[len(c)-1-right] != 'R':
        right += 1
    if (left + right) >= len(c):
        break
    Wcount += 1
    Rcount += 1
    left += 1
    right += 1

print(Wcount)

Submission status: Runtime Error

Solution code:
n = int(input())
C = input()
c_R = 0
c_W = 0
for c in C:
    if c == "R": c_R += 1
    else: c_W += 1
ans = min(c_R, c_W)


if C[0] + C[1] == "WR":
    C[2:].find("WR") != -1:
    C = list(C)
    C[0] = "R"
    C[C[2:].find("WR") + 1] = "W"
    C = "".join(C)
    
ind1 = C.find("RWR")
if ind1 != -1:
    ind2 = C[ind1 + 2:].find("WR")
else:
    ind2 = -1


while ((ind1 != -1) & (ind2 != -1)):
    C = list(C)
    C[ind1 + 1] = "R"
    C[ind2 + 1] = "W"
    C = "".join(C)

    ind1 = C.find("RWR")
    if ind1 != -1:
        ind2 = C[ind1 + 2:].find("WR")
    else:
        ind2 = -1


    


c_R = 0
c_W = 0
for c in C:
    if c == "R": c_R += 1
    else: c_W += 1
ans = min(ans,c_R, c_W)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if n_red == 0:
    print(0)
    exit()

toku = 0
for i in range(1, n_red):
    current_index = red_index_list[i]
    if current_index != 0:
        before_index = red_index_list[i-1]
        space = current_index - before_index - 1
    else:
        space = current_index

    if (space == 0):
        toku += 1
    else:
        while True:
            if current_index == red_index_list[-1]:
                print(n_red - toku)
                exit()

            red_index_list.pop(-1)
            space -= 1
            if (space == 0):
                toku += 1
                break

print(n_red-toku)

Submission status: Runtime Error

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

count = 0
a = 0
b = len(c)-1

while a < b:
  if c[a] == "W":
    while c[b] != "R":
      b -= 1

    if c[a] == "W" and c[b] == "R":
      count += 1
      a += 1
      b -= 1
  else:
    a += 1
  
    
print(count)

Submission status: Runtime Error

Solution code:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    C = input()

    counter = dict()
    for c in C:
        counter[c] = counter.get(c, 0) + 1
    r = counter[R] 

    _counter = dict()
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter.get(R, 0)

    ans = _r
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
data=list(input())
if "W" not in data or "R" not in data:
    print(0)
else:
    s=0
    while True:
        first_w_index=int(data.index("W"))
        data2=reversed(data)
        final_r_index=N-1-int(data2.index("R"))
        if first_w_index < final_r_index:
            data[first_w_index]="R"
            data[final_r_index]="W"
            s += 1
        else:
            print(s)
            break

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# X = int(input())
# if X >= 30:
#     print('Yes')
# else:
#     print('No')

# import numpy

# N, D = map(int, input().split())
# X = [0] * N
# Y = [0] * N
# count = 0
# for i in range(N):
#     X[i], Y[i] = map(int, input().split())
#     if numpy.sqrt(X[i]**2 + Y[i]**2) <= D:
#         count += 1
# print(count)

# K = int(input())
# if K % 2 == 0 or K % 5 == 0:
#     print('-1')
# else:
#     num = 0
#     for i in range(10**7):
#         num = num * 10 + 7
#         if K > num:
#             continue
#         elif num % K == 0:
#             print(i+1)
#             break

def change(c, white, red, i, j):
    temp = c[i]
    c[i] = c[j]
    c[j] = temp
    return c

def reload(c):
    white = []
    red = []
    for i in range(len(c)):
        if c[i] == 'W':
            white.append(i)
        elif c[i] == 'R':
            red.append(i)
    return [white, red]

N = int(input())
c = list(str(input()))
white, red = reload(c)
i = 0
while True:
    if min(white) < max(red):
        c = change(c, white, red, min(white), max(red))
        i += 1
        white, red = reload(c)
        input()
    else:
        break
print(i)

Submission status: Runtime Error

Solution code:
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter.get(R, 0)

    ans = _r
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(str(input()))
i= 0
j =N -1
count = 0
while(i<j):
	while (S[i]!= 'W'):
		i +=1
	while (S[j]!= 'R'):
		j -=1
	count += 1
	i +=1
	j -=1
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
c = list(input())
cFixed = [x for x in c]
cFixed.sort()
for i in range(n):
	if cFixed[i] == "R":
		lastR = i
ans = 0
for i in range(lastR+1):
	if c[i] == "W":
		ans += 1

print(ans)