Submission #209125

# Submission time Handle Problem Language Result Execution time Memory
209125 2020-03-13T08:52:15 Z Siberian The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
723 ms 63676 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}

const int MAXN = 2010;
int a[MAXN][MAXN];
int n, m;
pair<int, int> pos_min;
void read() {
	cin >> n >> m;
	pos_min = {0, 0};

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] < a[pos_min.first][pos_min.second])
				pos_min = {i, j};
		}
	}
}

vector<int> table_left[MAXN], table_right[2][MAXN];

void build() {
	for (int i = 0; i < n; i++) {
		vector<int> have(m);
		for (int j = 0; j < m; j++)
			have[j] = a[i][j];
		table_left[i] = table_right[0][i] = table_right[1][i] = have;
		
		for (int j = 1; j < m; j++)
			chkmax(table_left[i][j], table_left[i][j - 1]);

		for (int j = m - 2; j >= 0; j--)
			chkmin(table_right[0][i][j], table_right[0][i][j + 1]);
		
		for (int j = m - 2; j >= 0; j--)
			chkmax(table_right[1][i][j], table_right[1][i][j + 1]);
	}
}

const int INF = 1e9 + 10;

bool check(int mid, vector<int> len) {
	int Min = INF, Max = -INF;
	for (int i = 0; i < n; i++) {
		if (len[i] == m - 1) continue;
		chkmin(Min, table_right[0][i][len[i] + 1]);
		chkmax(Max, table_right[1][i][len[i] + 1]);
	}
	return Max - Min <= mid;
}

bool check(int mid) {
	int x = a[pos_min.first][pos_min.second] + mid;
	vector<int> len;
	for (int i = 0; i < n; i++) {
		int pos;
		pos = --upper_bound(all(table_left[i]), x) - table_left[i].begin();
		len.push_back(pos);
	}

	vector<int> have;
	have = len;
	for (int i = 1; i < n; i++) {
		chkmin(have[i], have[i - 1]);
	}
	if (check(mid, have)) return true;
	have = len;
	for (int i = n - 2; i >= 0; i--) {
		chkmin(have[i], have[i + 1]);
	}
	if (check(mid, have)) return true;
	return false;
}

int calc() {
	build();
	int l = -1, r = INF;
	while (l < r - 1) {
		int mid = (l + r) / 2;
		if (check(mid))
			r = mid;
		else
			l = mid;
	}
	return r;
}

void rebuild() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m - 1 - j; j++) {
			swap(a[i][j], a[i][m - 1 - j]);
		}
	}
	pos_min.second = m - 1 - pos_min.second;
}

int ans;

void run() {
	ans = INF;
	chkmin(ans, calc());
	rebuild();
	chkmin(ans, calc());
}

void write() {
	cout << ans << endl;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	read();
	run();
	write();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 508 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 504 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 508 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 504 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 632 KB Output is correct
16 Correct 10 ms 1912 KB Output is correct
17 Correct 14 ms 2168 KB Output is correct
18 Correct 12 ms 2168 KB Output is correct
19 Correct 12 ms 2168 KB Output is correct
20 Correct 11 ms 2040 KB Output is correct
21 Correct 12 ms 2296 KB Output is correct
22 Correct 13 ms 2296 KB Output is correct
23 Correct 14 ms 2296 KB Output is correct
24 Correct 13 ms 2080 KB Output is correct
25 Correct 16 ms 2296 KB Output is correct
26 Correct 13 ms 2296 KB Output is correct
27 Correct 14 ms 2296 KB Output is correct
28 Correct 14 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 508 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 504 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 632 KB Output is correct
16 Correct 10 ms 1912 KB Output is correct
17 Correct 14 ms 2168 KB Output is correct
18 Correct 12 ms 2168 KB Output is correct
19 Correct 12 ms 2168 KB Output is correct
20 Correct 11 ms 2040 KB Output is correct
21 Correct 12 ms 2296 KB Output is correct
22 Correct 13 ms 2296 KB Output is correct
23 Correct 14 ms 2296 KB Output is correct
24 Correct 13 ms 2080 KB Output is correct
25 Correct 16 ms 2296 KB Output is correct
26 Correct 13 ms 2296 KB Output is correct
27 Correct 14 ms 2296 KB Output is correct
28 Correct 14 ms 2296 KB Output is correct
29 Correct 550 ms 60332 KB Output is correct
30 Correct 521 ms 59936 KB Output is correct
31 Correct 600 ms 63484 KB Output is correct
32 Correct 581 ms 63584 KB Output is correct
33 Correct 489 ms 55488 KB Output is correct
34 Correct 553 ms 63480 KB Output is correct
35 Correct 694 ms 63568 KB Output is correct
36 Correct 635 ms 60592 KB Output is correct
37 Correct 723 ms 63676 KB Output is correct