#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, nn, mm;
int a[N][N], b[N][N], c[N][N], ptr[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) {
n = nn;
m = mm;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = c[i][j];
}
}
for (int i = 0; i < 4; i++) {
if (solve(diff)) return true;
mirror();
if (solve(diff)) return true;
mirror();
rotate();
}
return false;
}
int main() {
#ifdef sony
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
srand(time(0));
cin >> nn >> mm;
for (int i = 1; i <= nn; i++) {
for (int j = 1; j <= mm; j++) {
scanf("%d", &c[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:119:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i][j]);
~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:133:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << ans;
~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
3 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
732 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
732 KB |
Output is correct |
9 |
Correct |
2 ms |
732 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 |
828 KB |
Output is correct |
14 |
Correct |
3 ms |
828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
3 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
732 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
732 KB |
Output is correct |
9 |
Correct |
2 ms |
732 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 |
828 KB |
Output is correct |
14 |
Correct |
3 ms |
828 KB |
Output is correct |
15 |
Correct |
5 ms |
2748 KB |
Output is correct |
16 |
Correct |
23 ms |
3796 KB |
Output is correct |
17 |
Correct |
48 ms |
3820 KB |
Output is correct |
18 |
Correct |
67 ms |
3896 KB |
Output is correct |
19 |
Correct |
47 ms |
3896 KB |
Output is correct |
20 |
Correct |
62 ms |
3896 KB |
Output is correct |
21 |
Correct |
86 ms |
3948 KB |
Output is correct |
22 |
Correct |
100 ms |
3948 KB |
Output is correct |
23 |
Correct |
65 ms |
3948 KB |
Output is correct |
24 |
Correct |
77 ms |
3948 KB |
Output is correct |
25 |
Correct |
69 ms |
3948 KB |
Output is correct |
26 |
Correct |
94 ms |
3948 KB |
Output is correct |
27 |
Correct |
94 ms |
3948 KB |
Output is correct |
28 |
Correct |
104 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
612 KB |
Output is correct |
3 |
Correct |
2 ms |
612 KB |
Output is correct |
4 |
Correct |
2 ms |
732 KB |
Output is correct |
5 |
Correct |
3 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
732 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
732 KB |
Output is correct |
9 |
Correct |
2 ms |
732 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 |
828 KB |
Output is correct |
14 |
Correct |
3 ms |
828 KB |
Output is correct |
15 |
Correct |
5 ms |
2748 KB |
Output is correct |
16 |
Correct |
23 ms |
3796 KB |
Output is correct |
17 |
Correct |
48 ms |
3820 KB |
Output is correct |
18 |
Correct |
67 ms |
3896 KB |
Output is correct |
19 |
Correct |
47 ms |
3896 KB |
Output is correct |
20 |
Correct |
62 ms |
3896 KB |
Output is correct |
21 |
Correct |
86 ms |
3948 KB |
Output is correct |
22 |
Correct |
100 ms |
3948 KB |
Output is correct |
23 |
Correct |
65 ms |
3948 KB |
Output is correct |
24 |
Correct |
77 ms |
3948 KB |
Output is correct |
25 |
Correct |
69 ms |
3948 KB |
Output is correct |
26 |
Correct |
94 ms |
3948 KB |
Output is correct |
27 |
Correct |
94 ms |
3948 KB |
Output is correct |
28 |
Correct |
104 ms |
3948 KB |
Output is correct |
29 |
Execution timed out |
4049 ms |
50880 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |