#include<bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream &out, string str) {
for(char c : str) out << c;
return out;
}
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for(auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#ifdef DEBUG
# define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
# define debug(...) false
#endif
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
template<class T> int size(T && a) { return a.size(); }
using LL = long long;
using PII = pair<int, int>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
int mx = 0, mn = 1e9;
REP(i, n) REP(j, m) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
debug(mn, mx);
auto check = [&](int h) {
debug(h);
REP(t, 4) {
int l = 0, r = m;
bool ok = true;
REP(i, n) debug(i, a[i]);
REP(i, n) {
l = 0;
REP(j, m) {
if(a[i][j] - mn > h && mx - a[i][j] > h) ok = false;
if(a[i][j] - mn <= h && mx - a[i][j] > h) l = max(l, j), debug(i, j, l);
if(a[i][j] - mn > h && mx - a[i][j] <= h) r = min(r, j - 1), debug(i, j, r);
}
if(l > r) ok = false;
}
if(ok) return true;
reverse(a.begin(), a.end());
if(t % 2 == 0) REP(i, n) reverse(a[i].begin(), a[i].end());
}
return false;
};
int l = 0, r = mx - mn;
while(l < r) {
int m = (l + r) / 2;
if(check(m))
r = m;
else
l = m + 1;
}
cout << l << "\n";
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:57:15: warning: statement has no effect [-Wunused-value]
debug(mn, mx);
^
joioi.cpp: In lambda function:
joioi.cpp:59:11: warning: statement has no effect [-Wunused-value]
debug(h);
^
joioi.cpp:63:28: warning: statement has no effect [-Wunused-value]
REP(i, n) debug(i, a[i]);
^
joioi.cpp:68:77: warning: right operand of comma operator has no effect [-Wunused-value]
if(a[i][j] - mn <= h && mx - a[i][j] > h) l = max(l, j), debug(i, j, l);
^
joioi.cpp:69:81: warning: right operand of comma operator has no effect [-Wunused-value]
if(a[i][j] - mn > h && mx - a[i][j] <= h) r = min(r, j - 1), debug(i, j, r);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |