当前位置:网站首页>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)
总结
思维 + 贡献 + 边界处理
边栏推荐
- Test process arrangement (3)
- Assertion of unittest framework
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
- Leetcode T47: 全排列II
- 聊聊保证线程安全的 10 个小技巧
- GCC [6] - 4 stages of compilation
- 富文本编辑:wangEditor使用教程
- Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
- [FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
猜你喜欢
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
软件测试之测试评估
Vscode common plug-ins summary
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Unity Shader学习(三)试着绘制一个圆
Introducing testfixture into unittest framework
迅为IMX6Q开发板QT系统移植tinyplay
sharding key type not supported
NowCoder 反转链表
DDD application and practice of domestic hotel transactions -- Code
随机推荐
LiveData
R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
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
Error in find command: paths must precede expression (turn)
Rich text editing: wangeditor tutorial
docker-compose公网部署redis哨兵模式
LifeCycle
How to operate and invest games on behalf of others at sea
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
Basic mode of service mesh
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
Use of tiledlayout function in MATLAB
【信息检索】链接分析
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
数据仓库面试问题准备
Blob, text geometry or JSON column'xxx'can't have a default value query question
第十七章 进程内存
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
基于51单片机的超声波测距仪
ViewModel 初体验