【题目链接】: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;
}

最新文章

  1. HTML DOM scale() 方法
  2. [转]为何需要调用“super viewDidLoad
  3. secure erase 时必须umount
  4. 让谷歌浏览器 chrome 支持小于12px的字体
  5. CodeForces 146A Lucky Ticket
  6. 一. Logback与p6spy
  7. andorid源码中察看版本
  8. Git 安装与使用(二)
  9. 【USACO 3.1.1】最短网络
  10. [Cacti] memcache安装执行、cacti监控memcache实战
  11. 在X64系统中PowerDesigner无法连接MySQL的解决方法
  12. 【审核】检查iOS项目中是否使用了IDFA
  13. C# - 设计模式 - 虚模式
  14. js 内置对象参考 (Array,String, Math, Data, Number)
  15. linux udp 函数说明
  16. Caffe多线程环境下检测缓慢问题
  17. Centos 6.8 系统升级默认的Python版本
  18. 转换 transform
  19. ThinkPHP联表查询
  20. Android开发:轻松实现图片倒影效果

热门文章

  1. hdu 1257/1800 - 贪心,dp
  2. hiho1079 - 数据结构 线段树(入门题,离散化)
  3. shell简单监控脚本模板
  4. js&amp;jQ判断checkbox表单是否被选中
  5. vue 所有的路由跳转加一个统一参数
  6. 在学校机房联想硬盘保护下安装Linux,并配置锐捷客户端
  7. [洛谷P1920]成功密码
  8. [codevs3269]混合背包
  9. 关于Subversion主从备份方式的调整(全量、增量脚本)更新
  10. John Morgan:黎曼几何、曲率、Ricci流以及在三维流形上的应用二讲