#include<cstdio> #include<vector> using namespace std; void searchForTwo(vector<int>& incomes) { int lastIx = 0; int minIx = 0; int min = incomes[0]; int candidateMinIx = minIx; int candidateMin = min; for (int i = 1; i < incomes.size(); i++) { if (incomes[i] > min) { lastIx = i; minIx = candidateMinIx; min = candidateMin; } else { candidateMinIx = i; candidateMin = incomes[i]; } } if (lastIx == incomes.size() - 1) { printf("NIE\n"); } else { printf("TAK\n"); printf("%d\n", lastIx + 1); } } void searchForThree(vector<int>& incomes) { int lastMinIx = 0; int min = incomes[0]; int firstMaxIx = 0; int max = incomes[0]; for (int i = 1; i < incomes.size(); i++) { if (incomes[i] <= min) { lastMinIx = i; min = incomes[i]; } if (incomes[i] > max) { firstMaxIx = i; max = incomes[i]; } } if (lastMinIx > 0) { printf("TAK\n"); if (lastMinIx < incomes.size() - 1) { printf("%d %d\n", lastMinIx, lastMinIx + 1); } else { printf("%d %d\n", lastMinIx - 1, lastMinIx); } } else if (firstMaxIx < incomes.size() - 1) { printf("TAK\n"); if (firstMaxIx > 0) { printf("%d %d\n", firstMaxIx, firstMaxIx + 1); } else { printf("%d %d\n", firstMaxIx + 1, firstMaxIx + 2); } } else { printf("NIE\n"); } } void searchForFourOrMore(vector<int>& incomes, int partsNum) { int firstInversionIx = -1; for (int i = 0; i < incomes.size() - 1; i++) { if (incomes[i] >= incomes[i + 1]) { firstInversionIx = i; break; } } if (firstInversionIx == -1) { printf("NIE\n"); } else { printf("TAK\n"); int ixsToFindNum; vector<bool> selectedIxs(incomes.size(), false); if (firstInversionIx == 0) { selectedIxs[0] = true; selectedIxs[1] = true; ixsToFindNum = partsNum - 3; } else if (firstInversionIx == incomes.size() - 2) { selectedIxs[incomes.size() - 3] = true; selectedIxs[incomes.size() - 2] = true; ixsToFindNum = partsNum - 3; } else { selectedIxs[firstInversionIx - 1] = true; selectedIxs[firstInversionIx] = true; selectedIxs[firstInversionIx + 1] = true; ixsToFindNum = partsNum - 4; } int i = 0; while (ixsToFindNum > 0) { if (!selectedIxs[i]) { selectedIxs[i] = true; ixsToFindNum--; } i++; } bool firstWritten = false; for (int i = 0; i < selectedIxs.size(); i++) { if (selectedIxs[i]) { if (!firstWritten) { printf("%d", i + 1); firstWritten = true; } else { printf(" %d", i + 1); } } } printf("\n"); } } int main() { int incomesNum; int partsNum; scanf("%d %d\n", &incomesNum, &partsNum); vector<int> incomes(incomesNum); for (int i = 0; i < incomesNum; i++) { scanf("%d", &incomes[i]); } if (partsNum == 2) { searchForTwo(incomes); } else if (partsNum == 3) { searchForThree(incomes); } else { searchForFourOrMore(incomes, partsNum); } }
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 | #include<cstdio> #include<vector> using namespace std; void searchForTwo(vector<int>& incomes) { int lastIx = 0; int minIx = 0; int min = incomes[0]; int candidateMinIx = minIx; int candidateMin = min; for (int i = 1; i < incomes.size(); i++) { if (incomes[i] > min) { lastIx = i; minIx = candidateMinIx; min = candidateMin; } else { candidateMinIx = i; candidateMin = incomes[i]; } } if (lastIx == incomes.size() - 1) { printf("NIE\n"); } else { printf("TAK\n"); printf("%d\n", lastIx + 1); } } void searchForThree(vector<int>& incomes) { int lastMinIx = 0; int min = incomes[0]; int firstMaxIx = 0; int max = incomes[0]; for (int i = 1; i < incomes.size(); i++) { if (incomes[i] <= min) { lastMinIx = i; min = incomes[i]; } if (incomes[i] > max) { firstMaxIx = i; max = incomes[i]; } } if (lastMinIx > 0) { printf("TAK\n"); if (lastMinIx < incomes.size() - 1) { printf("%d %d\n", lastMinIx, lastMinIx + 1); } else { printf("%d %d\n", lastMinIx - 1, lastMinIx); } } else if (firstMaxIx < incomes.size() - 1) { printf("TAK\n"); if (firstMaxIx > 0) { printf("%d %d\n", firstMaxIx, firstMaxIx + 1); } else { printf("%d %d\n", firstMaxIx + 1, firstMaxIx + 2); } } else { printf("NIE\n"); } } void searchForFourOrMore(vector<int>& incomes, int partsNum) { int firstInversionIx = -1; for (int i = 0; i < incomes.size() - 1; i++) { if (incomes[i] >= incomes[i + 1]) { firstInversionIx = i; break; } } if (firstInversionIx == -1) { printf("NIE\n"); } else { printf("TAK\n"); int ixsToFindNum; vector<bool> selectedIxs(incomes.size(), false); if (firstInversionIx == 0) { selectedIxs[0] = true; selectedIxs[1] = true; ixsToFindNum = partsNum - 3; } else if (firstInversionIx == incomes.size() - 2) { selectedIxs[incomes.size() - 3] = true; selectedIxs[incomes.size() - 2] = true; ixsToFindNum = partsNum - 3; } else { selectedIxs[firstInversionIx - 1] = true; selectedIxs[firstInversionIx] = true; selectedIxs[firstInversionIx + 1] = true; ixsToFindNum = partsNum - 4; } int i = 0; while (ixsToFindNum > 0) { if (!selectedIxs[i]) { selectedIxs[i] = true; ixsToFindNum--; } i++; } bool firstWritten = false; for (int i = 0; i < selectedIxs.size(); i++) { if (selectedIxs[i]) { if (!firstWritten) { printf("%d", i + 1); firstWritten = true; } else { printf(" %d", i + 1); } } } printf("\n"); } } int main() { int incomesNum; int partsNum; scanf("%d %d\n", &incomesNum, &partsNum); vector<int> incomes(incomesNum); for (int i = 0; i < incomesNum; i++) { scanf("%d", &incomes[i]); } if (partsNum == 2) { searchForTwo(incomes); } else if (partsNum == 3) { searchForThree(incomes); } else { searchForFourOrMore(incomes, partsNum); } } |