Problem:


Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7



Sample input:
6 1
LRLRRL


Sample output:
3

Submission status: Wrong Answer

Solution code:
import sys
import re
n, k = [int(i) for i in sys.stdin.readline().split()]
s = input()
cnt = sum([len(i) - 1 for i in re.findall(r"R{2,}|L{2,}", s)])
for i in range(k):
    left = s.find("LR")
    right = s.rfind("RL")
    if left == -1 or right == -1:
        cnt += 1
        break
    else:
        cnt += 2
    s = s[left : right]
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
t=list()
ans=0
count=1
for i in range(n-1):
	if s[i]==s[i+1]:
		count+=1
	else:
		if count >= 2:
			ans += count-1
		t.append(1)
		count=1
if count >= 2:
	ans += count-1
t.append(1)
if len(t) <= 2*k+1:
	ans += len(t)
else:
	ans += 2*k+1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

left_ac = [0]
right_ac = [0]

for i in range(N):
    s = S[i]
    if s == "L":
        if i - 1 >= 0:
            if S[i - 1] == "L":
                left_ac.append(left_ac[-1] + 1)
            else:
                left_ac.append(left_ac[-1])
        else:
            left_ac.append(left_ac[-1])
        right_ac.append(right_ac[-1])
    else:
        if i + 1 < N:
            if S[i + 1] == "R":
                right_ac.append(right_ac[-1] + 1)
            else:
                right_ac.append(right_ac[-1])
        else:
            right_ac.append(right_ac[-1])
        left_ac.append(left_ac[-1])
left_ac.pop(0)
right_ac.pop(0)
# print(left_ac)
# print(right_ac)

left = 0
right = 0
ans = right_ac[-1] + left_ac[-1]
remain = K
while left < N:
    s = S[left]
    while remain > 0 and right < N:
        if S[right] != s:
            if (right + 1 < N and S[right + 1] == s) or (right == N - 1 and S[N - 1] != s):
                remain -= 1
        right += 1

    if s == "L":
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left > 0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)
    else:
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left >=0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)

    if right == left:
        right += 1
    if left + 1 < N and S[left + 1] != s:
        remain += 1
    left += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()

ranlength_l = []
count = 1
for i in range(1,len(S)):
    if(S[i] != S[i-1]):
        ranlength_l.append([S[i-1],count])
        count = 1
    else:
        count += 1
    if(i==len(S)-1):
        ranlength_l.append([S[len(S)-1],count])

target = S[0]
if(target=="L"):
    changed = "R"
else:
    changed = "L"

ans = N-len(ranlength_l)
for const in (ranlength_l[1:len(ranlength_l)-1]):
    if(K==0):
        break
    if(const[0]==target):
        ans += 2
        K = K-1

if(K!=0):
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()

L = []
R = []
cnt = 1
for i in range(N):
    if i == 0:
        continue
    if S[i-1] == S[i]:
        cnt += 1
        if i == N-1:
            if S[i] == "L":
                L.append([i-cnt+1,cnt])
            else:
                R.append([i-cnt+1,cnt])
    else:
        if S[i] == "L":
            R.append([i-cnt,cnt])
        else:
            L.append([i-cnt,cnt])
        if i == N-1:
            if S[i] == "L":
                L.append([N-1,1])
            else:
                R.append([N-1,1])
        cnt = 1
R.sort(key=lambda x:x[1])
L.sort(key=lambda x:x[1])

#Rを最大化
ans = 0
res = 0
c = 0
s = S
while c < K:
    if L:
        ind,x = L.pop()
        if ind == N-1:
            continue
        c += 1
        s = s[:ind]+"R"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
#Lを最大化
c = 0
s = S
res = 0
while c < K:
    if R:
        ind,x = R.pop()
        if ind == 0:
            continue
        c += 1
        s = s[:ind]+"L"*x+s[ind+x:]
    else:
        break
#評価
for i in range(N):
    if s[i] == "L":
        if i == 0:
            continue
        if s[i-1] == "L":
            res += 1
    if s[i] == "R":
        if i == N-1:
            continue
        if s[i+1] == "R":
            res += 1
if ans < res:
    ans = res
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in  input().split()]
S = list(input())
ans = [0]
a = []
for i in range(N-1):
  if S[i]!=S[i+1]:
  	ans.append(i)
