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:
n = int(input())
saidan = input()

isiall = min(saidan.count('W'), saidan.count('R'))

tesuu = 0
for i in range(len(saidan)//2):
    r_iti = saidan.rfind('R')
    w_iti = saidan.find('W')
    
    if r_iti == -1 or w_iti == -1:
        break
    
    saidan = saidan[w_iti+1 : r_iti]
    tesuu += 1
    
    print(tesuu, saidan)
    
print(min(tesuu, isiall))

Submission status: Runtime Error

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)
elif c[0] == "R" and c[-1] == "W":
    for i in range(n-1):
        if c[i] == "W" and c[i+1] == "R":
            ans += 1
    print(ans)
elif c[0] == "R" and c[-1] == "R":
    for i, v in enumerate(a):
        if i != 0 and v == "W":
            c[i] = "R"
            c[-1] = "W"
    ans += 1
    for i in range(n-1):
        if c[i] == "W" and c[i+1] == "R":
            ans += 1
    print(ans)
else:
    for i, v in enumerate(reversed(a)):
        if i != 0 and v == "R":
            c[n-1-i] = "W"
            c[0] = "R"
    ans += 1
    for i in range(n-1):
        if c[i] == "W" and c[i+1] == "R":
            ans += 1
    print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
cstr=input()
clist=['']*N
for i in range(N):
    clist[i]=cstr[i]
rindexlist=[i for i,x in enumerate(clist) if x=='R']
windexlist=[i for i,x in enumerate(clist) if x=='W']
rindexlist.sort(reverse=True)
lenw=len(windexlist)
ans=0
#print(lenw)
for i in range(lenw):
    #print(i)
    if i<=len(rindexlist):
        if windexlist[i]<rindexlist[i]:
            ans+=1
if lenw<len(rindexlist):
    if windexlist==[]:
        ans=0
    else:
        ans+=len(rindexlist)-lenw
print(ans)

Submission status: Runtime Error

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    n=int(readline())
    ss=readline().rstrip()
    j=len(ss)
    ans=0
    for i in range(len(ss)):
        if ss[i]=="W":
            j-=1
            while ss[j]!="R":
                j-=1
            if j > i:
                ans+=1
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Runtime Error

Solution code:
def main():
  n = int(input())
  li = list(input())
  i = 0
  j = n-1
  ans = 0
  while(i < j):
    while(li[i] == "R" and i < j):
      i += 1
    while(li[j] == "W" and i < j):
      j -= 1
    if li[i] = "W" and li[j] = "R":
      li[i] = "R"
      li[j] = "W"
      ans += 1
  print(ans)    

if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

ans = 0
w = 0
r = 0
u = 0
p = 0
for i in range(n):
    while c[u] == 'R':
        #print(79)
        u += 1
        if u >= n-1:
            p = 1
            break
        if u <= i-1:
            p = 1
            break
    if p == 1:
        break

    if c[n-i-1] == 'R':
        r += 1
        u += 1
        #print(0)
    else:
        ans += r
        r = 0
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

C = input()
firstW = -1
for i in range(N):
    if C[i] == "W":
        firstW = i
        break
for i in range(N-1,-1,-1):
    if C[i] == "R":
        firstR = i
        break

def checkR(ri,wi):
    for i in range(ri,wi,-1):
        if C[i] == "R":
            return i
    else:
        return -1
def checkW(ri,wi):
    for i in range(wi,ri):
        if C[i] == "W":
            return i
    else:
        return -1


if firstW == -1 or firstW == N-1:
    print(0)
else:
    ri = firstR
    wi = firstW
    cnt = 0
    while 1:
        wi = checkW(ri,wi+1)
        ri = checkR(ri-1,wi)
        if wi == -1 or ri == -1:
            break
        cnt += 1
    print(cnt+1)

Submission status: Runtime Error

Solution code:
import sys
N = int(input())
c = input()
ans = 0
exchange = 0
for i in range(N):
    if c[i] == "W":
        exchange = i
        break
else:
    print(0)
    sys.exit()

for i in range(N//2):
    # print(i+1,"回目")
    # print("exchange=",exchange)
    # print("c=",c)
    if c[-(i+1)] =="R":
        # c = list(c)
        # c[exchange] = "R"
        # c[-(i+1)] = "W"
        # c = "".join(c)
        c = c[:exchange]+"R"+c[exchange+1:]
        print("newc=",c)
        c = c[:-(i+1)]+"W"+c[N-i:]
        # print("newc=",c)
        ans += 1
        for k in range(exchange+1,N):
            if c[k] == "W":
                exchange = k
                break
        else:
            break
    
print(ans)

Submission status: Runtime Error

Solution code:
_ = input()
colors = [i for i in input()]
num = 0
while True:
    if 'W' not in colors:
        break
    idxW = colors.index('W')
    idxR = len(colors) - colors[::-1].index('R') - 1
    if idxW>idxR:
        break
    idxR_last = colors[::-1].index('R')
    colors[-idxR_last-1] = 'W'
    colors[idxW] = 'R'
    num += 1
print(num)

Submission status: Runtime Error

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

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

    upper_bound = min(wtotal, rtotal)

    left = -1
    right = len(CS)

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

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

        if right == left:
            break

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


main()

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))

Submission status: Runtime Error

Solution code:
N = int(input())
C = list(input())
ans = 0
r = 0
l = N - 1
while r+1 != l:
    while True:
        if C[r] != C[l]:
            ans += 1
            break
        else:
            l -= 1
            if r+1 == l:
                break
    r += 1
print(ans)

Submission status: Runtime Error

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

firstRidx = None
Rrenzoku = 0
Rcount = 0
flag = True
for i in range(len(c)):
    if c[i] == 'R':
        Rcount += 1
        if firstRidx is None:
            firstRidx = i
        if i != 0 and i != firstRidx and c[i-1] == 'W':
            flag = False
        if flag:
            Rrenzoku += 1

if firstRidx <= Rcount - Rrenzoku:
    print(Rcount - Rrenzoku)
elif firstRidx > Rcount - Rrenzoku:
    print(firstRidx)

Submission status: Runtime Error

Solution code:
N = int(input())
c = list(input())
ans = 0
#print(c)
for i in range(N):
    flag = 0
    if c[i] == "R":
        continue
    elif c[i] == "W":
        #print(c, ans, i, sep=" ",)
        for j in reversed(range(N)):
            if i == j:
                flag = 1
                break
            if c[j] == "R":
                c[i], c[j] = c[j], c[i]
                ans += 1
                print(c, ans, sep=" ")
                break
        else:
            continue
        if flag == 1:
            break

print(ans)

Submission status: Runtime Error

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

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

#pragma region Macro
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define SORT(v) sort(v.begin(), v.end())
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define vvi vector<vector<int>>
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#pragma endregion

void solve(){
	#define int ll
	// ここに処理を記述
	// コンパイル時はエイリアス「gpp」--> c++17でコンパイル
	
	int n;
	cin >> n;

	string c;
	cin >> c;

	int left = 0;
	int right = n-1;
	int ans = 0;
	while(left < right){
		while(c[left] == 'R'){
			if(left > n-2){
				break;
			}
			left++;
		}
		while(c[right] == 'W'){
			if(right < 1){
				break;
			}
			right--;
		}

		if(right < 1 || left > n-2) break;
		
		c[left] = 'R';
		c[right] = 'W';
		left++;
		right--;
		ans++;
	}

	cout << ans << endl;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	
	solve();
	return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l):
      ans += 1
  print(ans)
	
else:
  print(len(l))

Submission status: Runtime Error

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    stones = input()

    w = []
    r = []

    for i in range(N):
        if stones[i] == 'W':
            w.append(i)
        else:
            r.append(i)

    res = 0
    for i, val in enumerate(w):
        if val <= r[-1*(i+1)]:
            res += 1
        else:
            break
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
import numpy as np

N=int(input())
c=np.array(list(input()))
n=np.count_nonzero(c == 'R')
i=0
j=N-1
cnt=0
if N==n:
    print(cnt)
    exit(0)
while 1:
    while c[i]!='W' and i<n:
        i+=1
    while c[j]!='R':
        j-=1
    if i>=n:
        print(cnt)
        exit(0)
    temp=c[i]
    c[i]=c[j]
    c[j]=temp
    cnt+=1

