#include <vector> #include <iostream> using namespace std; int LongestIncreasingSubsequenceLength(std::vector<int>& v) { if (v.size() == 0) // boundary case return 0; std::vector<int> tail(v.size(), 0); int length = 1; // always points empty slot in tail tail[0] = v[0]; for (int i = 1; i < v.size(); i++) { // Do binary search for the element in // the range from begin to begin + length auto b = tail.begin(), e = tail.begin() + length; auto it = lower_bound(b, e, v[i]); // If not present change the tail element to v[i] if (it == tail.begin() + length) tail[length++] = v[i]; else *it = v[i]; } return length; } int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int lis = LongestIncreasingSubsequenceLength(v); //cout << "LIS " << LongestIncreasingSubsequenceLength(v) << endl; //cout << "k: " << endl; if (k > lis) { cout << "TAK" << endl; for (int i = 1; i < k; i++) { cout << i << " "; } cout << endl; return 0; } else { // calculate min vector<int> minimum(n); int min = 1000000001; for (int i = 0; i < n; i++) { if (v[i] < min) { min = v[i]; } minimum[i] = min; //cout << minimum[i] << " "; } //cout << endl; for (int i = 1; i < n; i++) { if (minimum[i - 1] >= v[i]) { //cout << "smaller or equal detected! we can win if we have k > 2" << endl; if (k > 2) { // wrap current element in two segments on sides cout << "TAK" << endl; int shift = 1; // already wrapped //cout << i << " " << k << endl; if (i < k + 1) { } else if (i == k + 1) { // wrapped from left, save 1 shift++; } else { // not wrapped, save 2 shift += 2; } //cout << shift << endl; for (int i = 1 + shift; i < k + shift; i++) { cout << i << " "; } cout << endl; return 0; } else if (k == 2 || i == n - 1) { // last element // just add 1-element segment at the end cout << "TAK" << endl; for (int i = n - 1 - k; i < n; i++) { cout << i << " "; } cout << endl; return 0; } } } } cout << "NIE" << endl; 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 | #include <vector> #include <iostream> using namespace std; int LongestIncreasingSubsequenceLength(std::vector<int>& v) { if (v.size() == 0) // boundary case return 0; std::vector<int> tail(v.size(), 0); int length = 1; // always points empty slot in tail tail[0] = v[0]; for (int i = 1; i < v.size(); i++) { // Do binary search for the element in // the range from begin to begin + length auto b = tail.begin(), e = tail.begin() + length; auto it = lower_bound(b, e, v[i]); // If not present change the tail element to v[i] if (it == tail.begin() + length) tail[length++] = v[i]; else *it = v[i]; } return length; } int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int lis = LongestIncreasingSubsequenceLength(v); //cout << "LIS " << LongestIncreasingSubsequenceLength(v) << endl; //cout << "k: " << endl; if (k > lis) { cout << "TAK" << endl; for (int i = 1; i < k; i++) { cout << i << " "; } cout << endl; return 0; } else { // calculate min vector<int> minimum(n); int min = 1000000001; for (int i = 0; i < n; i++) { if (v[i] < min) { min = v[i]; } minimum[i] = min; //cout << minimum[i] << " "; } //cout << endl; for (int i = 1; i < n; i++) { if (minimum[i - 1] >= v[i]) { //cout << "smaller or equal detected! we can win if we have k > 2" << endl; if (k > 2) { // wrap current element in two segments on sides cout << "TAK" << endl; int shift = 1; // already wrapped //cout << i << " " << k << endl; if (i < k + 1) { } else if (i == k + 1) { // wrapped from left, save 1 shift++; } else { // not wrapped, save 2 shift += 2; } //cout << shift << endl; for (int i = 1 + shift; i < k + shift; i++) { cout << i << " "; } cout << endl; return 0; } else if (k == 2 || i == n - 1) { // last element // just add 1-element segment at the end cout << "TAK" << endl; for (int i = n - 1 - k; i < n; i++) { cout << i << " "; } cout << endl; return 0; } } } } cout << "NIE" << endl; return 0; } |