이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int MOD = 998244353;
int grid[2005][2005];
int vis[2005][2005];
int proces[2005][2005];
int dfs(int i, int j) {
vis[i][j] = 1;
int x = 1;
if(grid[i+1][j ] != -1 && vis[i+1][j ]==0) x += dfs(i+1, j );
if(grid[i-1][j ] != -1 && vis[i-1][j ]==0) x += dfs(i-1, j );
if(grid[i ][j+1] != -1 && vis[i ][j+1]==0) x += dfs(i , j+1);
if(grid[i ][j-1] != -1 && vis[i ][j-1]==0) x += dfs(i , j-1);
return x;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long seed;
asm("rdtsc" : "=A"(seed));
srand(seed);
int n, m;
cin >> n >> m;
multiset<int> rem;
memset(grid, -1, sizeof(grid));
int mi=1,mj=1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> grid[i][j];
if(grid[i][j] > grid[mi][mj])
mi=i,mj=j;
rem.insert(grid[i][j]);
}
}
int aMax = grid[mi][mj];
int aMin = grid[mi][mj];
int ans = 1ll<<40;
priority_queue<pair<int, pair<int, int>>> pq;
pq.push({grid[mi][mj], {mi, mj}});
proces[mi][mj] = 1;
while(!pq.empty()) {
int i = pq.top().second.first;
int j = pq.top().second.second;
grid[i][j] = pq.top().first;
int storeGrid = grid[i][j];
pq.pop();
if((int)(grid[i+1][j ]==-1)+
(int)(grid[i-1][j ]==-1)+
(int)(grid[i ][j+1]==-1)+
(int)(grid[i ][j-1]==-1) >= 2) {
grid[i][j] = -1;
int x = 0;
memset(vis, 0, sizeof(vis));
if(grid[i+1][j ]!=-1) x=dfs(i+1, j );
else if(grid[i-1][j ]!=-1) x=dfs(i-1, j );
else if(grid[i ][j+1]!=-1) x=dfs(i , j+1);
else if(grid[i ][j-1]!=-1) x=dfs(i , j-1);
grid[i][j] = storeGrid;
if(x+1 != rem.size()) {
proces[i][j]=0;
continue;
}
}
aMin = min(aMin, storeGrid);
rem.erase(rem.find(storeGrid));
if(rem.empty()) break;
ans = min(ans, max(aMax-aMin, *rem.rbegin()-*rem.begin()));
grid[i][j] = -1;
if(grid[i-1][j ]!=-1 && proces[i-1][j ]==0) {pq.push({grid[i-1][j ], {i-1, j }}); proces[i-1][j ]=1; };
if(grid[i+1][j ]!=-1 && proces[i+1][j ]==0) {pq.push({grid[i+1][j ], {i+1, j }}); proces[i+1][j ]=1; };
if(grid[i ][j-1]!=-1 && proces[i ][j-1]==0) {pq.push({grid[i ][j-1], {i , j-1}}); proces[i ][j-1]=1; };
if(grid[i ][j+1]!=-1 && proces[i ][j+1]==0) {pq.push({grid[i ][j+1], {i , j+1}}); proces[i ][j+1]=1; };
}
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
joioi.cpp: In function 'int main()':
joioi.cpp:76:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(x+1 != rem.size()) {
~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |