Submission #870968

#TimeUsernameProblemLanguageResultExecution timeMemory
870968MisterReaperSajam (COCI18_sajam)C++17
45 / 90
3334 ms2396 KiB
//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64

const int MAXN = 1E3 + 5;

char matrix[MAXN][MAXN];

#define ONLINE_JUDGE
void solve() {
    int n, k;
    cin >> n >> k;
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cin >> matrix[i][j];
        }
    }

    auto reversex = [&](int x) -> void {
        for(int i = 1; i <= n; i++) {
            matrix[x][i] = (matrix[x][i] == 'o' ? 'x' : 'o');
        }
    };

    auto reversey = [&](int x) -> void {
        for(int i = 1; i <= n; i++) {
            matrix[i][x] = (matrix[i][x] == 'o' ? 'x' : 'o');
        }
    };

    int anss = int(1E18);
    for(int t = 1; t <= n; t++) {
        vector <bool> rev(n);
        for(int i = 1; i <= n; i++) {
            if(matrix[i][t] != 'o') {
                reversex(i);
                rev[i] = true;
            }
        }

        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int cnto = 0;
            for(int j = 1; j <= n; j++) {
                cnto += (matrix[j][i] == 'o');
            }

            ans += min(cnto, n - cnto);
        }

        /*
        cerr << t << " :: " << ans << "\n";
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cerr << matrix[i][j];
            }
            cerr << "\n";
        }
        */

        assert(0 <= ans);
        anss = min(anss, ans);

        for(int i = 1; i <= n; i++) {
            if(rev[i]) {
                reversex(i);
            }
        }
    }

    for(int t = 1; t <= n; t++) {
        vector <bool> rev(n);
        for(int i = 1; i <= n; i++) {
            if(matrix[t][i] != 'o') {
                reversey(i);
                rev[i] = true;
            }
        }

        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int cnto = 0;
            for(int j = 1; j <= n; j++) {
                cnto += (matrix[i][j] == 'o');
            }

            ans += min(cnto, n - cnto);
        }

        /*
        cerr << t << " :: " << ans << "\n";
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cerr << matrix[i][j];
            }
            cerr << "\n";
        }
        */

        assert(0 <= ans);
        anss = min(anss, ans);

        for(int i = 1; i <= n; i++) {
            if(rev[i]) {
                reversey(i);
            }
        }
    }

    {
        int ans = 0;
        for(int i = 0; i <= n; i++) {
            int cnt = 0;
            for(int j = 0; j <= n; j++) {
                cnt += matrix[i][j] == 'o';
            }

            ans += min(cnt, n - cnt);
        }

        anss = min(anss, ans);
    }

    {
        int ans = 0;
        for(int i = 0; i <= n; i++) {
            int cnt = 0;
            for(int j = 0; j <= n; j++) {
                cnt += matrix[j][i] == 'o';
            }

            ans += min(cnt, n - cnt);
        }

        anss = min(anss, ans);
    }

    assert(0 <= anss);
    cerr << anss;
    cout << (anss <= k ? "DA" : "NE");
    
    return;
}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen(".in", "r", stdin);
        freopen(".out", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t = 1; //cin >> t;
    for(int i = 1; i <= t; i++) {
        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...