Submission #36481667


Source Code Expand

Copy
from collections import deque
N,K = map(int, input().split())
S = list(input())
# S0/1
cnt = 1
before = S[0]
conv_S = deque() # 0/1
is_handstand_deq = deque() #
for i in range(1,N):
if S[i] == before:
cnt += 1
else:
conv_S.append(cnt)
is_handstand_deq.append(before)
cnt = 1
before = S[i]
conv_S.append(cnt)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
from collections import deque

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

# Sを0/1の連続個数に変換する
cnt = 1
before = S[0]
conv_S = deque()            # 0/1が何連続しているか
is_handstand_deq = deque()  # 初期で逆立ちしているか

for i in range(1,N):
    if S[i] == before:
        cnt += 1
    else:
        conv_S.append(cnt)
        is_handstand_deq.append(before)
        cnt = 1
        before = S[i]

conv_S.append(cnt)
is_handstand_deq.append(before)

ans = 0

section = deque()   # 注目する人の並び
section_sum = 0     # 上記内での合計人数
remain_order = K    # 指示できる残り回数

while len(conv_S) != 0:

    if is_handstand_deq[0] == "1":
        # 逆立している人は無条件に加えてOK
        person_cnt = conv_S.popleft()
        is_handstand = is_handstand_deq.popleft()

        section.append((person_cnt,is_handstand))
        section_sum += person_cnt

    elif is_handstand_deq[0] == "0":
        # 直立している人には指示が必要
        if remain_order == 0:
            # 指示できる回数が0だったら、指示回数を回復するまで左端の方を繰り出す
            while len(section) != 0:
                person_cnt,is_handstand = section.popleft()
                section_sum -= person_cnt

                if is_handstand == "0":
                    remain_order += 1
                    break
        
        person_cnt = conv_S.popleft()
        is_handstand = is_handstand_deq.popleft()
        
        section.append((person_cnt,is_handstand))
        remain_order -= 1
        section_sum += person_cnt
    
    ans = max(ans,section_sum)

print(ans)

Submission Info

Submission Time
Task D - Handstand
User tonomotohide
Language Python (3.8.2)
Score 400
Code Size 1750 Byte
Status AC
Exec Time 134 ms
Memory 14972 KB

Judge Result

Set Name All Sample
Score / Max Score 400 / 400 0 / 0
Status
AC × 18
AC × 3
Set Name Test Cases
All sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15
Sample sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 AC 23 ms 9320 KB
sample_02 AC 19 ms 9408 KB
sample_03 AC 24 ms 9120 KB
testcase_01 AC 35 ms 9952 KB
testcase_02 AC 36 ms 9552 KB
testcase_03 AC 85 ms 10480 KB
testcase_04 AC 19 ms 9384 KB
testcase_05 AC 129 ms 11444 KB
testcase_06 AC 134 ms 12588 KB
testcase_07 AC 125 ms 14972 KB
testcase_08 AC 121 ms 14952 KB
testcase_09 AC 127 ms 14604 KB
testcase_10 AC 39 ms 9960 KB
testcase_11 AC 35 ms 9632 KB
testcase_12 AC 34 ms 9452 KB
testcase_13 AC 39 ms 10084 KB
testcase_14 AC 37 ms 9944 KB
testcase_15 AC 21 ms 9156 KB


2025-04-15 (Tue)
10:48:44 +00:00