#include <bits/stdc++.h>
using namespace std;
const int maxn = 1007;
#define ll long long
ll n, m, k;
ll plot[maxn][maxn];
ll heights[maxn];
ll lft[maxn];
ll rt[maxn];
ll pov = -1;
ll calc(){
stack<ll> prije;
for (ll i = 0; i < m; i++) {
while (!prije.empty() && heights[prije.top()] >= heights[i]) {
prije.pop();
}
if (!prije.empty()) {
lft[i] = prije.top();
}
else {
lft[i] = -1;
}
prije.push(i);
}
stack<ll> poslije;
for (ll i = m-1; i >= 0; i--) {
while (!poslije.empty() && heights[poslije.top()] >= heights[i]) {
poslije.pop();
}
if (!poslije.empty()) {
rt[i] = poslije.top();
} else {
rt[i] = m;
}
poslije.push(i);
}
ll sol = 0;
for (ll i = 0; i < m; i++)
{
sol = max(sol, heights[i] * (rt[i] - lft[i] - 1));
}
return sol;
}
bool check(ll height){
for (int i = 0; i < m; i++) {
heights[i] = 0;
}
pov = -1;
for(ll i = 0; i < n; i++){
for(ll j = 0; j < m; j++){
if(plot[i][j] >= height){
heights[j]++;
} else {
heights[j] = 0;
}
}
ll r = calc();
pov = max(pov, r);
}
return pov >= k;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> k;
ll mxv = -1;
ll mv = LLONG_MAX;
for(ll i = 0; i < n; i++){
for(ll j = 0; j < m; j++){
cin >> plot[i][j];
mxv = max(plot[i][j], mxv);
mv = min(plot[i][j], mv);
}
}
ll lo = mv;
ll hi = mxv;
while(lo < hi){
ll mid = (lo+hi)/2+1;
if(check(mid)){
lo = mid;
} else {
hi = mid-1;
}
}
check(lo);
cout << lo << ' ' << pov << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
2528 KB |
Output is correct |
9 |
Correct |
5 ms |
2524 KB |
Output is correct |
10 |
Correct |
16 ms |
2968 KB |
Output is correct |
11 |
Correct |
33 ms |
3164 KB |
Output is correct |
12 |
Correct |
9 ms |
3800 KB |
Output is correct |
13 |
Correct |
21 ms |
2908 KB |
Output is correct |
14 |
Correct |
33 ms |
3932 KB |
Output is correct |
15 |
Correct |
32 ms |
5252 KB |
Output is correct |
16 |
Correct |
47 ms |
4440 KB |
Output is correct |
17 |
Correct |
20 ms |
5260 KB |
Output is correct |
18 |
Correct |
125 ms |
7972 KB |
Output is correct |
19 |
Correct |
108 ms |
7824 KB |
Output is correct |
20 |
Correct |
293 ms |
12116 KB |
Output is correct |
21 |
Correct |
261 ms |
13648 KB |
Output is correct |
22 |
Correct |
338 ms |
17920 KB |
Output is correct |
23 |
Correct |
422 ms |
17912 KB |
Output is correct |
24 |
Correct |
160 ms |
10736 KB |
Output is correct |
25 |
Correct |
130 ms |
11116 KB |
Output is correct |