이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define N 2005
#define F first
#define S second
int n,m,a[N][N],b[N][N],ans=2e9;
typedef pair<int,int> pa;
pa cal(int n,int m,int v){
bitset<N> dp[N];
for(int i=0;i<=n;i++) dp[i] = 1;
for(int i=0;i<=m;i++) dp[n+1][i] = 1;
for(int i=n;i>0;i--) for(int j=1;a[i][j]<=v&&dp[i+1][j]&&dp[i][j-1];j++) dp[i][j]=1;
int mn[2], mx[2];
mn[0] = mn[1] = 1e9;
mx[0] = mx[1] = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
mn[dp[i][j]]=min(mn[dp[i][j]],a[i][j]);
mx[dp[i][j]]=max(mx[dp[i][j]],a[i][j]);
}
}
if(!dp[n][1]) return {0,1e9};
if(dp[1][m]) return {1e9,0};
// printf(">> %d : %d %d %d %d\n",v,mx1,mn1,mx0,mn0);
if(mn[1] >= mn[0]) return {0,1e9};
return {mx[1]-mn[1],mx[0]-mn[0]};
}
int sol(int n,int m){
int be = 0,ed = 1e9,ans=1e9;
while(be <= ed){
int mid = (be+ed)/2;
pa V = cal(n,m,mid);
ans = min(ans,max(V.F,V.S));
// printf("%d : %d %d\n",mid,V.F,V.S);
if(V.F > V.S) ed = mid-1;
else be = mid+1;
}
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
ans = min(ans,sol(n,m));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j] = a[i][j];
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) a[i][j] = b[n-j+1][i];
ans = min(ans,sol(m,n));
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) b[i][j] = a[i][j];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j] = b[m-j+1][i];
ans = min(ans,sol(n,m));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j] = a[i][j];
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) a[i][j] = b[n-j+1][i];
ans = min(ans,sol(m,n));
printf("%d",ans);
}
/*
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10
3 4
1 1 1 1
1 1 9 2
2 2 2 2
3 4
2 2 2 2
9 2 1 1
1 1 1 1
*/
컴파일 시 표준 에러 (stderr) 메시지
joioi.cpp: In function 'int main()':
joioi.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joioi.cpp:48:51: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |