#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3 + 5;
int a[N][N], x[N][N], n, m, nd, ps[N][N];
int c (int dp[]) {
int i = 0, ar = 0, ans = 0;
stack <int> st;
while (i < m) {
if (st.size() == 0 || dp[st.top() + 1] <= dp[i + 1]) st.push(i++);
else {
int tp = dp[st.top() + 1];
st.pop();
ar = tp * i;
if (st.size()) ar = tp * (i - st.top() - 1);
ans = max(ans, ar);
// cout << ar << ' ' << i << '\n';
}
}
while (st.size()) {
int tp = dp[st.top() + 1];
st.pop();
ar = tp * i;
if (st.size()) ar = tp * (i - st.top() - 1);
ans = max(ans, ar);
// cout << ar << ' ' << i << '\n';
} return ans;
}
int check (int d) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] >= d) x[i][j] = 1;
else x[i][j] = 0;
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + x[i][j];
}
}
int ans = c(x[1]);
// cout << ans << ' ';
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (x[i][j]) x[i][j] += x[i - 1][j];
}
ans = max(ans, c(x[i]));
// cout << ans << ' ';
}
// cout << '\n';
if (ans >= nd) return ans;
else return 0;
}
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> nd;
int mx = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
a[i][j] = 0, x[i][j] = 0, ps[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
}
int l = 0, r = mx, res = 0, ans = 0;
while (l <= r) {
int md = (l + r) / 2;
// cout << md << '\n';
if (check(md) > 0) l = md + 1, res = md, ans = check(md);
else r = md - 1;
} cout << res << ' ' << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
5 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
6 ms |
12184 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
13 ms |
12244 KB |
Output is correct |
10 |
Correct |
34 ms |
12372 KB |
Output is correct |
11 |
Correct |
52 ms |
12676 KB |
Output is correct |
12 |
Correct |
16 ms |
12244 KB |
Output is correct |
13 |
Correct |
40 ms |
12500 KB |
Output is correct |
14 |
Correct |
48 ms |
12748 KB |
Output is correct |
15 |
Correct |
55 ms |
12756 KB |
Output is correct |
16 |
Correct |
91 ms |
13256 KB |
Output is correct |
17 |
Correct |
47 ms |
12628 KB |
Output is correct |
18 |
Correct |
207 ms |
14948 KB |
Output is correct |
19 |
Correct |
155 ms |
14008 KB |
Output is correct |
20 |
Correct |
463 ms |
17736 KB |
Output is correct |
21 |
Correct |
417 ms |
18420 KB |
Output is correct |
22 |
Correct |
553 ms |
21840 KB |
Output is correct |
23 |
Correct |
605 ms |
21808 KB |
Output is correct |
24 |
Correct |
202 ms |
14968 KB |
Output is correct |
25 |
Correct |
220 ms |
15020 KB |
Output is correct |