結果
| 問題 |
No.3114 0→1
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-19 00:14:54 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,261 bytes |
| コンパイル時間 | 544 ms |
| コンパイル使用メモリ | 12,032 KB |
| 実行使用メモリ | 22,336 KB |
| 最終ジャッジ日時 | 2025-04-19 00:14:58 |
| 合計ジャッジ時間 | 3,418 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | WA * 30 |
ソースコード
def min_operations(n, s):
# For each position, track the excess of 0s over 1s
# in the suffix starting at that position
suffix_balance = [0] * (n + 1)
# Calculate the suffix balance
for i in range(n - 1, -1, -1):
if s[i] == '0':
suffix_balance[i] = suffix_balance[i + 1] + 1
else: # s[i] == '1'
suffix_balance[i] = suffix_balance[i + 1] - 1
# Initialize an array to track the minimum operations needed
# to make each suffix a good string
min_ops = [0] * (n + 1)
# Process the string from right to left
for i in range(n - 1, -1, -1):
if s[i] == '0':
# If adding a 0 creates an imbalance, we need to convert it
excess = suffix_balance[i]
if excess > 0:
# We need to convert this 0 to 1
min_ops[i] = min_ops[i + 1] + 1
else:
# We can keep this 0
min_ops[i] = min_ops[i + 1]
else: # s[i] == '1'
# Adding a 1 always helps the balance
min_ops[i] = min_ops[i + 1]
return min_ops[0]
def main():
n = int(input())
s = input().strip()
print(min_operations(n, s))
if __name__ == "__main__":
main()