ans.append(N-1)
#print(ans)
a = []
for i in range(1,len(ans)):
  a.append(ans[i]-ans[i-1])
a[0] +=1
ans2 = sum(a)-len(a)
#print(a)
#print(ans2)
ans2 = ans2 +(2*K if len(a)*(len(a)+1)//2>=K else len(a)*(len(a)+1)//2*2)
print(ans2)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int,input().split())
A =list(input())

b = ''
li = []
count = 1

for a in A:
    if a == b:
        count+=1
    else:
        li.append(count)
        count = 1
    b = a

li.pop(0)
li.append(count)

for k in range(K):
    index = np.argmax(li)
    if len(li) == 1:
        break
    elif index == 0:
        li[index + 1] += li[index]
        li.pop(index)

    else:
        li[index-1] += li[index]+1
        li.pop(index)

result = sum(li) - len(li)
print(result)

Submission status: Wrong Answer

Solution code:
from math import ceil
n,k=map(int,input().split())
s=list(input())
if s[0]=='R':
    s=['L' if i=='R' else 'R' for i in s]
p=[]
for i in range(n-1):
    if s[i]!=s[i+1]:
    	p.append(i)

unti=len(p)/2
kaisu=ceil(len(p)/2)

if kaisu<=k:
    print(n-1)
else:
	if unti%2==0:
		print(n-1-(kaisu-k)*2)
	else:
		print(n-1-(kaisu-k)*2+1)
	# for i in range(k:)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s = list(str(input()))
eq=0
ch=0
for i in range(n-1):
    if s[i] == s[i+1]:
        eq+=1
    else:
        ch+=1
if eq != 0:
    eq_an = eq +1
else:
    eq_an = eq
if ch//2 + 1 <= k:
    print(n-1)
else:
    ans = eq_an + 2*k -1
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

left_ac = [0]
right_ac = [0]

prev = ""
for i in range(N):
    s = S[i]
    if s == "L":
        if i - 1 >= 0:
            if S[i - 1] == "L":
                left_ac.append(left_ac[-1] + 1)
            else:
                left_ac.append(left_ac[-1])
        else:
            left_ac.append(left_ac[-1])
        right_ac.append(right_ac[-1])
    else:
        if i + 1 < N:
            if S[i + 1] == "R":
                right_ac.append(right_ac[-1] + 1)
            else:
                right_ac.append(right_ac[-1])
        else:
            right_ac.append(right_ac[-1])
        left_ac.append(left_ac[-1])
left_ac.pop(0)
right_ac.pop(0)
# print(left_ac)
# print(right_ac)

left = 0
right = 0
ans = right_ac[-1] + left_ac[-1]
remain = K
while left < N:
    s = S[left]
    while remain > 0 and right < N:
        if S[right] != s:
            if (right + 1 < N and S[right + 1] == s) or (right == N - 1 and S[N - 1] != s):
                remain -= 1
        right += 1

    if s == "L":
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left > 0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)
    else:
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left >=0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)

    if right == left:
        right += 1
    elif left + 1 < N and S[left + 1] != s:
        remain += 1
    left += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
#LR数できまる.
#一番外はRとLをそれぞれ付けて考える
#Lの次にRならLに変えてRが続くならそれもLに返る
LR = 0

for j in range(N-1):
    if S[j] == "L" and S[j+1]== "R":
        LR += 1

if LR > K:
    ans = N - (LR-K)*2
else:
    ans = N

if S[0] == S[-1]:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()+'X'
if n==1:
  print(0)
  exit()
if n==2:
  print(1)
  exit()
A=[0]
l=0
r=0
for i in range(n):
  if S[i]=='L':
    l+=1
    if S[i+1]!='L':
      A.append(l)
      l=0
  if S[i]=='R':
    r-=1
    if S[i+1]!='R':
      A.append(r)
      r=0
A=sorted(A)
z=A.index(0)
L=sum(A[z+1:])-len(A[z+1:])-sum(A[:min(z,k)])+min(len(A)-z-1,k)
R=sum(A[:z])+len(A[:z])-sum(A[-min(n-z,k):])-min(z,k)
if len(A[z+1:])==min(len(A)-z-1,k):
  L-=1
if len(A[:z])==min(z,k):
  R+=1
