# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
173156 | VEGAnn | Bomb (IZhO17_bomb) | C++14 | 1010 ms | 77820 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
#define PB push_back
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2510;
const int oo = 2e9;
int n, m, ans, a[N][N], mx = oo, pr[N][N], best[N], nt[N][N], down[N][2], up[N][2];
stack<int> st;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
char c; cin >> c;
a[i][j] = (c == '1' ? 1 : 0);
}
for (int j = 1; j <= m; j++){
for (int i = 1; i <= n; ){
if (a[i][j] == 0)
i++;
else {
int i1 = i;
while (i1 <= n && a[i1][j] == 1)
i1++;
mx = min(mx, i1 - i);
i = i1;
}
}
}
if (mx == oo){
cout << n * m;
return 0;
}
fill(best, best + n + 1, oo);
for (int i = 1; i <= n; i++){
int pre = 0;
for (int j = 1; j <= m; j++){
if (a[i][j] == 0)
pre = j;
pr[i][j] = pre + 1;
}
pre = m + 1;
for (int j = m; j > 0; j--){
if (a[i][j] == 0)
pre = j;
nt[i][j] = pre - 1;
}
}
for (int j = 1; j <= m; j++){
for (int i = 1; i <= n; ){
if (a[i][j] == 0)
i++;
else {
int i1 = i;
while (i1 <= n && a[i1][j] == 1)
i1++;
while (sz(st)) st.pop();
for (int ii = i; ii < i1; ii++){
while (sz(st) && pr[st.top()][j] <= pr[ii][j])
st.pop();
down[ii][0] = (sz(st) == 0 ? i : st.top() + 1);
st.push(ii);
}
while (sz(st)) st.pop();
for (int ii = i; ii < i1; ii++){
while (sz(st) && nt[st.top()][j] >= nt[ii][j])
st.pop();
down[ii][1] = (sz(st) == 0 ? i : st.top() + 1);
st.push(ii);
}
while (sz(st)) st.pop();
for (int ii = i1 - 1; ii >= i; ii--){
while (sz(st) && pr[st.top()][j] <= pr[ii][j])
st.pop();
up[ii][0] = (sz(st) == 0 ? i1 - 1 : st.top() - 1);
st.push(ii);
}
while (sz(st)) st.pop();
for (int ii = i1 - 1; ii >= i; ii--){
while (sz(st) && nt[st.top()][j] >= nt[ii][j])
st.pop();
up[ii][1] = (sz(st) == 0 ? i1 - 1 : st.top() - 1);
st.push(ii);
}
i = i1;
}
}
for (int i = 1; i <= n; i++){
if (a[i][j] == 0) continue;
int l = max(down[i][0], down[i][1]);
int r = min(up[i][0], up[i][1]);
int cur = r - l + 1;
best[cur] = min(best[cur], nt[i][j] - pr[i][j] + 1);
}
}
best[n + 1] = oo;
for (int i = n; i > 0; i--) {
best[i] = min(best[i + 1], best[i]);
if (best[i] < oo && i <= mx)
ans = max(ans, best[i] * i);
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |