Submission #362065

# Submission time Handle Problem Language Result Execution time Memory
362065 2021-02-01T16:30:20 Z Galebickosikasa The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
625 ms 80620 KB
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
 
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map 
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v) for (auto& x: v) cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

// #define int ll

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

using namespace std;

#ifdef LOCAL
	#define dbg(x) cerr << #x << " : " << x << '\n'
#else 
	#define dbg(x)
#endif

//tg: @runningcherry
 
template <typename T1, typename T2> ostream& operator << (ostream& out, const pair<T1, T2>& v) {
	out << v.fi << ", " << v.se;
	return out;
}

template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
	for (auto& x: v) out << x << " ";
	return out;
}

template <typename T1, typename T2> istream& operator >> (istream& in, pair<T1, T2>& a) {
	in >> a.fi >> a.se;
	return in;
}

const ll inf = (ll) 1e9;
const ld pi = asin (1) * 2;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

const int maxn = 2000 + 20;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int goo[maxn][maxn], n, m, mxp[maxn][maxn], mnp[maxn][maxn], mxs[maxn][maxn], mns[maxn][maxn], mn = inf;
set <int> gay;

inline int checkl (int x) {
	vector<int> kek (n, -1);
	int mx = mn + x;
	fr (i, n) {
		while (kek[i] != -1 && mnp[i][kek[i]] < mn) --kek[i];
		while (kek[i] + 1 < m && mnp[i][kek[i] + 1] >= mn && mxp[i][kek[i] + 1] <= mx) ++kek[i];
	}
	int last = kek[0];
	int mx1 = -inf, mn1 = inf;
	fr (i, n) {
		chkmin (last, kek[i]);
		if (last + 1 < m) {
			chkmin (mn1, mns[i][last + 1]);
			chkmax (mx1, mxs[i][last + 1]);
		}
	}
	if (mx1 - mn1 <= x) return 1;
	last = kek.back ();
	mx1 = -inf, mn1 = inf;
	for (int i = n - 1; i >= 0; --i) {
		chkmin (last, kek[i]);
		if (last + 1 < m) {
			chkmin (mn1, mns[i][last + 1]);
			chkmax (mx1, mxs[i][last + 1]);
		}
	}
	if (mx1 - mn1 <= x) return 1;
	return 0;
}

inline int checkr (int x) {
	vector<int> kek (n, m);
	int mx = mn + x;
	fr (i, n) {
		while (kek[i] != m && mns[i][kek[i]] < mn) ++kek[i];
		while (kek[i] > 0 && mns[i][kek[i] - 1] >= mn && mxs[i][kek[i] - 1] <= mx) --kek[i];
	}
	int last = kek[0];
	int mx1 = -inf, mn1 = inf;
	fr (i, n) {
		chkmax (last, kek[i]);
		if (last > 0) {
			chkmin (mn1, mnp[i][last - 1]);
			chkmax (mx1, mxp[i][last - 1]);
		}
	}
	if (mx1 - mn1 <= x) return 1;
	last = kek.back ();
	mx1 = -inf, mn1 = inf;
	for (int i = n - 1; i >= 0; --i) {
		chkmax (last, kek[i]);
		if (last > 0) {
			chkmin (mn1, mnp[i][last - 1]);
			chkmax (mx1, mxp[i][last - 1]);
		}
	}
	if (mx1 - mn1 <= x) return 1;
	return 0;
}

int bs () {
	int l = -1, r = 1e9;
	while (r - l > 1) {
		int m = (r + l) >> 1;
		if (checkl (m) || checkr (m)) r = m;
		else l = m;
	}
	return r;
}

signed main () {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> n >> m;
	fr (i, n) fr (j, m) cin >> goo[i][j];
	fr (i, n) {
		mnp[i][0] = mxp[i][0] = goo[i][0];
		fl (j, 1, m) {
			mnp[i][j] = min (mnp[i][j - 1], goo[i][j]);
			mxp[i][j] = max (mxp[i][j - 1], goo[i][j]);
		}
		mns[i][m - 1] = mxs[i][m - 1] = goo[i][m - 1];
		for (int j = m - 2; j >= 0; --j) {
			mns[i][j] = min (mns[i][j + 1], goo[i][j]);
			mxs[i][j] = max (mxs[i][j + 1], goo[i][j]);
		}
	}
	fr (i, n) fr (j, m) chkmin (mn, goo[i][j]);
	cout << bs () << '\n';






}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 532 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 7 ms 5100 KB Output is correct
17 Correct 9 ms 5356 KB Output is correct
18 Correct 8 ms 5356 KB Output is correct
19 Correct 8 ms 5356 KB Output is correct
20 Correct 7 ms 4844 KB Output is correct
21 Correct 10 ms 5484 KB Output is correct
22 Correct 9 ms 5484 KB Output is correct
23 Correct 9 ms 5484 KB Output is correct
24 Correct 8 ms 4972 KB Output is correct
25 Correct 9 ms 5484 KB Output is correct
26 Correct 9 ms 5484 KB Output is correct
27 Correct 10 ms 5484 KB Output is correct
28 Correct 10 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 532 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 7 ms 5100 KB Output is correct
17 Correct 9 ms 5356 KB Output is correct
18 Correct 8 ms 5356 KB Output is correct
19 Correct 8 ms 5356 KB Output is correct
20 Correct 7 ms 4844 KB Output is correct
21 Correct 10 ms 5484 KB Output is correct
22 Correct 9 ms 5484 KB Output is correct
23 Correct 9 ms 5484 KB Output is correct
24 Correct 8 ms 4972 KB Output is correct
25 Correct 9 ms 5484 KB Output is correct
26 Correct 9 ms 5484 KB Output is correct
27 Correct 10 ms 5484 KB Output is correct
28 Correct 10 ms 5484 KB Output is correct
29 Correct 494 ms 75652 KB Output is correct
30 Correct 540 ms 80076 KB Output is correct
31 Correct 507 ms 79980 KB Output is correct
32 Correct 541 ms 80208 KB Output is correct
33 Correct 436 ms 70252 KB Output is correct
34 Correct 515 ms 80108 KB Output is correct
35 Correct 587 ms 80620 KB Output is correct
36 Correct 517 ms 79980 KB Output is correct
37 Correct 625 ms 80620 KB Output is correct