답안 #370332

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
370332 2021-02-23T19:48:15 Z shivensinha4 The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2266 ms 61644 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, 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 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 384 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 364 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 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 384 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 364 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 492 KB Output is correct
16 Correct 11 ms 1004 KB Output is correct
17 Correct 17 ms 1132 KB Output is correct
18 Correct 16 ms 1132 KB Output is correct
19 Correct 16 ms 1132 KB Output is correct
20 Correct 16 ms 1004 KB Output is correct
21 Correct 18 ms 1260 KB Output is correct
22 Correct 17 ms 1236 KB Output is correct
23 Correct 17 ms 1260 KB Output is correct
24 Correct 15 ms 1132 KB Output is correct
25 Correct 17 ms 1260 KB Output is correct
26 Correct 17 ms 1260 KB Output is correct
27 Correct 17 ms 1388 KB Output is correct
28 Correct 18 ms 1260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 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 384 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 364 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 492 KB Output is correct
16 Correct 11 ms 1004 KB Output is correct
17 Correct 17 ms 1132 KB Output is correct
18 Correct 16 ms 1132 KB Output is correct
19 Correct 16 ms 1132 KB Output is correct
20 Correct 16 ms 1004 KB Output is correct
21 Correct 18 ms 1260 KB Output is correct
22 Correct 17 ms 1236 KB Output is correct
23 Correct 17 ms 1260 KB Output is correct
24 Correct 15 ms 1132 KB Output is correct
25 Correct 17 ms 1260 KB Output is correct
26 Correct 17 ms 1260 KB Output is correct
27 Correct 17 ms 1388 KB Output is correct
28 Correct 18 ms 1260 KB Output is correct
29 Correct 2016 ms 50632 KB Output is correct
30 Correct 1882 ms 57104 KB Output is correct
31 Correct 2039 ms 53012 KB Output is correct
32 Correct 2074 ms 53164 KB Output is correct
33 Correct 1785 ms 61644 KB Output is correct
34 Correct 2099 ms 53108 KB Output is correct
35 Correct 2219 ms 55936 KB Output is correct
36 Correct 1951 ms 47872 KB Output is correct
37 Correct 2266 ms 56088 KB Output is correct