Advertisement
Gosunov

Untitled

Mar 28th, 2023
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define all(a) (a).begin(), (a).end()
  4.  
  5. const int maxn = 1500;
  6. const int maxc = 52;
  7.  
  8. int n, C, q;
  9.  
  10. int dp[maxn+2][maxn+2][maxc];
  11.  
  12. void build() {
  13.     for (int i = 1; i <= n; ++i)
  14.         for (int j = 1; j <= n; ++j)
  15.             for (int c = 0; c < maxc; ++c)
  16.                 dp[i][j][c] += dp[i][j - 1][c] + dp[i - 1][j][c] - dp[i - 1][j - 1][c];
  17. }
  18.  
  19. bool ok(int l, int r, int t, int b) {
  20.     int res = 0;
  21.     for (int c = 0; c < maxc; ++c) {
  22.         int k = dp[b][r][c] - dp[b][l - 1][c] - dp[t - 1][r][c] + dp[t - 1][l - 1][c];
  23.         res += (bool)k;
  24.     }
  25.     return res == C;
  26. }
  27.  
  28. inline int square(int l, int r, int t, int b) {
  29.     return (r - l + 1) * (b - t + 1);
  30. }
  31.  
  32. void solve() {
  33.     cin >> n >> C >> q;
  34.     n += 1;
  35.  
  36.     for (int t = 0; t < q; ++t) {
  37.         int i, j;
  38.         char c;
  39.         cin >> i >> j >> c;
  40.         i += 1;
  41.         j += 1;
  42.         if (c >= 'a') c -= 6;
  43.         c -= 'A';
  44.         dp[i][j][(int)c] += 1;
  45.     }
  46.     build();
  47.    
  48.     int ans_l = 1;
  49.     int ans_r = n;
  50.     int ans_t = 1;
  51.     int ans_b = n;
  52.     int ans_s = 1e9;
  53.     for (int t = 1; t <= n; ++t) {
  54.         int b = t;
  55.         while (b <= n && !ok(1, n, t, b)) b++;
  56.         for (; b <= n; ++b) {
  57.             int l = 1;
  58.             for (int r = 1; r <= n; ++r) {
  59.                 while (l + 1 <= r && ok(l + 1, r, t, b)) {
  60.                     l++;
  61.                 }
  62.                 if (int s = square(l, r, t, b); s < ans_s && ok(l, r, t, b)) {
  63.                     ans_s = s;
  64.                     ans_l = l;
  65.                     ans_r = r;
  66.                     ans_t = t;
  67.                     ans_b = b;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     cout << ans_t - 1 << ' ' << ans_l - 1 << ' ' << ans_b - 1 << ' ' << ans_r - 1 << ' ' << ans_s << '\n';
  73.     memset(dp, 0, sizeof dp);
  74. }
  75.  
  76. signed main() {
  77.     ios::sync_with_stdio(0); cin.tie(0);
  78.     int t = 1;
  79.     cin >> t;
  80.     while (t--) {
  81.         static int k = 0;
  82.         cout << "Case #" << ++k << ": ";
  83.         solve();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement