#include<iostream> #include<vector> using namespace std; long long f2(vector<long long>&v, long long n){ vector<long long>p(n),t(n); p[0]=v[0]; for(long long i=1;i<n;i++){ p[i]=min(p[i-1],v[i]); } t[n-1]=v[n-1]; for(long long i=n-2;i>=0;i--){ t[i]=max(t[i+1],v[i]); } for(long long i=0;i<n-1;i++){ if(p[i]>=t[i+1]){return i;} } return -1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long n,k; cin >> n >> k; //cout<<n<<" "<<k<<endl; vector<long long>v(n); for(auto&i:v){cin>>i;/*cout<<i<<" ";*/} //cout<<endl; if(k==2){ long long f=f2(v,n); if(f==-1){cout<<"NIE\n";} else{cout<<"TAK\n"<<f+1<<endl;} return 0; } if(k==3){ vector<long long>pmx(n); pmx[0]=v[0]; for(long long i=1;i<n;i++){pmx[i]=max(pmx[i-1],v[i]);} long long w=n-1; while(true){ if(w>0 && pmx[w]>pmx[w-1]){w--;} else if(w==0){cout<<"NIE\n";return 0;} else{break;} } if(w==n-1){ if(pmx[0]==pmx[n-1]){cout<<"TAK\n 1 2\n";return 0;} for(long long i=1;i<n;i++){ if(v[i]==pmx[n-1]){cout<<"TAK\n"<<i<<" "<<i+1<<endl;return 0;} } } long long f=f2(v,w+1); if(f==-1){cout<<"NIE\n";return 0;} cout<<"TAK\n"<<f+1<<" "<<w+1<<endl; return 0; } vector<long long>pmx(n); vector<bool>out(n); pmx[0]=v[0]; for(long long i=1;i<n;i++){pmx[i]=max(pmx[i-1],v[i]);} long long w=n-1; while(true){ if(w>0 && pmx[w]>pmx[w-1]){w--;} else if(w==0){ cout<<"NIE\n"; return 0; } else{break;} } if(w==n-1){ if(pmx[0]==pmx[n-1]){ cout<<"TAK\n"; for(long long i=0;i<k-1;i++){cout<<i+1<<" ";} cout<<endl; } else{ for(long long i=0;i<n;i++){ if(v[i]==pmx[n-1]){out[i]=true;out[i+1]=true;break;} } for(long long i=0;i<k-3;i++){ if(out[i]){k++;} else{out[i]=true;} } cout<<"TAK\n"; for(long long i=0;i<n;i++){ if(out[i]){cout<<i+1<<" ";} } cout<<endl; } } else{ out[w]=true; for(long long i=0;i<=w;i++){ if(v[i]==pmx[w]){out[w]=true;out[i+1]=true;break;} } for(long long i=0;i<k-4;i++){ if(out[i]){k++;} else{out[i]=true;} } cout<<"TAK\n"; for(long long i=0;i<n;i++){ if(out[i]){cout<<i+1<<" ";} } cout<<endl; } }
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 | #include<iostream> #include<vector> using namespace std; long long f2(vector<long long>&v, long long n){ vector<long long>p(n),t(n); p[0]=v[0]; for(long long i=1;i<n;i++){ p[i]=min(p[i-1],v[i]); } t[n-1]=v[n-1]; for(long long i=n-2;i>=0;i--){ t[i]=max(t[i+1],v[i]); } for(long long i=0;i<n-1;i++){ if(p[i]>=t[i+1]){return i;} } return -1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long n,k; cin >> n >> k; //cout<<n<<" "<<k<<endl; vector<long long>v(n); for(auto&i:v){cin>>i;/*cout<<i<<" ";*/} //cout<<endl; if(k==2){ long long f=f2(v,n); if(f==-1){cout<<"NIE\n";} else{cout<<"TAK\n"<<f+1<<endl;} return 0; } if(k==3){ vector<long long>pmx(n); pmx[0]=v[0]; for(long long i=1;i<n;i++){pmx[i]=max(pmx[i-1],v[i]);} long long w=n-1; while(true){ if(w>0 && pmx[w]>pmx[w-1]){w--;} else if(w==0){cout<<"NIE\n";return 0;} else{break;} } if(w==n-1){ if(pmx[0]==pmx[n-1]){cout<<"TAK\n 1 2\n";return 0;} for(long long i=1;i<n;i++){ if(v[i]==pmx[n-1]){cout<<"TAK\n"<<i<<" "<<i+1<<endl;return 0;} } } long long f=f2(v,w+1); if(f==-1){cout<<"NIE\n";return 0;} cout<<"TAK\n"<<f+1<<" "<<w+1<<endl; return 0; } vector<long long>pmx(n); vector<bool>out(n); pmx[0]=v[0]; for(long long i=1;i<n;i++){pmx[i]=max(pmx[i-1],v[i]);} long long w=n-1; while(true){ if(w>0 && pmx[w]>pmx[w-1]){w--;} else if(w==0){ cout<<"NIE\n"; return 0; } else{break;} } if(w==n-1){ if(pmx[0]==pmx[n-1]){ cout<<"TAK\n"; for(long long i=0;i<k-1;i++){cout<<i+1<<" ";} cout<<endl; } else{ for(long long i=0;i<n;i++){ if(v[i]==pmx[n-1]){out[i]=true;out[i+1]=true;break;} } for(long long i=0;i<k-3;i++){ if(out[i]){k++;} else{out[i]=true;} } cout<<"TAK\n"; for(long long i=0;i<n;i++){ if(out[i]){cout<<i+1<<" ";} } cout<<endl; } } else{ out[w]=true; for(long long i=0;i<=w;i++){ if(v[i]==pmx[w]){out[w]=true;out[i+1]=true;break;} } for(long long i=0;i<k-4;i++){ if(out[i]){k++;} else{out[i]=true;} } cout<<"TAK\n"; for(long long i=0;i<n;i++){ if(out[i]){cout<<i+1<<" ";} } cout<<endl; } } |