# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
761089 | Blagoj | Quality Of Living (IOI10_quality) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
int r, c, h, w, a[3002][3002], dp[3002][3002];
const int s = 801;
bool b(int v) {
int s = (h * w) / 2 + 1;
for (int i = 0; i < r; i++) {
int cnt = 0;
for (int j = 0; j < c; j++) {
if (a[i][j] <= v) cnt++;
dp[i][j] = cnt;
if (i - 1 >= 0) {
dp[i][j] += dp[i - 1][j];
}
if (i == h - 1 && j == w - 1) {
if (dp[i][j] >= s) return 1;
}
else if (i == h - 1 && j >= w) {
if (dp[i][j] - dp[i][j - w] >= s) return 1;
}
else if (i >= h && j == w - 1) {
if (dp[i][j] - dp[i - h][j] >= s) return 1;
}
else if (i >= h - 1 && j >= w - 1) {
if (dp[i][j] - dp[i - h][j] - dp[i][j - w] + dp[i - h][j - w] >= s) return 1;
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> r >> c >> h >> w;
vector<int> v;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> a[i][j];
v.push_back(a[i][j]);
}
}
int l = 0, rr = r * c;
sort(all(v));
int ans = INT_MAX;
while (l + 1 < rr) {
int m = (l + rr) / 2;
if (b(v[m])) {
ans = min(ans, v[m]);
rr = m;
}
else {
l = m;
}
}
if (b(v[0])) ans = v[0];
cout << ans;
}