# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1255117 | mingga | The Kingdom of JOIOI (JOI17_joioi) | C++20 | 28 ms | 16196 KiB |
// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2005;
int n, m, a[N][N], b[N][N], mn;
bool check(int mid) {
memset(b, 0, sizeof b);
int mx_m = m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= mx_m; j++) {
if(a[i][j] > mn + mid) {
mx_m = j - 1;
break;
}
b[i][j] = 1;
}
}
int mn1 = inf, mx1 = -inf;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(b[i][j]) continue;
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
}
if(mx1 - mn1 <= mid or mx1 == -inf) return 1;
memset(b, 0, sizeof b);
mx_m = m;
for(int i = n; i > 0; i--) {
for(int j = 1; j <= mx_m; j++) {
if(a[i][j] > mn + mid) {
mx_m = j - 1;
break;
}
b[i][j] = 1;
}
}
mn1 = inf, mx1 = -inf;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(b[i][j]) continue;
mn1 = min(mn1, a[i][j]);
mx1 = max(mx1, a[i][j]);
}
}
return (mx1 - mn1) <= mid or mx1 == -inf;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n >> m;
mn = inf;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
}
}
int l = 0, r = 1e9, ans;
while(l <= r) {
int m = (l + r) >> 1;
if(check(m)) {
ans = m;
r = m - 1;
} else l = m + 1;
}
cout << ans << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |