#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class DisjointSetUnion { int n; vector<int> root; vector<int> size; int find(int v) { if(root[v] != v) { root[v] = find(root[v]); } return root[v]; } public: int sets; explicit DisjointSetUnion(int n) : n(n), root(vector<int>(n)), size(vector<int>(n)), sets(0) { for (int i = 0; i < n; ++i) { root[i] = i; size[i] = 1; } } void create() { ++sets; } void join(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (size[a] < size[b]) swap(a, b); size[a] += size[b]; root[b] = a; --sets; } void clear() { sets = 0; for (int i = 0; i < n; ++i) { root[i] = i; size[i] = 1; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<vector<int>> board(2, vector<int>(n)); vector<vector<int>> adj(2 * n + 1); DisjointSetUnion U(2 * n + 1); vector<long long> res(k + 1); for (int i = 0; i < n; ++i) cin >> board[0][i]; for (int i = 0; i < n; ++i) cin >> board[1][i]; for (int i = 0; i < n; ++i) { if (board[0][(n + i - 1) % n] < board[0][i]) adj[board[0][i]].push_back(board[0][(n + i - 1) % n]); if (board[0][(i + 1) % n] < board[0][i]) adj[board[0][i]].push_back(board[0][(i + 1) % n]); if (board[1][i] < board[0][i]) adj[board[0][i]].push_back(board[1][i]); if (board[1][(n + i - 1) % n] < board[1][i]) adj[board[1][i]].push_back(board[1][(n + i - 1) % n]); if (board[1][(i + 1) % n] < board[1][i]) adj[board[1][i]].push_back(board[1][(i + 1) % n]); if (board[0][i] < board[1][i]) adj[board[1][i]].push_back(board[0][i]); } for (int i = 1; i <= 2 * n; ++i) { for (int j = i; j <= 2 * n; ++j) { U.create(); for (int v : adj[j]) { if (v >= i) U.join(j, v); } if (U.sets <= k) ++res[U.sets]; } U.clear(); } for (int i = 1; i <= k; ++i) { cout << res[i] << " "; } }
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 | #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class DisjointSetUnion { int n; vector<int> root; vector<int> size; int find(int v) { if(root[v] != v) { root[v] = find(root[v]); } return root[v]; } public: int sets; explicit DisjointSetUnion(int n) : n(n), root(vector<int>(n)), size(vector<int>(n)), sets(0) { for (int i = 0; i < n; ++i) { root[i] = i; size[i] = 1; } } void create() { ++sets; } void join(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (size[a] < size[b]) swap(a, b); size[a] += size[b]; root[b] = a; --sets; } void clear() { sets = 0; for (int i = 0; i < n; ++i) { root[i] = i; size[i] = 1; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<vector<int>> board(2, vector<int>(n)); vector<vector<int>> adj(2 * n + 1); DisjointSetUnion U(2 * n + 1); vector<long long> res(k + 1); for (int i = 0; i < n; ++i) cin >> board[0][i]; for (int i = 0; i < n; ++i) cin >> board[1][i]; for (int i = 0; i < n; ++i) { if (board[0][(n + i - 1) % n] < board[0][i]) adj[board[0][i]].push_back(board[0][(n + i - 1) % n]); if (board[0][(i + 1) % n] < board[0][i]) adj[board[0][i]].push_back(board[0][(i + 1) % n]); if (board[1][i] < board[0][i]) adj[board[0][i]].push_back(board[1][i]); if (board[1][(n + i - 1) % n] < board[1][i]) adj[board[1][i]].push_back(board[1][(n + i - 1) % n]); if (board[1][(i + 1) % n] < board[1][i]) adj[board[1][i]].push_back(board[1][(i + 1) % n]); if (board[0][i] < board[1][i]) adj[board[1][i]].push_back(board[0][i]); } for (int i = 1; i <= 2 * n; ++i) { for (int j = i; j <= 2 * n; ++j) { U.create(); for (int v : adj[j]) { if (v >= i) U.join(j, v); } if (U.sets <= k) ++res[U.sets]; } U.clear(); } for (int i = 1; i <= k; ++i) { cout << res[i] << " "; } } |