Submission #912567

# Submission time Handle Problem Language Result Execution time Memory
912567 2024-01-19T15:50:32 Z penguin133 The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
437 ms 180868 KB
#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, A[2005][2005], P[2005][2005], S[2005][2005], P2[2005][2005], S2[2005][2005], P3[2005][2005], S3[2005][2005], P4[2005][2005], S4[2005][2005];

void solve(){
	cin >> n >> m;
	int mn = 1e18, mx = 0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)cin >> A[i][j], mn = min(mn, A[i][j]), mx = max(mx, A[i][j]);
	}
	for(int i=1;i<=m;i++){
		P2[0][i] = S2[n + 1][i] = 1e18;
		for(int j=1;j<=n;j++){
			P[j][i] = max(P[j-1][i], A[j][i]);
			P2[j][i] = min(P2[j-1][i], A[j][i]);
		}
		for(int j=n;j>=1;j--){
			S[j][i] = max(S[j+1][i], A[j][i]);
			S2[j][i] = min(S2[j+1][i], A[j][i]);
		}
	}
	for(int i=1;i<=n;i++){
		P4[i][0] = S4[i][m+1] = 1e18;
		for(int j=1;j<=m;j++){
			P3[i][j] = max(P3[i][j-1], A[i][j]);
			P4[i][j] = min(P4[i][j-1], A[i][j]);
		}
		for(int j=m;j>=1;j--){
			S3[i][j] = max(S3[i][j+1], A[i][j]);
			S4[i][j] = min(S4[i][j+1], A[i][j]);
		}
	}
	int lo = 0, hi = (int)1e9, ans = hi;
	while(lo <= hi){
		int mid = (lo + hi) >> 1;
		int ptr = n, mn2 = 1e18;
		for(int i=1;i<=m;i++){
			while(ptr >= 1 && (P[ptr][i] > mn + mid || S2[ptr + 1][i] < mx - mid))ptr--;
			mn2 = min(mn2, S2[ptr + 1][i]);
		}
		bool f = (mn2 >= mx - mid);
		ptr = n, mn2 = 1e18;
		for(int i=m;i>=1;i--){
			while(ptr >= 1 && (P[ptr][i] > mn + mid || S2[ptr + 1][i] < mx - mid))ptr--;
			mn2 = min(mn2, S2[ptr + 1][i]);
		}
		f |= (mn2 >= mx - mid);
		int mx2 = 0;
		ptr = n;
		for(int i=1;i<=m;i++){
			while(ptr >= 1 && (P2[ptr][i] < mx - mid || S[ptr + 1][i] > mn + mid))ptr--;
			mx2 = max(mx2, S[ptr + 1][i]);
		}
		f |= (mx2 <= mn + mid);
		ptr = n, mx2 = 0;
		for(int i=m;i>=1;i--){
			while(ptr >= 1 && (P2[ptr][i] < mx - mid || S[ptr + 1][i] > mn + mid))ptr--;
			mx2 = max(mx2, S[ptr + 1][i]);
		}
		f |= (mx2 <= mn + mid);
		
		ptr = m, mn2 = 1e18;
		for(int i=1;i<=n;i++){
			while(ptr >= 1 && (P3[i][ptr] > mn + mid || S4[i][ptr + 1] < mx - mid))ptr--;
			mn2 = min(mn2, S4[i][ptr + 1]);
		}
		f |= (mn2 >= mx - mid);
		ptr = m, mn2 = 1e18;
		for(int i=n;i>=1;i--){
			while(ptr >= 1 && (P3[i][ptr] > mn + mid || S4[i][ptr + 1] < mx - mid))ptr--;
			mn2 = min(mn2, S4[i][ptr + 1]);
		}
		f |= (mn2 >= mx - mid);
		
		ptr = m, mx2 = 0;
		for(int i=1;i<=n;i++){
			while(ptr >= 1 && (P4[i][ptr] < mx - mid || S3[i][ptr + 1] > mn + mid))ptr--;
			mx2 = max(mx2, S3[i][ptr + 1]);
		}
		f |= (mx2 <= mn + mid);
		ptr = m, mx2 = 0;
		for(int i=n;i>=1;i--){
			while(ptr >= 1 && (P4[i][ptr] < mx - mid || S3[i][ptr + 1] > mn + mid))ptr--;
			mx2 = max(mx2, S3[i][ptr + 1]);
		}
		f |= (mx2 <= mn + mid);
		
		if(f)ans = mid, hi = mid - 1;
		else lo = mid + 1;
	}
	cout << ans;
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

joioi.cpp: In function 'void solve()':
joioi.cpp:18:11: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   18 |  int mn = 1e18, mx = 0;
      |           ^~~~
joioi.cpp:23:29: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   23 |   P2[0][i] = S2[n + 1][i] = 1e18;
      |                             ^~~~
joioi.cpp:34:27: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   34 |   P4[i][0] = S4[i][m+1] = 1e18;
      |                           ^~~~
joioi.cpp:47:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   47 |   int ptr = n, mn2 = 1e18;
      |                      ^~~~
joioi.cpp:53:18: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   53 |   ptr = n, mn2 = 1e18;
      |                  ^~~~
joioi.cpp:73:18: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   73 |   ptr = m, mn2 = 1e18;
      |                  ^~~~
joioi.cpp:79:18: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   79 |   ptr = m, mn2 = 1e18;
      |                  ^~~~
joioi.cpp: At global scope:
joioi.cpp:105:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  105 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 7 ms 26204 KB Output is correct
17 Correct 9 ms 26460 KB Output is correct
18 Correct 8 ms 26460 KB Output is correct
19 Correct 9 ms 26460 KB Output is correct
20 Correct 7 ms 21880 KB Output is correct
21 Correct 9 ms 26460 KB Output is correct
22 Correct 9 ms 26204 KB Output is correct
23 Correct 9 ms 26456 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
25 Correct 9 ms 26472 KB Output is correct
26 Correct 9 ms 26460 KB Output is correct
27 Correct 9 ms 26264 KB Output is correct
28 Correct 9 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 7 ms 26204 KB Output is correct
17 Correct 9 ms 26460 KB Output is correct
18 Correct 8 ms 26460 KB Output is correct
19 Correct 9 ms 26460 KB Output is correct
20 Correct 7 ms 21880 KB Output is correct
21 Correct 9 ms 26460 KB Output is correct
22 Correct 9 ms 26204 KB Output is correct
23 Correct 9 ms 26456 KB Output is correct
24 Correct 8 ms 21852 KB Output is correct
25 Correct 9 ms 26472 KB Output is correct
26 Correct 9 ms 26460 KB Output is correct
27 Correct 9 ms 26264 KB Output is correct
28 Correct 9 ms 26460 KB Output is correct
29 Correct 320 ms 161080 KB Output is correct
30 Correct 338 ms 163428 KB Output is correct
31 Correct 348 ms 164796 KB Output is correct
32 Correct 346 ms 165060 KB Output is correct
33 Correct 333 ms 155792 KB Output is correct
34 Correct 362 ms 165092 KB Output is correct
35 Correct 418 ms 180544 KB Output is correct
36 Correct 371 ms 175040 KB Output is correct
37 Correct 437 ms 180868 KB Output is correct