#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <unordered_set> #include <stack> using namespace std; long long int t[500001]; long long int maxR[500001]; long long int minR[500001]; stack<long long int> S; string brut(long long int* tab, int n, int k, int start, long long int last_min, long long int last_max, long long int * maxR){ if(k==1) { //cout << " k: " << k << " start: " << start << " NIE" << "\n"; if(!S.empty()){ S.pop(); } return "NIE"; } long long int tmpMin = tab[start]; long long int tmpMax = tab[start]; for(int i = start; i < n-k+1; ++i){ //cout << "tmpMax: " << tmpMax << " last_min: " << last_min << " tmpMin: " << tmpMin << " maxR[i]: " << maxR[i] << "\n"; if(tmpMax <= last_min || tmpMin >= maxR[i]){ S.push(i); //cout << " k: " << k << " i: " << i << " TAK" << "\n"; return "TAK"; }else{ //cout << "HMMM" << "\n"; if(tmpMin > tab[i]){ tmpMin = tab[i]; } if(tmpMax < tab[i]){ tmpMax = tab[i]; } string res = brut(tab, n, k-1, i+1, tmpMin, tmpMax, maxR); if(res == "TAK"){ S.push(i); //cout << " k: " << k << " i: " << i << " TAK" << "\n"; return "TAK"; } } } //cout << " k: " << k << " i: " << start << " NIE" << "\n"; if(!S.empty()){ S.pop(); } return "NIE"; } int main(){ int n = 0; cin >> n; int k = 0; cin >> k; for(int i = 0; i < n; ++i){ cin >> t[i]; } /*if(k == 2){ if(t[0] < t[n-1]){ cout << "NIE\n"; } }*/ maxR[n-1]=t[n-1]; minR[n-1]=t[n-1]; for(int i = n-2; i >= 0; --i){ if(t[i] < minR[i+1]){ minR[i]=t[i]; maxR[i]=maxR[i+1]; }else if (t[i] > maxR[i+1]){ maxR[i]=t[i]; minR[i]=minR[i+1]; }else{ minR[i]=minR[i+1]; maxR[i]=maxR[i+1]; } } string result = brut(t, n, k, 0, 0, 0, maxR); cout << result << "\n"; if(result == "TAK"){ int last = -1; while(!S.empty()){ last = S.top(); cout << (last+1) << " "; S.pop(); k--; } while(k > 1){ last++; cout << (last+1) << " "; k--; } cout << "\n"; } 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <unordered_set> #include <stack> using namespace std; long long int t[500001]; long long int maxR[500001]; long long int minR[500001]; stack<long long int> S; string brut(long long int* tab, int n, int k, int start, long long int last_min, long long int last_max, long long int * maxR){ if(k==1) { //cout << " k: " << k << " start: " << start << " NIE" << "\n"; if(!S.empty()){ S.pop(); } return "NIE"; } long long int tmpMin = tab[start]; long long int tmpMax = tab[start]; for(int i = start; i < n-k+1; ++i){ //cout << "tmpMax: " << tmpMax << " last_min: " << last_min << " tmpMin: " << tmpMin << " maxR[i]: " << maxR[i] << "\n"; if(tmpMax <= last_min || tmpMin >= maxR[i]){ S.push(i); //cout << " k: " << k << " i: " << i << " TAK" << "\n"; return "TAK"; }else{ //cout << "HMMM" << "\n"; if(tmpMin > tab[i]){ tmpMin = tab[i]; } if(tmpMax < tab[i]){ tmpMax = tab[i]; } string res = brut(tab, n, k-1, i+1, tmpMin, tmpMax, maxR); if(res == "TAK"){ S.push(i); //cout << " k: " << k << " i: " << i << " TAK" << "\n"; return "TAK"; } } } //cout << " k: " << k << " i: " << start << " NIE" << "\n"; if(!S.empty()){ S.pop(); } return "NIE"; } int main(){ int n = 0; cin >> n; int k = 0; cin >> k; for(int i = 0; i < n; ++i){ cin >> t[i]; } /*if(k == 2){ if(t[0] < t[n-1]){ cout << "NIE\n"; } }*/ maxR[n-1]=t[n-1]; minR[n-1]=t[n-1]; for(int i = n-2; i >= 0; --i){ if(t[i] < minR[i+1]){ minR[i]=t[i]; maxR[i]=maxR[i+1]; }else if (t[i] > maxR[i+1]){ maxR[i]=t[i]; minR[i]=minR[i+1]; }else{ minR[i]=minR[i+1]; maxR[i]=maxR[i+1]; } } string result = brut(t, n, k, 0, 0, 0, maxR); cout << result << "\n"; if(result == "TAK"){ int last = -1; while(!S.empty()){ last = S.top(); cout << (last+1) << " "; S.pop(); k--; } while(k > 1){ last++; cout << (last+1) << " "; k--; } cout << "\n"; } return 0; } |