#include <iostream> #include <array> #include <vector> #include <algorithm> using ull = unsigned long long; const int MAX_N = static_cast<int> (1e3 + 9); unsigned N, K, p; int collapseTime(std::vector<int> sequence, int sol = 0) { auto n = sequence.size(); if(n == 1 || sol > K) { return sol; } std::vector<int> newSequence{}; if(sequence[0] > sequence[1]) { newSequence.emplace_back(sequence[0]); } for(int i = 1; i < n - 1; ++i) { if(sequence[i] > sequence[i - 1] && sequence[i] > sequence[i + 1]) { newSequence.emplace_back(sequence[i]); } } if(sequence[n - 1] > sequence[n - 2]) { newSequence.emplace_back(sequence[n - 1]); } return collapseTime(newSequence, sol + 1); }; int main() { std::cin >> N >> K >> p; std::vector<int> permutation{}; for(int i = 1; i <= N; ++i) { permutation.emplace_back(i); } unsigned result = 0; do { if(collapseTime(permutation) == K) { ++result; } } while(std::next_permutation(permutation.begin(), permutation.end())); std::cout << result << 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 | #include <iostream> #include <array> #include <vector> #include <algorithm> using ull = unsigned long long; const int MAX_N = static_cast<int> (1e3 + 9); unsigned N, K, p; int collapseTime(std::vector<int> sequence, int sol = 0) { auto n = sequence.size(); if(n == 1 || sol > K) { return sol; } std::vector<int> newSequence{}; if(sequence[0] > sequence[1]) { newSequence.emplace_back(sequence[0]); } for(int i = 1; i < n - 1; ++i) { if(sequence[i] > sequence[i - 1] && sequence[i] > sequence[i + 1]) { newSequence.emplace_back(sequence[i]); } } if(sequence[n - 1] > sequence[n - 2]) { newSequence.emplace_back(sequence[n - 1]); } return collapseTime(newSequence, sol + 1); }; int main() { std::cin >> N >> K >> p; std::vector<int> permutation{}; for(int i = 1; i <= N; ++i) { permutation.emplace_back(i); } unsigned result = 0; do { if(collapseTime(permutation) == K) { ++result; } } while(std::next_permutation(permutation.begin(), permutation.end())); std::cout << result << std::endl; return 0; } |