当前位置:网站首页>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
边栏推荐
- R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
- 为什么图片传输要使用base64编码
- 基于51单片机的超声波测距仪
- 测试流程整理(3)
- The game goes to sea and operates globally
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
- AI与生命科学
- opencv3.2 和opencv2.4安装
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
猜你喜欢
随机推荐
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
Leetcode T49: 字母异位词分组
MySQL的存储过程练习题
LifeCycle
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Oppo find N2 product form first exposure: supplement all short boards
Leetcode T48:旋转图像
10.(地图数据篇)离线地形数据处理(供Cesium使用)
一文概览2D人体姿态估计
Real time data warehouse
海外游戏代投需要注意的
sql优化之查询优化器
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
数据湖(十三):Spark与Iceberg整合DDL操作
利用Shap值进行异常值检测
Chapter 17 process memory
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
使用CLion编译OGLPG-9th-Edition源码




![去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)




