#include <bits/stdc++.h>
using namespace std;
#define sh short
const int N = 2e3+110;
const int INF = 1e9;
int a[N][N], cnt[N], h, w;
vector<int> v;
void fs(int &number) {
register int c;
number = 0;
c = getchar();
for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48;
}
bool cmp(sh x, sh y) {
return a[cnt[x]+1][x] > a[cnt[y]+1][y];
}
int solve() {
memset(cnt, 0, sizeof(cnt));
int res=INF, lis=INF, uis=-1;
multiset<int> js;
for (int i=1; i<=h; ++i) {
for (int j=1; j<=w; ++j) js.insert(a[i][j]);
}
priority_queue<sh, vector<sh>, function<bool(sh, sh)>> pq(cmp);
pq.push(w);
for (int k=0; k<(int)v.size(); ++k) {
while (!pq.empty()) {
if (a[cnt[pq.top()]+1][pq.top()] <= v[k]) {
sh x=pq.top(); pq.pop();
int val=a[cnt[x]+1][x];
js.erase(js.find(val));
lis=min(lis, val), uis=max(uis, val);
++cnt[x];
if (cnt[x] == h) continue;
if (x == w || cnt[x+1] >= cnt[x]+1) pq.push(x);
if (x == 1) continue;
if (cnt[x] == cnt[x-1]+1) pq.push(x-1);
} else break;
}
int lf;
if (js.empty()) lf=INF;
else lf=(*js.rbegin())-(*js.begin());
res=min(res, max(lf, uis-lis));
}
return res;
}
void hor() {
for (int i=1; i<h/2+1; ++i) {
for (int j=1; j<=w; ++j) swap(a[i][j], a[h-i+1][j]);
}
}
void ver() {
for (int i=1; i<=h; ++i) {
for (int j=1; j<w/2+1; ++j) swap(a[i][j], a[i][w-j+1]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
fs(h), fs(w);
priority_queue<sh, vector<sh>, function<bool(sh, sh)>> pq(cmp);
multiset<int> js;
for (int i=1; i<=h; ++i) {
for (int j=1; j<=w; ++j) {
fs(a[i][j]);
v.push_back(a[i][j]);
js.insert(a[i][j]);
}
} sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
if (h >= 1e3) {
cout<<-1<<"\n";
return 0;
}
int ans=solve();
hor(); ans=min(ans, solve()); hor();
ver();
ans=min(ans, solve());
hor(); ans=min(ans, solve()); hor();
printf("%d\n", ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
59 ms |
4908 KB |
Output is correct |
17 |
Correct |
98 ms |
5200 KB |
Output is correct |
18 |
Correct |
81 ms |
5180 KB |
Output is correct |
19 |
Correct |
89 ms |
5216 KB |
Output is correct |
20 |
Correct |
87 ms |
4660 KB |
Output is correct |
21 |
Correct |
86 ms |
5200 KB |
Output is correct |
22 |
Correct |
80 ms |
5064 KB |
Output is correct |
23 |
Correct |
82 ms |
5196 KB |
Output is correct |
24 |
Correct |
72 ms |
4560 KB |
Output is correct |
25 |
Correct |
86 ms |
5184 KB |
Output is correct |
26 |
Correct |
84 ms |
5180 KB |
Output is correct |
27 |
Correct |
92 ms |
5200 KB |
Output is correct |
28 |
Correct |
85 ms |
5124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
59 ms |
4908 KB |
Output is correct |
17 |
Correct |
98 ms |
5200 KB |
Output is correct |
18 |
Correct |
81 ms |
5180 KB |
Output is correct |
19 |
Correct |
89 ms |
5216 KB |
Output is correct |
20 |
Correct |
87 ms |
4660 KB |
Output is correct |
21 |
Correct |
86 ms |
5200 KB |
Output is correct |
22 |
Correct |
80 ms |
5064 KB |
Output is correct |
23 |
Correct |
82 ms |
5196 KB |
Output is correct |
24 |
Correct |
72 ms |
4560 KB |
Output is correct |
25 |
Correct |
86 ms |
5184 KB |
Output is correct |
26 |
Correct |
84 ms |
5180 KB |
Output is correct |
27 |
Correct |
92 ms |
5200 KB |
Output is correct |
28 |
Correct |
85 ms |
5124 KB |
Output is correct |
29 |
Execution timed out |
4064 ms |
179520 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |