1731D - Valiant's New Map - CodeForces Solution


binary search brute force data structures dp two pointers *1700

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

#define ll long long
#define lld long double
#define ff first
#define ss second
#define pb push_back
#define vr(v) v.begin(),v.end()
#define rv(v) v.rbegin(),v.rend()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Davit cout.tie(NULL);

using namespace std;
const int INF = 1e9;
// printf("%.9f\n", x);
//cout << fixed<<setprecision(n);
//    freopen("x.in", "r", stdin);
//    freopen("x.out", "w", stdout);

//mt19937 mt(time(nullptr));
//__builtin_clz(x): the number of zeros at the beginning of the number
//__builtin_ctz(x): the number of zeros at the end of the number
//__builtin_popcount(x): the number of 1s

int n, m;


bool ok(int mid, vector<vector<int>> v) {
    vector<vector<int>> pref(n + 1, vector<int>(m + 1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (v[i][j] >= mid) v[i][j] = 1;
            else v[i][j] = 0;
        }
    }
//    cerr << "1\n";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            pref[i + 1][j + 1] = pref[i][j + 1] + pref[i + 1][j] - pref[i][j] + v[i][j];
        }
    }
//    cerr << "2\n";
    bool isok = 0;
    for (int i = mid; i <= n; i++) {
        for (int j = mid; j <= m; j++) {
            int cur = pref[i][j] - pref[i][j - mid] - pref[i - mid][j] + pref[i - mid][j - mid];
            if (cur == mid * mid)return 1;
        }
    }

    return 0;
}


void solve() {
    cin >> n >> m;
    vector<vector<int>> maze(n, vector<int>(m));
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++)cin >> maze[i][j];

    int l = 0, r = n + 1;
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (ok(mid, maze))l = mid;
        else r = mid;
    }
    cout << l << "\n";
}


int main() {
    int t = 1;
    cin >> t;
    while (t--)solve();
}


Comments

Submit
0 Comments
More Questions

1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String