#include <algorithm> #include <vector> #include <iostream> #include <random> #include <unordered_map> using ll = long long; using ull = unsigned long long; using namespace std; auto v1 = new vector<int>(), v2 = new std::vector<int>(); ull calcTime(const vector<int>& in) { ull time = 0; v1->clear(); v2->clear(); v1->insert(begin(*v1), begin(in), end(in)); while(v1->size() > 1) { //for (auto x : v) //{ // cout << x << ","; //} //cout << endl; if ((*v1)[0] > (*v1)[1]) v2->push_back((*v1)[0]); for(int i = 1; i < v1->size() - 1; ++i) { if((*v1)[i] > (*v1)[i - 1] && (*v1)[i] > (*v1)[i + 1]) { v2->push_back((*v1)[i]); } } if ((*v1)[v1->size() - 1] > (*v1)[v1->size() - 2]) v2->push_back((*v1)[v1->size() - 1]); ++time; std::swap(v1, v2); v2->clear(); } return time; } ull solveBrut(ull n, ull k, ull p) { std::vector<int> v(n); std::generate(v.begin(), v.end(), [] { static int i = 1; return i++; }); ull result = 0; do { const auto time = calcTime(v); if(time == k) { /*if (true) { for (auto x : v) { cout << x << ","; } cout << endl; }*/ ++result; } } while (std::next_permutation(v.begin(), v.end())); return result; } int main() { //std::random_device rnd; //std::uniform_int_distribution<int> dist(0, 100); //unordered_map<int, int> results; //results.reserve(10); //for (int i = 0; i < 100000; ++i) //{ // std::vector<int> v(10); // int x = 1; // std::generate(v.begin(), v.end(), [&x] { // return x++; // }); // std::shuffle(v.begin(), v.end(), rnd); // const auto time = calcTime(v); // if(time == 1) // { // for (auto y: v) // { // cout << y << ","; // } // cout << endl; // } // ++results[calcTime(v)]; //} //for(auto x : results) //{ // cout << x.first << " " << x.second << endl; //} //solveBrut(10, 3, 0); //return 0; ull n, k, p; cin >> n >> k >> p; v1->reserve(n); v2->reserve(n); cout << (n == 1 ? 0 : solveBrut(n, k, p)); 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 | #include <algorithm> #include <vector> #include <iostream> #include <random> #include <unordered_map> using ll = long long; using ull = unsigned long long; using namespace std; auto v1 = new vector<int>(), v2 = new std::vector<int>(); ull calcTime(const vector<int>& in) { ull time = 0; v1->clear(); v2->clear(); v1->insert(begin(*v1), begin(in), end(in)); while(v1->size() > 1) { //for (auto x : v) //{ // cout << x << ","; //} //cout << endl; if ((*v1)[0] > (*v1)[1]) v2->push_back((*v1)[0]); for(int i = 1; i < v1->size() - 1; ++i) { if((*v1)[i] > (*v1)[i - 1] && (*v1)[i] > (*v1)[i + 1]) { v2->push_back((*v1)[i]); } } if ((*v1)[v1->size() - 1] > (*v1)[v1->size() - 2]) v2->push_back((*v1)[v1->size() - 1]); ++time; std::swap(v1, v2); v2->clear(); } return time; } ull solveBrut(ull n, ull k, ull p) { std::vector<int> v(n); std::generate(v.begin(), v.end(), [] { static int i = 1; return i++; }); ull result = 0; do { const auto time = calcTime(v); if(time == k) { /*if (true) { for (auto x : v) { cout << x << ","; } cout << endl; }*/ ++result; } } while (std::next_permutation(v.begin(), v.end())); return result; } int main() { //std::random_device rnd; //std::uniform_int_distribution<int> dist(0, 100); //unordered_map<int, int> results; //results.reserve(10); //for (int i = 0; i < 100000; ++i) //{ // std::vector<int> v(10); // int x = 1; // std::generate(v.begin(), v.end(), [&x] { // return x++; // }); // std::shuffle(v.begin(), v.end(), rnd); // const auto time = calcTime(v); // if(time == 1) // { // for (auto y: v) // { // cout << y << ","; // } // cout << endl; // } // ++results[calcTime(v)]; //} //for(auto x : results) //{ // cout << x.first << " " << x.second << endl; //} //solveBrut(10, 3, 0); //return 0; ull n, k, p; cin >> n >> k >> p; v1->reserve(n); v2->reserve(n); cout << (n == 1 ? 0 : solveBrut(n, k, p)); return 0; } |