Submission #687564

# Submission time Handle Problem Language Result Execution time Memory
687564 2023-01-26T15:02:51 Z NK_ The Kingdom of JOIOI (JOI17_joioi) C++17
60 / 100
4000 ms 131264 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M; cin >> N >> M;
	vector<vector<int>> A(N, vector<int>(M)); for(auto& v : A) for(auto& x : v) cin >> x;

	auto range = [&](const set<int>& S) {
		return *rbegin(S) - *begin(S);
	};	

	auto solve = [&]() {
		vector<vector<int>> P = A;
		vector<array<int, 3>> I;
		set<int> JOI, IOI;
		for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
			JOI.insert(A[i][j]);
			I.push_back({A[i][j], i, j});
			if (i != 0) P[i][j] = min(P[i][j], P[i-1][j]);
			if (j != 0) P[i][j] = min(P[i][j], P[i][j-1]);
		}

		int ans = int(1e9);
		sort(rbegin(I), rend(I));
		for(int i = 0; i < int(size(I)); i++) {
			int j = i;
			while(j < int(size(I)) && I[i][0] == I[j][0]) {
				int r = I[j][1], c = I[j][2];
				IOI.insert(P[r][c]);
				j++;
			}
			IOI.insert(I[i][0]);
			JOI.erase(I[i][0]);

			if (size(JOI) == 0) break;

			// cout << I[i][0] << endl;

			// for(auto x : JOI) cout << x << " ";
			// cout << endl;

			// for(auto x : IOI) cout << x << " ";
			// cout << endl;

			int res = max(range(JOI), range(IOI));
			// cout << range(JOI) << " " << range(IOI) << endl;
			ans = min(ans, res);
			i = j-1;
		}

		return ans;
	};

	int ans = int(1e9);
	for(int f1 = 0; f1 < 2; f1++) {
		for(int f2 = 0; f2 < 2; f2++) {
			// cout << 2 * f1 + f2 << nl;
			ans = min(ans, solve());

			for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
				int o = M - 1 - j;
				if (j <= o) swap(A[i][j], A[i][o]);
			}
		}

		for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) {
			int o = N - 1 - i;
			if (i <= o) swap(A[i][j], A[o][j]);
		}
	}

	cout << ans << nl;



    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 31 ms 1864 KB Output is correct
17 Correct 102 ms 3268 KB Output is correct
18 Correct 106 ms 3260 KB Output is correct
19 Correct 98 ms 3316 KB Output is correct
20 Correct 88 ms 3152 KB Output is correct
21 Correct 99 ms 3416 KB Output is correct
22 Correct 106 ms 3652 KB Output is correct
23 Correct 100 ms 3548 KB Output is correct
24 Correct 85 ms 3252 KB Output is correct
25 Correct 111 ms 3472 KB Output is correct
26 Correct 102 ms 3356 KB Output is correct
27 Correct 96 ms 3612 KB Output is correct
28 Correct 105 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 31 ms 1864 KB Output is correct
17 Correct 102 ms 3268 KB Output is correct
18 Correct 106 ms 3260 KB Output is correct
19 Correct 98 ms 3316 KB Output is correct
20 Correct 88 ms 3152 KB Output is correct
21 Correct 99 ms 3416 KB Output is correct
22 Correct 106 ms 3652 KB Output is correct
23 Correct 100 ms 3548 KB Output is correct
24 Correct 85 ms 3252 KB Output is correct
25 Correct 111 ms 3472 KB Output is correct
26 Correct 102 ms 3356 KB Output is correct
27 Correct 96 ms 3612 KB Output is correct
28 Correct 105 ms 3416 KB Output is correct
29 Execution timed out 4051 ms 131264 KB Time limit exceeded
30 Halted 0 ms 0 KB -