#include <cstdio> void scal(double* L, int from, int sr, int to); void sortuj(int* L, int from, int to); int L_pom[500000]; int main() { int n; int k; scanf("%i", &n); scanf("%i", &k); //int* tab = new int[n]; //int* idxs = new int[n]; int tab[n]; int sort_tab[n]; int idxs[n]; int count_idxs = 0; bool possible = true; bool ever_possible = true; for (int i = 0; i < n; ++i) { int tmp; scanf("%i", &tmp); tab[i] = tmp; sort_tab[i] = tmp; if (tab[i] != tab[i-1] && i > 0) possible = false; } int max = -1; int min = tab[0]; int last_idx_max = 0; int last_idx_min = 0; int l_max = 0; int l_min = 0; int is_dup = 0; int dup_nr = -1; sortuj(sort_tab, 0, n - 1); for (int i = n - 2; i >= 0; --i) { if (sort_tab[i] == sort_tab[i + 1]) { is_dup = 1; dup_nr = sort_tab[i]; break; } } int min_max_count = 0; for (int i = 0; i < n; ++i) { if (tab[i] > max) { max = tab[i]; last_idx_max = 0; l_max = 0; } if (tab[i] < min) { min = tab[i]; last_idx_min = 0; l_min = 0; } if (tab[i] == min || tab[i] == max) { min_max_count++; } if (tab[i] == max) { last_idx_max = i; l_max++; } if (tab[i] == min) { last_idx_min = i; l_min++; } if (tab[i] == tab[i + 1] && (tab[i] == dup_nr || dup_nr == -1)) { if (i == 0 || i == n - 1) is_dup = 1; else is_dup = 2; } } if (dup_nr == -1) { int ii = 0; bool found = false; for (ii = 1; ii < n; ++ii) { if (tab[ii - 1] > tab[ii]) { found = true; break; } } if (!found) { ever_possible = false; } } for (int i = 0; i < n; ++i) { if (tab[i] == min && tab[i+1] == max && min_max_count != n && last_idx_min < last_idx_max && k == 2) { ever_possible = false; } if (last_idx_min == 0 && last_idx_max == n - 1 && !(is_dup != 0 && k - is_dup >= 2)) { ever_possible = false; } if (count_idxs != k - 1) if (tab[i + 1] == max || (n - i) == (k - count_idxs) || (tab[i] == dup_nr && i != 0)) { // tutaj to coś - count_idxs idxs[count_idxs] = i + 1; count_idxs++; } } if (l_max == l_min && l_max + l_min == n && n > 2 && n != k) { ever_possible = false; } if (!ever_possible) printf("NIE"); else { printf("TAK"); printf("\n"); for (int i = 0; i < k - 1; ++i) { printf("%d ", idxs[i]); } } return 0; } void scal(int* L, int from, int sr, int to) { for (int i = from; i <= to; i++) L_pom[i] = L[i]; int i = from, j = sr + 1; for (int k = from; k <= to; k++) { if (i <= sr) { if (j <= to) { L[k] = (L_pom[j] < L_pom[i]) ? L_pom[j++] : L_pom[i++]; } else { L[k] = L_pom[i++]; } } else { L[k] = L_pom[j++]; } } } void sortuj(int* L, int from, int to) { if (to <= from) return; int sr = (to + from) / 2; sortuj(L, from, sr); sortuj(L, sr + 1, to); scal(L, from, sr, to); }
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <cstdio> void scal(double* L, int from, int sr, int to); void sortuj(int* L, int from, int to); int L_pom[500000]; int main() { int n; int k; scanf("%i", &n); scanf("%i", &k); //int* tab = new int[n]; //int* idxs = new int[n]; int tab[n]; int sort_tab[n]; int idxs[n]; int count_idxs = 0; bool possible = true; bool ever_possible = true; for (int i = 0; i < n; ++i) { int tmp; scanf("%i", &tmp); tab[i] = tmp; sort_tab[i] = tmp; if (tab[i] != tab[i-1] && i > 0) possible = false; } int max = -1; int min = tab[0]; int last_idx_max = 0; int last_idx_min = 0; int l_max = 0; int l_min = 0; int is_dup = 0; int dup_nr = -1; sortuj(sort_tab, 0, n - 1); for (int i = n - 2; i >= 0; --i) { if (sort_tab[i] == sort_tab[i + 1]) { is_dup = 1; dup_nr = sort_tab[i]; break; } } int min_max_count = 0; for (int i = 0; i < n; ++i) { if (tab[i] > max) { max = tab[i]; last_idx_max = 0; l_max = 0; } if (tab[i] < min) { min = tab[i]; last_idx_min = 0; l_min = 0; } if (tab[i] == min || tab[i] == max) { min_max_count++; } if (tab[i] == max) { last_idx_max = i; l_max++; } if (tab[i] == min) { last_idx_min = i; l_min++; } if (tab[i] == tab[i + 1] && (tab[i] == dup_nr || dup_nr == -1)) { if (i == 0 || i == n - 1) is_dup = 1; else is_dup = 2; } } if (dup_nr == -1) { int ii = 0; bool found = false; for (ii = 1; ii < n; ++ii) { if (tab[ii - 1] > tab[ii]) { found = true; break; } } if (!found) { ever_possible = false; } } for (int i = 0; i < n; ++i) { if (tab[i] == min && tab[i+1] == max && min_max_count != n && last_idx_min < last_idx_max && k == 2) { ever_possible = false; } if (last_idx_min == 0 && last_idx_max == n - 1 && !(is_dup != 0 && k - is_dup >= 2)) { ever_possible = false; } if (count_idxs != k - 1) if (tab[i + 1] == max || (n - i) == (k - count_idxs) || (tab[i] == dup_nr && i != 0)) { // tutaj to coś - count_idxs idxs[count_idxs] = i + 1; count_idxs++; } } if (l_max == l_min && l_max + l_min == n && n > 2 && n != k) { ever_possible = false; } if (!ever_possible) printf("NIE"); else { printf("TAK"); printf("\n"); for (int i = 0; i < k - 1; ++i) { printf("%d ", idxs[i]); } } return 0; } void scal(int* L, int from, int sr, int to) { for (int i = from; i <= to; i++) L_pom[i] = L[i]; int i = from, j = sr + 1; for (int k = from; k <= to; k++) { if (i <= sr) { if (j <= to) { L[k] = (L_pom[j] < L_pom[i]) ? L_pom[j++] : L_pom[i++]; } else { L[k] = L_pom[i++]; } } else { L[k] = L_pom[j++]; } } } void sortuj(int* L, int from, int to) { if (to <= from) return; int sr = (to + from) / 2; sortuj(L, from, sr); sortuj(L, sr + 1, to); scal(L, from, sr, to); } |