Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: E. Rectangles
- // Contest: Codeforces - Codeforces Round 1058 (Div. 2)
- // URL: https://codeforces.com/contest/2160/problem/E
- // Memory Limit: 1024 MB
- // Time Limit: 4000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- namespace rngs = std::ranges;
- using ll = long long;
- using a2l = array<ll, 2>;
- using vl = vector<ll>;
- void solve()
- {
- ll n, m, transpose = 0;
- cin >> n >> m;
- vector<string> vs(n);
- for (auto &x : vs)
- cin >> x;
- if (n > m) {
- vector<string> vs2(m, string(n, ' '));
- for (ll i = 0; i < n; ++i)
- for (ll j = 0; j < m; ++j)
- vs2[j][i] = vs[i][j];
- swap(vs, vs2);
- swap(n, m);
- transpose = 1;
- }
- assert(n <= m);
- vector<vl> ans(n, vl(m, LLONG_MAX / 2));
- for (ll i = 0; i < n; ++i) {
- vl opt(m, LLONG_MAX / 2);
- for (ll j = n - 1; j > i; --j) {
- for (ll k = 0, last = -1; k < m; ++k) {
- if (vs[i][k] == '1' && vs[j][k] == '1') {
- if (last != -1) {
- ll area = (k - last + 1) * (j - i + 1);
- for (ll u = last; u <= k; ++u)
- opt[u] = min(opt[u], area);
- }
- last = k;
- }
- }
- for (ll k = 0; k < m; ++k)
- ans[j][k] = min(ans[j][k], opt[k]);
- }
- for (ll k = 0; k < m; ++k)
- ans[i][k] = min(ans[i][k], opt[k]);
- }
- if (transpose) {
- vector<vl> ans2(m, vl(n, LLONG_MAX / 2));
- for (ll i = 0; i < n; ++i)
- for (ll j = 0; j < m; ++j)
- ans2[j][i] = ans[i][j];
- swap(ans, ans2);
- swap(n, m);
- }
- for (const auto &row : ans) {
- for (auto col : row)
- cout << (col == LLONG_MAX / 2 ? 0 : col) << ' ';
- cout << '\n';
- }
- }
- int main(int argc, char **argv)
- {
- std::ios::sync_with_stdio(false);
- std::cin.tie(nullptr);
- ll t = 1;
- cin >> t;
- while (t--)
- solve();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment