#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <algorithm>
#include <utility>
#include <time.h>
#include <vector>
#include <bitset>
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
#define pii pair<int,int>
#define F first
#define S second
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
const int maxN = 2e3 + 326, INF = 2e9 + 326;
int N, M, arr[maxN][maxN], sz, ii, jj;
vector<int> lisan;
vector<short> cnt;
vector<vector<int>> pos;
bitset<maxN> vis[maxN];
inline char readchar() {
static const size_t bufsize = 65536;
static char buf[bufsize];
static char *p = buf, *end = buf;
if (p == end) end = buf + fread_unlocked(buf, 1, bufsize, stdin), p = buf;
return *p++;
}
inline void const Read(int & p) {
p = 0;
int tmp = 0;
char c = readchar();
tmp = !(c^'-');
while (c < '0' || c > '9') {
c = readchar();
}
while (c >= '0' && c <= '9')
p = (p<<3)+(p<<1)+(c^48), c = readchar();
p = tmp?-p:p;
}
inline void take(const int y, const int x, int &mx, int &mn, int &d){
if(d == 0){
for(ii = y; ii >= 0 && !vis[ii][x]; ii--){
for(jj= x; jj >= 0 && !vis[ii][jj]; jj--){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 1){
for(ii = y; ii < N && !vis[ii][x]; ii++){
for(jj= x; jj >= 0 && !vis[ii][jj]; jj--){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 2){
for(ii = y; ii < N && !vis[ii][x]; ii++){
for(jj= x; jj < M && !vis[ii][jj]; jj++){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
} else if(d == 3){
for(ii = y; ii >= 0 && !vis[ii][x]; ii--){
for(jj= x; jj < M && !vis[ii][jj]; jj++){
vis[ii][jj] = true;
cnt[arr[ii][jj]]--;
mx = max(mx, arr[ii][jj]);
mn = min(mn, arr[ii][jj]);
}
}
}
}
inline void rot(){
for(ii = 0; ii < sz; ii++){
cnt[ii] = 0;
}
for(ii = 0; ii < N; ii++) for(jj = 0; jj < M; jj++) {
vis[ii].reset();
cnt[arr[ii][jj]]++;
}
}
int main(){
Read(N);
Read(M);
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
Read(arr[i][j]);
lisan.push_back(arr[i][j]);
}
}
sort(lisan.begin(), lisan.end());
sz = unique(lisan.begin(), lisan.end()) - lisan.begin();
pos.resize(sz + 5);
cnt.resize(sz + 5);
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++){
arr[i][j] = lower_bound(lisan.begin(), lisan.begin() + sz, arr[i][j]) - lisan.begin();
cnt[arr[i][j]]++;
pos[arr[i][j]].emplace_back(i * M + j);
}
int ans = lisan[sz - 1] - lisan[0];
for(int jizz = 0; jizz < 4; jizz++){
clock_t t = clock();
//cout << "in jizz = " << jizz << endl;
int mx = -INF, mn = INF, f = 0, e = sz - 1;
for(int i = 0; i < sz; i++){
//take all of them
//
//cout << "i = " << i << ", = " << lisan[i] << endl;
for(int id : pos[i]){
take(id / M, id - id / M * M, mx, mn, jizz);
}
if(lisan[mx] - lisan[mn] > ans) break;
/*
for(int y = 0; y < N; y++) for(int x = 0; x < M; x++){
cout << vis[y][x] << " \n"[x == M - 1];
}
*/
while(f < sz - 1 && !cnt[f]) f++;
while(e && !cnt[e]) e--;
if(f == sz || !e) break;
ans = min(ans, max(lisan[e] - lisan[f], lisan[mx] - lisan[mn]));
if(lisan[e] - lisan[f] < lisan[mx] - lisan[mn]) break;
//cout << "ans = " << ans << endl;
if((clock() - t) > 0.70 * CLOCKS_PER_SEC) break;
}
rot();
}
cout << ans << '\n';
}
Compilation message
joioi.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
joioi.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
12 ms |
1768 KB |
Output is correct |
17 |
Correct |
25 ms |
3816 KB |
Output is correct |
18 |
Correct |
29 ms |
3688 KB |
Output is correct |
19 |
Correct |
28 ms |
3816 KB |
Output is correct |
20 |
Correct |
25 ms |
3432 KB |
Output is correct |
21 |
Correct |
25 ms |
3816 KB |
Output is correct |
22 |
Correct |
32 ms |
3816 KB |
Output is correct |
23 |
Correct |
37 ms |
3816 KB |
Output is correct |
24 |
Correct |
36 ms |
3572 KB |
Output is correct |
25 |
Correct |
25 ms |
3816 KB |
Output is correct |
26 |
Correct |
25 ms |
3816 KB |
Output is correct |
27 |
Correct |
21 ms |
3816 KB |
Output is correct |
28 |
Correct |
23 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
12 ms |
1768 KB |
Output is correct |
17 |
Correct |
25 ms |
3816 KB |
Output is correct |
18 |
Correct |
29 ms |
3688 KB |
Output is correct |
19 |
Correct |
28 ms |
3816 KB |
Output is correct |
20 |
Correct |
25 ms |
3432 KB |
Output is correct |
21 |
Correct |
25 ms |
3816 KB |
Output is correct |
22 |
Correct |
32 ms |
3816 KB |
Output is correct |
23 |
Correct |
37 ms |
3816 KB |
Output is correct |
24 |
Correct |
36 ms |
3572 KB |
Output is correct |
25 |
Correct |
25 ms |
3816 KB |
Output is correct |
26 |
Correct |
25 ms |
3816 KB |
Output is correct |
27 |
Correct |
21 ms |
3816 KB |
Output is correct |
28 |
Correct |
23 ms |
3816 KB |
Output is correct |
29 |
Correct |
2097 ms |
62368 KB |
Output is correct |
30 |
Correct |
1847 ms |
60448 KB |
Output is correct |
31 |
Correct |
1969 ms |
61576 KB |
Output is correct |
32 |
Correct |
2019 ms |
65616 KB |
Output is correct |
33 |
Correct |
1726 ms |
56740 KB |
Output is correct |
34 |
Correct |
1968 ms |
61552 KB |
Output is correct |
35 |
Execution timed out |
4070 ms |
261640 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |