#include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <string> #include <stack> #define LL long long using namespace std; vector<int> solveBigK(const vector<int> &v, int k) { assert (k >= 4); const int n = v.size(); int firstBiggerIdx = -1; int nonBiggerAfterIdx = -1; for (int i=0; i + 1 < n; i++) { if (v[i+1] <= v[i]) { firstBiggerIdx = i; nonBiggerAfterIdx = i+1; break; } } if (firstBiggerIdx == -1) return vector<int>(); int currentEnd = 0; vector<int> out; if (firstBiggerIdx == 0) { out.emplace_back(0); k--; out.emplace_back(1); k--; currentEnd = 2; while (k > 1) { out.push_back(currentEnd++); k--; } } else if (nonBiggerAfterIdx == v.size() - 1) { currentEnd = 0; while (k > 3) { out.emplace_back(currentEnd++); k--; } out.emplace_back(firstBiggerIdx - 1); k--; out.emplace_back(firstBiggerIdx); k--; } else { currentEnd = 0; while (k > 4) { out.emplace_back(currentEnd++); k--; } out.emplace_back(firstBiggerIdx - 1); k--; out.emplace_back(firstBiggerIdx); k--; out.emplace_back(nonBiggerAfterIdx); k--; currentEnd = nonBiggerAfterIdx + 1; while (k > 1) { out.emplace_back(currentEnd++); k--; } } return out; } bool strictlyIncreasing(int a, int b, int c) { return a < b && b < c; } vector<int> solve(const vector<int> &v, const int k) { // if (k>=4) return solveBigK(v, k); const int n = v.size(); vector<int> prefixMin(n, 0); vector<int> suffixMax(n, 0); prefixMin[0] = v[0]; suffixMax[n-1] = v[n-1]; for (int i=1; i < n; i++) { prefixMin[i] = min(v[i], prefixMin[i-1]); } for (int i=n-2; i >=0; i--) { suffixMax[i] = max(v[i], suffixMax[i+1]); } if (k == 2) { for (int i=0; i < n-1; i++) { if (prefixMin[i] >= suffixMax[i+1]) { vector<int> res; res.push_back(i); return res; } } return vector<int>(); } // K == 3 for (int i=1; i < n-1; i++) { int elem = v[i]; if (!strictlyIncreasing(prefixMin[i-1], v[i], suffixMax[i+1])) { vector<int> res; res.push_back(i-1); res.push_back(i); return res; } } return vector<int>(); } int findMinGreaterThan(const vector<int> &v, int start, int end, int atLeast) { const int INF = 1000000010; int x = INF; for (int i=start; i <= end; i++) { if (v[i] > atLeast && v[i] < x) { x = v[i]; } } if (x == INF) x = -1; return x; } bool verify(const vector<int> &v, const vector<int> &answer) { int m = -1; int start = 0; auto intervals = answer; intervals.push_back(v.size() - 1); for (int i=0; i < intervals.size(); i++) { start = i == 0 ? 0 : intervals[i-1] + 1; int end = intervals[i]; int x = findMinGreaterThan(v, start, end, m); if (x <= m) { //printf("Found x=%d and m=%d\n", x, m); return true; } m = x; } return false; } int main() { int n, k; scanf("%d%d", &n, &k); vector<int> v(n); for (int i=0; i < n; i++) { int x; scanf("%d", &x); v[i] = x; } vector<int> res = solve(v, k); if (res.empty()) { printf("NIE\n"); } else { printf("TAK\n"); assert (res.size() == k - 1); // assert (verify(v, res)); for (auto &x : res) { printf("%d ", x+1); } printf("\n"); } 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <string> #include <stack> #define LL long long using namespace std; vector<int> solveBigK(const vector<int> &v, int k) { assert (k >= 4); const int n = v.size(); int firstBiggerIdx = -1; int nonBiggerAfterIdx = -1; for (int i=0; i + 1 < n; i++) { if (v[i+1] <= v[i]) { firstBiggerIdx = i; nonBiggerAfterIdx = i+1; break; } } if (firstBiggerIdx == -1) return vector<int>(); int currentEnd = 0; vector<int> out; if (firstBiggerIdx == 0) { out.emplace_back(0); k--; out.emplace_back(1); k--; currentEnd = 2; while (k > 1) { out.push_back(currentEnd++); k--; } } else if (nonBiggerAfterIdx == v.size() - 1) { currentEnd = 0; while (k > 3) { out.emplace_back(currentEnd++); k--; } out.emplace_back(firstBiggerIdx - 1); k--; out.emplace_back(firstBiggerIdx); k--; } else { currentEnd = 0; while (k > 4) { out.emplace_back(currentEnd++); k--; } out.emplace_back(firstBiggerIdx - 1); k--; out.emplace_back(firstBiggerIdx); k--; out.emplace_back(nonBiggerAfterIdx); k--; currentEnd = nonBiggerAfterIdx + 1; while (k > 1) { out.emplace_back(currentEnd++); k--; } } return out; } bool strictlyIncreasing(int a, int b, int c) { return a < b && b < c; } vector<int> solve(const vector<int> &v, const int k) { // if (k>=4) return solveBigK(v, k); const int n = v.size(); vector<int> prefixMin(n, 0); vector<int> suffixMax(n, 0); prefixMin[0] = v[0]; suffixMax[n-1] = v[n-1]; for (int i=1; i < n; i++) { prefixMin[i] = min(v[i], prefixMin[i-1]); } for (int i=n-2; i >=0; i--) { suffixMax[i] = max(v[i], suffixMax[i+1]); } if (k == 2) { for (int i=0; i < n-1; i++) { if (prefixMin[i] >= suffixMax[i+1]) { vector<int> res; res.push_back(i); return res; } } return vector<int>(); } // K == 3 for (int i=1; i < n-1; i++) { int elem = v[i]; if (!strictlyIncreasing(prefixMin[i-1], v[i], suffixMax[i+1])) { vector<int> res; res.push_back(i-1); res.push_back(i); return res; } } return vector<int>(); } int findMinGreaterThan(const vector<int> &v, int start, int end, int atLeast) { const int INF = 1000000010; int x = INF; for (int i=start; i <= end; i++) { if (v[i] > atLeast && v[i] < x) { x = v[i]; } } if (x == INF) x = -1; return x; } bool verify(const vector<int> &v, const vector<int> &answer) { int m = -1; int start = 0; auto intervals = answer; intervals.push_back(v.size() - 1); for (int i=0; i < intervals.size(); i++) { start = i == 0 ? 0 : intervals[i-1] + 1; int end = intervals[i]; int x = findMinGreaterThan(v, start, end, m); if (x <= m) { //printf("Found x=%d and m=%d\n", x, m); return true; } m = x; } return false; } int main() { int n, k; scanf("%d%d", &n, &k); vector<int> v(n); for (int i=0; i < n; i++) { int x; scanf("%d", &x); v[i] = x; } vector<int> res = solve(v, k); if (res.empty()) { printf("NIE\n"); } else { printf("TAK\n"); assert (res.size() == k - 1); // assert (verify(v, res)); for (auto &x : res) { printf("%d ", x+1); } printf("\n"); } return 0; } |