#include <algorithm> #include <cstdio> #include <cstring> #include <vector> int countTime(std::vector<unsigned int> data) { int time = 0; while (data.size() > 1) { std::vector<unsigned int> data2; if (data[0] > data[1]) { data2.push_back(data[0]); } for (unsigned int i = 1; i < data.size() - 1; ++i) { if (data[i - 1] < data[i] && data[i + 1] < data[i]) { data2.push_back(data[i]); } } if (data[data.size() - 1] > data[data.size() - 2]) { data2.push_back(data[data.size() - 1]); } time++; data = data2; } return time; } void generate(int* permutationsTime, const unsigned int n, unsigned int* data) { std::vector<unsigned int> _data; do { for (unsigned int i = 0; i < n; ++i) _data.push_back(data[i]); const auto pt = countTime(_data); permutationsTime[pt]++; _data.clear(); } while (std::next_permutation(data, data + n)); } int main() { unsigned int n, k, p; scanf("%u %u %u\n", &n, &k, &p); int* permutationsTime = new int[k + 1]; for (unsigned int i = 0; i <= k; ++i) { permutationsTime[i] = 0; } unsigned int* data = new unsigned int[n]; for (unsigned int i = 0; i < n; ++i) data[i] = i + 1; generate(permutationsTime, n, data); printf("%d\n", permutationsTime[k]); delete[] permutationsTime; }
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 | #include <algorithm> #include <cstdio> #include <cstring> #include <vector> int countTime(std::vector<unsigned int> data) { int time = 0; while (data.size() > 1) { std::vector<unsigned int> data2; if (data[0] > data[1]) { data2.push_back(data[0]); } for (unsigned int i = 1; i < data.size() - 1; ++i) { if (data[i - 1] < data[i] && data[i + 1] < data[i]) { data2.push_back(data[i]); } } if (data[data.size() - 1] > data[data.size() - 2]) { data2.push_back(data[data.size() - 1]); } time++; data = data2; } return time; } void generate(int* permutationsTime, const unsigned int n, unsigned int* data) { std::vector<unsigned int> _data; do { for (unsigned int i = 0; i < n; ++i) _data.push_back(data[i]); const auto pt = countTime(_data); permutationsTime[pt]++; _data.clear(); } while (std::next_permutation(data, data + n)); } int main() { unsigned int n, k, p; scanf("%u %u %u\n", &n, &k, &p); int* permutationsTime = new int[k + 1]; for (unsigned int i = 0; i <= k; ++i) { permutationsTime[i] = 0; } unsigned int* data = new unsigned int[n]; for (unsigned int i = 0; i < n; ++i) data[i] = i + 1; generate(permutationsTime, n, data); printf("%d\n", permutationsTime[k]); delete[] permutationsTime; } |