#include <bits/stdc++.h> using namespace std; int n,k; int arr[500005]; void solveForKEq2() { int minInFirst=arr[0]; int minInCurrent = arr[1]; int lastIndexOfFirstGroup=0; for(int i=1;i<n;i++) { minInCurrent = min(minInCurrent, arr[i]); if(arr[i]>minInFirst) { lastIndexOfFirstGroup=i; minInFirst = min(minInCurrent, minInFirst); } } if(lastIndexOfFirstGroup==n-1) cout<<"NIE"<<endl; else cout<<"TAK"<<endl<<lastIndexOfFirstGroup+1<<endl; } int solveForKEq3OrHigher() { for(int i=1;i<n;i++) { // //cerr<<i<<" "<<n<<" "<<arr[i]<<" "<<arr[0]<<endl; if(arr[i]<=arr[0]) { // //cerr<<"case 2 "<<i<<" "<<arr[i]<<" "<<arr[0]<<endl; cout<<"TAK"<<endl; for(int j=1;j<=k-3 && j<i;j++) cout<<j<<" "; cout<<i<<" "<<i+1<<" "; for(int j=i+2;j<k;j++) cout<<j<<" "; cout<<endl; return 0; } } for(int i=n-2;i>0;i--) { // //cerr<<i<<" "<<n<<" "<<arr[i]<<" "<<arr[0]<<endl; if(arr[i]>=arr[n-1]) { // //cerr<<"case 2 "<<i<<" "<<arr[i]<<" "<<arr[0]<<endl; cout<<"TAK"<<endl; for(int j=1;j<=k-3 && j<i;j++) cout<<j<<" "; cout<<i<<" "<<i+1<<" "; for(int j=i+2;j<k;j++) cout<<j<<" "; cout<<endl; return 0; } } cout<<"NIE"<<endl; return 0; return 0; } int solveForKEq4OrHigher() { int currentMax = arr[0]; int currentMaxIndex = 0; int result1; int result2; for(int i=1;i<n;i++) { if(arr[i]>currentMax) { currentMax = arr[i]; currentMaxIndex = i; //cerr<<"tu-0"<<endl; } else if(arr[i] <= currentMax) { result1=currentMaxIndex; result2=i; cout<<"TAK"<<endl; for(int j=1;j<k-4 &&j<result1;j++) { //cerr<<"tu1"<<endl; cout<<j<<" "; } if(result1>0) { cout<<result1<<" "<<result1+1<<" "; //cerr<<"tu2"<<endl; } else { //cerr<<"tu3"<<endl; cout<<1<<" "; } for(int j=result1+2;j<k-3 &&j<result2;j++) { cout<<j<<" "; //cerr<<"tu4"<<endl; } if(result2==n-1 && result2==result1+1) { //cerr<<"tu5"<<endl; ; } else if(result2==n-1 && result2!=result1+1) { cout<<result2<<" "; //cerr<<"tu6"<<endl; } else if(result2==result1+1) { cout<<result2+1<<" "; //cerr<<"tu7"<<endl; } else { cout<<result2<<" "<<result2+1<<" "; //cerr<<"tu8"<<endl; } for(int j=result2+2;j<k;j++) { //cerr<<"tu9"<<endl; cout<<j<<" "; } return 0; } } cout<<"NIE"<<endl; return 0; } int main() { ios_base::sync_with_stdio(0); cin>>n>>k; bool areAsc=true; cin>>arr[0]; for(int i=1;i<n;i++) { cin>>arr[i]; if(arr[i]<=arr[i-1]) areAsc=false; } if(areAsc) { cout<<"NIE"<<endl; return 0; } if(k==2) { solveForKEq2(); } else if(k==3) { solveForKEq3OrHigher(); } else {solveForKEq4OrHigher();} 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <bits/stdc++.h> using namespace std; int n,k; int arr[500005]; void solveForKEq2() { int minInFirst=arr[0]; int minInCurrent = arr[1]; int lastIndexOfFirstGroup=0; for(int i=1;i<n;i++) { minInCurrent = min(minInCurrent, arr[i]); if(arr[i]>minInFirst) { lastIndexOfFirstGroup=i; minInFirst = min(minInCurrent, minInFirst); } } if(lastIndexOfFirstGroup==n-1) cout<<"NIE"<<endl; else cout<<"TAK"<<endl<<lastIndexOfFirstGroup+1<<endl; } int solveForKEq3OrHigher() { for(int i=1;i<n;i++) { // //cerr<<i<<" "<<n<<" "<<arr[i]<<" "<<arr[0]<<endl; if(arr[i]<=arr[0]) { // //cerr<<"case 2 "<<i<<" "<<arr[i]<<" "<<arr[0]<<endl; cout<<"TAK"<<endl; for(int j=1;j<=k-3 && j<i;j++) cout<<j<<" "; cout<<i<<" "<<i+1<<" "; for(int j=i+2;j<k;j++) cout<<j<<" "; cout<<endl; return 0; } } for(int i=n-2;i>0;i--) { // //cerr<<i<<" "<<n<<" "<<arr[i]<<" "<<arr[0]<<endl; if(arr[i]>=arr[n-1]) { // //cerr<<"case 2 "<<i<<" "<<arr[i]<<" "<<arr[0]<<endl; cout<<"TAK"<<endl; for(int j=1;j<=k-3 && j<i;j++) cout<<j<<" "; cout<<i<<" "<<i+1<<" "; for(int j=i+2;j<k;j++) cout<<j<<" "; cout<<endl; return 0; } } cout<<"NIE"<<endl; return 0; return 0; } int solveForKEq4OrHigher() { int currentMax = arr[0]; int currentMaxIndex = 0; int result1; int result2; for(int i=1;i<n;i++) { if(arr[i]>currentMax) { currentMax = arr[i]; currentMaxIndex = i; //cerr<<"tu-0"<<endl; } else if(arr[i] <= currentMax) { result1=currentMaxIndex; result2=i; cout<<"TAK"<<endl; for(int j=1;j<k-4 &&j<result1;j++) { //cerr<<"tu1"<<endl; cout<<j<<" "; } if(result1>0) { cout<<result1<<" "<<result1+1<<" "; //cerr<<"tu2"<<endl; } else { //cerr<<"tu3"<<endl; cout<<1<<" "; } for(int j=result1+2;j<k-3 &&j<result2;j++) { cout<<j<<" "; //cerr<<"tu4"<<endl; } if(result2==n-1 && result2==result1+1) { //cerr<<"tu5"<<endl; ; } else if(result2==n-1 && result2!=result1+1) { cout<<result2<<" "; //cerr<<"tu6"<<endl; } else if(result2==result1+1) { cout<<result2+1<<" "; //cerr<<"tu7"<<endl; } else { cout<<result2<<" "<<result2+1<<" "; //cerr<<"tu8"<<endl; } for(int j=result2+2;j<k;j++) { //cerr<<"tu9"<<endl; cout<<j<<" "; } return 0; } } cout<<"NIE"<<endl; return 0; } int main() { ios_base::sync_with_stdio(0); cin>>n>>k; bool areAsc=true; cin>>arr[0]; for(int i=1;i<n;i++) { cin>>arr[i]; if(arr[i]<=arr[i-1]) areAsc=false; } if(areAsc) { cout<<"NIE"<<endl; return 0; } if(k==2) { solveForKEq2(); } else if(k==3) { solveForKEq3OrHigher(); } else {solveForKEq4OrHigher();} return 0; } |