ACM_Notebook_new

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ngthanhtrung23/ACM_Notebook_new

:heavy_check_mark: Misc/tests/aizu_dpl_3_b_largest_01_rectangle.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_B"

#include "../../template.h"
#include "../left_nearest_smaller.h"

long long largestHistogram(const vector<int>& a) {
    int n = a.size();
    auto left = leftNearestSmaller(a);
    auto right = rightNearestSmaller(a);

    long long res = 0;
    for (int i = 0; i < n; i++) {
        int l = left[i] + 1;
        int r = right[i] - 1;
        res = max(res, a[i] * (r - l + 1LL));
    }
    return res;
}

void solve() {
    int n_row, n_col; cin >> n_row >> n_col;
    vector<vector<int>> a(n_row, vector<int> (n_col));
    for (auto& row : a) {
        for (auto& x : row) {
            cin >> x;
            x = 1 - x;
        }
    }

    long long res = 0;
    for (int r = 0; r < n_row; r++) {
        if (r > 0) {
            for (int c = 0; c < n_col; c++) {
                if (a[r][c]) a[r][c] = a[r-1][c] + 1;
                else a[r][c] = 0;
            }
        }
        res = max(res, largestHistogram(a[r]));
    }
    cout << res << endl;
}
#line 1 "Misc/tests/aizu_dpl_3_b_largest_01_rectangle.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_B"

#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }

#define sqr(x) ((x) * (x))

// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
    return out << '(' << p.first << ", " << p.second << ')';
}

template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
    out << '{';
    for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
        out << (it == beg ? "" : ", ") << *it;
    }
    return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")"; 
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); 
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
    return print_tuple_utils<0, tuple<U...>>(out, t);
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
    return uniform_int_distribution<long long> (0, r-1)(rng);
}

template<typename T>
vector<T> read_vector(int n) {
    vector<T> res(n);
    for (int& x : res) cin >> x;
    return res;
}

void solve();

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}
#line 1 "Misc/left_nearest_smaller.h"
// Nearest smaller {{{
// Tested:
// - https://cses.fi/problemset/task/1645
// - https://cses.fi/problemset/task/1142
// - https://oj.vnoi.info/problem/kagain
//
// return:
// - left[i] = largest j such that
//      j < i
//      a[j] < a[i]
// - no such j -> left[i] = -1
vector<int> leftNearestSmaller(const vector<int>& a) {
    int n = a.size();
    vector<int> left(n);
    stack<int> st;  // positions of candidates, A is increasing
    st.push(-1);
    for (int i = 0; i < n; i++) {
        while (st.top() >= 0 && a[st.top()] >= a[i]) st.pop();
        left[i] = st.top();
        st.push(i);
    }
    return left;
}

// return:
// - right[i] = smallest j such that:
//      j > i
//      a[j] < a[i]
// - no such j -> right[i] = n
vector<int> rightNearestSmaller(const vector<int>& a) {
    int n = a.size();
    vector<int> right(n);
    stack<int> st;   // positions of candidates, A is increasing
    st.push(n);
    for (int i = n-1; i >= 0; i--) {
        while (st.top() < n && a[st.top()] >= a[i]) st.pop();
        right[i] = st.top();
        st.push(i);
    }
    return right;
}
// }}}
#line 5 "Misc/tests/aizu_dpl_3_b_largest_01_rectangle.test.cpp"

long long largestHistogram(const vector<int>& a) {
    int n = a.size();
    auto left = leftNearestSmaller(a);
    auto right = rightNearestSmaller(a);

    long long res = 0;
    for (int i = 0; i < n; i++) {
        int l = left[i] + 1;
        int r = right[i] - 1;
        res = max(res, a[i] * (r - l + 1LL));
    }
    return res;
}

void solve() {
    int n_row, n_col; cin >> n_row >> n_col;
    vector<vector<int>> a(n_row, vector<int> (n_col));
    for (auto& row : a) {
        for (auto& x : row) {
            cin >> x;
            x = 1 - x;
        }
    }

    long long res = 0;
    for (int r = 0; r < n_row; r++) {
        if (r > 0) {
            for (int c = 0; c < n_col; c++) {
                if (a[r][c]) a[r][c] = a[r-1][c] + 1;
                else a[r][c] = 0;
            }
        }
        res = max(res, largestHistogram(a[r]));
    }
    cout << res << endl;
}
Back to top page