제출 #1345580

#제출 시각아이디문제언어결과실행 시간메모리
1345580chithanhnguyenCollecting Mushrooms (NOI18_collectmushrooms)C++20
100 / 100
11 ms18792 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

void solve() {
    int n, m, d, k;
    cin >> n >> m >> d >> k;
    vector<pii> sprinkers_pos, mushrooms_pos;

    for (int i = 1; i <= n; ++i) {
        string s; cin >> s;
        s = ' ' + s;
        for (int j = 1; j <= m; ++j) {
            if (s[j] == 'M') mushrooms_pos.push_back({i, j});
            else if (s[j] == 'S') sprinkers_pos.push_back({i, j});
        }
    }

    vector<vector<int>> grid(n + 5, vector<int>(m + 5, 0));

    auto updateRect = [&] (int r1, int c1, int r2, int c2) {
        if (r1 > r2 || c1 > c2) return;
        grid[r1][c1] += 1;
        grid[r1][c2 + 1] -= 1;
        grid[r2 + 1][c1] -= 1;
        grid[r2 + 1][c2 + 1] += 1;
    };

    for (auto u : sprinkers_pos) {
        int r1 = max(1, u.fi - d);
        int c1 = max(1, u.se - d);
        int r2 = min(n, u.fi + d);
        int c2 = min(m, u.se + d);
        
        updateRect(r1, c1, r2, c2);
    }

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            grid[i][j] = (grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1] + grid[i][j]);
    
    int res = 0;
    for (auto u : mushrooms_pos)
        if (grid[u.fi][u.se] >= k) res += 1;
    cout << res;
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...