#include <bits/stdc++.h> using namespace std; vector<bool> vis; int l, r; int n, k; int res[10]; vector<int> a[2]; vector<pair<int,int>> d = {{1, 0}, {0, -1}, {0, 1}}; void dfs(int x, int y) { vis[x*n+y] = 1; for(auto&& [dx, dy] : d) { int x1 = (x + dx + 2) % 2; int y1 = (y + dy + n) % n; if(l <= a[x1][y1] && a[x1][y1] <= r && !vis[x1*n+y1]) dfs(x1, y1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; a[0].resize(n); a[1].resize(n); for(auto &&e : a[0]) cin >> e; for(auto &&e : a[1]) cin >> e; for(l = 1; l <= 2*n; ++l) { for(r = l; r <= 2*n; ++r) { vis.clear(); vis.resize(2*n); int s = 0; for(int x = 0; x < 2; ++x) for(int y = 0; y < n; ++y) { if(l <= a[x][y] && a[x][y] <= r && !vis[x*n+y]) { dfs(x, y); if(++s > k) break; } } if(s <= k) ++res[s-1]; } } for(int i = 0; i < k; ++i) cout << res[i] << ' '; cout << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; vector<bool> vis; int l, r; int n, k; int res[10]; vector<int> a[2]; vector<pair<int,int>> d = {{1, 0}, {0, -1}, {0, 1}}; void dfs(int x, int y) { vis[x*n+y] = 1; for(auto&& [dx, dy] : d) { int x1 = (x + dx + 2) % 2; int y1 = (y + dy + n) % n; if(l <= a[x1][y1] && a[x1][y1] <= r && !vis[x1*n+y1]) dfs(x1, y1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; a[0].resize(n); a[1].resize(n); for(auto &&e : a[0]) cin >> e; for(auto &&e : a[1]) cin >> e; for(l = 1; l <= 2*n; ++l) { for(r = l; r <= 2*n; ++r) { vis.clear(); vis.resize(2*n); int s = 0; for(int x = 0; x < 2; ++x) for(int y = 0; y < n; ++y) { if(l <= a[x][y] && a[x][y] <= r && !vis[x*n+y]) { dfs(x, y); if(++s > k) break; } } if(s <= k) ++res[s-1]; } } for(int i = 0; i < k; ++i) cout << res[i] << ' '; cout << '\n'; } |