当前位置:网站首页>Codeforce:c. sum of substrings
Codeforce:c. sum of substrings
2022-07-04 14:27:00 【White speed Dragon King's review】
analysis
We want to make f Minimum
Boundary situation : No, 1 The answer is always 0
Then each 1, If in the middle, the contribution must be 1 + 10 = 11
The only way to reduce the contribution is to put it on one end
Put it on your head : Contribution becomes 10
Put at the end : Sharing becomes 1
So we count s all 1 The closest distance from the head and tail d1 and d2
Then go to the end first
Judge k Whether the value of is greater than or equal to d1 + d2, d2, d1 It is divided into four states
Specially , If only one 1 Under the circumstances , It's impossible to go head and tail again
So this situation should be greater than or equal to d1 + d2 Just go to d2 that will do
ac code
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = list(map(int, input().split()))
s = input()
cnt_1 = 0
for c in s:
if c == '1':
cnt_1 += 1
if cnt_1 == 0:
print(0)
continue
d1, d2 = 0, 0 # d1 from head, d2 from tail
for i in range(n):
if s[i] == '1':
d1 = i
break
for i in range(n):
if s[n - 1 - i] == '1':
d2 = i
break
# 1: not change
# 2: go to head
# 3: go to tail
# 10: go to tail and head
# we are delighted to go to tail first, and secondly go to head
flag = 1
if k >= d1 + d2:
flag = 10
elif k >= d2:
flag = 3
elif k >= d1:
flag = 2
else:
flag = 1
# different cases
ans = 0
if flag == 10:
if cnt_1 >= 2:
ans = 1 + 10 + 11 * (cnt_1 - 2) # cnt_1 >= 2
else:
ans = 1
elif flag == 3:
ans = 1 + 11 * (cnt_1 - 1)
elif flag == 2:
ans = 10 + 11 * (cnt_1 - 1)
else:
ans = 11 * cnt_1
print(ans)
summary
thinking + contribution + Boundary treatment
边栏推荐
- Solutions to the problems of miui12.5 red rice k20pro using Au or povo2
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- 数据埋点的一些问题和想法
- [matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
- Excel quickly merges multiple rows of data
- Test process arrangement (3)
- Opencv3.2 and opencv2.4 installation
- Compile oglpg-9th-edition source code with clion
- LiveData
- How to operate and invest games on behalf of others at sea
猜你喜欢
随机推荐
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
测试流程整理(3)
流行框架:Glide的使用
软件测试之测试评估
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
游戏出海,全球化运营
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
ViewModel 初体验
Blob, text geometry or JSON column'xxx'can't have a default value query question
Leetcode T48: rotating images
AI与生命科学
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Gorm data insertion (transfer)
GCC [6] - 4 stages of compilation
尊重他人的行为
DDD application and practice of domestic hotel transactions -- Code
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Count the running time of PHP program and set the maximum running time of PHP
Visual Studio调试方式详解