Submission #206246

# Submission time Handle Problem Language Result Execution time Memory
206246 2020-03-02T19:57:01 Z opukittpceno_hhr The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
1535 ms 20856 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>

using namespace std;

const int N = 2001;
const int INF = 1e9 + 239;

int n, m;
int a[N][N];

int c1(int l, int r) {
	auto can = [&](int i, int cnt) {
		int j = m - 1;
		int ok = 1;
		for (int c = 0; c < cnt; c++) {
			ok &= (l <= a[i][j] && a[i][j] <= r);
			j--;
		}
		return ok;
	};
	vector<int> tk(n);
	int pnt = 0;
	for (int cnt = 0; cnt <= m; cnt++) {
		if (can(n - 1, cnt)) {
			pnt = cnt;
		}
	}
	if (pnt == 0) return false;
	tk[n - 1] = pnt;
	for (int i = n - 2; i >= 0; i--) {
		while (!can(i, pnt)) {
			pnt--;
		}
		tk[i] = pnt;
	}
	// cerr << l << ' ' << r << endl;
	// for (auto t : tk) {
	// 	cerr << t << ' ';
	// }
	// cerr << endl;
	int cmax = -1;
	int cmin = INF;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m - tk[i]; j++) {
			cmax = max(cmax, a[i][j]);
			cmin = min(cmin, a[i][j]);
		}
	}
	return cmax - cmin <= r - l;
}

int c2(int l, int r) {
	auto can = [&](int i, int cnt) {
		int j = 0;
		int ok = 1;
		for (int c = 0; c < cnt; c++) {
			ok &= (l <= a[i][j] && a[i][j] <= r);
			j++;
		}
		return ok;
	};
	vector<int> tk(n);
	int pnt = 0;
	for (int cnt = 0; cnt <= m; cnt++) {
		if (can(0, cnt)) {
			pnt = cnt;
		}
	}
	if (pnt == 0) return false;
	tk[0] = pnt;
	for (int i = 1; i < n; i++) {
		while (!can(i, pnt)) {
			pnt--;
		}
		tk[i] = pnt;
	}
	int cmax = -1;
	int cmin = INF;
	for (int i = 0; i < n; i++) {
		for (int j = tk[i]; j < m; j++) {
			cmax = max(cmax, a[i][j]);
			cmin = min(cmin, a[i][j]);
		}
	}
	return cmax - cmin <= r - l;
}

int solve() {
	int r = -1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			r = max(r, a[i][j]);
		}
	}
	auto check = [&](int x) {
		return c1(r - x, r) || c2(r - x, r);
	};
	int tl = -1;
	int tr = INF;
	while (tr - tl > 1) {
		int tm = (tl + tr) / 2;
		if (check(tm)) tr = tm;
		else tl = tm;
	}
	return tr;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}
	int ans = solve();
	for (int i = 0; i < n; i++) {
		reverse(a[i], a[i] + m);
	}
	ans = min(ans, solve());
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 6 ms 380 KB Output is correct
16 Correct 14 ms 1528 KB Output is correct
17 Correct 17 ms 1532 KB Output is correct
18 Correct 16 ms 1528 KB Output is correct
19 Correct 17 ms 1528 KB Output is correct
20 Correct 15 ms 1400 KB Output is correct
21 Correct 21 ms 1656 KB Output is correct
22 Correct 20 ms 1656 KB Output is correct
23 Correct 20 ms 1656 KB Output is correct
24 Correct 22 ms 1528 KB Output is correct
25 Correct 19 ms 1660 KB Output is correct
26 Correct 22 ms 1656 KB Output is correct
27 Correct 21 ms 1672 KB Output is correct
28 Correct 21 ms 1660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 6 ms 380 KB Output is correct
16 Correct 14 ms 1528 KB Output is correct
17 Correct 17 ms 1532 KB Output is correct
18 Correct 16 ms 1528 KB Output is correct
19 Correct 17 ms 1528 KB Output is correct
20 Correct 15 ms 1400 KB Output is correct
21 Correct 21 ms 1656 KB Output is correct
22 Correct 20 ms 1656 KB Output is correct
23 Correct 20 ms 1656 KB Output is correct
24 Correct 22 ms 1528 KB Output is correct
25 Correct 19 ms 1660 KB Output is correct
26 Correct 22 ms 1656 KB Output is correct
27 Correct 21 ms 1672 KB Output is correct
28 Correct 21 ms 1660 KB Output is correct
29 Correct 1029 ms 17604 KB Output is correct
30 Correct 1014 ms 20856 KB Output is correct
31 Correct 1102 ms 18424 KB Output is correct
32 Correct 1100 ms 18320 KB Output is correct
33 Correct 901 ms 14312 KB Output is correct
34 Correct 1094 ms 18320 KB Output is correct
35 Correct 1535 ms 16200 KB Output is correct
36 Correct 1171 ms 16248 KB Output is correct
37 Correct 1382 ms 16248 KB Output is correct