Submission #1177130

#TimeUsernameProblemLanguageResultExecution timeMemory
1177130TsaganaThe Kingdom of JOIOI (JOI17_joioi)C++20
100 / 100
324 ms31912 KiB
#include<bits/stdc++.h>

#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second

using namespace std;

int n, m, mn, mx, ans;
int a[2010][2010];

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

void calc(int l = 0, int r = ans) {
	while (l <= r) {
		int k = (l + r) / 2;
		if (check(k)) {ans = k; r = k - 1;}
		else l = k + 1;
	}
}

void flip1() {
	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 flip2() {
	for (int j = 1; j <= m / 2; j++)
	for (int i = 1; i <= n; i++)
		swap(a[i][j], a[i][m-j+1]);
}

void solve () {
	mn = INT_MAX, mx = 0;

	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]);
	}
	ans = mx - mn;

	calc();
	flip1();
	calc();
	flip2();
	calc();
	flip1();
	calc();

	cout << ans;
}
signed main() {IOS solve(); return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...