Submission #648140

# Submission time Handle Problem Language Result Execution time Memory
648140 2022-10-05T13:48:12 Z ghostwriter The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
956 ms 16136 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int oo = 1e9 + 5;
const int N = 2e3 + 1;
int h, w, a[N][N], mx = 0, my = 0;
bool check(int x) {
	int len = w, minv = oo, maxv = -oo;
	FOS(i, h, 1) {
		int j = 1, clen = len;
		WHILE(a[mx][my] - a[i][j] <= x && clen--) ++j;
		--j;
		FOR(z, j + 1, w) {
			minv = min(minv, a[i][z]);
			maxv = max(maxv, a[i][z]);
		}
		len = j;
	}
	if (minv == oo || maxv - minv <= x) return 1;
	len = w; minv = oo; maxv = -oo;
	FOR(i, 1, h) {
		int j = 1, clen = len;
		WHILE(a[mx][my] - a[i][j] <= x && clen--) ++j;
		--j;
		FOR(z, j + 1, w) {
			minv = min(minv, a[i][z]);
			maxv = max(maxv, a[i][z]);
		}
		len = j;
	}
	if (minv == oo || maxv - minv <= x) return 1;
	len = w; minv = oo; maxv = -oo;
	FOS(i, h, 1) {
		int j = w, clen = len;
		WHILE(a[mx][my] - a[i][j] <= x && clen--) --j;
		++j;
		FOR(z, 1, j - 1) {
			minv = min(minv, a[i][z]);
			maxv = max(maxv, a[i][z]);
		}
		len = w - j + 1;
	}
	if (minv == oo || maxv - minv <= x) return 1;
	len = w; minv = oo; maxv = -oo;
	FOR(i, 1, h) {
		int j = w, clen = len;
		WHILE(a[mx][my] - a[i][j] <= x && clen--) --j;
		++j;
		FOR(z, 1, j - 1) {
			minv = min(minv, a[i][z]);
			maxv = max(maxv, a[i][z]);
		}
		len = w - j + 1;
	}
	if (minv == oo || maxv - minv <= x) return 1;
	return 0;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> h >> w;
    FOR(i, 1, h)
    FOR(j, 1, w)
    	cin >> a[i][j];
    FOR(i, 1, h)
    FOR(j, 1, w)
    	if (a[i][j] > a[mx][my])
    		tie(mx, my) = mtp(i, j);
    int l = 0, r = 1e9 - 1, ans = -1;
    WHILE(l <= r) {
    	int mid = l + (r - l) / 2;
    	if (check(mid)) {
    		ans = mid;
    		r = mid - 1;
    	}
    	else l = mid + 1;
    }
    cout << ans;
    return 0;
}
/*
8 6
23 23 10 11 16 21
15 26 19 28 19 20
25 26 28 16 15 11
11 8 19 11 15 24
14 19 15 14 24 11
10 8 11 7 6 14
23 5 19 23 17 17
18 11 21 14 20 16
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

joioi.cpp: In function 'bool check(int)':
joioi.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
joioi.cpp:56:2: note: in expansion of macro 'FOS'
   56 |  FOS(i, h, 1) {
      |  ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:60:3: note: in expansion of macro 'FOR'
   60 |   FOR(z, j + 1, w) {
      |   ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:68:2: note: in expansion of macro 'FOR'
   68 |  FOR(i, 1, h) {
      |  ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:72:3: note: in expansion of macro 'FOR'
   72 |   FOR(z, j + 1, w) {
      |   ^~~
joioi.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
joioi.cpp:80:2: note: in expansion of macro 'FOS'
   80 |  FOS(i, h, 1) {
      |  ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:84:3: note: in expansion of macro 'FOR'
   84 |   FOR(z, 1, j - 1) {
      |   ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:92:2: note: in expansion of macro 'FOR'
   92 |  FOR(i, 1, h) {
      |  ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:96:3: note: in expansion of macro 'FOR'
   96 |   FOR(z, 1, j - 1) {
      |   ^~~
joioi.cpp: In function 'int main()':
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:110:5: note: in expansion of macro 'FOR'
  110 |     FOR(i, 1, h)
      |     ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:111:5: note: in expansion of macro 'FOR'
  111 |     FOR(j, 1, w)
      |     ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:113:5: note: in expansion of macro 'FOR'
  113 |     FOR(i, 1, h)
      |     ^~~
joioi.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
joioi.cpp:114:5: note: in expansion of macro 'FOR'
  114 |     FOR(j, 1, w)
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 1236 KB Output is correct
17 Correct 7 ms 1288 KB Output is correct
18 Correct 8 ms 1292 KB Output is correct
19 Correct 8 ms 1236 KB Output is correct
20 Correct 8 ms 1184 KB Output is correct
21 Correct 8 ms 1292 KB Output is correct
22 Correct 11 ms 1252 KB Output is correct
23 Correct 8 ms 1236 KB Output is correct
24 Correct 9 ms 1108 KB Output is correct
25 Correct 9 ms 1236 KB Output is correct
26 Correct 12 ms 1284 KB Output is correct
27 Correct 10 ms 1236 KB Output is correct
28 Correct 9 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 1236 KB Output is correct
17 Correct 7 ms 1288 KB Output is correct
18 Correct 8 ms 1292 KB Output is correct
19 Correct 8 ms 1236 KB Output is correct
20 Correct 8 ms 1184 KB Output is correct
21 Correct 8 ms 1292 KB Output is correct
22 Correct 11 ms 1252 KB Output is correct
23 Correct 8 ms 1236 KB Output is correct
24 Correct 9 ms 1108 KB Output is correct
25 Correct 9 ms 1236 KB Output is correct
26 Correct 12 ms 1284 KB Output is correct
27 Correct 10 ms 1236 KB Output is correct
28 Correct 9 ms 1236 KB Output is correct
29 Correct 616 ms 15200 KB Output is correct
30 Correct 570 ms 15972 KB Output is correct
31 Correct 680 ms 15976 KB Output is correct
32 Correct 617 ms 16064 KB Output is correct
33 Correct 576 ms 14100 KB Output is correct
34 Correct 579 ms 15976 KB Output is correct
35 Correct 956 ms 16136 KB Output is correct
36 Correct 643 ms 15884 KB Output is correct
37 Correct 828 ms 16100 KB Output is correct