pb_jiang

CF2160E

Oct 13th, 2025
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. // Problem: E. Rectangles
  2. // Contest: Codeforces - Codeforces Round 1058 (Div. 2)
  3. // URL: https://codeforces.com/contest/2160/problem/E
  4. // Memory Limit: 1024 MB
  5. // Time Limit: 4000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using vl = vector<ll>;
  21.  
  22. void solve()
  23. {
  24.     ll n, m, transpose = 0;
  25.     cin >> n >> m;
  26.     vector<string> vs(n);
  27.     for (auto &x : vs)
  28.         cin >> x;
  29.     if (n > m) {
  30.         vector<string> vs2(m, string(n, ' '));
  31.         for (ll i = 0; i < n; ++i)
  32.             for (ll j = 0; j < m; ++j)
  33.                 vs2[j][i] = vs[i][j];
  34.         swap(vs, vs2);
  35.         swap(n, m);
  36.         transpose = 1;
  37.     }
  38.     assert(n <= m);
  39.  
  40.     vector<vl> ans(n, vl(m, LLONG_MAX / 2));
  41.     for (ll i = 0; i < n; ++i) {
  42.         vl opt(m, LLONG_MAX / 2);
  43.         for (ll j = n - 1; j > i; --j) {
  44.             for (ll k = 0, last = -1; k < m; ++k) {
  45.                 if (vs[i][k] == '1' && vs[j][k] == '1') {
  46.                     if (last != -1) {
  47.                         ll area = (k - last + 1) * (j - i + 1);
  48.                         for (ll u = last; u <= k; ++u)
  49.                             opt[u] = min(opt[u], area);
  50.                     }
  51.                     last = k;
  52.                 }
  53.             }
  54.             for (ll k = 0; k < m; ++k)
  55.                 ans[j][k] = min(ans[j][k], opt[k]);
  56.         }
  57.         for (ll k = 0; k < m; ++k)
  58.             ans[i][k] = min(ans[i][k], opt[k]);
  59.     }
  60.  
  61.     if (transpose) {
  62.         vector<vl> ans2(m, vl(n, LLONG_MAX / 2));
  63.         for (ll i = 0; i < n; ++i)
  64.             for (ll j = 0; j < m; ++j)
  65.                 ans2[j][i] = ans[i][j];
  66.         swap(ans, ans2);
  67.         swap(n, m);
  68.     }
  69.  
  70.     for (const auto &row : ans) {
  71.         for (auto col : row)
  72.             cout << (col == LLONG_MAX / 2 ? 0 : col) << ' ';
  73.         cout << '\n';
  74.     }
  75. }
  76.  
  77. int main(int argc, char **argv)
  78. {
  79.     std::ios::sync_with_stdio(false);
  80.     std::cin.tie(nullptr);
  81.  
  82.     ll t = 1;
  83.     cin >> t;
  84.     while (t--)
  85.         solve();
  86.  
  87.     return 0;
  88. };
  89.  
Advertisement
Add Comment
Please, Sign In to add comment