// B2 - Podwyzki #include <iostream> #include <vector> #include <algorithm> using namespace std; #define INF 1000000001; bool isPossible(int *val, int startPos, int endPos, vector<int> *edges) { int borderPos; int borderVal; int minVal; int maxVal; int minPos; int maxPos; // wrong period? if (endPos - startPos <= 1) { return false; } // possible to split in half? borderPos = startPos; borderVal = minVal = val[startPos]; for (int i = startPos + 1; i < endPos; ++i) { if (val[i] > borderVal) { borderPos = i; borderVal = minVal; } if (val[i] < borderVal) { minVal = val[i]; } } if (borderPos + 1 < endPos) { edges->push_back(borderPos + 1); return true; } // max value somewhere in the middle? maxVal = -1; for (int i = startPos; i < endPos; ++i) { if (val[i] > maxVal) { maxVal = val[i]; maxPos = i; } } if (maxPos < endPos - 1) { edges->push_back(maxPos); edges->push_back(maxPos + 1); return true; } // min value somewhere in the middle? minVal = INF; for (int i = endPos - 1; i >= startPos; --i) { if (val[i] < minVal) { minVal = val[i]; minPos = i; } } if (minPos > startPos) { edges->push_back(minPos); edges->push_back(minPos + 1); return true; } // cut ending and beginning, then recursion vector<int> sortedSub(val + startPos, val + endPos); sort(sortedSub.begin(), sortedSub.end()); for (int i = startPos; i < endPos; ++i) { minPos = i; if (val[i] != sortedSub[i - startPos] || (i < endPos - 1 && sortedSub[i - startPos] == sortedSub[i - startPos + 1])) { break; } } for (int i = endPos - 1; i >= startPos; --i) { maxPos = i + 1; if (val[i] != sortedSub[i - startPos] || (i > startPos && sortedSub[i - startPos] == sortedSub[i - startPos - 1])) { break; } } if (isPossible(val, minPos, maxPos, edges)) { edges->push_back(minPos); edges->push_back(maxPos); return true; } else { return false; } } int main() { ios_base::sync_with_stdio(0); int n, k, a; int *val; vector<int> edges; int spare; bool *isBorder; cin >> n; cin >> k; val = new int[n]; isBorder = new bool[n]; for (int i = 0; i < n; ++i) { cin >> a; val[i] = a; isBorder[i] = false; } if (isPossible(val, 0, n, &edges) == false || edges.size() > k - 1) { cout << "NIE" << endl; } else { cout << "TAK" << endl; sort(edges.begin(), edges.end()); spare = k - 1 - edges.size(); for (vector<int>::iterator it = edges.begin(); it != edges.end(); ++it) { isBorder[*it - 1] = true; } for (int i = 0; i < n - 1; ++i) { if (spare > 0 && isBorder[i] == false) { isBorder[i] = true; --spare; } if (isBorder[i]) { cout << i + 1 << " "; } } cout << endl; } delete [] val; delete [] isBorder; 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 136 | // B2 - Podwyzki #include <iostream> #include <vector> #include <algorithm> using namespace std; #define INF 1000000001; bool isPossible(int *val, int startPos, int endPos, vector<int> *edges) { int borderPos; int borderVal; int minVal; int maxVal; int minPos; int maxPos; // wrong period? if (endPos - startPos <= 1) { return false; } // possible to split in half? borderPos = startPos; borderVal = minVal = val[startPos]; for (int i = startPos + 1; i < endPos; ++i) { if (val[i] > borderVal) { borderPos = i; borderVal = minVal; } if (val[i] < borderVal) { minVal = val[i]; } } if (borderPos + 1 < endPos) { edges->push_back(borderPos + 1); return true; } // max value somewhere in the middle? maxVal = -1; for (int i = startPos; i < endPos; ++i) { if (val[i] > maxVal) { maxVal = val[i]; maxPos = i; } } if (maxPos < endPos - 1) { edges->push_back(maxPos); edges->push_back(maxPos + 1); return true; } // min value somewhere in the middle? minVal = INF; for (int i = endPos - 1; i >= startPos; --i) { if (val[i] < minVal) { minVal = val[i]; minPos = i; } } if (minPos > startPos) { edges->push_back(minPos); edges->push_back(minPos + 1); return true; } // cut ending and beginning, then recursion vector<int> sortedSub(val + startPos, val + endPos); sort(sortedSub.begin(), sortedSub.end()); for (int i = startPos; i < endPos; ++i) { minPos = i; if (val[i] != sortedSub[i - startPos] || (i < endPos - 1 && sortedSub[i - startPos] == sortedSub[i - startPos + 1])) { break; } } for (int i = endPos - 1; i >= startPos; --i) { maxPos = i + 1; if (val[i] != sortedSub[i - startPos] || (i > startPos && sortedSub[i - startPos] == sortedSub[i - startPos - 1])) { break; } } if (isPossible(val, minPos, maxPos, edges)) { edges->push_back(minPos); edges->push_back(maxPos); return true; } else { return false; } } int main() { ios_base::sync_with_stdio(0); int n, k, a; int *val; vector<int> edges; int spare; bool *isBorder; cin >> n; cin >> k; val = new int[n]; isBorder = new bool[n]; for (int i = 0; i < n; ++i) { cin >> a; val[i] = a; isBorder[i] = false; } if (isPossible(val, 0, n, &edges) == false || edges.size() > k - 1) { cout << "NIE" << endl; } else { cout << "TAK" << endl; sort(edges.begin(), edges.end()); spare = k - 1 - edges.size(); for (vector<int>::iterator it = edges.begin(); it != edges.end(); ++it) { isBorder[*it - 1] = true; } for (int i = 0; i < n - 1; ++i) { if (spare > 0 && isBorder[i] == false) { isBorder[i] = true; --spare; } if (isBorder[i]) { cout << i + 1 << " "; } } cout << endl; } delete [] val; delete [] isBorder; return 0; } |