#include <bits/stdc++.h> #define maxn 500005 using namespace std; int a[maxn]; int pref_mini[maxn]; int suff_maks[maxn]; bool wyn; bool out[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for(int i = 1; i <= n; ++i) cin >> a[i]; pref_mini[0] = 1000000005; for(int i = 1; i <= n; ++i) pref_mini[i] = min(pref_mini[i-1], a[i]); suff_maks[n+1] = 0; for(int i = n; i; --i) suff_maks[i] = max(suff_maks[i+1], a[i]); if(k == 2) { for(int i = 1; i < n; ++i) if(pref_mini[i] >= suff_maks[i+1]) { wyn = 1; out[i] = 1; break; } } else if(k == 3) { int mini = pref_mini[n]; int maks = suff_maks[1]; if(a[1] == maks) { wyn = 1; out[1] = out[2] = 1; } if(!wyn) { if(a[n] == mini) { wyn = 1; out[1] = out[n-1] = 1; } } if(!wyn) { for(int i = 2; i < n; ++i) if(a[i] == mini || a[i] == maks) { wyn = 1; out[i-1] = out[i] = 1; break; } } } else // if(k >= 4) { int ile = 0; for(int i = 1; i < n; ++i) if(a[i] >= a[i+1]) { wyn = 1; if(i > 1) ++ile, out[i-1] = 1; ++ile, out[i] = 1; if(i+1 < n) ++ile, out[i+1] = 1; break; } if(wyn) { for(int i = 1; i < n; ++i) { if(ile < k-1 && !out[i]) ++ile, out[i] = 1; } } } if(wyn) { cout << "TAK" << "\n"; for(int i = 1; i < n; ++i) if(out[i]) cout << i << " "; } else cout << "NIE"; 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 | #include <bits/stdc++.h> #define maxn 500005 using namespace std; int a[maxn]; int pref_mini[maxn]; int suff_maks[maxn]; bool wyn; bool out[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for(int i = 1; i <= n; ++i) cin >> a[i]; pref_mini[0] = 1000000005; for(int i = 1; i <= n; ++i) pref_mini[i] = min(pref_mini[i-1], a[i]); suff_maks[n+1] = 0; for(int i = n; i; --i) suff_maks[i] = max(suff_maks[i+1], a[i]); if(k == 2) { for(int i = 1; i < n; ++i) if(pref_mini[i] >= suff_maks[i+1]) { wyn = 1; out[i] = 1; break; } } else if(k == 3) { int mini = pref_mini[n]; int maks = suff_maks[1]; if(a[1] == maks) { wyn = 1; out[1] = out[2] = 1; } if(!wyn) { if(a[n] == mini) { wyn = 1; out[1] = out[n-1] = 1; } } if(!wyn) { for(int i = 2; i < n; ++i) if(a[i] == mini || a[i] == maks) { wyn = 1; out[i-1] = out[i] = 1; break; } } } else // if(k >= 4) { int ile = 0; for(int i = 1; i < n; ++i) if(a[i] >= a[i+1]) { wyn = 1; if(i > 1) ++ile, out[i-1] = 1; ++ile, out[i] = 1; if(i+1 < n) ++ile, out[i+1] = 1; break; } if(wyn) { for(int i = 1; i < n; ++i) { if(ile < k-1 && !out[i]) ++ile, out[i] = 1; } } } if(wyn) { cout << "TAK" << "\n"; for(int i = 1; i < n; ++i) if(out[i]) cout << i << " "; } else cout << "NIE"; return 0; } |