提交时间:2024-12-08 14:20:58

运行 ID: 35273

// 100 #include <bits/stdc++.h> using namespace std; const int N = 5005, M = 505; bool test1; int n, m, S, tot; bool vis[M]; int L[M], R[M], X[M], Y[M]; int t[M], pos, lower[N], upper[N]; int mx[M][M][M], ans; bool test2; inline int read() { int x = 0; char c; c = getchar(); while (!('0' <= c && c <= '9')) c = getchar(); while ('0' <= c && c <= '9') { x = (x << 1) + (x << 3) + (c ^ '0'); c = getchar(); } return x; } int main() { //freopen("ex_disinfect1.in", "r", stdin); //freopen("disinfect.out", "w", stdout); n = read(); m = read(); S = read(); memset(L, 0x3f, sizeof(L)); memset(X, 0x3f, sizeof(X)); auto clk = clock(); int c; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { c = read(); tot += !vis[c]; vis[c] = true; L[c] = min(L[c], j); R[c] = max(R[c], j); X[c] = min(X[c], i); Y[c] = max(Y[c], i); } // cerr << "check" << endl; // cerr << (double)(clock() - clk) / CLOCKS_PER_SEC << endl; // return 0; for (c = 1; c <= 500; c++) if (L[c] <= m) t[++pos] = X[c]; sort(t + 1, t + pos + 1); pos = unique(t + 1, t + pos + 1) - t - 1; for (c = 1; c <= 500; c++) { if (L[c] <= m) lower[X[c]] = upper[X[c]] = lower_bound(t + 1, t + pos + 1, X[c]) - t; } lower[n + 1] = 501; for (int i = n; i >= 1; i--) if (!lower[i]) lower[i] = lower[i + 1]; upper[0] = 0; for (int i = 1; i <= n; i++) if (!upper[i]) upper[i] = upper[i - 1]; for (c = 1; c <= 500; c++) { if (L[c] > m) continue; for (int i = 1; i <= 500; i++) for (int j = 1; j <= 500; j++) { if (!(L[i] <= L[c] && R[c] <= R[j])) continue; int x = lower[max(Y[c] - S / (R[j] - L[i] + 1) + 1, 1)]; int y = upper[X[c]]; if (x > y) continue; mx[i][j][x]++; mx[i][j][y + 1]--; } } for (int i = 1; i <= 500; i++) for (int j = 1; j <= 500; j++) for (int k = 1; k <= pos; k++) { mx[i][j][k] += mx[i][j][k - 1]; ans = max(ans, mx[i][j][k]); } cout << tot - ans; cerr << (&test2 - &test1) / 1024 / 1024 << endl; return 0; }