#include <bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>
using namespace std;
const int N = (int) 2e3 + 10;
const int M = (int) 2018 * 2018;
const int K = (int) 15;
const int INF = (int) 1e9 + 7;
const int mod = (int) 998244353;
const ld EPS = (ld) 1e-9;
const ll LINF = (ll) 1e18;
int n, m;
int a[N][N], ptr[N], b[N][N];
bool boo[N][N];
bool solve(int diff) {
int mx_i = 1, mx_j = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[mx_i][mx_j] < a[i][j]) {
mx_i = i;
mx_j = j;
}
}
}
// a[mx_i][mx_j] <= a[i][j] + diff
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[mx_i][mx_j] <= diff + a[i][j]) {
boo[i][j] = 1;
} else {
boo[i][j] = 0;
}
}
}
ptr[0] = m;
for (int i = 1; i <= n; i++) {
ptr[i] = ptr[i - 1];
for (int j = 1; j <= ptr[i - 1]; j++) {
if (!boo[i][j]) {
ptr[i] = j - 1;
break;
}
}
}
int mx = 0, mn = INF;
for (int i = 1; i <= n; i++) {
for (int j = ptr[i] + 1; j <= m; j++) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
if (mx_j > ptr[mx_i])
return false;
return mx - mn <= diff;
}
void mirror() {
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 rotate() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
b[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = b[i][j];
}
}
}
bool ok(int diff) {
bool ok = false;
for (int i = 0; i < 4; i++) {
ok |= solve(diff);
mirror();
ok |= solve(diff);
mirror();
rotate();
}
return ok;
}
int main() {
#ifdef sony
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
srand(time(0));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
int l = 1, r = INF;
int ans;
while (l <= r) {
int mid = (l + r) / 2;
if (ok(mid)) {
r = mid - 1;
ans = mid;
} else {
l = mid + 1;
}
}
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:113:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:127:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << ans;
~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
740 KB |
Output is correct |
6 |
Correct |
2 ms |
740 KB |
Output is correct |
7 |
Correct |
4 ms |
740 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
3 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
740 KB |
Output is correct |
12 |
Correct |
3 ms |
740 KB |
Output is correct |
13 |
Correct |
3 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
740 KB |
Output is correct |
6 |
Correct |
2 ms |
740 KB |
Output is correct |
7 |
Correct |
4 ms |
740 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
3 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
740 KB |
Output is correct |
12 |
Correct |
3 ms |
740 KB |
Output is correct |
13 |
Correct |
3 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
15 |
Correct |
9 ms |
2716 KB |
Output is correct |
16 |
Correct |
96 ms |
3108 KB |
Output is correct |
17 |
Correct |
116 ms |
3400 KB |
Output is correct |
18 |
Correct |
108 ms |
3660 KB |
Output is correct |
19 |
Correct |
133 ms |
3900 KB |
Output is correct |
20 |
Correct |
91 ms |
4300 KB |
Output is correct |
21 |
Correct |
105 ms |
4580 KB |
Output is correct |
22 |
Correct |
148 ms |
4968 KB |
Output is correct |
23 |
Correct |
103 ms |
5352 KB |
Output is correct |
24 |
Correct |
97 ms |
5796 KB |
Output is correct |
25 |
Correct |
116 ms |
6156 KB |
Output is correct |
26 |
Correct |
105 ms |
6572 KB |
Output is correct |
27 |
Correct |
110 ms |
6932 KB |
Output is correct |
28 |
Correct |
117 ms |
7440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
4 ms |
576 KB |
Output is correct |
5 |
Correct |
2 ms |
740 KB |
Output is correct |
6 |
Correct |
2 ms |
740 KB |
Output is correct |
7 |
Correct |
4 ms |
740 KB |
Output is correct |
8 |
Correct |
2 ms |
740 KB |
Output is correct |
9 |
Correct |
3 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
740 KB |
Output is correct |
12 |
Correct |
3 ms |
740 KB |
Output is correct |
13 |
Correct |
3 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
15 |
Correct |
9 ms |
2716 KB |
Output is correct |
16 |
Correct |
96 ms |
3108 KB |
Output is correct |
17 |
Correct |
116 ms |
3400 KB |
Output is correct |
18 |
Correct |
108 ms |
3660 KB |
Output is correct |
19 |
Correct |
133 ms |
3900 KB |
Output is correct |
20 |
Correct |
91 ms |
4300 KB |
Output is correct |
21 |
Correct |
105 ms |
4580 KB |
Output is correct |
22 |
Correct |
148 ms |
4968 KB |
Output is correct |
23 |
Correct |
103 ms |
5352 KB |
Output is correct |
24 |
Correct |
97 ms |
5796 KB |
Output is correct |
25 |
Correct |
116 ms |
6156 KB |
Output is correct |
26 |
Correct |
105 ms |
6572 KB |
Output is correct |
27 |
Correct |
110 ms |
6932 KB |
Output is correct |
28 |
Correct |
117 ms |
7440 KB |
Output is correct |
29 |
Execution timed out |
4011 ms |
62792 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |