Submission #1304153

#TimeUsernameProblemLanguageResultExecution timeMemory
1304153cubedCollecting Mushrooms (NOI18_collectmushrooms)C++20
100 / 100
14 ms10700 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define endl '\n'
#define f first
#define s second
#define pb(x) push_back(x)
 
const int MOD = 1e9+7;
const int inf =1e9;
const ll INF = 1e18;
const ll INV2 = 500000004;

class DSU {
    vector<int> parent, size;
public:
    DSU(int n) {
        parent.resize(n+1);
        size.resize(n+1);
        for (int i=0; i<=n; i++) {
            parent[i]=i;
            size[i]=1;
        }
    }

    int find (int node) {
        if (node==parent[node]) return node;
        return parent[node]=find(parent[node]);
    }

    void unite (int u, int v) {
        int pv=find(v);
        int pu=find(u);

        if (pv==pu) return;
        if (size[pu]<size[pv]) swap(pu, pv);

        parent[pv]=pu;
        size[pu]+=size[pv];
    }

    int getsize (int node) {
        return size[find(node)];
    }
};

ll getsqrt(ll x) {
    ll val = sqrtl(x) + 2;
    while (val * val > x) val--;
    return val;
}

bool safe (int i, int j, int n, int m) {
    if (i<0 || j<0 || i>=n || j>=m) return false;
    return true;
}

ll gcd(ll a, ll b) {
    while (b != 0) {
        ll temp=b;
        b=a%b;
        a=temp;
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b)*b;
}

vector<int> dx={-1, 0, 1, 0};
vector<int> dy={0, 1, 0, -1};

// SOLUTION STARTS FROM HERE //


void solve() {
    int r, c, d, k;
    cin>>r>>c>>d>>k;

    vector<vector<char>> a(r, vector<char>(c));
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            cin>>a[i][j];
        }
    }

    vector<vector<int>> add(r+2, vector<int>(c+2, 0));
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            if (a[i][j]=='S') {
                int r1 = max(0, i-d);
                int r2 = min(r-1, i+d);
                int c1 = max(0, j-d);
                int c2 = min(c-1, j+d);

                add[r1][c1] += 1;
                add[r1][c2+1] -= 1;
                add[r2+1][c1] -= 1;
                add[r2+1][c2+1] += 1;
            }
        }
    }

    vector<vector<int>> water(r, vector<int>(c));
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            water[i][j] = add[i][j];
            if (i > 0) water[i][j] += water[i-1][j];
            if (j > 0) water[i][j] += water[i][j-1];
            if (i > 0 && j > 0) water[i][j] -= water[i-1][j-1];
        }
    }

    int ans=0;

    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            if (a[i][j]=='M' && water[i][j]>=k) {
                ans++;
            }
        }
    }

    cout<<ans<<endl;
}

bool multi=false;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
   

    //freopen("convention2.in", "r", stdin);
	//freopen("convention2.out", "w", stdout);

    int t=1;
    if (multi) cin>>t;
 
    while (t--) 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...