print(max(L,-R))

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split(' ')))
S = input() + 'Z'
old = ''
ct = 0
cts = []
sums = []
for i in range(len(S)):  # 1e5
    if old != S[i]:
        old = S[i]
        if ct:
            cts.append(ct)
            if not sums:
                sums.append(ct)
            else:
                sums.append(sums[-1] + ct)
        ct = 0
    ct += 1
sums = [0, 0] + sums

ans = -1
lensums = len(sums)
# RRL
# LLRでNG
LorR = False
for uragaesu in range(K, K + 1):  # 1e5
    for start in range(N):  # 1e5 / 2 ... TLE?
        if start + 2 * uragaesu + 1 >= lensums:
            break
        if ans < sums[start + 2 * uragaesu + 1] - sums[start]:
            ans = sums[start + 2 * uragaesu + 1] - sums[start]
            LorR = start == 0 or start + 2 * uragaesu + 1 == lensums
    if ans == sums[-1]:
        break
renzoku = (ans if ans >= 0 else sums[-1])
print(renzoku - (renzoku == sums[-1] or LorR))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())

sl = 0
for i in range(N-1, -1, -1):
    if S[i] == S[0]:
        sr = i
        break
    else:
        sl += 1
if sl == 0:
    sl = 1

only = True
for s in S:
    if s != S[0]:
        only = False
        break

if only:
    ans = N-1
else:
    r = sr
    l = 0
    change = 0
    while l<=r and change <= K:
        if S[l] != S[l+1]:
            if S[r] != S[r-1]:
                change += 1
                l += 1
                r -= 1
            else:
                r -= 1
        else:
            l += 1
    if change < K:
        ans = N-1
    elif change == K:
        ans = sr + sl-1
    else:
        ans = l-1 + (sr-r-1) + sl-1
        for i in range(l, r):
            if S[i] == S[i+1]:
                ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
t=list()
ans=0
count=1
for i in range(n-1):
	if s[i]==s[i+1]:
		count+=1
	else:
		if count >= 2:
			ans += count-1
		t.append(1)
		count=1
if count >= 2:
	ans += count-1
t.append(1)
if len(t) <= 2*k+1:
	ans += len(t)
else:
	ans += 2*k
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1
     
if min(lrcnt,rlcnt)>=k*2:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  lrcnt-=min(lrcnt,rlcnt)
  rlcnt-=min(lrcnt,rlcnt) 
  if k>=2:
    if lrcnt>0 and rlch>0:
      ans+=1
    if rlcnt>0 and lrch>0:
      ans+=1
  else:
    if lrcnt>0 and rlch>0:
      ans+=1
    else:
      if rlcnt>0 and lrch>0:
        ans+=1
  
print(ans)

Submission status: Wrong Answer

Solution code:
temp = raw_input().split()
S = raw_input()

N = int(temp[0])
K = int(temp[1])

ini_score = 0
ex_count = 0


S = list(S)

for i in range(N-1):
    if S[i] == S[i+1]:
        ini_score = ini_score + 1

num_double_interface = 0
for i in range(1,N-1):
    if (S[i] != S[i-1]) and (S[i]!= S[i+1]):
#        print i, "double"                                                                                                                              
        if S[i] == "L":
            S[i] = "R"
        else:
            S[i] = "L"
        num_double_interface = num_double_interface + 1
        ex_count = ex_count + 1

    if ex_count==K:
        print ini_score + 2*num_double_interface
        exit()



current_score = ini_score + 2*num_double_interface
max_score = N-1


if (max_score - current_score) > ex_count:
    print ini_score + 2*num_double_interface + (K-ex_count)
else:
    print max_score

Submission status: Wrong Answer

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

# 幸福度が増すパターン
# L:R--R:L  L:L---L:L +2
# R:L--L:R  R:R---R:R +2

# :R---R:L  :L---L:L  +1
# :R---R:R
# :R---R:L  :L---L:L  +1
# L:R---R

# :L---L:R

# L:R を 1
# R:L を 2
#
# 1-2 2-1

def solve(N, K, S):
    ans = 0
    last = S[0]
    lr = 0
    rl = 0
    for s in S[1:]:
        if last != s:
            if last == 'L':
                lr += 1
            else:
                rl += 2
        else:
            ans += 1
        last = s

    n = min(lr, rl)
    if n >= K:
        ans += K*2
    else:
        ans += n*2
        if lr != rl:
            ans += 1

    return ans


