#include <iostream> #include <vector> #include <iomanip> #include <climits> using namespace std; int minBiggerThen(int *arr, int a, int b, int x) { int result = INT_MAX; for(int i = a; i <= b; ++i) { if (arr[i] > x) { result = min(arr[i], result); } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int val[n]; for(int i = 0; i < n; ++i) cin >> val[i]; int **minn = new int*[n]; int **prev = new int*[n]; for(int i = 0; i < n; ++i) { minn[i] = new int[k]{0}; prev[i] = new int[k]{0}; } int tmp = val[0]; for(int i = 0; i < n; ++i) { minn[i][0] = tmp = min(tmp, val[i]); } for(int i = 0; i < n; ++i) { int curr = 0; for(int j = 1; j < min(k - 1, i + 1); ++j) { int res = val[i]; for(int l = i; l > 0; --l) { int minBigger = minBiggerThen(val, l, i, minn[l - 1][j - 1]); if(minn[i][j] < minBigger) { minn[i][j] = minBigger; prev[i][j] = l - 1; } } } } for(int l = n - 1; l > 0; --l) { int minBigger = minBiggerThen(val, l, n - 1, minn[l - 1][k - 2]); if(minn[n - 1][k - 1] < minBigger) { minn[n - 1][k - 1] = minBigger; prev[n - 1][k - 1] = l - 1; } } if(minn[n - 1][k - 1] < INT_MAX) { cout << "NIE" << endl; } else { cout << "TAK" << endl; vector<int> path; int i = prev[n - 1][k - 1]; int c = k - 2; while(c >= 0) { path.push_back(i); i = prev[i][c]; c--; } for(auto iter = path.crbegin(); iter != path.crend(); ++iter) cout << *iter + 1<< " "; cout << endl; } for(int i = 0; i < n; ++i) { delete minn[i]; delete prev[i]; } delete minn; delete prev; 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 | #include <iostream> #include <vector> #include <iomanip> #include <climits> using namespace std; int minBiggerThen(int *arr, int a, int b, int x) { int result = INT_MAX; for(int i = a; i <= b; ++i) { if (arr[i] > x) { result = min(arr[i], result); } } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int val[n]; for(int i = 0; i < n; ++i) cin >> val[i]; int **minn = new int*[n]; int **prev = new int*[n]; for(int i = 0; i < n; ++i) { minn[i] = new int[k]{0}; prev[i] = new int[k]{0}; } int tmp = val[0]; for(int i = 0; i < n; ++i) { minn[i][0] = tmp = min(tmp, val[i]); } for(int i = 0; i < n; ++i) { int curr = 0; for(int j = 1; j < min(k - 1, i + 1); ++j) { int res = val[i]; for(int l = i; l > 0; --l) { int minBigger = minBiggerThen(val, l, i, minn[l - 1][j - 1]); if(minn[i][j] < minBigger) { minn[i][j] = minBigger; prev[i][j] = l - 1; } } } } for(int l = n - 1; l > 0; --l) { int minBigger = minBiggerThen(val, l, n - 1, minn[l - 1][k - 2]); if(minn[n - 1][k - 1] < minBigger) { minn[n - 1][k - 1] = minBigger; prev[n - 1][k - 1] = l - 1; } } if(minn[n - 1][k - 1] < INT_MAX) { cout << "NIE" << endl; } else { cout << "TAK" << endl; vector<int> path; int i = prev[n - 1][k - 1]; int c = k - 2; while(c >= 0) { path.push_back(i); i = prev[i][c]; c--; } for(auto iter = path.crbegin(); iter != path.crend(); ++iter) cout << *iter + 1<< " "; cout << endl; } for(int i = 0; i < n; ++i) { delete minn[i]; delete prev[i]; } delete minn; delete prev; return 0; } |