#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, a[2020][2020], tmp[2020][2020], d[2020];
bool solve(int val, int x, int y){
int mn = a[x][y];
for (int i=1;i<=n;i++){
for (d[i]=0;d[i]<d[i-1];d[i]++){
if (d[i]==m || a[i][d[i]+1] - mn > val) break;
}
}
if (d[x] < y) return 0;
//if (val==11) {for (int i=1;i<=n;i++) printf("%d ", d[i]); printf("\n");}
int mx = 0;
mn = 1e9;
for (int i=1;i<=n;i++){
assert(d[i] <= d[i-1]);
for (int j=d[i]+1;j<=m;j++){
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
return mx - mn <= val;
}
int getans(int x, int y){
int l = 0, r = 1e9, ret = 1e9;
while(l<=r){
int mid = (l+r)>>1;
if (solve(mid, x, y)) ret = mid, r = mid-1;
else l = mid+1;
}
//printf("YES: %d\n", ret);
return ret;
}
void flipx(int &x, int &y){
x = n+1-x;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
tmp[n+1-i][j] = a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
a[i][j] = tmp[i][j];
}
}
}
void flipy(int &x, int &y){
y = m+1-y;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
tmp[i][m+1-j] = a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
a[i][j] = tmp[i][j];
}
}
}
int main(){
scanf("%d %d", &n, &m);
d[0] = m;
int mn = 2e9, x, y;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
scanf("%d", a[i]+j);
if (mn > a[i][j]){
mn = a[i][j];
x = i, y = j;
}
}
}
int ans = getans(x, y); flipx(x, y);
ans = min(ans, getans(x, y)); flipy(x, y);
ans = min(ans, getans(x, y)); flipx(x, y);
ans = min(ans, getans(x, y));
printf("%d\n", ans);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joioi.cpp:78:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", a[i]+j);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
2308 KB |
Output is correct |
17 |
Correct |
9 ms |
2412 KB |
Output is correct |
18 |
Correct |
9 ms |
2444 KB |
Output is correct |
19 |
Correct |
10 ms |
2388 KB |
Output is correct |
20 |
Correct |
9 ms |
2132 KB |
Output is correct |
21 |
Correct |
9 ms |
2364 KB |
Output is correct |
22 |
Correct |
8 ms |
2380 KB |
Output is correct |
23 |
Correct |
10 ms |
2388 KB |
Output is correct |
24 |
Correct |
10 ms |
2128 KB |
Output is correct |
25 |
Correct |
14 ms |
2452 KB |
Output is correct |
26 |
Correct |
10 ms |
2456 KB |
Output is correct |
27 |
Correct |
15 ms |
2388 KB |
Output is correct |
28 |
Correct |
9 ms |
2452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
2308 KB |
Output is correct |
17 |
Correct |
9 ms |
2412 KB |
Output is correct |
18 |
Correct |
9 ms |
2444 KB |
Output is correct |
19 |
Correct |
10 ms |
2388 KB |
Output is correct |
20 |
Correct |
9 ms |
2132 KB |
Output is correct |
21 |
Correct |
9 ms |
2364 KB |
Output is correct |
22 |
Correct |
8 ms |
2380 KB |
Output is correct |
23 |
Correct |
10 ms |
2388 KB |
Output is correct |
24 |
Correct |
10 ms |
2128 KB |
Output is correct |
25 |
Correct |
14 ms |
2452 KB |
Output is correct |
26 |
Correct |
10 ms |
2456 KB |
Output is correct |
27 |
Correct |
15 ms |
2388 KB |
Output is correct |
28 |
Correct |
9 ms |
2452 KB |
Output is correct |
29 |
Correct |
642 ms |
30344 KB |
Output is correct |
30 |
Correct |
601 ms |
31868 KB |
Output is correct |
31 |
Correct |
653 ms |
32024 KB |
Output is correct |
32 |
Correct |
686 ms |
32056 KB |
Output is correct |
33 |
Correct |
578 ms |
28068 KB |
Output is correct |
34 |
Correct |
737 ms |
32004 KB |
Output is correct |
35 |
Correct |
694 ms |
31932 KB |
Output is correct |
36 |
Correct |
603 ms |
31772 KB |
Output is correct |
37 |
Correct |
712 ms |
31992 KB |
Output is correct |