zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    poj1087最大流拆点
    3月15上午的函数练习
    3月15
    3月13上午
    3月13
    3月12
    break语句
    3月11
    3月10号
    3月9号
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626218.html
Copyright © 2011-2022 走看看