【Round #36 (Div. 2 only) B】Safe Spots
【题目链接】:https://csacademy.com/contest/round-36/task/safe-spots/
【题意】
给你n个数字构成的序列;
每个位置上的数都由0和1组成;
对于每个0;
假设其位置为i;
如果[i-k..i+k]这个范围内1的个数不超过1,则称它合法;
问符合要求的这样的0的个数.
【题解】
前缀和.
直接获取sum[i+k]-sum[i-k-1]就是这个范围里面1的个数了;
(程序用的是其他方法..维护第i个数字前k个数里面有多少个1。以及后面k个数字里面有多少个1)
【Number Of WA】
0
【反思】
第一反应不是前缀和的做法说明我还很菜?
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5;
int n,k,a[N+100],pre[N+100],after[N+100],now;
int main(){
//Open();
Close();
cin >> n >> k;
rep1(i,1,n)
cin >> a[i];
now = 0;
rep1(i,1,n){
pre[i] = now;
if (a[i]==1) now++;
if (i-k>=1 && a[i-k]==1) now--;
}
now = 0;
rep2(i,n,1){
after[i] = now;
if (a[i]==1) now++;
if (i+k<=n && a[i+k]==1) now--;
}
int ans = 0;
rep1(i,1,n)
if (a[i]==0 && pre[i]+after[i]<=1)
ans++;
cout << ans << endl;
return 0;
}
【Round #36 (Div. 2 only) B】Safe Spots的更多相关文章
- 【Round #36 (Div. 2 only) C】Socks Pairs
[题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...
- 【CS Round #36 (Div. 2 only) A】Bicycle Rental
[题目链接]:https://csacademy.com/contest/round-36/task/bicycle-rental/ [题意] 让你从n辆车中选一辆车; 每一辆车有3个属性 1.到达车 ...
- 【CS Round #37 (Div. 2 only) D】Reconstruct Graph
[Link]:https://csacademy.com/contest/round-37/task/reconstruct-graph/statement/ [Description] 给你一张图; ...
- 【CS Round #37 (Div. 2 only) B】Group Split
[Link]:https://csacademy.com/contest/round-37/task/group-split/ [Description] 让你把一个数分成两个数a.b的和; (a,b ...
- 【CS Round #37 (Div. 2 only) A】Boring Number
[Link]:https://csacademy.com/contest/round-37/task/boring-number/ [Description] 让你找离平均数最近的一个数的下标; [S ...
- 【CS Round #39 (Div. 2 only) D】Seven-segment Display
[Link]:https://csacademy.com/contest/round-39/task/seven-segment-display/ [Description] 0..9各自有一个数字, ...
- 【CS Round #39 (Div. 2 only) C】Reconstruct Sum
[Link]:https://csacademy.com/contest/round-39/task/reconstruct-sum/ [Description] 给你一个数字S; 让你找有多少对A, ...
- 【CS Round #39 (Div. 2 only) B】Circle Elimination
[Link]:https://csacademy.com/contest/round-39/task/circle-elimination/ [Description] [Solution] 把n个点 ...
- 【CS Round #39 (Div. 2 only) A】Removed Pages
[Link]: [Description] [Solution] 每读入一个x; 把a[(x-1)/2]置为1即可; 统计1的个数 [NumberOf WA] [Reviw] [Code] /* */ ...
随机推荐
- js邮箱,汉字,数字 表单验证
//电子邮箱验证 function isEmail(str) { var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_ ...
- 数字游戏(string的sort的应用)
题目描述 牛牛举办了一场数字游戏,有n个玩家参加这个游戏,游戏开始每个玩家选定一个数,然后将这个数写在纸上(十进制数,无前缀零),然后接下来对于每一个数字将其数位按照非递减顺序排列,得到新的数,新数的 ...
- FCC高级编程篇之Symmetric Difference
Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...
- split方法切割数组
指定的字符串按"o"截取 当一个base64需要剪去前面的部分的时候 var params={ "imgJustBase64":this.zheng.split ...
- 全国所有省市县地理坐标Json格式
https://www.cnblogs.com/yzbubble/p/7707129.html
- 紫书 习题 11-16 UVa 1669(树形dp)
想了很久, 以为是网络流最大流, 后来建模建不出来, 无奈. 后来看了 https://blog.csdn.net/hao_zong_yin/article/details/79441180 感觉思路 ...
- Hello World FastCGI
什么是FastCGI,google吧,測试一个用C++实现的FastCGI程序. 1, Nginx 安装.http://nginx.org/en/download.html.下载解压.configur ...
- java生成MD5校验码
在Java中,java.security.MessageDigest (rt.jar中)已经定义了 MD5 的计算,所以我们只需要简单地调用即可得到 MD5 的128 位整数.然后将此 128 位计 ...
- php中的页面跳转和重定向
php中的页面跳转和重定向 ThinkPHP中跳转和重定向的区别 跳转: 浏览器认为: 当前URL请求成功, 重新请求新的URL. 浏览器会 记录当前的URL 和 新的URL 在请求历史记录中. 回退 ...
- [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)
题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...