#include <iostream> #include <string> #include <vector> #include <sstream> using namespace std; void CheckForConditions(int n, vector<int>& division, vector<int>& seq, bool& found) { int smallest = seq[0]; for (int i = 0; i < division[0]; i++) { if (seq[i] < smallest) { smallest = seq[i]; } } for (int i = 1; i < division.size()+1; i++) { int biggerSmallest = 0; bool isBigger = false; int end; if (i == division.size()) { end = n; } else { end = division[i]; } for (int j = division[i - 1]; j < end; j++) { if (biggerSmallest == 0) { biggerSmallest = seq[j]; } if (seq[j] > smallest) { if (seq[j] <= biggerSmallest) { biggerSmallest = seq[j]; isBigger = true; } } else { biggerSmallest = 0; } } if (isBigger == false) { found = true; return; } smallest = biggerSmallest; } } void FindAllPossibilities(int n, int k, vector<int>& division, int last_index, vector<int>& seq, bool& found) { if (k == 0) { CheckForConditions(n, division, seq, found); return; } for (int i = last_index + 1; i < n; i++) { division.push_back(i); FindAllPossibilities(n, k - 1, division, i, seq, found); if (found) { return; } division.pop_back(); } } int main() { int n, k; cin >> n; cin >> k; bool found = false; vector<int> seq; string numbers, temp; cin.ignore(); getline(cin, numbers); istringstream stream(numbers); while (getline(stream, temp, ' ')) { seq.push_back(stoi(temp)); } vector<int> division; FindAllPossibilities(n, k - 1, division, 0, seq, found); if (found) { cout << "TAK\n"; for (int i = 0; i < division.size(); i++) { cout << division[i] << " "; } } 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 | #include <iostream> #include <string> #include <vector> #include <sstream> using namespace std; void CheckForConditions(int n, vector<int>& division, vector<int>& seq, bool& found) { int smallest = seq[0]; for (int i = 0; i < division[0]; i++) { if (seq[i] < smallest) { smallest = seq[i]; } } for (int i = 1; i < division.size()+1; i++) { int biggerSmallest = 0; bool isBigger = false; int end; if (i == division.size()) { end = n; } else { end = division[i]; } for (int j = division[i - 1]; j < end; j++) { if (biggerSmallest == 0) { biggerSmallest = seq[j]; } if (seq[j] > smallest) { if (seq[j] <= biggerSmallest) { biggerSmallest = seq[j]; isBigger = true; } } else { biggerSmallest = 0; } } if (isBigger == false) { found = true; return; } smallest = biggerSmallest; } } void FindAllPossibilities(int n, int k, vector<int>& division, int last_index, vector<int>& seq, bool& found) { if (k == 0) { CheckForConditions(n, division, seq, found); return; } for (int i = last_index + 1; i < n; i++) { division.push_back(i); FindAllPossibilities(n, k - 1, division, i, seq, found); if (found) { return; } division.pop_back(); } } int main() { int n, k; cin >> n; cin >> k; bool found = false; vector<int> seq; string numbers, temp; cin.ignore(); getline(cin, numbers); istringstream stream(numbers); while (getline(stream, temp, ' ')) { seq.push_back(stoi(temp)); } vector<int> division; FindAllPossibilities(n, k - 1, division, 0, seq, found); if (found) { cout << "TAK\n"; for (int i = 0; i < division.size(); i++) { cout << division[i] << " "; } } else { cout << "NIE"; } } |