if __name__ == '__main__':
    N, K = map(int, input().split())
    S = input()

    ans = solve(N, K, S)

    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Slist = []
for k in range(N):
    Slist.append(S[k])
#LR数できまる.
#一番外はRとLをそれぞれ付けて考える
#Lの次にRならLに変えてRが続くならそれもLに返る
LR = 0
count = 0
for j in range(N-1):
    if Slist[j] == "L" and Slist[j+1]== "R":
        LR += 1

if LR > K:
    ans = N - (LR-K)*2
else:
    ans = N

if Slist[0] == Slist[-1]:
    ans -= 1

print(ans)

#print(Slist)

Submission status: Wrong Answer

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

def main():
    n,k=map(int,input().split())
    s=input()
    b=[]
    for i in range(n):
        b.append(1 if s[i]=="R" else 0)
#    print(b)
    kohuku=0

    if b.count(0)>n//2:
        h=1
    else:
        h=0

    a=[]
    temp=0
#    print(a,n//2,h)
    for i in range(n):
        if b[i]==h:
            temp+=1
#            if i==0 or i==n:
#                temp-=0.5
        else:
            a.append([temp,i])
            temp=0
    a=sorted(a,reverse=True)

#    print(a)
    aaa = [x[0] for x in a]
    if len(aaa)-aaa.count(0)<=k:
        k=len(aaa)-aaa.count(0)
    for i in range(k):

        for l in range(a[i][1]-a[i][0],a[i][1]):
            b[l]=-(h-1)
#    print(b)
    for i in range(1,n):
        if b[i]==1 and b[i-1]==1:
            kohuku+=1
        if b[i]==0 and b[i-1]==0:
            kohuku+=1

    print(kohuku)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
a = []
b = []

count = 1
for i in range(1, n):
    if s[i] == s[i-1]:
        count += 1
    else:
        if s[i-1] == "L":
            a.append(count)
        else:
            b.append(count)
        count = 1
if n == 1:
    i = 0
if s[i] == "L":
    a.append(count)
else:
    b.append(count)
count = 1

a.sort(reverse=True)
b.sort(reverse=True)
ans1 = sum(a) - len(a)
ans2 = sum(b) - len(b)
for i in range(min(2*k, len(b))):
    ans1 += b[i]
if len(b) <= 2*k:
    l = len(b)
    ans1 += (2*k - l)
ans1 = min(n-1, ans1)
for i in range(min(2*k, len(a))):
    ans2 += a[i]
if len(a) <= 2*k:
    l = len(a)
    ans2 += (2*k - l)
ans2 = min(n-1, ans2)
ans = max(ans1, ans2)
if ans1 != ans2:
    ans = min(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

left_ac = [0]
right_ac = [0]

prev = ""
for i in range(N):
    s = S[i]
    if s == "L":
        if i - 1 >= 0:
            if S[i - 1] == "L":
                left_ac.append(left_ac[-1] + 1)
            else:
                left_ac.append(left_ac[-1])
        else:
            left_ac.append(left_ac[-1])
        right_ac.append(right_ac[-1])
    else:
        if i + 1 < N:
            if S[i + 1] == "R":
                right_ac.append(right_ac[-1] + 1)
            else:
                right_ac.append(right_ac[-1])
        else:
            right_ac.append(right_ac[-1])
        left_ac.append(left_ac[-1])
left_ac.pop(0)
right_ac.pop(0)
# print(left_ac)
# print(right_ac)

left = 0
right = 0
ans = right_ac[-1] + left_ac[-1]
remain = K
while left < N:
    s = S[left]
    while remain > 0 and right < N:
        if S[right] != s:
            if (right + 1 < N and S[right + 1] == s) or (right == N - 1 and S[N - 1] != s):
                remain -= 1
        right += 1

    if s == "L":
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left > 0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)
    else:
        #print(s, right - left, left, right)
        tmp = right - left
        if right == N:
            tmp -= 1
        if right < N - 1:
            tmp += right_ac[-1] - right_ac[right+1]
            tmp += left_ac[-1] - left_ac[right+1]
        if left > 0:
            tmp += left_ac[left - 1] - left_ac[0]
            tmp += right_ac[left - 1] - right_ac[0]
        ans = max(ans, tmp)

    if right == left:
        right += 1
    elif left + 1 < N and S[left + 1] != s:
        remain += 1
    left += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
