1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| # include <algorithm> # include <iostream> # include <cstring> # include <cstdio> # include <queue> # define R register # define LL long long
using namespace std;
int n,m,h[510][510],tot,ans,q[250100][2];
int v[510][510]; struct pp{ int l,r; pp(){l = 2147483647,r = 0;} }tt[510];
inline void in(R int &a){ R char c = getchar();R int x = 0,f = 1; while(!isdigit(c)) {if(c == '-') f = -1; c = getchar();} while(isdigit(c)) x = (x<<1)+(x<<3)+c-'0',c = getchar(); a = x*f; }
inline bool cmp(pp a,pp b){return a.l < b.l;}
inline int bfs(R int t){ R int head = 0,tail = 0; q[++tail][0] = 1,q[tail][1] = t; v[1][t] = t; while(head < tail){ R int x = q[++head][0],y = q[head][1]; if(x==n) tt[t].l = min(tt[t].l,y),tt[t].r = max(tt[t].r,y); if(x+1<=n&&v[x+1][y]!=t&&h[x+1][y]<h[x][y]) v[x+1][y] = t,q[++tail][0] = x+1,q[tail][1] = y; if(x-1>=1&&v[x-1][y]!=t&&h[x-1][y]<h[x][y]) v[x-1][y] = t,q[++tail][0] = x-1,q[tail][1] = y; if(y+1<=m&&v[x][y+1]!=t&&h[x][y+1]<h[x][y]) v[x][y+1] = t,q[++tail][0] = x,q[tail][1] = y+1; if(y-1>=1&&v[x][y-1]!=t&&h[x][y-1]<h[x][y]) v[x][y-1] = t,q[++tail][0] = x,q[tail][1] = y-1; } }
int yg(){ in(n),in(m); for(R int i=1; i<=n; ++i) for(R int j=1; j<=m; ++j) in(h[i][j]); for(R int i=1; i<=m; ++i) if(h[1][i-1] <= h[1][i]&&h[1][i+1] <= h[1][i]) bfs(i); for(R int i=1; i<=m; ++i) if(!v[n][i]) tot++; if(tot) {printf("0\n%d",tot); exit(0);} sort(tt+1,tt+m+1,cmp); R int s = 0; for(R int i=1; i<=m; ++i){ if(tt[i].r<=s) continue; R int st = i,to = 0; while(tt[st].l <= s+1) to = max(to,tt[st++].r); s = to,ans++; if(s==m) break; i = st-1; } printf("1\n%d",ans); }
int youngsc = yg(); int main(){;}
|