#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int arr[2002][2002];
int totalMin, totalMax;
int zero[2002][2], one[2002][2];
void flipArr(){
for(int i=1; i<=n; i++) if(i<n+1-i){
for(int j=0; j<2; j++){
swap(zero[i][j], zero[n+1-i][j]);
swap(one[i][j], one[n+1-i][j]);
}
}
// for(int i=1; i<=n; i++) for(int j=0; j<2; j++){
// if(1<=zero[i][j] && zero[i][j]<=m) zero[i][j] = m+1-zero[i][j];
// if(1<=one[i][j] && one[i][j] <= m) one[i][j] = m+1-one[i][j];
// }
}
void swapArr(){
for(int i=1; i<=n; i++) for(int j=0; j<2; j++) swap(zero[i][j], one[i][j]);
}
bool solve(){
/// 왼쪽은 0, 오른쪽은 1
/// 경계는 i가 커질수록 단조증가
int zeroRight = 0;
for(int i=1; i<=n; i++){
if(one[i][0] < zero[i][1]) return false;
if(zeroRight >= one[i][0]) return false;
zeroRight = max(zeroRight, zero[i][1]);
}
return true;
}
bool able(int diff){
int limSmall = totalMin + diff;
int limBig = totalMax - diff;
if(diff==13){
// puts("");
}
for(int i=1; i<=n; i++){
zero[i][0] = one[i][0] = 1e9;
zero[i][1] = one[i][1] = 0;
for(int j=1; j<=m; j++){
if(limSmall < arr[i][j] && arr[i][j] < limBig) return false;
if(arr[i][j] <= limSmall && arr[i][j] < limBig) zero[i][0] = min(zero[i][0],j), zero[i][1] = max(zero[i][1],j);
if(arr[i][j] > limSmall && arr[i][j] >= limBig) one[i][0] = min(one[i][0],j), one[i][1] = max(one[i][1],j);
}
}
if(solve()) return true;
flipArr();
if(solve()) return true;
swapArr();
if(solve()) return true;
flipArr();
if(solve()) return true;
return false;
}
int main(){
totalMin = 1e9, totalMax = 0;
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
scanf("%d", &arr[i][j]);
totalMin = min(totalMin, arr[i][j]);
totalMax = max(totalMax, arr[i][j]);
}
}
int MIN = 0, MAX = 1e9, ANS = 1e9;
while(MIN <= MAX){
int MID = (MIN + MAX) / 2;
if(able(MID)) ANS = MID, MAX = MID-1;
else MIN = MID+1;
}
printf("%d", ANS);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joioi.cpp:72:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &arr[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
19 |
Correct |
8 ms |
1492 KB |
Output is correct |
20 |
Correct |
7 ms |
1320 KB |
Output is correct |
21 |
Correct |
8 ms |
1620 KB |
Output is correct |
22 |
Correct |
8 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1600 KB |
Output is correct |
24 |
Correct |
7 ms |
1476 KB |
Output is correct |
25 |
Correct |
11 ms |
1620 KB |
Output is correct |
26 |
Correct |
10 ms |
1544 KB |
Output is correct |
27 |
Correct |
10 ms |
1576 KB |
Output is correct |
28 |
Correct |
7 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
6 ms |
1492 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
19 |
Correct |
8 ms |
1492 KB |
Output is correct |
20 |
Correct |
7 ms |
1320 KB |
Output is correct |
21 |
Correct |
8 ms |
1620 KB |
Output is correct |
22 |
Correct |
8 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1600 KB |
Output is correct |
24 |
Correct |
7 ms |
1476 KB |
Output is correct |
25 |
Correct |
11 ms |
1620 KB |
Output is correct |
26 |
Correct |
10 ms |
1544 KB |
Output is correct |
27 |
Correct |
10 ms |
1576 KB |
Output is correct |
28 |
Correct |
7 ms |
1620 KB |
Output is correct |
29 |
Correct |
490 ms |
15232 KB |
Output is correct |
30 |
Correct |
538 ms |
15920 KB |
Output is correct |
31 |
Correct |
693 ms |
16128 KB |
Output is correct |
32 |
Correct |
453 ms |
16016 KB |
Output is correct |
33 |
Correct |
379 ms |
14232 KB |
Output is correct |
34 |
Correct |
592 ms |
16020 KB |
Output is correct |
35 |
Correct |
574 ms |
15924 KB |
Output is correct |
36 |
Correct |
551 ms |
15920 KB |
Output is correct |
37 |
Correct |
822 ms |
16104 KB |
Output is correct |