n,k=map(int,input().split())
l=sorted([len(list(g)) for k,g in groupby(input())])[::-1]
ans=sum(l[:min(2*k,n)])
ans-=(ans==n)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1
     
if min(lrcnt,rlcnt)>=k*2:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  lrcnt-=min(lrcnt,rlcnt)
  rlcnt-=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  if k>=2:
    if lrcnt>0 and rlch>0:
      ans+=1
    if rlcnt>0 and lrch>0:
      ans+=1
  else:
    if lrcnt>0 and rlch>0:
      ans+=1
    else:
      if rlcnt>0 and lrch>0:
        ans+=1
  
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
msg=input()



left=(msg[0]!="R")
right=(msg[-1]!="L")
msg2=msg[1:-1]
cntrl=msg.count("RL")


first=n-cntrl*2-left-right
print("f",first)
ans=first+2*min(k,msg2.count("RL"))
if ans==n:
  ans=n-1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=list(input())

if n==1:
  print(0)
  exit()

count=0
for i in range(1,n//2+1):
  if count==k:
    break
  if s[i-1]==s[i+1] and s[i]!=s[i-1]:
    s[i]=s[i-1]
    count+=1

  if count==k:
    break
  if s[n-i]==s[n-i-2] and s[n-i-1]!=s[n-i]:
    s[n-i-1]=s[n-i]
    count+=1

result=0
if count!=k:
  for i in range(1,n):
    if s[i-1]!=s[i]:
      result+=1
    if count==k:
      break
    
if s[0]=='R' and s[1]=='R':
  result+=1
if s[-1]=='L' and s[-2]=='L':
  result+=1
for i in range(1,n-1):
  if (s[i]=='R' and s[i+1]=='R') or (s[i]=='L' and s[i-1]=='L'):
    result+=1
print(result)

Submission status: Wrong Answer

Solution code:
import math

N,K = list(map(int, input().split()))
S = input()

happiness = 0
unh = 0

for i in range(N-1):
    if S[i] == 'L' and S[i+1] == 'L':
        happiness +=1
    elif S[i] == 'R' and S[i+1] == 'R':
        happiness +=1
    else:
        unh +=1

if K <= math.ceil(unh//2):
    K = K
elif K > math.ceil(unh//2):
    K = math.ceil(unh//2)
    happiness +=1

print(happiness + 2*K)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
S = [0]+list(map(lambda x:-1 if x == "L" else 1, input()))+[0]
be_lists = {-1:[], 0:[], 1:[]}
begin = 0
for i in range(1, N+2):
  if S[i] != S[begin]:
    be_lists[S[begin]].append([begin,i-1])
    begin = i
il_lists = {-1:[], 1:[]}
for d in (-1, 1):
  for i, be in enumerate(be_lists[d]):
    il_lists[d].append([i, be[1]-be[0]+1])
  il_lists[d].sort(key=lambda x:x[1], reverse=True)
ans = 0
for d in (-1,1):
  tmp_S = S.copy()
  for _, il in zip(range(K), il_lists[d]):
    be = be_lists[d][il[0]]
    for i in range(be[0], be[1]+1):
      tmp_S[i] = -d
  tmp_ans = 0
  for i in range(1, N+1):
    if tmp_S[i] == tmp_S[i+tmp_S[i]]:
      tmp_ans += 1
  ans = max(ans, tmp_ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

count=sum(i==0 or S[i-1]!=S[i] for i in range(N))
print(max(count-2*K,1))

Submission status: Wrong Answer

Solution code:
temp = raw_input().split()
S = raw_input()

N = int(temp[0])
K = int(temp[1])

ini_score = 0
ex_count = 0


S = list(S)

for i in range(N-1):
    if S[i] == S[i+1]:
        ini_score = ini_score + 1

num_double_interface = 0
for i in range(1,N-1):
    if (S[i] != S[i-1]) and (S[i]!= S[i+1]):
#        print i, "double"                                                                                                                              
        if S[i] == "L":
            S[i] = "R"
        else:
            S[i] = "L"
        num_double_interface = num_double_interface + 1
        ex_count = ex_count + 1

    if ex_count==K:
        print ini_score + 2*num_double_interface
        exit()


num_single_interface = 0
for i in range(N-1):
    if (S[i]!=S[i+1]):
#        print i, "single"                                                                                                                              
        num_single_interface = num_single_interface + 1
        for j in range(i):
            if S[j] == "L":
                S[j] = "R"
            else:
                S[j] = "L"

    if ex_count== K:
        print ini_score + 2*num_double_interface + 1*num_single_interface
        exit()

print ini_score + 2*num_double_interface + 1*num_single_interface

Submission status: Wrong Answer

Solution code:
import math
input = raw_input

def score(s):
    n = len(s)
    ans = 0
    for i in range(n):
        if s[i] == "L" and i >= 1 and s[i-1] == "L":
            ans += 1
        elif s[i] == "R" and i <= n-2 and s[i+1] =="R":
            ans += 1
    return ans

N,K = map(int, input().split(" "))
S = list(input())

if S=="RL":
    print(0)
    exit()
if S=="LR":
    print(0)
    exit()
if S=="RLR" or S=="LRL":
    if K == 1:
        print(1)
        exit()
    else:
        print(2)
        exit()

s = score(S)
k = 0
lr = 0
rl = 0
for i in range(N-1):
    if S[i] == "L" and S[i+1] =="R":
        lr += 1
    if S[i] == "R" and S[i+1] =="L":
        rl += 1
lrl = 0
rlr = 0
for i in range(N-2):
    if S[i] == "L" and S[i+1] =="R" and S[i+2] =="L":
        lrl += 1
    if S[i] == "R" and S[i+1] =="L" and S[i+2] =="R":
        rlr += 1

if (lrl == 1 and rl == 1 and lr == 1) or (rlr == 1 and rl == 1 and lr == 1):
    if K >= 2:
        print(s+2)
        exit()
    elif K == 1:
        print(s+1)
        exit()
    else:
        print(s)
        exit()

#print(s)
#print(lr, rl)
c = min(K, lr, rl)
s += 2 * c
if K > min(lr, rl):
    if S[0] == "L" and rl > lr:
        s += 1
    elif S[-1] == "R" and rl < lr:
        s += 1
print(s)

Submission status: Wrong Answer

Solution code:
import re

N, K = map(int, input().split())
S = input().strip()

LL = re.finditer('L+', S)
RR = re.finditer('R+', S)

happy = 0
next_happy = []

for l in LL:
    happy += len(l.group(0)) - 1
    if l.regs[0][0] == 0 or l.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for r in RR:
    happy += len(r.group(0)) - 1
    if r.regs[0][0] == 0 or r.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for i in range(K):
    happy += sum(sorted(next_happy, reverse=True)[:K])

if happy >= N:
    happy = N - 1

print(happy)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(input())

if K<N//2+1:
    for i in range(K):
        flag=0
        old=S[0]
        reverse=[]

        for j in range(1,N):
    #         print(j,old,S[j])
            if S[j]!=old:
                reverse.append(j)
                old=S[j]

            if len(reverse)==2:

                for k in range(reverse[0],reverse[1]):

                    if S[k]=='R':
                        S[k]='L'
                    else:
                        S[k]='R'
                break

        if len(reverse)==1:
                 for k in range(reverse[0],N):
                    if S[k]=='R':
                        S[k]='L'
                    else:
                        S[k]='R'
    old='aaa'
    ans=0
    for s in S:
        if old==s:
            ans+=1
        old=s
    print(ans)
else:
    print(N)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

res = 0
pre = None
add = {2: 0}
add[1] = 0 if S[0] == S[-1] else 1
for s in S:
    if pre is None:
        pass
    elif pre == s:
        res += 1
    elif pre == "R":
        add[1] = 0
        add[2] += 1
    else:
        add[1] = 1
    pre = s

if add[2] > K:
    res += 2 * K
    print(res)
else:
    res += 2 * add[2]
    K -= add[2]
    if K > 0:
        res += add[1]
    print(res)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=list(input())
lrcnt=0
rlcnt=0
lrch=0
rlch=0
ans=0

for i in range(len(s)-1):
  if s[i]=='L' and s[i+1]=='R':
    lrcnt+=2
  if s[i]=='L' and s[i+1]=='L':
    ans+=1
  if s[i]=='R' and s[i+1]=='L':
    rlcnt+=2
  if s[i]=='R' and s[i+1]=='R':
    ans+=1
if s[0]=='R':
  lrch+=1
else:
  rlch+=1
if s[-1]=='L':
  lrch+=1
else:
  rlch+=1
     
if min(lrcnt,rlcnt)>=k:
  ans+=k*2
else:
  ans+=min(lrcnt,rlcnt)
  lrcnt-=min(lrcnt,rlcnt)
  rlcnt-=min(lrcnt,rlcnt)
  k-=min(lrcnt,rlcnt)//2
  if k>=2:
    if lrcnt>0 and rlch>0:
      ans+=1
    if rlcnt>0 and lrch>0:
      ans+=1
  else:
    if lrcnt>0 and rlch>0:
      ans+=1
    else:
      if rlcnt>0 and lrch>0:
        ans+=1
  
print(ans)

Submission status: Wrong Answer

Solution code:

def count(S):
    ans = 0
    for i in range(len(S)):
        if S[i] == "L" and i != 0 and S[i - 1] == "L":
            ans += 1
        if S[i] == "R" and i != len(S) - 1 and S[i + 1] == "R":
            ans += 1

    return ans


def move(S):
    start = 0
    for i in range(len(S) - 1):
        if S[i] != S[i + 1]:
            start = i
            break

    end = 0
    for i in range(len(S) - 1):
        index = (len(S) - 1) - i
        if S[index] == S[start] and S[index - 1] == S[start + 1]:
            end = index
            break

    if end < start:
        return S

    start = start + 1
    end = end - 1

    reverce = list()
    for i in reversed(S[start:end + 1]):
        if i == "R":
            reverce.append("L")
        else:
            reverce.append("R")

    pre = S[:start]
    nex = S[end+1:]

    ans = list()
    ans.extend(pre)
    ans.extend(reverce)
    ans.extend(nex)

    return ans


def move2(S):
    start = 0
    for i in range(len(S) - 1):
        if S[i] != S[i + 1]:
            start = i
            break

    reverce = list()
    for i in reversed(S[:start+1]):
        if i == "R":
            reverce.append("L")
        else:
            reverce.append("R")

    nex = S[start+1:]
    ans = list()
    ans.extend(reverce)
    ans.extend(nex)

    return ans


N, K = map(int, input().split())
S = list(input())

for i in range(K):
    S1 = move(S)
    if count(S1) == count(S):
        S2 = move2(S)
        if count(S2) > count(S1):
            S = S2
        else:
            S = S1
    else:
        S = S1


ans = count(S)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from pprint import pprint
N, K  = map(int,input().split())
S = input()

same_L_cnt = 0

up_cnt = 0
same_cnt = 0
before = ""
buf = []
st = 0
ed = 0


    
for i in range(N):
    now_S = S[i]
    if now_S == 'L':
        now_S = 1
    else:
        now_S = 0
    # 前回と同じか確認
    if now_S == before:
        # 同じならカウントアップ
        same_cnt += 1
    # L R R ..... R Lが出現したか確認する
    buf.append(now_S)
    #print('------ buffer ------')
    #pprint(buf)
    #print('--------------------')
    if buf[0] != 1:
        buf = []
    elif len(buf) == 2 and buf[1] == 1:
        buf = [1]
    elif len(buf) < 3:
        pass
    else:
        t = buf[1:len(buf)-2]
        if sum(t) != 0:
            buf = []
        elif buf[len(buf)-1] == 1:
            up_cnt += 1
            buf = [1]
        else:
            pass
    before = now_S
#print(up_cnt)
#print(same_cnt)

# 右端パターン 
up_R_flg = 0
if sum(buf) == 1:
    up_R_flg = 1
# 右端パターン
# 右端Rで一回でもLが出ていれば+1できる
up_L_flg = 0
if S[0] == "R" and same_cnt != N-1:
    up_L_flg = 1

pt = same_cnt
rev_cnt = min(K , up_cnt)
remain_cnt = K - rev_cnt
extra = 0
if up_R_flg and remain_cnt > 0:
    extra += 1
    remain_cnt -= 1
cnt = same_cnt + rev_cnt * 2 + extra


if up_L_flg and remain_cnt and cnt != N-1:
    cnt += 1
print(cnt)