#include <bits/stdc++.h> #define INF 2147483647 #define LINF 9223372036854775807 #define NINF -2147483648 #define NLINF -9223372036854775808 #define M 1000000007 #define M1 998244353 #define K 31 #define P 2137 using namespace std; using db=double; using dbl=long double; using ll=long long; using pi=pair<int,int>; using pl=pair<ll,ll>; using vi=vector<int>; using vl=vector<ll>; using gr=vector<vector<int> >; using grl=vector<vector<ll> >; #define fp(x, a, b) for (int (x) = (a); (x) < (b); (x)++) #define f(x, n) for (int (x) = 0; (x) < (n); (x)++) #define fnp(x, a, b) for (int (x) = (b) - 1; (x) >= (a); (x)--) #define fn(x, n) for (int (x) = (n - 1); (x) >= 0; (x)--) #define sgn(x) (x) > 0 ? 1 : (x) == 0 ? 0 : -1 #define gcd(a, b) __gcd((a), (b)) #define lcm(a, b) (a) * (b) / gcd((a), (b)) #define x first #define y second #define mp make_pair #define pb push_back #define s(x) x.size() #define all(x) x.begin(), x.end() #define ans(x) cout<<(x)<<"\n" #define yes cout<<"TAK\n"; #define no cout<<"NIE\n"; #define fl cout.flush() #define debarr(x, n) f(i, (n)){cout<<(x)[i]<<" ";}cout<<"\n" #define debgr(x, n) f(i, (n)){f(j, s((x)[i])){cout<<(x)[i][j]<<" ";}cout<<"\n";} mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); void input(); void compute(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while(T--) { input(); compute(); } return 0; } #define N 500001 int n, k; int a[N]; deque<pi> B; void push(int x) { int cnt = 0; while(s(B) > 0 && B.back().x >= x) { cnt += B.back().y + 1; B.pop_back(); } B.pb(mp(x, cnt)); } void pop() { if(B.front().y == 0) { B.pop_front(); } else { B.front().y -= 1; } } void input() { cin >> n >> k; f(i, n) { cin >> a[i]; } } void k2() { int A = INF; f(i, n) { push(-a[i]); } f(i, n - 1) { A = min(A, a[i]); pop(); if(A >= -B.front().x) { yes; ans(i + 1); return; } } no; } void k3() { multiset<int> B; f(i, n) { B.insert(a[i]); } int begin = *B.begin(); int end = *(--B.end()); int countBegin = B.count(*B.begin()); multiset<int>::iterator it = B.end(); it--; int countEnd = B.count(*it); if(a[0] == begin && a[n - 1] == end && countBegin + countEnd == 2) { no; return; } yes; if(a[0] == end || a[n - 1] == begin) { cout<<1<<" "<<n - 1<<"\n"; return; } int d = (begin != a[0] || countBegin > 1) ? begin : end; fp(i, 1, n - 1) { if(a[i] == d) { cout<<i<<" "<<i + 1<<"\n"; return; } } } void k4() { vi good; vi an; f(i, n - 1) { if(a[i] >= a[i + 1]) { k--; if(i > 0) { good.pb(i); an.pb(i); k--; } if(i + 1 < n) { good.pb(i + 1); an.pb(i + 1); k--; } if(i + 2 < n) { good.pb(i + 2); an.pb(i + 2); k--; } yes; if(k > 0) { fp(j, 1, n) { if(good[0] != j && good[1] != j && (s(good) <= 2 || (s(good) > 2 && good[2] != j))) { an.pb(j); k--; if(k == 0) { break; } } } } sort(all(an)); f(j, s(an)) { cout<<an[j]<<" "; } return; } } no; } void compute() { switch(k) { case 2: k2(); break; case 3: k3(); break; default: k4(); break; } }
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | #include <bits/stdc++.h> #define INF 2147483647 #define LINF 9223372036854775807 #define NINF -2147483648 #define NLINF -9223372036854775808 #define M 1000000007 #define M1 998244353 #define K 31 #define P 2137 using namespace std; using db=double; using dbl=long double; using ll=long long; using pi=pair<int,int>; using pl=pair<ll,ll>; using vi=vector<int>; using vl=vector<ll>; using gr=vector<vector<int> >; using grl=vector<vector<ll> >; #define fp(x, a, b) for (int (x) = (a); (x) < (b); (x)++) #define f(x, n) for (int (x) = 0; (x) < (n); (x)++) #define fnp(x, a, b) for (int (x) = (b) - 1; (x) >= (a); (x)--) #define fn(x, n) for (int (x) = (n - 1); (x) >= 0; (x)--) #define sgn(x) (x) > 0 ? 1 : (x) == 0 ? 0 : -1 #define gcd(a, b) __gcd((a), (b)) #define lcm(a, b) (a) * (b) / gcd((a), (b)) #define x first #define y second #define mp make_pair #define pb push_back #define s(x) x.size() #define all(x) x.begin(), x.end() #define ans(x) cout<<(x)<<"\n" #define yes cout<<"TAK\n"; #define no cout<<"NIE\n"; #define fl cout.flush() #define debarr(x, n) f(i, (n)){cout<<(x)[i]<<" ";}cout<<"\n" #define debgr(x, n) f(i, (n)){f(j, s((x)[i])){cout<<(x)[i][j]<<" ";}cout<<"\n";} mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); void input(); void compute(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while(T--) { input(); compute(); } return 0; } #define N 500001 int n, k; int a[N]; deque<pi> B; void push(int x) { int cnt = 0; while(s(B) > 0 && B.back().x >= x) { cnt += B.back().y + 1; B.pop_back(); } B.pb(mp(x, cnt)); } void pop() { if(B.front().y == 0) { B.pop_front(); } else { B.front().y -= 1; } } void input() { cin >> n >> k; f(i, n) { cin >> a[i]; } } void k2() { int A = INF; f(i, n) { push(-a[i]); } f(i, n - 1) { A = min(A, a[i]); pop(); if(A >= -B.front().x) { yes; ans(i + 1); return; } } no; } void k3() { multiset<int> B; f(i, n) { B.insert(a[i]); } int begin = *B.begin(); int end = *(--B.end()); int countBegin = B.count(*B.begin()); multiset<int>::iterator it = B.end(); it--; int countEnd = B.count(*it); if(a[0] == begin && a[n - 1] == end && countBegin + countEnd == 2) { no; return; } yes; if(a[0] == end || a[n - 1] == begin) { cout<<1<<" "<<n - 1<<"\n"; return; } int d = (begin != a[0] || countBegin > 1) ? begin : end; fp(i, 1, n - 1) { if(a[i] == d) { cout<<i<<" "<<i + 1<<"\n"; return; } } } void k4() { vi good; vi an; f(i, n - 1) { if(a[i] >= a[i + 1]) { k--; if(i > 0) { good.pb(i); an.pb(i); k--; } if(i + 1 < n) { good.pb(i + 1); an.pb(i + 1); k--; } if(i + 2 < n) { good.pb(i + 2); an.pb(i + 2); k--; } yes; if(k > 0) { fp(j, 1, n) { if(good[0] != j && good[1] != j && (s(good) <= 2 || (s(good) > 2 && good[2] != j))) { an.pb(j); k--; if(k == 0) { break; } } } } sort(all(an)); f(j, s(an)) { cout<<an[j]<<" "; } return; } } no; } void compute() { switch(k) { case 2: k2(); break; case 3: k3(); break; default: k4(); break; } } |