C. Sum of Substrings题解
C. Sum of Substrings
题目大概意思,给你一个01串,求和最小,其中和是该串所有相邻字符所组成的十进制数的和。
如:0110, sum = 01 + 11 + 10 = 22。
通过观察我们可以发现,除了第一个位置和最后一个位置,其他位置上的1对和的贡献都是11。
所以我们只需要特殊考虑挪1到第一个和最后一个位置上。
题目没说必须挪,所以我们可以不用挪。
另外,注意只有一个1的情况!
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t, n, k;
char st[N];
signed main(){
cin >> t;
while(t --){
cin >> n >> k;
memset(st, 0, sizeof st);
cin >> st;
int pos1 = 0, pos2 = 0;
int cnt = 0;
bool flag = false;
for(int i = 0; i < strlen(st); i++){
if(pos1 == 0 && st[i] == '1' && flag == false){
pos1 = i;
flag = true;
}
if(st[i] == '1') cnt ++;
}
for(int i = n - 1; i >= 0; i--){
if(pos2 == 0 && st[i] == '1'){
pos2 = i;
break;
}
}
if(cnt == 0){
cout << 0 << endl;
continue;
}
if(k == 0){
int res = 0;
if(st[0] == '1') res = 10, cnt --;
if(st[n - 1] == '1') res += 1, cnt --;
cout << res + cnt * 11 << endl;
continue;
}
if(cnt == 1){
if(k >= n - pos2 - 1){
cout << 1 << endl;
}
else if(k >= pos1){
cout << 10 << endl;
}
else cout << 11 << endl;
continue;
}
int ans = 0;
if(pos2 == n - 1){
ans = 1;
cnt --;
}
else{
if(k >= n - pos2 - 1){
k -= n - pos2 - 1;
ans += 1;
cnt --;
}
}
if(pos1 == 0){
ans += 10;
cnt --;
}
else{
if(k >= pos1){
ans += 10;
cnt --;
}
}
cout << ans + 11 * cnt << endl;
}
return 0;
}
C. Sum of Substrings题解的更多相关文章
- CF 1400F x-prime Substrings 题解【AC自动机+DP】
CF 1400F.x-prime Substrings 题意: 给定一个由\('1'\)到\('9'\)组成的字符串\(s\)和一个数\(x\),定义一个串为\(x-prime\)串,当且仅当这个串上 ...
- [LeetCode] Range Sum Query - Mutable 题解
题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...
- Educational Codeforces Round 37-F.SUM and REPLACE题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...
- CF102B Sum of Digits 题解
Content 给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和.问你几次操作之后可以将 \(n\) 变为一位数. 数据范围:\(1\leqslant n\leqsla ...
- POJ3415:Common Substrings——题解
http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...
- SPOJ8222/NSUBSTR:Substrings——题解
https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...
- SPOJ694/DISUBSTR:Distinct Substrings——题解
https://vjudge.net/problem/SPOJ-DISUBSTR https://www.luogu.org/problemnew/show/SP694 http://www.spoj ...
- CF1139A Even Substrings 题解
Content 有一个长度为 \(n\) 的数字串 \(s\),试求出代表偶数的子串个数. 数据范围:\(1\leqslant n\leqslant 65000\),\(s\) 仅包含数字 \(1\s ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
随机推荐
- [转帖]nginx 剖析 request_time和upstream_response_time的误区、区别
https://cloud.tencent.com/developer/article/1767981 首先,澄清一个误区 upstream_response_time必须在upstream配置时才能 ...
- Python学习之十六_virsh批量获取虚拟机IP地址的方法
Python学习之十六_virsh批量获取虚拟机IP地址的方法 Linux命令说明 for j in \ $(for i in `virsh list |grep -v Id |grep runnin ...
- [转帖]快速定位MySQL数据库当前消耗CPU最高的sql语句
概述 One of our customers recently asked whether it is possible to identify, from the MySQL side, the ...
- 【转帖】淫技巧 | 如何查看已连接的wifi密码
主题使用方法:https://github.com/xitu/juejin-markdown-themes theme: juejin highlight: github 一.引言 在实际工作中,常常 ...
- [转帖]Springboot容器化镜像设置堆内存大小
参考资料:Best Practices: Java Memory Arguments for Containers - DZone Java 有三种方式设置最大堆内存大小: 1. -Xmx 2. -X ...
- [转帖]腾讯北极星 Polaris 试用
https://www.cnblogs.com/QIAOXINGXING001/p/15482012.html 了解.试用 昨天稀土开发者大会2021提到了腾讯开源的北极星, 试用一下; 官网: 北极 ...
- 银河麒麟v10 安装 virt-manager 的过程
上个月公司购置一台飞腾2000+ 银河麒麟v10的机器. 想着能够利用虚拟化 安装一下虚拟机 提高测试灵活度 找了一下原厂要了一下相关的命令 在这里进行一下测试工作. 第一步: 安装必备的包 yum ...
- 《PalWorld/幻兽帕鲁》旧电脑linux搭建服务器
关键词: PalWorld, Linux, Natapp, 内网穿透, 幻兽帕鲁 注 意 文 章 时 效 性 最近幻兽帕鲁爆火,steam一上线好友列表一串正在游玩哈哈哈,自己也是蹭一波热度,顺便试用 ...
- Unity Editor开发中查找属性的两种写法对比
从2017开始,在editor脚本中查找属性是这样写的 var m_Script = serializedObject.FindProperty("m_Script"); Seri ...
- x86 x64 arm64的区别
我们常说的高通 865,麒麟990 不是 CPU 是 SoC(System On Chip),SoC 除了 CPU 外,还有 GPU,还有可选的浮点数加速器,专用于深度模型的加速器,等等.除此以外,S ...