#include <bits/stdc++.h> using namespace std; vector<int> find_solution_2(vector<int> income){ vector<int> result; int min_value = INT_MAX; int max_value = 0; vector<int> mins; vector<int> maxs; for(int i = 0; i < income.size(); i++){ min_value = min(min_value, income[i]); mins.push_back(min_value); } for(int i = income.size() -1; i >= 0; i--){ max_value = max(max_value, income[i]); maxs.push_back(max_value); } reverse(maxs.begin(), maxs.end()); for(int i = 0; i < income.size() - 1; i++){ if(mins[i] >= maxs[i+1]){ result.push_back(i+1); return result; } } return result; } vector<int> find_solution_3(vector<int> income){ vector<int> result; int min_value = *min_element(income.begin(), income.end()); int max_value = *max_element(income.begin(), income.end()); for(int i = 1; i < income.size(); i++){ if(income[i] == min_value) { if(i == income.size() - 1){ result.push_back(i-1); result.push_back(i); } else { result.push_back(i); result.push_back(i+1); } return result; } } for(int i = 0; i < income.size() - 1; i++){ if(income[i] == max_value) { if(i == 0){ result.push_back(i+1); result.push_back(i+2); } else { result.push_back(i); result.push_back(i+1); } return result; } } return result; } vector<int> find_solution_4(vector<int> income, int k){ vector<int> result; int idx = -1; for (int i = 0; i < income.size() - 1; i++){ if(income[i] >= income[i+1]){ if(i != 0) result.push_back(i); result.push_back(i+1); if(i+2 != income.size()) result.push_back(i+2); idx = i; break; } } if(idx == -1) return result; for(int i = 1; i < income.size(); i++){ if(result.size() < k - 1 && (i < idx || i >= idx+3)) result.push_back(i); } sort(result.begin(), result.end()); return result; } int main() { int n, k; cin >> n >> k; vector<int> income; vector<int> solution; for(int i = 0; i < n; i++){ int value; cin >> value; income.push_back(value); } if(k == 2) solution = find_solution_2(income); else if(k == 3) solution = find_solution_3(income); else solution = find_solution_4(income, k); if(solution.size() == 0){ cout << "NIE" << endl; } else { cout << "TAK" << endl; for(int i = 0; i < k - 1; i++) cout << solution[i] << " "; cout << endl; } }
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 | #include <bits/stdc++.h> using namespace std; vector<int> find_solution_2(vector<int> income){ vector<int> result; int min_value = INT_MAX; int max_value = 0; vector<int> mins; vector<int> maxs; for(int i = 0; i < income.size(); i++){ min_value = min(min_value, income[i]); mins.push_back(min_value); } for(int i = income.size() -1; i >= 0; i--){ max_value = max(max_value, income[i]); maxs.push_back(max_value); } reverse(maxs.begin(), maxs.end()); for(int i = 0; i < income.size() - 1; i++){ if(mins[i] >= maxs[i+1]){ result.push_back(i+1); return result; } } return result; } vector<int> find_solution_3(vector<int> income){ vector<int> result; int min_value = *min_element(income.begin(), income.end()); int max_value = *max_element(income.begin(), income.end()); for(int i = 1; i < income.size(); i++){ if(income[i] == min_value) { if(i == income.size() - 1){ result.push_back(i-1); result.push_back(i); } else { result.push_back(i); result.push_back(i+1); } return result; } } for(int i = 0; i < income.size() - 1; i++){ if(income[i] == max_value) { if(i == 0){ result.push_back(i+1); result.push_back(i+2); } else { result.push_back(i); result.push_back(i+1); } return result; } } return result; } vector<int> find_solution_4(vector<int> income, int k){ vector<int> result; int idx = -1; for (int i = 0; i < income.size() - 1; i++){ if(income[i] >= income[i+1]){ if(i != 0) result.push_back(i); result.push_back(i+1); if(i+2 != income.size()) result.push_back(i+2); idx = i; break; } } if(idx == -1) return result; for(int i = 1; i < income.size(); i++){ if(result.size() < k - 1 && (i < idx || i >= idx+3)) result.push_back(i); } sort(result.begin(), result.end()); return result; } int main() { int n, k; cin >> n >> k; vector<int> income; vector<int> solution; for(int i = 0; i < n; i++){ int value; cin >> value; income.push_back(value); } if(k == 2) solution = find_solution_2(income); else if(k == 3) solution = find_solution_3(income); else solution = find_solution_4(income, k); if(solution.size() == 0){ cout << "NIE" << endl; } else { cout << "TAK" << endl; for(int i = 0; i < k - 1; i++) cout << solution[i] << " "; cout << endl; } } |