" Solution" "Two-point answer "
"See the maximum value of the smallest, that must be two points, ah, the exam did not know what the brain did go ..." "
"First find the largest value in the entire matrix, as the right boundary of the two points, and then, the two-point answer, the matching conditions of the adjacent lattice and check set and to a set, when using less than equal to the current value of the lattice can be 1 and n*m Unicom, the current value is valid"
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
int N,M,A[1010][1010],ANS,MAXN;
int f[1000010];
int find (int x) {if (f[x]==x) return f[x];
F[x]=find (F[x]);
return f[x];
} inline void link (int x,int y) {int f1=find (x), F2=find (y);
F[F1]=F2;
} inline bool Check (int t) {for (int i=1;i<=n*m;++i) f[i]=i;
for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) if (a[i][j]<=t) {int now= (i-1) *m+j;
if (i+1<=n&&a[i+1][j]<=t) link (now,i*m+j);
if (j+1<=m&&a[i][j+1]<=t) link (now,now+1);
} if (find (1) ==find (n*m)) return 1;
return 0;
} inline void qsort (int l,int r) {int mid;
while (l<=r) {mid= (l+r) >>1;
if (check (mid)) R=mid-1,ans=mid;
else l=mid+1;
}} int main () {freopen ("murder.in", "R", stdin);
Freopen ("Murder.out", "w", stdout);
int i,j;
scanf ("%d%d", &n,&m); for (I=1;i<=n;++i) for (j=1;j<=m;++j) scanf ("%d", &a[i][j]), Maxn=max (A[I][J],MAXN);
Qsort (0,MAXN);
printf ("%d\n", ans);
return 0; }