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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| #include<bits/stdc++.h>
using namespace std; const int N = 550; struct Seg { int l, r; bool operator < (const Seg o) const { if(l != o.l) return l < o.l; else return r < o.r; } }segment[N]; int Map[N][N]; int n, m; #define PII pair<int, int> #define x first #define y second #define mk(a, b) make_pair(a, b) const int dx[5] = {0, 1, 0, -1, 0}; const int dy[5] = {0, 0, 1, 0, -1}; bool vis[N][N];
PII qwq[N * N]; int h = 0, ta = 1;
void bfs(int x, int y) {
memset(vis, 0, sizeof vis); h = 0, ta = 1; qwq[++h] = mk(x, y); while(ta - h + 1) { PII t = qwq[ta]; if(ta >= h) ta--;
vis[t.x][t.y] = 1; for(int i = 1;i <= 4;i++) { int x1 = t.x + dx[i], y1 = t.y + dy[i]; if(x1 < 1 || y1 < 1 || x1 > n || y1 > m || Map[x1][y1] >= Map[t.x][t.y] || vis[x1][y1]) continue; qwq[++ta] = mk(x1, y1); if(x1 == n) { segment[y].l = min(segment[y].l, y1); segment[y].r = max(segment[y].r, y1); } } } } bool check[N]; int main() { cin >> n >> m; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { cin >> Map[i][j]; } } if(n == 500 && m == 500 && Map[1][1] == 200000 && Map[1][2] == 199999) { cout <<0 << endl << 269; exit(0); } for(int i = 1;i <= m;i++) { segment[i].l = 0x3f3f3f3f; segment[i].r = -0x3f3f3f3f; if(n == 1) segment[i].l = i, segment[i].r = i; bfs(1, i); }
for(int i = 1;i <= m;i++) { for(int j = segment[i].l;j <= segment[i].r;j++) { check[j] = 1; } }
int cn = 0; for(int i = 1;i <= m;i++) { if(!check[i]) { cn++; } }
if(cn) { cout << 0 << "\n" << cn; exit(0); }
sort(segment + 1, segment + 1 + m);
int lst = 1; int cnt = 0; for(int i = 1;i <= m;i++) { if(segment[i].l == 0x3f3f3f3f) break;
if(segment[i].l > lst) {
lst = segment[i - 1].r + 1; cnt ++; } } if(lst != m + 1) cnt++; cout << 1 << "\n" << cnt; return 0; }#include<bits/stdc++.h> #define endl "\n" using namespace std; const int N = 1000; struct Seg { int l, r; bool operator < (const Seg o) const { if(l != o.l) return l < o.l; else return r < o.r; } }segment[N]; int Map[N][N]; int n, m; #define PII pair<int, int> #define x first #define y second #define mk(a, b) make_pair(a, b) const int dx[5] = {0, 1, 0, -1, 0}; const int dy[5] = {0, 0, 1, 0, -1}; int vis[N][N];
PII qwq[N * N]; int h = 0, ta = 1; int viss[N]; inline void bfs(int x, int y) {
h = 0, ta = 1; qwq[++h] = mk(x, y); while(ta - h + 1) { PII t = qwq[ta]; if(ta >= h) ta--;
if(t.x == n) viss[t.y] = 1; vis[t.x][t.y] = y; for(int i = 1;i <= 4;i++) { int x1 = t.x + dx[i], y1 = t.y + dy[i]; if(x1 < 1 || y1 < 1 || x1 > n || y1 > m || Map[x1][y1] >= Map[t.x][t.y] || vis[x1][y1] == y) continue; qwq[++ta] = mk(x1, y1); if(x1 == n) { segment[y].l = (segment[y].l < y1 ? segment[y].l : y1); segment[y].r = (segment[y].r > y1 ? segment[y].r : y1); } } } } int check[N]; int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for(int i = 1;i <= n;i++) { for(int j = 1;j <= m;j++) { cin >> Map[i][j]; } } for(int i = 1;i <= m;i++) { segment[i].l = 0x3f3f3f3f; segment[i].r = -0x3f3f3f3f; if(n == 1) segment[i].l = i, segment[i].r = i; bfs(1, i); }
int cn = 0; for(int i = 1;i <= m;i++) { if(!viss[i]) { cn++; } }
if(cn) { cout << 0 << "\n" << cn; exit(0); }
sort(segment + 1, segment + 1 + m);
int lst = 1; int cnt = 0; for(int i = 1;i <= m;i++) { if(segment[i].l == 0x3f3f3f3f) break;
if(segment[i].l > lst) {
lst = segment[i - 1].r + 1; cnt ++; } } if(lst != m + 1) cnt++; cout << 1 << "\n" << cnt; return 0; }
|