#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define LL long long
#define st first
#define nd second
#define endl '\n'
using namespace std;
const int MAXN=2005;
int a[MAXN][MAXN],n,m,ans=1e9,mx,mn,idx,l,r,add,mnn,b[MAXN][MAXN];
multiset<int> mpp;
void rotate1() {
//cout<<"fuk"<<endl;
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
b[i][m-j-1]=a[i][j];
}
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
a[i][j]=b[i][j];
}
}
void rotate2() {
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
b[n-i-1][j]=a[i][j];
}
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
a[i][j]=b[i][j];
}
}
int solve() {
/*for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}*/
int ret=1e9;
int x,y;
mn=1e9+1;
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
mpp.insert(a[i][j]);
if(mn>a[i][j]) {
mn=a[i][j];
x=i;
y=j;
}
}
//cout<<"xd"<<endl;
mx=0;
//cout<<x<<" "<<y<<" "<<mpp.size()<<endl;
int up[MAXN];
int cnt=0;
memset(up,0,sizeof up);
for(int j=0;j<=x;++j) {
up[j]=y+1;
for(int i=0;i<=y;++i) {
mx=max(mx,a[j][i]);
mpp.erase(mpp.find(a[j][i]));
++cnt;
}
}
//cout<<mpp.size()<<endl;
if(mpp.size()) ret=min(ret,max(mx-mn,*(--mpp.end())-*mpp.begin()));
//cout<<"xd"<<" "<<cnt<<endl;
while(!mpp.empty()) {
add=-1,mnn=1e9+1;
for(int i=0;i<n;++i) {
if(up[i]!=m) {
if(i&&up[i]==up[i-1])
continue;
if(mnn>a[i][up[i]]) {
mnn=a[i][up[i]];
add=i;
}
}
}
mx=max(mx,a[add][up[add]]);
mpp.erase(mpp.find(a[add][up[add]]));
//cout<<mpp.size()<<endl;
++up[add];
if(!mpp.empty()) ret=min(ret,max(mx-mn,*(--mpp.end())-*mpp.begin()));
//cout<<mpp.size()<<endl;
}
//cout<<"rekt"<<endl;
return ret;
}
int main() {
scanf("%d %d",&n,&m);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
scanf("%d",&a[i][j]);
}
ans=min(ans,solve());
rotate1();
//cout<<"malahmet"<<endl;
ans=min(ans,solve());
rotate2();
ans=min(ans,solve());
rotate1();
ans=min(ans,solve());
printf("%d\n",ans);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:105:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
^
joioi.cpp:108:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j]);
^
joioi.cpp: In function 'int solve()':
joioi.cpp:70:16: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
for(int i=0;i<=y;++i) {
^
joioi.cpp:68:15: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
for(int j=0;j<=x;++j) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33428 KB |
Output is correct |
2 |
Correct |
0 ms |
33428 KB |
Output is correct |
3 |
Correct |
0 ms |
33428 KB |
Output is correct |
4 |
Correct |
0 ms |
33428 KB |
Output is correct |
5 |
Correct |
0 ms |
33428 KB |
Output is correct |
6 |
Correct |
0 ms |
33428 KB |
Output is correct |
7 |
Correct |
0 ms |
33428 KB |
Output is correct |
8 |
Correct |
0 ms |
33428 KB |
Output is correct |
9 |
Correct |
0 ms |
33428 KB |
Output is correct |
10 |
Correct |
0 ms |
33428 KB |
Output is correct |
11 |
Correct |
0 ms |
33428 KB |
Output is correct |
12 |
Correct |
0 ms |
33428 KB |
Output is correct |
13 |
Correct |
0 ms |
33428 KB |
Output is correct |
14 |
Correct |
0 ms |
33428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33428 KB |
Output is correct |
2 |
Correct |
0 ms |
33428 KB |
Output is correct |
3 |
Correct |
0 ms |
33428 KB |
Output is correct |
4 |
Correct |
0 ms |
33428 KB |
Output is correct |
5 |
Correct |
0 ms |
33428 KB |
Output is correct |
6 |
Correct |
0 ms |
33428 KB |
Output is correct |
7 |
Correct |
0 ms |
33428 KB |
Output is correct |
8 |
Correct |
0 ms |
33428 KB |
Output is correct |
9 |
Correct |
0 ms |
33428 KB |
Output is correct |
10 |
Correct |
0 ms |
33428 KB |
Output is correct |
11 |
Correct |
0 ms |
33428 KB |
Output is correct |
12 |
Correct |
0 ms |
33428 KB |
Output is correct |
13 |
Correct |
0 ms |
33428 KB |
Output is correct |
14 |
Correct |
0 ms |
33428 KB |
Output is correct |
15 |
Correct |
0 ms |
33560 KB |
Output is correct |
16 |
Correct |
113 ms |
35144 KB |
Output is correct |
17 |
Correct |
156 ms |
35276 KB |
Output is correct |
18 |
Correct |
133 ms |
35276 KB |
Output is correct |
19 |
Correct |
156 ms |
35276 KB |
Output is correct |
20 |
Correct |
133 ms |
35144 KB |
Output is correct |
21 |
Correct |
159 ms |
35276 KB |
Output is correct |
22 |
Correct |
156 ms |
35276 KB |
Output is correct |
23 |
Correct |
156 ms |
35276 KB |
Output is correct |
24 |
Correct |
129 ms |
35144 KB |
Output is correct |
25 |
Correct |
166 ms |
35276 KB |
Output is correct |
26 |
Correct |
153 ms |
35276 KB |
Output is correct |
27 |
Correct |
149 ms |
35276 KB |
Output is correct |
28 |
Correct |
169 ms |
35276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33428 KB |
Output is correct |
2 |
Correct |
0 ms |
33428 KB |
Output is correct |
3 |
Correct |
0 ms |
33428 KB |
Output is correct |
4 |
Correct |
0 ms |
33428 KB |
Output is correct |
5 |
Correct |
0 ms |
33428 KB |
Output is correct |
6 |
Correct |
0 ms |
33428 KB |
Output is correct |
7 |
Correct |
0 ms |
33428 KB |
Output is correct |
8 |
Correct |
0 ms |
33428 KB |
Output is correct |
9 |
Correct |
0 ms |
33428 KB |
Output is correct |
10 |
Correct |
0 ms |
33428 KB |
Output is correct |
11 |
Correct |
0 ms |
33428 KB |
Output is correct |
12 |
Correct |
0 ms |
33428 KB |
Output is correct |
13 |
Correct |
0 ms |
33428 KB |
Output is correct |
14 |
Correct |
0 ms |
33428 KB |
Output is correct |
15 |
Correct |
0 ms |
33560 KB |
Output is correct |
16 |
Correct |
113 ms |
35144 KB |
Output is correct |
17 |
Correct |
156 ms |
35276 KB |
Output is correct |
18 |
Correct |
133 ms |
35276 KB |
Output is correct |
19 |
Correct |
156 ms |
35276 KB |
Output is correct |
20 |
Correct |
133 ms |
35144 KB |
Output is correct |
21 |
Correct |
159 ms |
35276 KB |
Output is correct |
22 |
Correct |
156 ms |
35276 KB |
Output is correct |
23 |
Correct |
156 ms |
35276 KB |
Output is correct |
24 |
Correct |
129 ms |
35144 KB |
Output is correct |
25 |
Correct |
166 ms |
35276 KB |
Output is correct |
26 |
Correct |
153 ms |
35276 KB |
Output is correct |
27 |
Correct |
149 ms |
35276 KB |
Output is correct |
28 |
Correct |
169 ms |
35276 KB |
Output is correct |
29 |
Execution timed out |
4000 ms |
147608 KB |
Execution timed out |
30 |
Halted |
0 ms |
0 KB |
- |