#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 2048;
int a[N][N];
int n, m;
int len[N];
bool can_extend(int i, int mx)
{
if (i > 0 && len[i-1] <= len[i])
return 0;
if (len[i] == m || a[i][len[i]] > mx)
return 0;
return 1;
}
int solve()
{
multiset<int> s1, s2;
vector<pair<int,int>> alts;
memset(len, 0, sizeof(len));
Loop (i,0,n) Loop (j,0,m) {
alts.push_back({a[i][j], i});
s2.insert(a[i][j]);
}
sort(alts.begin(), alts.end());
int ans = 2e9;
for (auto [mx, row] : alts) {
Loop (r,row,n) {
if (!can_extend(r, mx))
break;
while (can_extend(r, mx)) {
s1.insert(a[r][len[r]]);
s2.erase(s2.find(a[r][len[r]]));
len[r]++;
}
}
if (s1.empty() || s2.empty())
continue;
ans = min(ans, max(*s1.rbegin() - *s1.begin(),
*s2.rbegin() - *s2.begin()));
}
return ans;
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> m;
Loop (i,0,n) Loop (j,0,m)
cin >> a[i][j];
int ans = 2e9;
ans = min(ans, solve());
Loop (i,0,n/2) Loop (j,0,m)
swap(a[i][j], a[n-1-i][j]);
ans = min(ans, solve());
Loop (i,0,n) Loop (j,0,m/2)
swap(a[i][j], a[i][m-1-j]);
ans = min(ans, solve());
Loop (i,0,n/2) Loop (j,0,m)
swap(a[i][j], a[n-1-i][j]);
ans = min(ans, solve());
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
332 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
460 KB |
Output is correct |
16 |
Correct |
95 ms |
3284 KB |
Output is correct |
17 |
Correct |
118 ms |
3920 KB |
Output is correct |
18 |
Correct |
126 ms |
3944 KB |
Output is correct |
19 |
Correct |
115 ms |
4096 KB |
Output is correct |
20 |
Correct |
107 ms |
3580 KB |
Output is correct |
21 |
Correct |
127 ms |
4036 KB |
Output is correct |
22 |
Correct |
111 ms |
3940 KB |
Output is correct |
23 |
Correct |
125 ms |
4088 KB |
Output is correct |
24 |
Correct |
95 ms |
3648 KB |
Output is correct |
25 |
Correct |
123 ms |
4208 KB |
Output is correct |
26 |
Correct |
144 ms |
3952 KB |
Output is correct |
27 |
Correct |
121 ms |
4184 KB |
Output is correct |
28 |
Correct |
121 ms |
4152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 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 |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
460 KB |
Output is correct |
16 |
Correct |
95 ms |
3284 KB |
Output is correct |
17 |
Correct |
118 ms |
3920 KB |
Output is correct |
18 |
Correct |
126 ms |
3944 KB |
Output is correct |
19 |
Correct |
115 ms |
4096 KB |
Output is correct |
20 |
Correct |
107 ms |
3580 KB |
Output is correct |
21 |
Correct |
127 ms |
4036 KB |
Output is correct |
22 |
Correct |
111 ms |
3940 KB |
Output is correct |
23 |
Correct |
125 ms |
4088 KB |
Output is correct |
24 |
Correct |
95 ms |
3648 KB |
Output is correct |
25 |
Correct |
123 ms |
4208 KB |
Output is correct |
26 |
Correct |
144 ms |
3952 KB |
Output is correct |
27 |
Correct |
121 ms |
4184 KB |
Output is correct |
28 |
Correct |
121 ms |
4152 KB |
Output is correct |
29 |
Execution timed out |
4078 ms |
189692 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |