当前位置:网站首页>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
边栏推荐
- 软件测试之测试评估
- MySQL triggers
- Learn kernel 3: use GDB to track the kernel call chain
- The font of markdown grammar is marked in red
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
- scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
- MATLAB中tiledlayout函数使用
- Visual Studio调试方式详解
- 测试流程整理(3)
猜你喜欢
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Chapter 17 process memory
MySQL之详解索引
按照功能对Boost库进行分类
聊聊保证线程安全的 10 个小技巧
flink sql-client. SH tutorial
Leetcode 61: rotating linked list
递增的三元子序列[贪心训练]
随机推荐
Digi重启XBee-Pro S2C生产,有些差别需要注意
Oppo find N2 product form first exposure: supplement all short boards
第十六章 字符串本地化和消息字典(二)
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
gin集成支付宝支付
产业互联网则具备更大的发展潜能,具备更多的行业场景
Abnormal value detection using shap value
opencv3.2 和opencv2.4安装
The game goes to sea and operates globally
Sqlserver functions, creation and use of stored procedures
海外游戏代投需要注意的
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
Gorm read / write separation (rotation)
92.(cesium篇)cesium楼栋分层
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
实战解惑 | OpenCV中如何提取不规则ROI区域
MySQL triggers
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Solutions to the problems of miui12.5 red rice k20pro using Au or povo2