#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937_64 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N = 6e5+5; int n, k, a[N], dp[N], from[N]; int main() { scanf("%d%d", &n, &k); rep(i, 0, n) scanf("%d", &a[i]); int mn = INT_MAX, mx = INT_MIN, ans = INT_MAX, good = -1, good_mn, good_id = -1, ans_from, mx_from; fill(from, from + n, -1); rep(i, 0, n) { if (ans == INT_MAX) { dp[i] = INT_MAX; from[i] = -1; } else { dp[i] = ans + 1; from[i] = ans_from; } if (good < a[i] && mn != INT_MAX && mn >= a[i]) good = a[i], good_mn = mn, good_id = i; if (good != -1 && good_mn < a[i]) good = -1; if (good != -1 && dp[i] > 2) { from[i] = good_id - 1; dp[i] = 2; } if (mx >= a[i] && dp[i] > 3) { from[i] = mx_from; dp[i] = 3; } mn = min(mn, a[i]); if (mx <= a[i]) { mx = a[i]; mx_from = i; if (i != n - 1) from[i] = i - 1; } if (ans >= dp[i]) { ans = dp[i]; ans_from = i; } } set<int> res; VI res_v; if (dp[n - 1] <= k) { puts("TAK"); int ptr = n - 1; for (int i = 0; i < dp[n - 1] - 1; ++i) { res.insert(from[ptr]); ptr = from[ptr]; } rep(i, 0, n) { if (dp[n - 1] == k) break; if (!res.count(i)) res.insert(i), ++dp[n - 1]; } for (auto p : res) res_v.pb(p); reverse(all(res_v)); for (auto i : res) printf("%d ", i + 1); puts(""); } else puts("NIE"); }
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 | #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937_64 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N = 6e5+5; int n, k, a[N], dp[N], from[N]; int main() { scanf("%d%d", &n, &k); rep(i, 0, n) scanf("%d", &a[i]); int mn = INT_MAX, mx = INT_MIN, ans = INT_MAX, good = -1, good_mn, good_id = -1, ans_from, mx_from; fill(from, from + n, -1); rep(i, 0, n) { if (ans == INT_MAX) { dp[i] = INT_MAX; from[i] = -1; } else { dp[i] = ans + 1; from[i] = ans_from; } if (good < a[i] && mn != INT_MAX && mn >= a[i]) good = a[i], good_mn = mn, good_id = i; if (good != -1 && good_mn < a[i]) good = -1; if (good != -1 && dp[i] > 2) { from[i] = good_id - 1; dp[i] = 2; } if (mx >= a[i] && dp[i] > 3) { from[i] = mx_from; dp[i] = 3; } mn = min(mn, a[i]); if (mx <= a[i]) { mx = a[i]; mx_from = i; if (i != n - 1) from[i] = i - 1; } if (ans >= dp[i]) { ans = dp[i]; ans_from = i; } } set<int> res; VI res_v; if (dp[n - 1] <= k) { puts("TAK"); int ptr = n - 1; for (int i = 0; i < dp[n - 1] - 1; ++i) { res.insert(from[ptr]); ptr = from[ptr]; } rep(i, 0, n) { if (dp[n - 1] == k) break; if (!res.count(i)) res.insert(i), ++dp[n - 1]; } for (auto p : res) res_v.pb(p); reverse(all(res_v)); for (auto i : res) printf("%d ", i + 1); puts(""); } else puts("NIE"); } |