#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pi pair<int,int> #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define ST first #define ND second #define PB push_back void Solve() { int n, k; cin >> n >> k; vi A(n); for(int i=0; i<n; ++i) cin >> A[i]; if(k==2) { vi MP(n); vi MS(n); MP[0]=A[0]; MS[n-1]=A[n-1]; for(int i=1; i<n; ++i) MP[i]=min(MP[i-1], A[i]); for(int i=n-2; i>=0; --i) MS[i]=max(MS[i+1], A[i]); int en=-1; for(int i=0; i<n-1; ++i) if(MP[i]>=MS[i+1]) en=i; if(en==-1) cout << "NIE\n"; else cout << "TAK\n" << en+1 << "\n"; return; } if(k==3) { bool ok=true; for(int i=1; i<n; ++i) if(A[i]<=A[0]) ok=false; for(int i=0; i<n-1; ++i) if(A[i]>=A[n-1]) ok=false; if(ok) cout << "NIE\n"; else{ cout << "TAK\n"; int min=0; int maxi=n-1; for(int i=1; i<n; ++i) if(A[i]<=A[min]) min=i; for(int i=n-2; i>=0; --i) if(A[i]>=A[maxi]) maxi=i; if(maxi!=n-1) { cout << maxi << ' ' << maxi+1 << "\n"; return; } if(min==n-1) { cout << 1 << ' ' << n-1 << "\n"; return; } cout << min << ' ' << min+1 << "\n"; return; } } if(k>=4) { int inv=-1; for(int i=0; i<n-1; ++i) if(A[i]>=A[i+1]) inv=i; if(inv==-1) { cout << "NIE\n"; return; } cout << "TAK\n"; int left=k-4; if(inv==0 || inv==n-2) ++left; for(int i=0; i<inv-1; ++i) if(left>0) { --left; cout << i+1 << ' '; } if(inv!=0) cout << inv << ' '; cout << ' ' << inv+1 << ' '; if(inv!=n-2) cout << inv+2 << ' '; for(int i=inv+2; i<n; ++i) if(left>0) { --left; cout << i+1 << "\n"; } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; //cin >> t; t=1; while(t--) { Solve(); } 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 | #include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pi pair<int,int> #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define ST first #define ND second #define PB push_back void Solve() { int n, k; cin >> n >> k; vi A(n); for(int i=0; i<n; ++i) cin >> A[i]; if(k==2) { vi MP(n); vi MS(n); MP[0]=A[0]; MS[n-1]=A[n-1]; for(int i=1; i<n; ++i) MP[i]=min(MP[i-1], A[i]); for(int i=n-2; i>=0; --i) MS[i]=max(MS[i+1], A[i]); int en=-1; for(int i=0; i<n-1; ++i) if(MP[i]>=MS[i+1]) en=i; if(en==-1) cout << "NIE\n"; else cout << "TAK\n" << en+1 << "\n"; return; } if(k==3) { bool ok=true; for(int i=1; i<n; ++i) if(A[i]<=A[0]) ok=false; for(int i=0; i<n-1; ++i) if(A[i]>=A[n-1]) ok=false; if(ok) cout << "NIE\n"; else{ cout << "TAK\n"; int min=0; int maxi=n-1; for(int i=1; i<n; ++i) if(A[i]<=A[min]) min=i; for(int i=n-2; i>=0; --i) if(A[i]>=A[maxi]) maxi=i; if(maxi!=n-1) { cout << maxi << ' ' << maxi+1 << "\n"; return; } if(min==n-1) { cout << 1 << ' ' << n-1 << "\n"; return; } cout << min << ' ' << min+1 << "\n"; return; } } if(k>=4) { int inv=-1; for(int i=0; i<n-1; ++i) if(A[i]>=A[i+1]) inv=i; if(inv==-1) { cout << "NIE\n"; return; } cout << "TAK\n"; int left=k-4; if(inv==0 || inv==n-2) ++left; for(int i=0; i<inv-1; ++i) if(left>0) { --left; cout << i+1 << ' '; } if(inv!=0) cout << inv << ' '; cout << ' ' << inv+1 << ' '; if(inv!=n-2) cout << inv+2 << ' '; for(int i=inv+2; i<n; ++i) if(left>0) { --left; cout << i+1 << "\n"; } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; //cin >> t; t=1; while(t--) { Solve(); } return 0; } |