#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct node{
int x;
int pos;
};
bool cmp(node a,node b){
return a.x < b.x;
}
node a[200005];
int maxx[200005];
bool visited[200005];
int main()
{
ios::sync_with_stdio(0);
int cnt = 0;
int n,m;
cin >> n >> m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
int temp;
cin >> temp;
a[cnt].pos = i;
a[cnt++].x = temp;
}
}
sort(a,a+cnt,cmp);
int xx = 0;
int ans = 99999999;
for(int i = 0;i < cnt;i++){
if(!visited[a[i].pos]){
visited[a[i].pos] = 1;
xx++;
}
maxx[a[i].pos] = max(maxx[a[i].pos],a[i].x);
if(xx == n){
int nowmax = -1;
int nowmaxi;
int nowmin = 999999999;
int nowmini;
for(int j = 0;j < n;j++){
if(maxx[j] > nowmax){
nowmax = maxx[j];
nowmaxi = j;
}
if(maxx[j] < nowmin){
nowmin = maxx[j];
nowmini = j;
}
}
ans = min(ans,nowmax - nowmin);
visited[nowmini] = 0;
xx--;
}
}
cout << ans << endl;
return 0;
}
hihocoder 1722
最新推荐文章于 2020-06-17 21:05:27 发布