print(cnt)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = readline().decode().rstrip()

w = s.count('W')
r = s.count('R')
i_r = s.index('R')
r2 = s[:r:].count('R')
# print(w, r, i_r)
print(r-r2)

Submission status: Runtime Error

Solution code:
n = int(input())
WR = input()
count = 0
start = False
for c in WR:
  while c == "R" and false:
    continue
  if c == "W":
    start = True
  else:
    count += 1
print(count//2 + count%2)

Submission status: Runtime Error

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 W[N//2+1]=="W":
  Z+=1
print(Z)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/
using namespace std;
typedef long long int ll;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
// online submission
#endif
	int n;
	cin>>n;
	int r=0,w=0;
	string s;
	cin>>s;
	for(int i=0;i<n;i++){
		if(s[i]=='R'){
			r++;
		}
		else{
			w++;
		}
	}
	int ct=0;
	for(int i=0;i<n-1;i++){
		if(s[i]=='W'&&s[i+1]=='R'){
			ct++;
		}
	}
	cout<<min(ct,min(r,w));
}

Submission status: Runtime Error

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


int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int w=0,r=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='W')
        w++;
        else
        r++;
    }
    string st;
    for(int i=0;i<r;i++)
    st.push_back('R');
    for(int i=0;i<w;i++)
    st.push_back('W');
    int ans=0;
    for(int i=n-1;i>=n-w;i--)
    {
        if(s[i]!=st[i])
        ans+=1;
    }
    cout<<ans<<endl;
    //  RRWWRRRWWRR
    //  RRRRRWW
}

Submission status: Runtime Error

Solution code:
import numpy as np

def swip(a, b):
    tmp = c[a]
    c[a] = c[b]
    c[b] = tmp

def c_change(a):
    if c[a] == 'W':
        c[a] = 'R'
    else:
        c[a] = 'W'

n = int(input())
c = np.array(list(input()))

# done = 0
count = 0
i = 0

n_white = np.count_nonzero(c=='W')
n_red = np.count_nonzero(c=='R')

while True:
    i += 1
    if n_white:
        min_white = np.where(c == 'W')[0].tolist()[0]
        max_red = np.where(c == 'R')[0].tolist()[-1]
        if min_white < max_red:
            swip(min_white, max_red)
            count += 1
        else:
            break
    else:       # no white stones
        break

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
c = [list(input())]
 
i = 0
 
cullent = 'R'
 
while cullent == 'R':
  if i => N:
    break
  if c[i] == 'W':
    cullent = W
  i += 1
r = i
 
while cullent == 'W':
    if i => N:
    break
  if c[i] == 'R':
    cullent = R
  i += 1
w = i
Red = 0
if i => N:
  print(0)
else:
  for j in range(i,N):
    if c[j] == 'R':
      Red += 1
  print(Red)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
r_c = 0
ch = 0
for i in S:
  if i == "R":
    r_c += 1
for i in range(r_c):
  if i == "W"
    ch += 1
print(ch)

Submission status: Runtime Error

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

i = 0

cullent = 'R'

while cullent == 'R':
  if i => N:
    break
  if c[i] == 'W':
    cullent = W
  i += 1
r = i

while cullent == 'W':
    if i => N:
    break
  if c[i] == 'R':
    cullent = R
  i += 1
w = i
Red = 0
for j in range(i,N):
  if c[j] == 'R':
    Red += 1
  
print(Red)

Submission status: Runtime Error

Solution code:
a=int(input())
b=input()
r_num=0
w_num=0
for i in b :

  if i=='W' :
    w_num+=1
  else :
    r_num+=1

count=0
if r_num<w_num
  for i in range(r_num) :
    if b[i]=='R' :
      count+=1
else :
  for i in range(w_num) :
    if b[i]=='W' :
      count+=1
print(count)

Submission status: Runtime Error

Solution code:
#ABC174  D
n = int(input())
s = input() 
c = [str(i) for i in s]
count = c.count('R')
idx = c.index('R')
#print(count,idx)
ans = 0
if count == n:
  ans = 0
elif count > idx:
  ans = count - 1
else:
  ans = count
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
x=[]
y=[]

for i in range(n):
  if s[i]=='R':
    x+=[i]
  elif s[i]=='W':
    y+=[i]
    
ans=0

while True:
    if len(y)==0:
      print(ans)
      break
    elif x[-1]>y[0]:
      x.pop()
      y.pop(0)
      ans+=1
      continue
    
    else:
      print(ans)
      break

Submission status: Runtime Error

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)

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

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
x=[]
y=[]

for i in range(n):
  if s[i]=='R':
    x+=[i]
  elif s[i]=='W':
    y+=[i]
    
ans=0
if y==[]:
  print(ans)
else:
  while True:
    if x[-1]>y[0]:
      x.pop()
      y.pop(0)
      ans+=1
      continue
    
    else:
      print(ans)
      break

Submission status: Runtime Error

Solution code:
N = int(input())
C = input()
count = 0
if N == 2:
  if C == "WR":
    count = 1
elif N == 3:
  if (C == "WWR") or (C == "WRW") or (C == "WRR") or (C == "RWR"):
    count = 1
else:
  for i in range(N-3):
    if (C[i] == "W") and (C[i+1] == "R"):
      if (C[i+2] == "W") and (C[i+3] == "W"):
        count += 1
        C = C[:i] + "W" + C[i+2:]
      elif (C[i+2] == "W") and (C[i+3] == "R"):
        count += 2
        C = C[:i] + "WWW" + C[i+4:]
      else:
        count += 1

Submission status: Runtime Error

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

# print(line)
bottom_w = N
top = 0
count = 0
while(bottom_w > top):
    for i in reversed(range(bottom_w)):
        if(line[i]=='R'):
            bottom_w = i
            break

    for i in range(top,N):
        if(line[i]=='W' and bottom_w > i):
            # print("line",i,line[i])
            # print("swap",i,bottom_w)
            count += 1
            line[bottom_w] = 'W'
            top = i+1
            break
print(count)

Submission status: Runtime Error

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

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

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

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

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

    ans += 1

    if l > r:
        break

    l += 1
    r -= 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
c = input()
l = 0
r = n-1
ans = 0
while l<r:
    while l<n and c[l]=='R':
        l+=1
    while l>=0 and c[r]=='W':
        r-=1
    if l>r:
        break
    ans += 1
    l += 1
    r -= 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Cs = list(input())
from collections import deque
Q = deque()
for i, c in enumerate(Cs):
    if c=='R':
        Q.append(i)

ans = 0
for i in range(N):
    c = Cs[i]
    if c=='W':
        r = Q.pop()
        if r>i:
            ans += 1
        else:
            break
print(ans)

Submission status: Runtime Error

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

r = c.count("R")
rs = c[:r]
rsw = rs.count("W")
a = rsw

w = list(reversed(c)).index("R")
w = (N - 1) - w
ws = c[:w]
b = ws.count("W")

print(min(a,b))

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    N = int(input())
    C = list(input().strip())
    invC = C[::-1]
    ans = 0
    leftW = C.index('W')
    rightR = N - 1 - invC.index('R')
    while leftW < rightR:
        # print('C={}, invC={}'.format(C, invC))
        # print('leftW={}, rightR={}'.format(leftW, rightR))
        ans += 1
        C[rightR] = 'W'
        invC[-1-rightR] = 'W'
        C[leftW] = 'R'
        invC[-1-leftW] = 'R'
        leftW = C.index('W')
        rightR = N - 1 - invC.index('R')
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
c = [l for l in input()]
max_idx = len(c)-1

if 'W' not in c:
  print(0)
else:
  most_left_white = c.index('W')
  tmp = c[::-1]
  most_right_red = max_idx - tmp.index('R')

  cnt = 0
  while most_left_white<most_right_red:
    c[most_left_white] = 'R'
    c[most_right_red] = 'W'

    most_left_white = c.index('W')
    tmp = c[::-1]
    most_right_red = max_idx - tmp.index('R')
    cnt+=1
  print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(input())
b = []
for i in range(N):
  if a[i] == 'R':
    b.append(i)
M = len(b)-1
m = b[M]
ans = 0
for i in range(N):
  if i >= b[M]:
    break
  if i not in b:
    #print(i)
    M -= 1
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
"""According to a fortune-teller, a white stone placed to the immediate left of a red stone 
WR leads to nok
"""
n = int(raw_input())
s = list(raw_input())
import collections


def f(s):
	cc = collections.Counter(s)
	if cc['R'] == 0 or cc['W'] == 0:
		return 0
	i = 0
	j = len(s) - 1
	while(s[i] == 'R'): i +=1
	while(s[j] == 'W'): j +=1
	
	c = 0
	while(i < j):
		if s[i] == 'W' and s[j] == 'R':
			s[i],s[j] = s[j],s[i]
			i += 1
			j -= 1
			c += 1
		while(i < j and s[i] == 'R'): i+=1
		while(i < j and s[j] == 'W'): j-=1
	return min(c, cc['R'], cc['W'])

print f(s)

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

left = 0
right = N - 1

ans = 0
while right > left:
    while left < N and S[left] == 'R':
        left += 1
    while right > left and S[right] == 'W':
        right -= 1
    S[left], S[right] = S[right], S[left]
    ans += 1
    left += 1
    right -= 1

for i in range(right):
    if S[i] == 'W':
        S[i] = 'R'
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
cs = [i for i in input()]
cur = len(cs)-1
ans = 0
for i in range(len(cs)):
    if cs[i] == 'W':
        while(cs[cur] != 'R' or cur == i):
            cur -= 1
        ans += 1
        cur -= 1
    if i >= cur:
        break
print(ans)

Submission status: Runtime Error

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

rev = 0

# for i in range(1, len(l)-1):
#   if l[i-1] == 'W' and l[i] == 'R':
#    l.insert(0, l.pop(i))

rl = l.count('R')
wl = l.count('W')

return rl if rl < wl else wl

Submission status: Runtime Error

Solution code:
import re

n = int(input())
a = input()

def set_c(a):
    a = re.sub('^[R]*W', 'W', a)
    a = re.sub('R[W]*$', 'R', a)
    return a

for i in range(n):
    c = set_c(c)
    if c == '':
        break
    elif 'W' not in c or 'R' not in c:
        break
    c = 'R' + c[1:-1] + 'W'
        
print(i)

Submission status: Runtime Error

Solution code:
/**
 * The man who codes no bugs
 * McCheng is the man studying Maths in CUHK with IQ200
 * @author McCheng
 */

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

class DAlterAltar {
public:
    static constexpr int kStressCount = 0;
    static void generateTest(ostream& test) {}
	void solve(istream& cin, ostream& cout) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int cnt=0;
        int l=0,r=n-1;
        while (l<r){
            if (l>=n || r<0) break;
            if (s[r]=='W'){
                r--;
            }
            if (s[l]=='R')l++;
            if (l>=n || r<0) break;
            if (s[l]=='W' && s[r]=='R'){
                cnt++;
                l++;
                r--;
            }
            if (l>=n || r<0) break;
        }
        cout<<cnt;
	}
};

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    istream &in(cin);
    ostream &out(cout);
    out << fixed;
    out.precision(20);
    DAlterAltar solver;
    solver.solve(in, out);
}

Submission status: Runtime Error

Solution code:
/**
 * The man who codes no bugs
 * McCheng is the man studying Maths in CUHK with IQ200
 * @author McCheng
 */

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

class DAlterAltar {
public:
    static constexpr int kStressCount = 0;
    static void generateTest(ostream& test) {}
	void solve(istream& cin, ostream& cout) {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int cnt=0;
        int l=0,r=n-1;
        while (l<r){
            if (s[r]=='W'){
                r--;
            }
            if (s[l]=='R')l++;
            if (s[l]=='W' && s[r]=='R'){
                cnt++;
                l++;
                r--;
            }
        }
        cout<<cnt;
	}
};

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    istream &in(cin);
    ostream &out(cout);
    out << fixed;
    out.precision(20);
    DAlterAltar solver;
    solver.solve(in, out);
}

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
if("R" not in s):
  print(n)
