#include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- int n; vector<vector<int>> a; bool nei(int n, int i, int j, int x, int y) { return (j == y or (i == x and ((n + j - 1) % n == y or (j + 1) % n == y))); } void subtask1() { vector<pair<int, int>> pos(2 * n); REP(i, 2) REP(j, n) pos[a[i][j]] = pair(i, j); long long answer = 0; REP(k, 2 * n) { pair<int, int> cur = pos[k]; int len = 1; while(a[cur.first][cur.second] < 2 * n - 1) { int i = cur.first, j = cur.second; auto [x, y] = pos[a[i][j] + 1]; if(nei(n, i, j, x, y)) { ++len; cur = pair(x, y); } else break; cur = pair(x, y); } debug(len, k, a[cur.first][cur.second]); answer += 1LL * len * (len + 1) / 2; k = a[cur.first][cur.second]; } cout << answer << '\n'; } vector<vector<bool>> visited; void dfs(int i, int j, int l, int r) { visited[i][j] = true; vector<pair<int, int>> neis = {{i, (j + 1) % n}, {i, (n + j - 1) % n}, {i ^ 1, j}}; for(auto [x, y] : neis) { if(!visited[x][y] and l <= a[x][y] and a[x][y] <= r) dfs(x, y, l, r); } } void brucik(int k) { int n = size(a[0]); vector<pair<int, int>> pos(2 * n); REP(i, 2) REP(j, n) pos[a[i][j]] = pair(i, j); vector<int> answer(k); REP(l, 2 * n) FOR(r, l, 2 * n - 1) { int components = 0; visited = vector<vector<bool>>(2, vector<bool>(n, false)); FOR(x, l, r) { int i = pos[x].first, j = pos[x].second; if(!visited[i][j]) { dfs(i, j, l, r); ++components; } } if(components <= k) ++answer[components - 1]; } REP(i, k) cout << answer[i] << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> k; a = vector<vector<int>>(2, vector<int>(n)); REP(i, 2) REP(j, n) { cin >> a[i][j]; --a[i][j]; } brucik(k); }
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 | #include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- int n; vector<vector<int>> a; bool nei(int n, int i, int j, int x, int y) { return (j == y or (i == x and ((n + j - 1) % n == y or (j + 1) % n == y))); } void subtask1() { vector<pair<int, int>> pos(2 * n); REP(i, 2) REP(j, n) pos[a[i][j]] = pair(i, j); long long answer = 0; REP(k, 2 * n) { pair<int, int> cur = pos[k]; int len = 1; while(a[cur.first][cur.second] < 2 * n - 1) { int i = cur.first, j = cur.second; auto [x, y] = pos[a[i][j] + 1]; if(nei(n, i, j, x, y)) { ++len; cur = pair(x, y); } else break; cur = pair(x, y); } debug(len, k, a[cur.first][cur.second]); answer += 1LL * len * (len + 1) / 2; k = a[cur.first][cur.second]; } cout << answer << '\n'; } vector<vector<bool>> visited; void dfs(int i, int j, int l, int r) { visited[i][j] = true; vector<pair<int, int>> neis = {{i, (j + 1) % n}, {i, (n + j - 1) % n}, {i ^ 1, j}}; for(auto [x, y] : neis) { if(!visited[x][y] and l <= a[x][y] and a[x][y] <= r) dfs(x, y, l, r); } } void brucik(int k) { int n = size(a[0]); vector<pair<int, int>> pos(2 * n); REP(i, 2) REP(j, n) pos[a[i][j]] = pair(i, j); vector<int> answer(k); REP(l, 2 * n) FOR(r, l, 2 * n - 1) { int components = 0; visited = vector<vector<bool>>(2, vector<bool>(n, false)); FOR(x, l, r) { int i = pos[x].first, j = pos[x].second; if(!visited[i][j]) { dfs(i, j, l, r); ++components; } } if(components <= k) ++answer[components - 1]; } REP(i, k) cout << answer[i] << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> k; a = vector<vector<int>>(2, vector<int>(n)); REP(i, 2) REP(j, n) { cin >> a[i][j]; --a[i][j]; } brucik(k); } |