//============================================================================ // Name : 2b-pod.cpp // Author : poz // Version : // Description : https://sio2.mimuw.edu.pl/c/pa-2022-1/p/pod/ //============================================================================ #include <bits/stdc++.h> using namespace std; const string NIE = "NIE"; const string TAK = "TAK"; void printTwoFrom(int N, int K, const int first) { if (first == 0) { cout << "1 2"; for (int i = 3; i < K; i++) { cout << " " << i; } } else { int suffixLength = min(N - first, K - 1); int prefixLength = K - suffixLength; for (int i = 1; i < prefixLength; i++) { cout << i << " "; } for (int i = 0; i < suffixLength; i++) { cout << first + i << " "; } } cout << endl; } void printOneAfter(int endIndex) { cout << endIndex + 1 << endl; } void printBeforeAndAfter(int N, int middle) { if (middle == 0) { cout << "1 2" << endl; } else if (middle == N - 1) { cout << "1 " << middle << endl; } else { cout << middle << " " << middle + 1 << endl; } } void process_more_partitions(const int N, const int K, vector<int> &profit) { for (int i = 1; i < N; i++) { if (profit[i] <= profit[i - 1]) { vector<int> endIndexes; if ((i - 2) >= 0) { endIndexes.push_back(i - 2); } endIndexes.push_back(i - 1); endIndexes.push_back(i); cout << TAK << endl; printTwoFrom(N, K, i - 1); return; } } cout << NIE << endl; } void process_3_partitions(const int N, vector<int> &profit) { int last_min_pos = 0; int last_min = profit[last_min_pos]; int first_max_pos = 0; int first_max = profit[first_max_pos]; for (int i = 1; i < N; i++) { if (profit[i] <= last_min) { last_min_pos = i; last_min = profit[last_min_pos]; } if (profit[i] > first_max) { first_max_pos = i; first_max = profit[first_max_pos]; } } if (last_min_pos > 0) { cout << TAK << endl; printBeforeAndAfter(N, last_min_pos); } else if (first_max_pos < N - 1) { cout << TAK << endl; printBeforeAndAfter(N, first_max_pos); } else { cout << NIE << endl; } } void process_2_partitions(const int N, vector<int> &profit) { vector<int> min_from_left(N); vector<int> max_from_right(N); min_from_left[0] = profit[0]; max_from_right[N - 1] = profit[N - 1]; for (int i = 1; i < N; i++) { min_from_left[i] = min(min_from_left[i - 1], profit[i]); max_from_right[N - i - 1] = max(max_from_right[N - i], profit[N - i - 1]); } int pos = -1; for (int i = 0; i < N - 1; i++) { if (min_from_left[i] >= max_from_right[i + 1]) { pos = i; break; } } if (pos < 0) { cout << NIE << endl; } else { cout << TAK << endl; printOneAfter(pos); } } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, k; cin >> n; cin >> k; vector<int> profit(n); for (int i = 0; i < n; i++) { cin >> profit[i]; } if (k == 2) { process_2_partitions(n, profit); } else if (k == 3) { process_3_partitions(n, profit); } else { process_more_partitions(n, k, profit); } 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 123 124 125 126 127 128 129 130 131 132 133 134 135 | //============================================================================ // Name : 2b-pod.cpp // Author : poz // Version : // Description : https://sio2.mimuw.edu.pl/c/pa-2022-1/p/pod/ //============================================================================ #include <bits/stdc++.h> using namespace std; const string NIE = "NIE"; const string TAK = "TAK"; void printTwoFrom(int N, int K, const int first) { if (first == 0) { cout << "1 2"; for (int i = 3; i < K; i++) { cout << " " << i; } } else { int suffixLength = min(N - first, K - 1); int prefixLength = K - suffixLength; for (int i = 1; i < prefixLength; i++) { cout << i << " "; } for (int i = 0; i < suffixLength; i++) { cout << first + i << " "; } } cout << endl; } void printOneAfter(int endIndex) { cout << endIndex + 1 << endl; } void printBeforeAndAfter(int N, int middle) { if (middle == 0) { cout << "1 2" << endl; } else if (middle == N - 1) { cout << "1 " << middle << endl; } else { cout << middle << " " << middle + 1 << endl; } } void process_more_partitions(const int N, const int K, vector<int> &profit) { for (int i = 1; i < N; i++) { if (profit[i] <= profit[i - 1]) { vector<int> endIndexes; if ((i - 2) >= 0) { endIndexes.push_back(i - 2); } endIndexes.push_back(i - 1); endIndexes.push_back(i); cout << TAK << endl; printTwoFrom(N, K, i - 1); return; } } cout << NIE << endl; } void process_3_partitions(const int N, vector<int> &profit) { int last_min_pos = 0; int last_min = profit[last_min_pos]; int first_max_pos = 0; int first_max = profit[first_max_pos]; for (int i = 1; i < N; i++) { if (profit[i] <= last_min) { last_min_pos = i; last_min = profit[last_min_pos]; } if (profit[i] > first_max) { first_max_pos = i; first_max = profit[first_max_pos]; } } if (last_min_pos > 0) { cout << TAK << endl; printBeforeAndAfter(N, last_min_pos); } else if (first_max_pos < N - 1) { cout << TAK << endl; printBeforeAndAfter(N, first_max_pos); } else { cout << NIE << endl; } } void process_2_partitions(const int N, vector<int> &profit) { vector<int> min_from_left(N); vector<int> max_from_right(N); min_from_left[0] = profit[0]; max_from_right[N - 1] = profit[N - 1]; for (int i = 1; i < N; i++) { min_from_left[i] = min(min_from_left[i - 1], profit[i]); max_from_right[N - i - 1] = max(max_from_right[N - i], profit[N - i - 1]); } int pos = -1; for (int i = 0; i < N - 1; i++) { if (min_from_left[i] >= max_from_right[i + 1]) { pos = i; break; } } if (pos < 0) { cout << NIE << endl; } else { cout << TAK << endl; printOneAfter(pos); } } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, k; cin >> n; cin >> k; vector<int> profit(n); for (int i = 0; i < n; i++) { cin >> profit[i]; } if (k == 2) { process_2_partitions(n, profit); } else if (k == 3) { process_3_partitions(n, profit); } else { process_more_partitions(n, k, profit); } return 0; } |