답안 #387264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387264 2021-04-08T07:50:42 Z Trunkty The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
545 ms 37484 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int h,w,maxi;
int grid[2005][2005];
int hasmax[2005][3];

bool check(int a){
	bool yes=true;
	int minl=w,maxo=0,mino=1e9;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=minl;j++){
			if(maxi-grid[i][j]>a){
				minl = j-1;
				break;
			}
		}
		for(int j=minl+1;j<=w;j++){
			mino = min(mino,grid[i][j]);
			maxo = max(maxo,grid[i][j]);
		}
		if(minl<hasmax[i][2] or maxo-mino>a){
			yes = false;
			break;
		}
	}
	if(yes){
		return true;
	}
	yes=true;
	minl=w,maxo=0,mino=1e9;
	for(int i=h;i>=1;i--){
		for(int j=1;j<=minl;j++){
			if(maxi-grid[i][j]>a){
				minl = j-1;
				break;
			}
		}
		for(int j=minl+1;j<=w;j++){
			mino = min(mino,grid[i][j]);
			maxo = max(maxo,grid[i][j]);
		}
		if(minl<hasmax[i][2] or maxo-mino>a){
			yes = false;
			break;
		}
	}
	if(yes){
		return true;
	}
	yes=true;
	minl=1,maxo=0,mino=1e9;
	for(int i=1;i<=h;i++){
		for(int j=w;j>=minl;j--){
			if(maxi-grid[i][j]>a){
				minl = j+1;
				break;
			}
		}
		for(int j=1;j<minl;j++){
			mino = min(mino,grid[i][j]);
			maxo = max(maxo,grid[i][j]);
		}
		if(minl>hasmax[i][1] or maxo-mino>a){
			yes = false;
			break;
		}
	}
	if(yes){
		return true;
	}
	yes=true;
	minl=1,maxo=0,mino=1e9;
	for(int i=h;i>=1;i--){
		for(int j=w;j>=minl;j--){
			if(maxi-grid[i][j]>a){
				minl = j+1;
				break;
			}
		}
		for(int j=1;j<minl;j++){
			mino = min(mino,grid[i][j]);
			maxo = max(maxo,grid[i][j]);
		}
		if(minl>hasmax[i][1] or maxo-mino>a){
			yes = false;
			break;
		}
	}
	if(yes){
		return true;
	}
	return false;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> h >> w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin >> grid[i][j];
			maxi = max(maxi,grid[i][j]);
		}
	}
	for(int i=1;i<=h;i++){
		hasmax[i][1] = 1e9;
		for(int j=1;j<=w;j++){
			if(grid[i][j]==maxi){
				hasmax[i][1] = min(hasmax[i][1],j);
				hasmax[i][2] = j;
			}
		}
	}
	int low=0,high=maxi-1,mid;
	while(low!=high){
		mid = (low+high-(low+high)%2)/2;
		if(check(mid)){
			high = mid;
		}
		else{
			low = mid+1;
		}
	}
	
	cout << low;
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 4 ms 1388 KB Output is correct
17 Correct 6 ms 1516 KB Output is correct
18 Correct 6 ms 1516 KB Output is correct
19 Correct 6 ms 1516 KB Output is correct
20 Correct 5 ms 1388 KB Output is correct
21 Correct 8 ms 1644 KB Output is correct
22 Correct 7 ms 1772 KB Output is correct
23 Correct 7 ms 1644 KB Output is correct
24 Correct 6 ms 1536 KB Output is correct
25 Correct 7 ms 1644 KB Output is correct
26 Correct 7 ms 1644 KB Output is correct
27 Correct 8 ms 1644 KB Output is correct
28 Correct 8 ms 1644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 4 ms 1388 KB Output is correct
17 Correct 6 ms 1516 KB Output is correct
18 Correct 6 ms 1516 KB Output is correct
19 Correct 6 ms 1516 KB Output is correct
20 Correct 5 ms 1388 KB Output is correct
21 Correct 8 ms 1644 KB Output is correct
22 Correct 7 ms 1772 KB Output is correct
23 Correct 7 ms 1644 KB Output is correct
24 Correct 6 ms 1536 KB Output is correct
25 Correct 7 ms 1644 KB Output is correct
26 Correct 7 ms 1644 KB Output is correct
27 Correct 8 ms 1644 KB Output is correct
28 Correct 8 ms 1644 KB Output is correct
29 Correct 378 ms 37356 KB Output is correct
30 Correct 359 ms 37484 KB Output is correct
31 Correct 403 ms 24172 KB Output is correct
32 Correct 373 ms 24728 KB Output is correct
33 Correct 339 ms 34092 KB Output is correct
34 Correct 382 ms 24456 KB Output is correct
35 Correct 545 ms 26732 KB Output is correct
36 Correct 473 ms 24096 KB Output is correct
37 Correct 532 ms 26988 KB Output is correct