Submission #647174

# Submission time Handle Problem Language Result Execution time Memory
647174 2022-10-01T18:28:00 Z beaconmc The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
2207 ms 164456 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)

#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)

ll n,m;
ll grid[2001][2001];
ll grid2[2001][2001];

ll mx = -1;

ll minmaxl[2001][2001];
ll minmaxr[2001][2001];

void setup(){

	FORNEG(i,n-1,-1){
		minmaxl[i][0] = grid[i][0];
		FOR(j,1,m){
			minmaxl[i][j] = min(minmaxl[i][j-1], grid[i][j]);
		}
		if (i!=n-1){
			FOR(j,0,m){
				minmaxl[i][j] = min(minmaxl[i][j], minmaxl[i+1][j]);
			}
		}
	}

	FORNEG(i,n-1,-1){
		minmaxr[i][m-1] = grid[i][m-1];
		FORNEG(j,m-2,-1){
			minmaxr[i][j] = min(minmaxr[i][j+1], grid[i][j]);
		}
		if (i != n-1){
			FORNEG(j,m-1,-1){
				minmaxr[i][j] = min(minmaxr[i][j], minmaxr[i+1][j]);
			}
		}
	}
}

bool check(ll a){
	ll mini = 1000000000;
	ll maxi = -1;

	FOR(i,0,n){
		FOR(j,0,m){
			if (mx-minmaxl[i][j] > a){
            	mini = min(mini, grid[i][j]);
            	maxi = max(maxi, grid[i][j]);
			}
		}
	}

	if (maxi - mini <= a) return true;

	mini = 1000000000;
	maxi = -1;
	FOR(i,0,n){
		FOR(j,0,m){
			if (mx-minmaxr[i][j] > a){
            	mini = min(mini, grid[i][j]);
            	maxi = max(maxi, grid[i][j]);
			}
		}
	}

	if (maxi - mini <= a) return true;
	return false;

}
int main(){
	cin >> n >> m;
	FOR(i,0,n){
		FOR(j,0,m){
			cin >> grid[i][j];
			mx = max(mx, grid[i][j]);
			grid2[n-i-1][m-j-1] = grid[i][j];
		}
	}
	FOR(i,0,n){
		FOR(j,0,m){
			minmaxl[i][j] = 1000000000;
			minmaxr[i][j] = 1000000000;
		}
	}

	setup();

	ll lo = 0;
	ll hi = mx;
	while (lo<hi){
		ll mid = (lo+hi)/2;
		if (check(mid)){
			hi = mid;
		}
		else{
			lo = mid+1;
		}
	}

	ll ans = lo;

	FOR(i,0,n) FOR(j,0,m) grid[i][j] = grid2[i][j];

	FOR(i,0,n){
		FOR(j,0,m){
			minmaxl[i][j] = 1000000000;
			minmaxr[i][j] = 1000000000;
		}
	}



	setup();
	lo = 0;
	hi = mx;
	while (lo<hi){
		ll mid = (lo+hi)/2;
		if (check(mid)){
			hi = mid;
		}
		else{
			lo = mid+1;
		}
	}

	cout << min(ans,lo);



}
# 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 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 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 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 8 ms 4564 KB Output is correct
17 Correct 17 ms 4692 KB Output is correct
18 Correct 17 ms 4728 KB Output is correct
19 Correct 17 ms 4772 KB Output is correct
20 Correct 16 ms 4180 KB Output is correct
21 Correct 25 ms 4756 KB Output is correct
22 Correct 24 ms 4708 KB Output is correct
23 Correct 26 ms 4728 KB Output is correct
24 Correct 22 ms 4168 KB Output is correct
25 Correct 21 ms 4656 KB Output is correct
26 Correct 21 ms 4692 KB Output is correct
27 Correct 25 ms 4652 KB Output is correct
28 Correct 22 ms 4652 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 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 8 ms 4564 KB Output is correct
17 Correct 17 ms 4692 KB Output is correct
18 Correct 17 ms 4728 KB Output is correct
19 Correct 17 ms 4772 KB Output is correct
20 Correct 16 ms 4180 KB Output is correct
21 Correct 25 ms 4756 KB Output is correct
22 Correct 24 ms 4708 KB Output is correct
23 Correct 26 ms 4728 KB Output is correct
24 Correct 22 ms 4168 KB Output is correct
25 Correct 21 ms 4656 KB Output is correct
26 Correct 21 ms 4692 KB Output is correct
27 Correct 25 ms 4652 KB Output is correct
28 Correct 22 ms 4652 KB Output is correct
29 Correct 1383 ms 119360 KB Output is correct
30 Correct 1473 ms 125480 KB Output is correct
31 Correct 1440 ms 125544 KB Output is correct
32 Correct 1450 ms 125764 KB Output is correct
33 Correct 1269 ms 110424 KB Output is correct
34 Correct 1459 ms 125728 KB Output is correct
35 Correct 2093 ms 125724 KB Output is correct
36 Correct 1955 ms 158604 KB Output is correct
37 Correct 2207 ms 164456 KB Output is correct