#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int tab[N]; int minim[N]; int maxim[N]; int n, k; void case_2(){ minim[1] = tab[1]; for(int i = 2; i <= n; i++) minim[i] = min(minim[i - 1], tab[i]); maxim[n] = tab[n]; for(int i = n - 1; i > 0; i--) maxim[i] = max(maxim[i + 1], tab[i]); for(int i = 1; i < n; i++){ if(minim[i] >= maxim[i + 1]){ cout << "TAK\n" << i << "\n"; return; } } cout << "NIE\n"; } void case_3(){ int minim = tab[1]; for(int i = 1; i < n; i++){ minim = min(tab[i], minim); if(minim >= tab[i + 1]){ cout << "TAK\n"; if(i + 1 == n) cout << "1 " << i << "\n"; else cout << i << " " << i + 1 << "\n"; return; } } int maxim = tab[n]; for(int i = n; i > 1; i--){ maxim = max(maxim, tab[i]); if(tab[i - 1] >= maxim){ cout << "TAK\n"; if(i - 1 == 1) cout << 1 << " " << n - 1 << "\n"; else cout << i - 2 << " " << i - 1 << "\n"; return; } } cout << "NIE\n"; } void case_gen(){ for(int i = 1; i < n; i++){ if(tab[i] >= tab[i + 1]){ cout << "TAK\n"; int wyk = 2; if(i != 1) wyk++; if(i + 1 != n) wyk++; k -= wyk; if(i != 1){ int podzial = min(i - 2, k); k -= podzial; for(int j = 1; j <= podzial; j++){ cout << j << " "; } cout << i - 1 << " "; } cout << i << " "; if(i + 1 != n){ cout << i + 1 << " "; for(int j = 1; j <= k; j++){ cout << i + 1 + j << " "; } } return; } } cout << "NIE\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) cin >> tab[i]; if(k == 2) case_2(); else if(k == 3) case_3(); else case_gen(); }
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 | #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int tab[N]; int minim[N]; int maxim[N]; int n, k; void case_2(){ minim[1] = tab[1]; for(int i = 2; i <= n; i++) minim[i] = min(minim[i - 1], tab[i]); maxim[n] = tab[n]; for(int i = n - 1; i > 0; i--) maxim[i] = max(maxim[i + 1], tab[i]); for(int i = 1; i < n; i++){ if(minim[i] >= maxim[i + 1]){ cout << "TAK\n" << i << "\n"; return; } } cout << "NIE\n"; } void case_3(){ int minim = tab[1]; for(int i = 1; i < n; i++){ minim = min(tab[i], minim); if(minim >= tab[i + 1]){ cout << "TAK\n"; if(i + 1 == n) cout << "1 " << i << "\n"; else cout << i << " " << i + 1 << "\n"; return; } } int maxim = tab[n]; for(int i = n; i > 1; i--){ maxim = max(maxim, tab[i]); if(tab[i - 1] >= maxim){ cout << "TAK\n"; if(i - 1 == 1) cout << 1 << " " << n - 1 << "\n"; else cout << i - 2 << " " << i - 1 << "\n"; return; } } cout << "NIE\n"; } void case_gen(){ for(int i = 1; i < n; i++){ if(tab[i] >= tab[i + 1]){ cout << "TAK\n"; int wyk = 2; if(i != 1) wyk++; if(i + 1 != n) wyk++; k -= wyk; if(i != 1){ int podzial = min(i - 2, k); k -= podzial; for(int j = 1; j <= podzial; j++){ cout << j << " "; } cout << i - 1 << " "; } cout << i << " "; if(i + 1 != n){ cout << i + 1 << " "; for(int j = 1; j <= k; j++){ cout << i + 1 + j << " "; } } return; } } cout << "NIE\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) cin >> tab[i]; if(k == 2) case_2(); else if(k == 3) case_3(); else case_gen(); } |