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题解的更多相关文章

  1. CF 1400F x-prime Substrings 题解【AC自动机+DP】

    CF 1400F.x-prime Substrings 题意: 给定一个由\('1'\)到\('9'\)组成的字符串\(s\)和一个数\(x\),定义一个串为\(x-prime\)串,当且仅当这个串上 ...

  2. [LeetCode] Range Sum Query - Mutable 题解

    题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...

  3. Educational Codeforces Round 37-F.SUM and REPLACE题解

    一.题目 二.题目链接 http://codeforces.com/contest/920/problem/F 三.题意 给定$N$个范围在$[1, 1e6)$的数字和$M$个操作.操作有两种类型: ...

  4. CF102B Sum of Digits 题解

    Content 给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和.问你几次操作之后可以将 \(n\) 变为一位数. 数据范围:\(1\leqslant n\leqsla ...

  5. POJ3415:Common Substrings——题解

    http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...

  6. SPOJ8222/NSUBSTR:Substrings——题解

    https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...

  7. SPOJ694/DISUBSTR:Distinct Substrings——题解

    https://vjudge.net/problem/SPOJ-DISUBSTR https://www.luogu.org/problemnew/show/SP694 http://www.spoj ...

  8. CF1139A Even Substrings 题解

    Content 有一个长度为 \(n\) 的数字串 \(s\),试求出代表偶数的子串个数. 数据范围:\(1\leqslant n\leqslant 65000\),\(s\) 仅包含数字 \(1\s ...

  9. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  10. 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 ...

随机推荐

  1. 【面试题精讲】Redis如何实现分布式锁

    首发博客地址 系列文章地址 Redis 可以使用分布式锁来实现多个进程或多个线程之间的并发控制,以确保在给定时间内只有一个进程或线程可以访问临界资源.以下是一种使用 Redis 实现分布式锁的常见方法 ...

  2. MySQL shell 备份数据库

    MySQL shell 备份数据库 背景 之前使用 mysqldump 和 mysql source 的方式备份数据库非常缓慢 有时候要耗费非常长的时间 今天发现有一个可以快速备份数据库的 mysql ...

  3. [转帖]快速定位MySQL数据库当前消耗CPU最高的sql语句

    概述 One of our customers recently asked whether it is possible to identify, from the MySQL side, the ...

  4. [转帖]带你重走 TiDB TPS 提升 1000 倍的性能优化之旅

    https://tidb.net/blog/29074d86#TiDB%20%E6%80%A7%E8%83%BD%E5%92%8C%E7%A8%B3%E5%AE%9A%E6%80%A7%E7%9A%8 ...

  5. 【转帖】MySQL 8.0 hash join有重大缺陷?

    我并不这么看. 友情提醒:本文建议在PC端阅读. 徐春阳老师发文爆MySQL 8.0 hash join有重大缺陷. 文章核心观点如下:多表(比如3个个表)join时,只会简单的把表数据量小的放在前面 ...

  6. Ergonomics JVM 的一种FullGC的说明

    https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html 2 Ergonomics Ergo ...

  7. [转贴]细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4

    细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4 https://www.cnblogs.com/malecrab/p/5300503.html 1. U ...

  8. 京东金融APP-新交互技术“虚拟数字人”赋能世界杯主题营销

    作者:平台研发部,智能服务与产品部 距离加文·伍德提出web3.0已经过去8年时间,这8年加文·伍德创建的以太坊大放异彩,同时由web3.0引出的数字人.元宇宙也生根发芽,茁壮成长,带来了非凡的用户体 ...

  9. 【小实验】使用 wrk 的 docker 容器来压测另一个容器

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 GET 请求 想压测容器环境的服务性能,发现两个麻烦: 本 ...

  10. 【scikit-learn基础】--『回归模型评估』之准确率分析

    分类模型的评估和回归模型的评估侧重点不一样,回归模型一般针对连续型的数据,而分类模型一般针对的是离散的数据. 所以,评估分类模型时,评估指标与回归模型也很不一样,比如,分类模型的评估指标通常包括准确率 ...