#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define len(a) (int)a.size()
#define fi first
#define sc second
#define d1(w) cerr<<#w<<":"<<w<<endl;
#define d2(w,c) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<endl;
#define d3(w,c,z) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<" "<<#z<<":"<<z<<endl;
#define left isc+isc
#define right isc+isc+1
#define mid (l+r)/2
#define FAfi_IO ios_base::sync_with_fidio(false);
#define escl '\n'
typedef long long int ll;
const int maxn = 620;
const long long LINF = 1e18;
const int LOG = 31;
const int INF = 1e9;
const int N = 2e3 + 5;
const int M = 1e4 + 5;
const int SQ = 350;
const int MOD = 998244353;
typedef long long int lli;
typedef pair<int,int> pii;
int n,m,a[N][N],mn = INF,mx = -INF,ans;
void flip_row() {
for (int i = 1 ; i <= n / 2 ; i++)
for (int j = 1 ; j <= m ; j++)
swap(a[i][j],a[n - i + 1][j]);
}
void flip_col() {
for (int i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= m / 2 ; j++)
swap(a[i][j],a[i][m - j + 1]);
}
bool check (int val) {
int mxj = 1;
for (int i = 1 ; i <= n; i++) {
for (int j = 1 ; j <= m ; j++) {
if (a[i][j] < mx - val)
mxj = max(mxj,j + 1);
}
for (int j = 1 ; j <= m ; j++)
if (a[i][j] > mn + val && mxj > j)
return false;
}
return true;
}
void print() {
printf("\n\n\n");
for (int i = 1 ; i <= n ; i++,puts(""))
for (int j = 1 ; j <= m ; j++)
printf("%d ",a[i][j]);
printf("\n\n\n");
}
int solve() {
int l = 0 , r = mx - mn;
while (l < r) {
if (check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int main() {
scanf("%d %d",&n,&m);
for (int i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= m ; j++) {
scanf("%d",&a[i][j]);
mn = min(a[i][j],mn);
mx = max(a[i][j],mx);
}
ans = solve();
flip_row();
// print();
ans = min(ans,solve());
flip_col();
// print();
ans = min(ans,solve());
flip_row();
// print();
ans = min(ans,solve());
printf("%d\n",ans);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
joioi.cpp:83:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
684 KB |
Output is correct |
6 |
Correct |
3 ms |
684 KB |
Output is correct |
7 |
Correct |
3 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
9 |
Correct |
3 ms |
728 KB |
Output is correct |
10 |
Correct |
3 ms |
732 KB |
Output is correct |
11 |
Correct |
2 ms |
732 KB |
Output is correct |
12 |
Correct |
2 ms |
732 KB |
Output is correct |
13 |
Correct |
2 ms |
732 KB |
Output is correct |
14 |
Correct |
3 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
684 KB |
Output is correct |
6 |
Correct |
3 ms |
684 KB |
Output is correct |
7 |
Correct |
3 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
9 |
Correct |
3 ms |
728 KB |
Output is correct |
10 |
Correct |
3 ms |
732 KB |
Output is correct |
11 |
Correct |
2 ms |
732 KB |
Output is correct |
12 |
Correct |
2 ms |
732 KB |
Output is correct |
13 |
Correct |
2 ms |
732 KB |
Output is correct |
14 |
Correct |
3 ms |
732 KB |
Output is correct |
15 |
Correct |
3 ms |
732 KB |
Output is correct |
16 |
Correct |
7 ms |
1708 KB |
Output is correct |
17 |
Correct |
13 ms |
1908 KB |
Output is correct |
18 |
Correct |
14 ms |
2180 KB |
Output is correct |
19 |
Correct |
15 ms |
2448 KB |
Output is correct |
20 |
Correct |
13 ms |
2592 KB |
Output is correct |
21 |
Correct |
26 ms |
3088 KB |
Output is correct |
22 |
Correct |
24 ms |
3476 KB |
Output is correct |
23 |
Correct |
18 ms |
3476 KB |
Output is correct |
24 |
Correct |
15 ms |
3476 KB |
Output is correct |
25 |
Correct |
22 ms |
3496 KB |
Output is correct |
26 |
Correct |
19 ms |
3496 KB |
Output is correct |
27 |
Correct |
22 ms |
3496 KB |
Output is correct |
28 |
Correct |
15 ms |
3496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
684 KB |
Output is correct |
6 |
Correct |
3 ms |
684 KB |
Output is correct |
7 |
Correct |
3 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
9 |
Correct |
3 ms |
728 KB |
Output is correct |
10 |
Correct |
3 ms |
732 KB |
Output is correct |
11 |
Correct |
2 ms |
732 KB |
Output is correct |
12 |
Correct |
2 ms |
732 KB |
Output is correct |
13 |
Correct |
2 ms |
732 KB |
Output is correct |
14 |
Correct |
3 ms |
732 KB |
Output is correct |
15 |
Correct |
3 ms |
732 KB |
Output is correct |
16 |
Correct |
7 ms |
1708 KB |
Output is correct |
17 |
Correct |
13 ms |
1908 KB |
Output is correct |
18 |
Correct |
14 ms |
2180 KB |
Output is correct |
19 |
Correct |
15 ms |
2448 KB |
Output is correct |
20 |
Correct |
13 ms |
2592 KB |
Output is correct |
21 |
Correct |
26 ms |
3088 KB |
Output is correct |
22 |
Correct |
24 ms |
3476 KB |
Output is correct |
23 |
Correct |
18 ms |
3476 KB |
Output is correct |
24 |
Correct |
15 ms |
3476 KB |
Output is correct |
25 |
Correct |
22 ms |
3496 KB |
Output is correct |
26 |
Correct |
19 ms |
3496 KB |
Output is correct |
27 |
Correct |
22 ms |
3496 KB |
Output is correct |
28 |
Correct |
15 ms |
3496 KB |
Output is correct |
29 |
Correct |
811 ms |
17492 KB |
Output is correct |
30 |
Correct |
774 ms |
18364 KB |
Output is correct |
31 |
Correct |
884 ms |
18572 KB |
Output is correct |
32 |
Correct |
698 ms |
18572 KB |
Output is correct |
33 |
Correct |
559 ms |
18572 KB |
Output is correct |
34 |
Correct |
909 ms |
18572 KB |
Output is correct |
35 |
Correct |
1206 ms |
18572 KB |
Output is correct |
36 |
Correct |
1216 ms |
18572 KB |
Output is correct |
37 |
Correct |
1591 ms |
18572 KB |
Output is correct |