#include <iostream> #include <vector> using namespace std; /** Helpful links first implementation: https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ final implementation: https://www.geeksforgeeks.org/make-combinations-size-k/ */ vector<int> przychody; vector<int> result; bool found = false; int kk; #define INT_MAX 1000000009 void display(const vector<int>& v) { for (int i = 0; i < v.size(); ++i) printf("%d ", v[i]); printf("\n"); } void check(vector<int> data, int k) { int min = INT_MAX; int prev_min = 0; vector<int> ranges(k + 2); ranges[0] = 0; ranges[k + 1] = przychody.size(); for (int i = 1; i < k + 1; i++) { ranges[i] = data[i - 1]; } for (int j = 0; j < k + 1; j++) { min = INT_MAX; //cout << przychody[data[j]] << " "; for (int i = ranges[j]; i < ranges[j + 1]; i++) { if (min > przychody[i] && przychody[i] > prev_min) { min = przychody[i]; } } if (min == INT_MAX) { for (int i = 1; i <= k; i++) { result[i - 1] = ranges[i]; } found = true; break; //ressss = ranges; //cout << "Wrong" << endl; } prev_min = min; } //cout << endl; } void makeCombiUtil(vector<int>& tmp, int n, int left, int k) { if (found) { return; } // Pushing this vector to a vector of vector if (k == 0) { //ans.push_back(tmp); //display(tmp); check(tmp, kk); return; } // i iterates from left to n. First time // left will be 1 for (int i = left; i <= n; ++i) { tmp.push_back(i); makeCombiUtil(tmp, n, i + 1, k - 1); // Popping out last inserted element // from the vector tmp.pop_back(); } } void makeCombi(int n, int k) { vector<int> tmp; makeCombiUtil(tmp, n, 1, k); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; kk = k-1; przychody = vector<int>(n); result = vector<int>(k - 1); for (int i = 0; i < n; i++) { cin >> przychody[i]; } makeCombi(n - 1, k - 1); if (found) { cout << "TAK" << endl; display(result); } else { cout << "NIE"; } }
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 | #include <iostream> #include <vector> using namespace std; /** Helpful links first implementation: https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ final implementation: https://www.geeksforgeeks.org/make-combinations-size-k/ */ vector<int> przychody; vector<int> result; bool found = false; int kk; #define INT_MAX 1000000009 void display(const vector<int>& v) { for (int i = 0; i < v.size(); ++i) printf("%d ", v[i]); printf("\n"); } void check(vector<int> data, int k) { int min = INT_MAX; int prev_min = 0; vector<int> ranges(k + 2); ranges[0] = 0; ranges[k + 1] = przychody.size(); for (int i = 1; i < k + 1; i++) { ranges[i] = data[i - 1]; } for (int j = 0; j < k + 1; j++) { min = INT_MAX; //cout << przychody[data[j]] << " "; for (int i = ranges[j]; i < ranges[j + 1]; i++) { if (min > przychody[i] && przychody[i] > prev_min) { min = przychody[i]; } } if (min == INT_MAX) { for (int i = 1; i <= k; i++) { result[i - 1] = ranges[i]; } found = true; break; //ressss = ranges; //cout << "Wrong" << endl; } prev_min = min; } //cout << endl; } void makeCombiUtil(vector<int>& tmp, int n, int left, int k) { if (found) { return; } // Pushing this vector to a vector of vector if (k == 0) { //ans.push_back(tmp); //display(tmp); check(tmp, kk); return; } // i iterates from left to n. First time // left will be 1 for (int i = left; i <= n; ++i) { tmp.push_back(i); makeCombiUtil(tmp, n, i + 1, k - 1); // Popping out last inserted element // from the vector tmp.pop_back(); } } void makeCombi(int n, int k) { vector<int> tmp; makeCombiUtil(tmp, n, 1, k); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; kk = k-1; przychody = vector<int>(n); result = vector<int>(k - 1); for (int i = 0; i < n; i++) { cin >> przychody[i]; } makeCombi(n - 1, k - 1); if (found) { cout << "TAK" << endl; display(result); } else { cout << "NIE"; } } |