Submission #370338

# Submission time Handle Problem Language Result Execution time Memory
370338 2021-02-23T20:03:43 Z shivensinha4 The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2285 ms 47620 KB
#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'

int n, m;
vector<vi> mat;
const int INF = 1e9;
int fin;
vi nums;

void solve() {
	int l = 0, r = (int) nums.size()-1;
	while (r > 0 and nums[r-1]-nums[0] >= fin) r--;
	int ans = r;
	
	vi wd(n);
	while (l < r) {
		int mid = (l+r)/2;
		bool valid = true;
		for_(i, 0, n) {
			int pt = -1;
			for_(j, 0, m) {
				if (mat[i][j] > nums[mid]) break;
				pt = j;
			}
			if (i > 0) pt = min(pt, wd[i-1]);
			wd[i] = pt;
		}
		
		int mn = INF;
		if (valid) for_(i, 0, n) for_(j, wd[i]+1, m) {
					mn = min(mn, mat[i][j]);
				}
		if (nums.back()-mn > nums[mid]-nums[0]) valid = false;
		if (!valid) l = mid+1;
		else {
			r = ans = mid;
		}
	}
	
	fin = min(fin, nums[ans]-nums[0]);
}

void rotate() {
	vector<vi> temp(m, vi(n));
	for (int j = m-1; j >= 0; j--) {
		for_(i, 0, n) temp[m-j-1][i] = mat[i][j];
	}
	swap(n, m);
	swap(mat, temp);
}

void rotateAndSolve() {
	for_(i, 0, 4) {
		solve();
		rotate();
	}
}


int main() {
#ifdef mlocal
	freopen("test.in", "r", stdin);
#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> m;
	mat.resize(n, vi(m)); nums.reserve(n*m);
	for_(i, 0, n) for_(j, 0, m) {
			cin >> mat[i][j];
			nums.push_back(mat[i][j]);
		}
	
	sort(nums.begin(), nums.end());
	fin = nums.back()-nums[0];
	rotateAndSolve();
	for_(i, 0, n) for_(j, 0, m) mat[i][j] = -mat[i][j];
	reverse(nums.begin(), nums.end());
	for_(i, 0, nums.size()) nums[i] = -nums[i];
	rotateAndSolve();
	
	cout << fin << endl;
	
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 15 ms 860 KB Output is correct
17 Correct 17 ms 876 KB Output is correct
18 Correct 16 ms 896 KB Output is correct
19 Correct 20 ms 876 KB Output is correct
20 Correct 14 ms 768 KB Output is correct
21 Correct 17 ms 876 KB Output is correct
22 Correct 16 ms 852 KB Output is correct
23 Correct 16 ms 876 KB Output is correct
24 Correct 14 ms 748 KB Output is correct
25 Correct 16 ms 876 KB Output is correct
26 Correct 17 ms 876 KB Output is correct
27 Correct 17 ms 876 KB Output is correct
28 Correct 17 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 15 ms 860 KB Output is correct
17 Correct 17 ms 876 KB Output is correct
18 Correct 16 ms 896 KB Output is correct
19 Correct 20 ms 876 KB Output is correct
20 Correct 14 ms 768 KB Output is correct
21 Correct 17 ms 876 KB Output is correct
22 Correct 16 ms 852 KB Output is correct
23 Correct 16 ms 876 KB Output is correct
24 Correct 14 ms 748 KB Output is correct
25 Correct 16 ms 876 KB Output is correct
26 Correct 17 ms 876 KB Output is correct
27 Correct 17 ms 876 KB Output is correct
28 Correct 17 ms 876 KB Output is correct
29 Correct 1917 ms 45140 KB Output is correct
30 Correct 1905 ms 44372 KB Output is correct
31 Correct 2100 ms 47608 KB Output is correct
32 Correct 2094 ms 47528 KB Output is correct
33 Correct 1685 ms 41604 KB Output is correct
34 Correct 2045 ms 47620 KB Output is correct
35 Correct 2285 ms 47440 KB Output is correct
36 Correct 1869 ms 41708 KB Output is correct
37 Correct 2198 ms 47620 KB Output is correct