#include<bits/stdc++.h>
using namespace std;
vector<pair<int,pair<int,int>>> haha(0);
int bruh[2001][2001];
int n,m;
int calc() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
bruh[i][j] = 1;
}
}
vector<int> sm(m);
vector<int> big(m,-1);
int ans = haha[haha.size()-1].first-haha[0].first;
int br = 0,p = 0;
for(int i = 0; i < n*m; i++) {
int x = haha[i].second.first,y = haha[i].second.second;
for(int j = y; j < m; j++) {
if(big[j] >= sm[j]) {
br--;
}
if(big[j] < x) {
big[j] = x;
if(big[j] >= sm[j]) {
br++;
}
}
else {
break;
}
}
while(br > 0) {
int a = haha[p].second.first,b = haha[p].second.second;
bruh[a][b] = 0;
if(big[b] >= sm[b]) {
br--;
}
while(sm[b] < n && bruh[sm[b]][b] == 0) {
sm[b]++;
}
if(big[b] >= sm[b]) {
br++;
}
p++;
}
int c = haha[p-1].first-haha[0].first;
if(i < n*m) {
c = max(c,haha[n*m-1].first-haha[i+1].first);
}
ans = min(ans,c);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
int a,ans = INT_MAX;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> a;
haha.push_back({a,{i,j}});
}
}
sort(haha.begin(),haha.end());
ans = min(ans,calc());
for(int i = 0; i < n*m; i++) {
haha[i] = {haha[i].first,{n-1-haha[i].second.first,m-1-haha[i].second.second}};
}
ans = min(ans,calc());
for(int i = 0; i < n*m; i++) {
haha[i] = {haha[i].first,{n-1-haha[i].second.first,haha[i].second.second}};
}
ans = min(ans,calc());
for(int i = 0; i < n*m; i++) {
haha[i] = {haha[i].first,{n-1-haha[i].second.first,m-1-haha[i].second.second}};
}
ans = min(ans,calc());
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
464 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
464 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
2024 KB |
Output is correct |
17 |
Correct |
7 ms |
2220 KB |
Output is correct |
18 |
Correct |
7 ms |
2140 KB |
Output is correct |
19 |
Correct |
7 ms |
2260 KB |
Output is correct |
20 |
Correct |
8 ms |
2032 KB |
Output is correct |
21 |
Correct |
7 ms |
2256 KB |
Output is correct |
22 |
Correct |
7 ms |
2260 KB |
Output is correct |
23 |
Correct |
8 ms |
2260 KB |
Output is correct |
24 |
Correct |
6 ms |
1960 KB |
Output is correct |
25 |
Correct |
8 ms |
2316 KB |
Output is correct |
26 |
Correct |
8 ms |
2260 KB |
Output is correct |
27 |
Correct |
8 ms |
2356 KB |
Output is correct |
28 |
Correct |
7 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
464 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
2024 KB |
Output is correct |
17 |
Correct |
7 ms |
2220 KB |
Output is correct |
18 |
Correct |
7 ms |
2140 KB |
Output is correct |
19 |
Correct |
7 ms |
2260 KB |
Output is correct |
20 |
Correct |
8 ms |
2032 KB |
Output is correct |
21 |
Correct |
7 ms |
2256 KB |
Output is correct |
22 |
Correct |
7 ms |
2260 KB |
Output is correct |
23 |
Correct |
8 ms |
2260 KB |
Output is correct |
24 |
Correct |
6 ms |
1960 KB |
Output is correct |
25 |
Correct |
8 ms |
2316 KB |
Output is correct |
26 |
Correct |
8 ms |
2260 KB |
Output is correct |
27 |
Correct |
8 ms |
2356 KB |
Output is correct |
28 |
Correct |
7 ms |
2260 KB |
Output is correct |
29 |
Correct |
808 ms |
82032 KB |
Output is correct |
30 |
Correct |
750 ms |
81212 KB |
Output is correct |
31 |
Correct |
831 ms |
86200 KB |
Output is correct |
32 |
Correct |
889 ms |
86220 KB |
Output is correct |
33 |
Correct |
732 ms |
75124 KB |
Output is correct |
34 |
Correct |
839 ms |
86284 KB |
Output is correct |
35 |
Correct |
884 ms |
101636 KB |
Output is correct |
36 |
Correct |
709 ms |
90512 KB |
Output is correct |
37 |
Correct |
922 ms |
101984 KB |
Output is correct |