Submission #535407

# Submission time Handle Problem Language Result Execution time Memory
535407 2022-03-10T08:39:21 Z amunduzbaev The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2865 ms 102140 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
#define int long long

const int N = 2005;
const int inf = 1e9;
int a[N][N], is[N][N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	int mn = 1e9;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			mn = min(mn, a[i][j]);
		}
	}
	
	auto print = [&](int d){
		cout<<d<<"\n";
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++) cout<<is[i][j]<<" ";
			cout<<"\n";
		} cout<<"\n";
	};
	
	auto check = [&](int d){
		memset(is, 0, sizeof is);
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(a[i][j] <= mn + d) is[i][j] = 1;
			}
		}
		
		vector<int> pref(n), suff(n);
		for(int i=0;i<n;i++){
			int j=0;
			while(j<m && is[i][j]) j++;
			pref[i] = j;
			j=m-1;
			while(~j && is[i][j]) j--;
			suff[i] = j + 1;
		}
		
		{
			vector<int> tmp = pref;
			for(int i=1;i<n;i++) tmp[i] = min(tmp[i], tmp[i-1]);
			
			memset(is, 0, sizeof is);
			int M[2][2] = {
				{inf, 0},
				{inf, 0}
			};
			for(int i=0;i<n;i++){
				for(int j=0;j<tmp[i];j++) is[i][j] = 1;
				for(int j=0;j<m;j++){
					M[is[i][j]][0] = min(M[is[i][j]][0], a[i][j]);
					M[is[i][j]][1] = max(M[is[i][j]][1], a[i][j]);
				}
			}
			
			if(M[0][1] - M[0][0] <= d && M[1][1] - M[1][0] <= d){
				//~ print(d);
				return true;
			}
		}
		
		{
			vector<int> tmp = pref;
			for(int i=n-2;~i;i--) tmp[i] = min(tmp[i], tmp[i+1]);
			
			memset(is, 0, sizeof is);
			int M[2][2] = {
				{inf, 0},
				{inf, 0}
			};
			for(int i=0;i<n;i++){
				for(int j=0;j<tmp[i];j++) is[i][j] = 1;
				for(int j=0;j<m;j++){
					M[is[i][j]][0] = min(M[is[i][j]][0], a[i][j]);
					M[is[i][j]][1] = max(M[is[i][j]][1], a[i][j]);
				}
			}
			
			if(M[0][1] - M[0][0] <= d && M[1][1] - M[1][0] <= d){
				//~ print(d);
				return true;
			}
		}
		{
			vector<int> tmp = suff;
			for(int i=1;i<n;i++) tmp[i] = max(tmp[i], tmp[i-1]);
			
			memset(is, 0, sizeof is);
			int M[2][2] = {
				{inf, 0},
				{inf, 0}
			};
			for(int i=0;i<n;i++){
				for(int j=tmp[i];j<m;j++) is[i][j] = 1;
				for(int j=0;j<m;j++){
					M[is[i][j]][0] = min(M[is[i][j]][0], a[i][j]);
					M[is[i][j]][1] = max(M[is[i][j]][1], a[i][j]);
				}
			}
			
			if(M[0][1] - M[0][0] <= d && M[1][1] - M[1][0] <= d){
				//~ print(d);
				return true;
			}
		}
		
		{
			vector<int> tmp = suff;
			for(int i=n-2;~i;i--) tmp[i] = max(tmp[i], tmp[i+1]);
			
			memset(is, 0, sizeof is);
			int M[2][2] = {
				{inf, 0},
				{inf, 0}
			};
			for(int i=0;i<n;i++){
				for(int j=tmp[i];j<m;j++) is[i][j] = 1;
				for(int j=0;j<m;j++){
					M[is[i][j]][0] = min(M[is[i][j]][0], a[i][j]);
					M[is[i][j]][1] = max(M[is[i][j]][1], a[i][j]);
				}
			}
			
			if(M[0][1] - M[0][0] <= d && M[1][1] - M[1][0] <= d){
				//~ print(d);
				return true;
			}
		}
		
		return false;
	};
	
	int l = 0, r = inf;
	while(l < r){
		int m = (l + r) >> 1;
		if(check(m)) r = m;
		else l = m + 1;
	}
	
	cout<<l<<"\n";
}

Compilation message

joioi.cpp: In function 'int main()':
joioi.cpp:23:7: warning: variable 'print' set but not used [-Wunused-but-set-variable]
   23 |  auto print = [&](int d){
      |       ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 216 ms 31812 KB Output is correct
2 Correct 215 ms 31700 KB Output is correct
3 Correct 380 ms 31832 KB Output is correct
4 Correct 396 ms 31884 KB Output is correct
5 Correct 421 ms 31828 KB Output is correct
6 Correct 395 ms 31828 KB Output is correct
7 Correct 485 ms 31828 KB Output is correct
8 Correct 370 ms 31828 KB Output is correct
9 Correct 432 ms 31820 KB Output is correct
10 Correct 414 ms 31796 KB Output is correct
11 Correct 503 ms 31828 KB Output is correct
12 Correct 480 ms 31828 KB Output is correct
13 Correct 379 ms 31948 KB Output is correct
14 Correct 464 ms 31816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 31812 KB Output is correct
2 Correct 215 ms 31700 KB Output is correct
3 Correct 380 ms 31832 KB Output is correct
4 Correct 396 ms 31884 KB Output is correct
5 Correct 421 ms 31828 KB Output is correct
6 Correct 395 ms 31828 KB Output is correct
7 Correct 485 ms 31828 KB Output is correct
8 Correct 370 ms 31828 KB Output is correct
9 Correct 432 ms 31820 KB Output is correct
10 Correct 414 ms 31796 KB Output is correct
11 Correct 503 ms 31828 KB Output is correct
12 Correct 480 ms 31828 KB Output is correct
13 Correct 379 ms 31948 KB Output is correct
14 Correct 464 ms 31816 KB Output is correct
15 Correct 219 ms 31744 KB Output is correct
16 Correct 227 ms 32952 KB Output is correct
17 Correct 315 ms 33204 KB Output is correct
18 Correct 313 ms 33164 KB Output is correct
19 Correct 453 ms 33172 KB Output is correct
20 Correct 415 ms 33012 KB Output is correct
21 Correct 481 ms 33292 KB Output is correct
22 Correct 515 ms 33276 KB Output is correct
23 Correct 442 ms 33300 KB Output is correct
24 Correct 392 ms 33128 KB Output is correct
25 Correct 452 ms 33288 KB Output is correct
26 Correct 429 ms 33288 KB Output is correct
27 Correct 460 ms 33288 KB Output is correct
28 Correct 441 ms 33288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 31812 KB Output is correct
2 Correct 215 ms 31700 KB Output is correct
3 Correct 380 ms 31832 KB Output is correct
4 Correct 396 ms 31884 KB Output is correct
5 Correct 421 ms 31828 KB Output is correct
6 Correct 395 ms 31828 KB Output is correct
7 Correct 485 ms 31828 KB Output is correct
8 Correct 370 ms 31828 KB Output is correct
9 Correct 432 ms 31820 KB Output is correct
10 Correct 414 ms 31796 KB Output is correct
11 Correct 503 ms 31828 KB Output is correct
12 Correct 480 ms 31828 KB Output is correct
13 Correct 379 ms 31948 KB Output is correct
14 Correct 464 ms 31816 KB Output is correct
15 Correct 219 ms 31744 KB Output is correct
16 Correct 227 ms 32952 KB Output is correct
17 Correct 315 ms 33204 KB Output is correct
18 Correct 313 ms 33164 KB Output is correct
19 Correct 453 ms 33172 KB Output is correct
20 Correct 415 ms 33012 KB Output is correct
21 Correct 481 ms 33292 KB Output is correct
22 Correct 515 ms 33276 KB Output is correct
23 Correct 442 ms 33300 KB Output is correct
24 Correct 392 ms 33128 KB Output is correct
25 Correct 452 ms 33288 KB Output is correct
26 Correct 429 ms 33288 KB Output is correct
27 Correct 460 ms 33288 KB Output is correct
28 Correct 441 ms 33288 KB Output is correct
29 Correct 1656 ms 83732 KB Output is correct
30 Correct 1937 ms 84728 KB Output is correct
31 Correct 1837 ms 86348 KB Output is correct
32 Correct 1940 ms 86420 KB Output is correct
33 Correct 1418 ms 79416 KB Output is correct
34 Correct 2090 ms 86468 KB Output is correct
35 Correct 2264 ms 101868 KB Output is correct
36 Correct 2244 ms 96652 KB Output is correct
37 Correct 2865 ms 102140 KB Output is correct