Submission #444907

# Submission time Handle Problem Language Result Execution time Memory
444907 2021-07-15T20:29:44 Z aryan12 The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2152 ms 70712 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2005;
int n, m;
vector<vector<int> > a(N, vector<int> (N));
int maxVal = 0, minVal = INT_MAX;

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

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

bool Check(int max_delta) {
	int max_row = n;
	//cout << "max_delta = " << max_delta << "\n";
	for(int j = 1; j <= m; j++) {
		for(int i = 1; i <= max_row; i++) {
			if(maxVal - a[i][j] > max_delta) {
				max_row = i - 1;
				//cout << "In column " << j << ", the row max is being reduced to: " << i - 1 << "\n";
				break;
			}
		}
		for(int i = max_row + 1; i <= n; i++) {
			if(a[i][j] - minVal > max_delta) {
				//cout << "a[i][j] - minVal = " << a[i][j] - minVal << ", max_delta = " << max_delta << "\n";
				//cout << "a[i][j] = " << a[i][j] << ", minVal = " << minVal << "\n";
				//cout << "Exceeding the limits...can't take anymore\n";
				//cout << "Place = (" << i << ", " << j << ")\n";
				return false;
			}
		}
	}
	return true;
}

int FindVal() {
	int l = 0, r = maxVal - minVal + 1;
	int mid, ans = r;
	while(l <= r) {
		mid = (l + r) >> 1;
		if(Check(mid)) {
			ans = mid;
			r = mid - 1;
		}
		else {
			l = mid + 1;
		}
	}
	//cout << "ans = " << ans << "\n";
	return ans;
}

void Solve() {
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cin >> a[i][j];
			maxVal = max(maxVal, a[i][j]);
			minVal = min(minVal, a[i][j]);
		}
	}

	/*cout << "maxVal = " << maxVal << ", minVal = " << minVal << "\n";
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}*/

	int ans = FindVal();
	FlipColumns();

	/*for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}*/


	ans = min(ans, FindVal());
	FlipRows();

	/*for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}*/


	ans = min(ans, FindVal());
	FlipColumns();

	/*for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}*/


	cout << min(ans, FindVal()) << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31756 KB Output is correct
2 Correct 17 ms 31864 KB Output is correct
3 Correct 18 ms 31764 KB Output is correct
4 Correct 16 ms 31876 KB Output is correct
5 Correct 16 ms 31856 KB Output is correct
6 Correct 16 ms 31836 KB Output is correct
7 Correct 16 ms 31860 KB Output is correct
8 Correct 17 ms 31792 KB Output is correct
9 Correct 17 ms 31820 KB Output is correct
10 Correct 16 ms 31856 KB Output is correct
11 Correct 16 ms 31820 KB Output is correct
12 Correct 16 ms 31840 KB Output is correct
13 Correct 16 ms 31820 KB Output is correct
14 Correct 17 ms 31832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31756 KB Output is correct
2 Correct 17 ms 31864 KB Output is correct
3 Correct 18 ms 31764 KB Output is correct
4 Correct 16 ms 31876 KB Output is correct
5 Correct 16 ms 31856 KB Output is correct
6 Correct 16 ms 31836 KB Output is correct
7 Correct 16 ms 31860 KB Output is correct
8 Correct 17 ms 31792 KB Output is correct
9 Correct 17 ms 31820 KB Output is correct
10 Correct 16 ms 31856 KB Output is correct
11 Correct 16 ms 31820 KB Output is correct
12 Correct 16 ms 31840 KB Output is correct
13 Correct 16 ms 31820 KB Output is correct
14 Correct 17 ms 31832 KB Output is correct
15 Correct 17 ms 31764 KB Output is correct
16 Correct 19 ms 31820 KB Output is correct
17 Correct 22 ms 32128 KB Output is correct
18 Correct 22 ms 32116 KB Output is correct
19 Correct 22 ms 32056 KB Output is correct
20 Correct 24 ms 32024 KB Output is correct
21 Correct 24 ms 32324 KB Output is correct
22 Correct 25 ms 32112 KB Output is correct
23 Correct 26 ms 32204 KB Output is correct
24 Correct 24 ms 32160 KB Output is correct
25 Correct 25 ms 32256 KB Output is correct
26 Correct 25 ms 32248 KB Output is correct
27 Correct 26 ms 32124 KB Output is correct
28 Correct 25 ms 32184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31756 KB Output is correct
2 Correct 17 ms 31864 KB Output is correct
3 Correct 18 ms 31764 KB Output is correct
4 Correct 16 ms 31876 KB Output is correct
5 Correct 16 ms 31856 KB Output is correct
6 Correct 16 ms 31836 KB Output is correct
7 Correct 16 ms 31860 KB Output is correct
8 Correct 17 ms 31792 KB Output is correct
9 Correct 17 ms 31820 KB Output is correct
10 Correct 16 ms 31856 KB Output is correct
11 Correct 16 ms 31820 KB Output is correct
12 Correct 16 ms 31840 KB Output is correct
13 Correct 16 ms 31820 KB Output is correct
14 Correct 17 ms 31832 KB Output is correct
15 Correct 17 ms 31764 KB Output is correct
16 Correct 19 ms 31820 KB Output is correct
17 Correct 22 ms 32128 KB Output is correct
18 Correct 22 ms 32116 KB Output is correct
19 Correct 22 ms 32056 KB Output is correct
20 Correct 24 ms 32024 KB Output is correct
21 Correct 24 ms 32324 KB Output is correct
22 Correct 25 ms 32112 KB Output is correct
23 Correct 26 ms 32204 KB Output is correct
24 Correct 24 ms 32160 KB Output is correct
25 Correct 25 ms 32256 KB Output is correct
26 Correct 25 ms 32248 KB Output is correct
27 Correct 26 ms 32124 KB Output is correct
28 Correct 25 ms 32184 KB Output is correct
29 Correct 811 ms 53828 KB Output is correct
30 Correct 823 ms 53372 KB Output is correct
31 Correct 917 ms 55104 KB Output is correct
32 Correct 794 ms 55240 KB Output is correct
33 Correct 678 ms 51804 KB Output is correct
34 Correct 862 ms 55132 KB Output is correct
35 Correct 2152 ms 70664 KB Output is correct
36 Correct 1840 ms 65664 KB Output is correct
37 Correct 2102 ms 70712 KB Output is correct