感觉分类讨论的能有点弱。遇到复杂一点的分类讨论的题目,代码就写的巨长。

首先观察到处在中间位置的1对答案的贡献是11,具体在中间哪个位置是没有关系的。

只有两端的两个位置是比较特殊的

\(1位置处的1对答案的贡献是10\)

\(2位置处的1对答案的贡献是1\)

所有我们考虑将最左端第一次出现的1放到1位置

将最右端第一次出现的1放到n的位置。

贪心的考虑,如果能进行第二个操作我们优先进行第2个操作,因为我们希望答案最小,不能进行第2个操作的情况下我们进行第1种操作。

\(操作完后如果1位置能有1我们就将答案-1\)

\(如果n位置能有1我们就将答案-10\)

代码很丑\(qwq\)

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
#define x first
#define y second
#define pb push_back using namespace std;
const int N=2e5+10;
char a[N];
void solve()
{
int n,k;cin>>n>>k;
cin>>(a+1);
int l1=1,r1=n,cnt=0;
rep(i,1,n) if(a[i]=='1') cnt++;
//找左边第1个1的位置
rep(i,1,n) if(a[i]=='1')
{
l1=i;
break;
} //找右边第一个1的位置
fep(i,n,1) if(a[i]=='1')
{
r1=i;
break;
} if(cnt==0)
{
cout<<0<<endl;
return;
}
//只有1个1
else if(cnt==1)
{
int dl=l1-1,dr=n-r1;
//先判是否能道右边
if(k>=dr)
{
cout<<1<<endl;
return;
}
//左边
if(k>=dl)
{
cout<<10<<endl;
return;
}
else out<<11<<endl;
}
else
{
int dl=l1-1,dr=n-r1;
int ans=11*cnt;
//先判是否能到右边
if(k>=dr) ans-=10,k-=dr;
//左边
if(k>=dl) ans-=1;
cout<<ans<<endl;
} }
signed main()
{
IOS
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}

CodeCraft-22 and Codeforces Round 795 (Div. 2)C. Sum of Substrings(分类讨论、贪心)的更多相关文章

  1. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  2. Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B

    原博主:https://blog.csdn.net/amovement/article/details/80358962 B. Bus of Characters time limit per tes ...

  3. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  4. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  5. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

    A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  6. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  7. Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)

    https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...

  8. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心

    D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...

  9. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

  10. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

随机推荐

  1. Windows 10 关闭搜索栏中“热门搜索”的显示。

    Windows 10 关闭搜索栏中"热门搜索"的显示. 任务栏取消"显示搜索突出显示"的设置可能无法取消"热门搜索"的显示, 这就需要您尝试 ...

  2. Natapp 邀请码 积分

    邀请码: 29F145FC 充值95折

  3. TienChin-课程管理-删除课程

    CourseController.java @PreAuthorize("hasPermission('tienchin:course:remove')") @Log(title ...

  4. ansible使用,搭建mongo的replica-set小结

    ansible 前言 常用到的指令 查看ip是否可用 执行 执行,查看日志输出 查看这个 playbook 的执行会影响到哪些 hosts 设置服务器免密登录 ansible了解 变量名的使用 pla ...

  5. Go语言的100个错误使用场景(一)|代码和项目组织

    目录 前言 1. Go: Simple to learn but hard to master 1.1 Go 语言概述 1.2 简单不等于容易 1.3 使用 Go 的100个错误 2. Code an ...

  6. MySQL 之高级命令(精简笔记)

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...

  7. Metasploit 生成各种后门

    Metasploit 是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,同时该工具也是渗透测试环境中的利器,它支持多平台Payload的生成具有完全的跨平台性 ...

  8. css 修改复选框的样式

    效果图: 实现代码如下: /* 选中input标签类型为复选框的 */ input[type="checkbox"] { width: 16px; height: 16px; ve ...

  9. php批量更新多条数据

    /** * @param $table 表名 * @param array $multipleData 拼接的批量更新的数组格式: * $students = [['id' => 1, 'cit ...

  10. 针对SpringBoot服务端返回的空对象和空数组问题

    返回的Json会自动过滤掉空指针的对象,但是若遇到非空指针的没有任何内容的对象,举例如下: public class Person { private String name; private Int ...