当前位置:网站首页>codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
2022-07-04 12:52:00 【白速龙王的回眸】
分析
我们想让f最小
边界情况:没有1答案总是0
然后的话每个1,如果在中间的话贡献一定是1 + 10 = 11
唯一可能降低贡献的就是放在一头一尾
放在头:贡献变成10
放在尾:共享变成1
于是我们统计s所有1离头和尾最近的距离d1和d2
然后优先走到尾
判断k的取值是否大于等于d1 + d2, d2, d1分成四个状态
特别地,如果只有一个1的情况下,不可能又去头又去尾巴
所以这种情况当其大于等于d1 + d2就去d2即可
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)
总结
思维 + 贡献 + 边界处理
边栏推荐
- Leetcode T47: 全排列II
- Introducing testfixture into unittest framework
- 吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
- 为什么图片传输要使用base64编码
- 第十七章 进程内存
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- 聊聊保证线程安全的 10 个小技巧
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
- Unity Shader学习(三)试着绘制一个圆
猜你喜欢
C # WPF realizes the real-time screen capture function of screen capture box
Hardware Basics - diode Basics
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
sql优化之explain
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Product identification of intelligent retail cabinet based on paddlex
Install MySQL
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
Excel快速合并多行数据
软件测试之测试评估
随机推荐
ViewModel 初体验
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
MySQL之详解索引
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
Test process arrangement (3)
海外游戏代投需要注意的
Incremental ternary subsequence [greedy training]
opencv3.2 和opencv2.4安装
为什么图片传输要使用base64编码
Leetcode T47: 全排列II
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
gin集成支付宝支付
LifeCycle
Learning projects are self-made, and growth opportunities are self created
Error in find command: paths must precede expression (turn)
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
redis 日常笔记
奇妙秘境 码蹄集
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
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions