当前位置:网站首页>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)
总结
思维 + 贡献 + 边界处理
边栏推荐
- Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- IP lab monthly resumption · issue 5
- [FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
- Whether the loyalty agreement has legal effect
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
- Excel快速合并多行数据
- docker-compose公网部署redis哨兵模式
- MySQL的存储过程练习题
- Excel quickly merges multiple rows of data
猜你喜欢

Learn kernel 3: use GDB to track the kernel call chain

C# wpf 实现截屏框实时截屏功能
![递增的三元子序列[贪心训练]](/img/92/7efd1883c21c0e804ffccfb2231602.png)
递增的三元子序列[贪心训练]

RK1126平台OSD的实现支持颜色半透明度多通道支持中文

为什么图片传输要使用base64编码

商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?

Innovation and development of independent industrial software

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

Ruiji takeout notes

Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
随机推荐
MATLAB中tiledlayout函数使用
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Leetcode T49: 字母异位词分组
Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
GCC [6] - 4 stages of compilation
What is the real meaning and purpose of doing things, and what do you really want
Detailed index of 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
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
基于51单片机的超声波测距仪
Vscode common plug-ins summary
Excel quickly merges multiple rows of data
How to operate and invest games on behalf of others at sea
sql优化之查询优化器
Basic mode of service mesh
Gorm read / write separation (rotation)
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
Hardware Basics - diode Basics