当前位置:网站首页>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
边栏推荐
- Leetcode t49: grouping of alphabetic words
- Progress in architecture
- Innovation and development of independent industrial software
- Oppo find N2 product form first exposure: supplement all short boards
- Use of arouter
- AI and Life Sciences
- Nowcoder rearrange linked list
- 数据湖(十三):Spark与Iceberg整合DDL操作
- 测试流程整理(2)
- ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
猜你喜欢
Use of tiledlayout function in MATLAB
No servers available for service: xxxx
【信息检索】链接分析
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
一文概览2D人体姿态估计
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
flink sql-client. SH tutorial
Data center concept
Oppo find N2 product form first exposure: supplement all short boards
随机推荐
SqlServer函数,存储过程的创建和使用
ViewModel 初体验
Opencv3.2 and opencv2.4 installation
No servers available for service: xxxx
Oppo find N2 product form first exposure: supplement all short boards
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
92.(cesium篇)cesium楼栋分层
为什么图片传输要使用base64编码
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
opencv3.2 和opencv2.4安装
数据湖(十三):Spark与Iceberg整合DDL操作
Real time data warehouse
AI and Life Sciences
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
Nowcoder rearrange linked list
一种架构来完成所有任务—Transformer架构正在以一己之力统一AI江湖
NowCoder 反转链表
The game goes to sea and operates globally
Industrial Internet has greater development potential and more industry scenarios
第十六章 字符串本地化和消息字典(二)