Advertisement
Gosunov

Untitled

Mar 28th, 2023
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 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. 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.    
  47.     build();
  48.    
  49.     int ans_l = 1;
  50.     int ans_r = n;
  51.     int ans_t = 1;
  52.     int ans_b = n;
  53.     int ans_s = 1e9;
  54.     for (int t = 1; t <= n; ++t) {
  55.         for (int b = t; b <= n; ++b) {
  56.             int l = 1;
  57.             for (int r = 1; r <= n; ++r) {
  58.                 while (l + 1 <= r && ok(l + 1, r, t, b)) {
  59.                     l++;
  60.                 }
  61.                 if (ok(l, r, t, b)) {
  62.                     int s = square(l, r, t, b);
  63.                     if (s < ans_s) {
  64.                         ans_s = s;
  65.                         ans_l = l;
  66.                         ans_r = r;
  67.                         ans_t = t;
  68.                         ans_b = b;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     cout << ans_t - 1 << ' ' << ans_l - 1 << ' ' << ans_b - 1 << ' ' << ans_r - 1 << ' ' << ans_s << '\n';
  75.     memset(dp, 0, sizeof dp);
  76. }
  77.  
  78. signed main() {
  79.     ios::sync_with_stdio(0); cin.tie(0);
  80.     int t = 1;
  81.     cin >> t;
  82.     while (t--) {
  83.         static int k = 0;
  84.         cout << "Case #" << ++k << ": ";
  85.         solve();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement