Submission #893076

#TimeUsernameProblemLanguageResultExecution timeMemory
893076Alihan_8Dango Maker (JOI18_dango_maker)C++17
33 / 100
809 ms262144 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

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

    int n, m; cin >> n >> m;
    vector <string> s(n);
    for ( auto &u: s ) cin >> u;
    // 0 -> horizontal, 1 - vertical
    vector <char> mp =  {'R', 'G', 'W'};
    vector <vector<int>> G;
    int ct = 0;
    auto ok = [&](int u, int v, char x){
        return min(u, v) >= 0 && u < n && v < m && s[u][v] == x;
    };
    auto add = [&](int u, int v){
        G[u].pb(v), G[v].pb(u);
    };
    unordered_map <int,unordered_map<int,unordered_map<int,int>>> p;
    vector <ar<int,2>> Add;
    int tot = 0;
    for ( int i = 0; i < n; i++ ){
        for ( int j = 0; j < m; j++ ){
            vector <int> H;
            bool h = false;
            if ( j + 3 <= m && vector <char> {s[i][j], s[i][j + 1], s[i][j + 2]} == mp ){
                p[i][j][0] = ct++; h = true;
                if ( ok(i - 1, j + 1, 'R') && ok(i + 1, j + 1, 'W') ){
                    H.pb(p[i - 1][j + 1][1]);
                }
                if ( ok(i - 1, j + 2, 'G') && ok(i - 2, j + 2, 'R') ){
                    H.pb(p[i - 2][j + 2][1]);
                }
            }
            if ( i + 3 <= n && vector <char> {s[i][j], s[i + 1][j], s[i + 2][j]} == mp ){
                p[i][j][1] = ct++;
                H.pb(p[i][j][1]);
            }
            if ( h > 0 ){
                for ( auto &x: H ){
                    Add.pb({x, p[i][j][0]});
                }
            }
        }
    }
    G.resize(ct);
    for ( auto &[u, v]: Add ) add(u, v);
    vector <int> us(ct);
    vector <ar<int,2>> dp(ct, {1, 0});
    auto dfs = [&](auto dfs, int u) -> void{
        us[u] = true;
        for ( auto &v: G[u] ){
            if ( !us[v] ){
                dfs(dfs, v);
                dp[u][0] += dp[v][1];
                dp[u][1] += dp[v][0];
            }
        }
        chmax(dp[u][0], dp[u][1]);
    };
    int ans = 0;
    for ( int i = 0; i < ct; i++ ){
        if ( !us[i] ){
            dfs(dfs, i);
            ans += dp[i][0];
        }
    }
    cout << ans;

    cout << '\n';
}

Compilation message (stderr)

dango_maker.cpp: In function 'int main()':
dango_maker.cpp:50:9: warning: unused variable 'tot' [-Wunused-variable]
   50 |     int tot = 0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...