else:
  swaps=0
  i=0
  j=n-1
  c=0
  while(i<j):
    if(s[i]=="R"):
      i+=1
    if(s[j]=="W"):
      j-=1
    if(s[i]=="W"):
      while(s[j]!="R"):
        j-=1
      c+=1
      i+=1
      j-=1
print(c)

Submission status: Runtime Error

Solution code:
# D - Alter Altar

from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw:
    w = dw.popleft()
    r = dr.popleft()
    if w < r:
        ans += 1

    else:
        break
print(ans)

Submission status: Runtime Error

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

r = c.count("R")
rs = c[:r]
rsr = rs.count("R")
a = r-rsr

w = list(reversed(c)).index("R")
w = (N - 1) - w
ws = c[:w]
b = ws.count("W")

print(min(a,b))

Submission status: Runtime Error

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

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

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

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

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

    ans += 1

    if l > r:
        break

    l += 1
    r -= 1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
c = list(str(input()))
dw = deque()
dr = deque()

for i, c in enumerate(c, 1):
    if c == 'W':
        dw.append(i)
    else:
        dr.appendleft(i)

ans = 0
while dw:
    w = dw.popleft()
    r = dr.popleft()
    print(w, r)
    if w < r:
        ans += 1

    else:
        break
print(ans)

Submission status: Runtime Error

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.pop(0)
        else:
            break
    w1 = 0
    for i in range(n):
        if c[i] == 'W':
            w1 += 1
        else:
            break
    
    return max(x-1, w1)
print(solve())

Submission status: Runtime Error

Solution code:
N = int(input())
l = input()
arr = []
for i in range(N):
    arr.append(l[i])

c = 0
left = 0
right = N-1
while True:
    while True:
        if arr[left] == 'R':
            left += 1
        else:
            break
    while True:
        if arr[right] == 'W':
            right -= 1
        else:
            break
    arr[left] = 'R'
    arr[right] = 'W'
    if left >= right:
        break
    else:
        c += 1

print(c)

Submission status: Runtime Error

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

while left < right:
    if c[left] == 'W':
        flag = True
        while flag:
            if c[right] == 'R':
                c[right] = 'W'
                c[left] = 'R'
                counter += 1
                right -= 1
                left += 1
                flag = False
            else:
                right -= 1
    else:
        left += 1

print(counter)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input().strip())

r = S.count('R')
w = S.count('W')

ans = min(r, w)

ri = S.index('R')
r2 = S[:ri].count('R')
w2 = S[ri:].count('W')

ans = min(ans, r2+w2)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
WPrefix = [0]
RSuffix = [0]
for elem in S:
    if elem == "R":
        RSuffix += 1
for elem in S:
    if elem == "R":
        RSuffix.append(RSuffix[-1] - 1)
        WPrefix.append(WPrefix[-1])
    else:
        RSuffix.append(RSuffix[-1])
        WPrefix.append(WPrefix[-1] + 1)
minSwap = len(S)
for index in range(len(RSuffix)):
    if max(RSuffix[index],WPrefix[index]) < minSwap:
        minSwap = max(RSuffix[index],WPrefix[index])
print(minSwap)

Submission status: Runtime Error

Solution code:
from collections import Counter

_ = int(input())
s = input()
counter = Counter(s)
red = counter.get('R')
white = counter.get('W')

mismatches = 0
for i in range(red):
    if s[i] != 'R':
        mismatches += 1

print(mismatches)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
c=0
for i in range(n):
  if(s[i]=='W'and s[i+1]=='R'):
    c+=1
print(c)

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

left = 0
right = N - 1

ans = 0
while right - left > 1:
    while left < N and S[left] == 'R':
        left += 1
    while right > left and S[right] == 'W':
        right -= 1
    S[left], S[right] = S[right], S[left]
    ans += 1
    left += 1
    right -= 1

for i in range(right):
    if S[i] == 'W':
        S[i] = 'R'
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input().strip())

r = S.count('R')
w = S.count('W')

ans = min(r, w)

ri = S.index('R')
r2 = S[:ri+1].count('W')

ans = min(ans, r2)
print(ans)