#include <cstdint> #include <ios> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> int read_int() { int x; std::cin >> x; return x; } std::vector<int> read_vec(int n) { std::vector<int> v(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; } return v; } void print_vec(const std::vector<int> &v) { for (size_t i = 0; i < v.size(); ++i) { std::cout << v[i] << " "; } std::cout << std::endl; } std::map<int, int> build_positions(const std::vector<int> &v) { std::map<int, int> result; for (size_t i = 0; i < v.size(); ++i) { result.insert(std::make_pair(v[i], i)); } return result; } std::set<int> build_set(int min, int max) { std::set<int> s; for (int i = min; i <= max; ++i) { s.insert(i); } return s; } int first(const std::set<int> &s) { return *(s.begin()); } bool not_contains(const std::set<int> &s, int val) { return s.find(val) == s.cend(); } int left(const int N, int index) { if (index == 0) { return N - 1; } if (index == N) { return (N * 2) - 1; } return index - 1; } int right(const int N, int index) { if (index == N - 1) { return 0; } if (index == (2 * N) - 1) { return N; } return index + 1; } void search(int index, int min, int max, std::set<int> ¬_processed, const std::vector<int> &V, const int N) { std::queue<int> q; q.push(index); while (!q.empty()) { int current = q.front(); q.pop(); if (V[current] < min || V[current] > max || not_contains(not_processed, V[current])) { continue; } not_processed.erase(V[current]); // y q.push((current + N) % (N * 2)); // left q.push(left(N, current)); // right q.push(right(N, current)); } } void process(std::vector<int64_t> &results, int min, int max, const int N, const std::vector<int> &V, const std::map<int, int> &positions) { std::set<int> not_processed = build_set(min, max); unsigned patience = 0; while (!not_processed.empty()) { if (patience == results.size()) { return; } search(positions.at(first(not_processed)), min, max, not_processed, V, N); patience++; } results[patience - 1]++; } int main() { std::ios::sync_with_stdio(false); const int N = read_int(); const int K = read_int(); const std::vector<int> V = read_vec(N * 2); const std::map<int, int> positions = build_positions(V); ////////////////////////////// // std::cout << N << std::endl; // std::cout << K << std::endl; // print_vec(V); ////////////////////////////// std::vector<int64_t> results(K, 0); for (int i = 1; i <= 2 * N; ++i) { for (int j = i; j <= 2 * N; ++j) { if (i == j) { results[0]++; continue; } process(results, i, j, N, V, positions); } } for (size_t i = 0; i < results.size(); ++i) { std::cout << results[i] << " "; } std::cout<< std::endl; return 0; }
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 | #include <cstdint> #include <ios> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> int read_int() { int x; std::cin >> x; return x; } std::vector<int> read_vec(int n) { std::vector<int> v(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; } return v; } void print_vec(const std::vector<int> &v) { for (size_t i = 0; i < v.size(); ++i) { std::cout << v[i] << " "; } std::cout << std::endl; } std::map<int, int> build_positions(const std::vector<int> &v) { std::map<int, int> result; for (size_t i = 0; i < v.size(); ++i) { result.insert(std::make_pair(v[i], i)); } return result; } std::set<int> build_set(int min, int max) { std::set<int> s; for (int i = min; i <= max; ++i) { s.insert(i); } return s; } int first(const std::set<int> &s) { return *(s.begin()); } bool not_contains(const std::set<int> &s, int val) { return s.find(val) == s.cend(); } int left(const int N, int index) { if (index == 0) { return N - 1; } if (index == N) { return (N * 2) - 1; } return index - 1; } int right(const int N, int index) { if (index == N - 1) { return 0; } if (index == (2 * N) - 1) { return N; } return index + 1; } void search(int index, int min, int max, std::set<int> ¬_processed, const std::vector<int> &V, const int N) { std::queue<int> q; q.push(index); while (!q.empty()) { int current = q.front(); q.pop(); if (V[current] < min || V[current] > max || not_contains(not_processed, V[current])) { continue; } not_processed.erase(V[current]); // y q.push((current + N) % (N * 2)); // left q.push(left(N, current)); // right q.push(right(N, current)); } } void process(std::vector<int64_t> &results, int min, int max, const int N, const std::vector<int> &V, const std::map<int, int> &positions) { std::set<int> not_processed = build_set(min, max); unsigned patience = 0; while (!not_processed.empty()) { if (patience == results.size()) { return; } search(positions.at(first(not_processed)), min, max, not_processed, V, N); patience++; } results[patience - 1]++; } int main() { std::ios::sync_with_stdio(false); const int N = read_int(); const int K = read_int(); const std::vector<int> V = read_vec(N * 2); const std::map<int, int> positions = build_positions(V); ////////////////////////////// // std::cout << N << std::endl; // std::cout << K << std::endl; // print_vec(V); ////////////////////////////// std::vector<int64_t> results(K, 0); for (int i = 1; i <= 2 * N; ++i) { for (int j = i; j <= 2 * N; ++j) { if (i == j) { results[0]++; continue; } process(results, i, j, N, V, positions); } } for (size_t i = 0; i < results.size(); ++i) { std::cout << results[i] << " "; } std::cout<< std::endl; return 0; } |