Submission #1032916

# Submission time Handle Problem Language Result Execution time Memory
1032916 2024-07-24T10:47:51 Z juicy The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
718 ms 55020 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e3 + 5, inf = 1e9;

int n, m, mn = inf, mx;
int a[N][N];

bool check(int k) {
	int cut = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (a[i][j] < mx - k) {
				cut = max(cut, j);
			} 
		}
		for (int j = 1; j <= m; ++j) {
			if (a[i][j] > mn + k && cut >= j) {
				return 0;
			} 
		}
	}
	return 1;
}

int solve() {
	int L = 0, R = mx - mn, res = mx - mn;
	while (L <= R) {
		int md = (L + R) / 2;
		if (check(md)) {
			res = md;
			R = md - 1;
		} else {
			L = md + 1;
		}
	}
	return res;
}

void flip_row() {
	for (int j = 1; j <= m; ++j) {
		for (int i = 1; i <= n / 2; ++i) {
			swap(a[i][j], a[n - i + 1][j]);
		}
	}
}

void flip_col() {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m / 2; ++j) {
			swap(a[i][j], a[i][m - j + 1]);
		}
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j];
			mn = min(mn, a[i][j]);
			mx = max(mx, a[i][j]);
		}
	}	
	int res = solve();
	flip_row();
	res = min(res, solve());
	flip_col();
	res = min(res, solve());
	flip_row();
	res = min(res, solve());
	cout << res;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 4 ms 1628 KB Output is correct
18 Correct 4 ms 1628 KB Output is correct
19 Correct 6 ms 1628 KB Output is correct
20 Correct 7 ms 1372 KB Output is correct
21 Correct 6 ms 1624 KB Output is correct
22 Correct 7 ms 1584 KB Output is correct
23 Correct 8 ms 1880 KB Output is correct
24 Correct 7 ms 1656 KB Output is correct
25 Correct 10 ms 1628 KB Output is correct
26 Correct 7 ms 1624 KB Output is correct
27 Correct 6 ms 1764 KB Output is correct
28 Correct 6 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 4 ms 1628 KB Output is correct
18 Correct 4 ms 1628 KB Output is correct
19 Correct 6 ms 1628 KB Output is correct
20 Correct 7 ms 1372 KB Output is correct
21 Correct 6 ms 1624 KB Output is correct
22 Correct 7 ms 1584 KB Output is correct
23 Correct 8 ms 1880 KB Output is correct
24 Correct 7 ms 1656 KB Output is correct
25 Correct 10 ms 1628 KB Output is correct
26 Correct 7 ms 1624 KB Output is correct
27 Correct 6 ms 1764 KB Output is correct
28 Correct 6 ms 1624 KB Output is correct
29 Correct 285 ms 37224 KB Output is correct
30 Correct 335 ms 37568 KB Output is correct
31 Correct 398 ms 39136 KB Output is correct
32 Correct 284 ms 39100 KB Output is correct
33 Correct 238 ms 34128 KB Output is correct
34 Correct 349 ms 39356 KB Output is correct
35 Correct 615 ms 54820 KB Output is correct
36 Correct 528 ms 49544 KB Output is correct
37 Correct 718 ms 55020 KB Output is correct