#include <bits/stdc++.h>
using namespace std;
bool rosmal(vector<int>& a) {
int m = a.size();
if(m == 0) return true;
int kiedy = 0;
while(kiedy+1 < m && a[kiedy] <= a[kiedy+1])
kiedy++;
for (int i = kiedy; i < m-1; i++){
if(a[i] < a[i+1])
return false;
}
return true;
}
bool check(vector<int>& a) {
int n = a.size();
if(n == 0) return true;
vector<int> nz;
for (int i=0; i<n; i++){
if(a[i] > 0)
nz.push_back(i);
}
if(!(nz.size() == n)){
int m = nz.size();
int start = nz[0];
for (int i=0; i<m; i++){
int idx = (start + i) % n;
if(a[idx] == 0) return false;
}
for (int i = m; i < n; i++){
int idx = (start + i) % n;
if(a[idx] > 0) return false;
}
vector<int> b;
for (int i=0; i<m; i++){
int idx = (start + i) % n;
b.push_back(a[idx]-1);
}
if(!rosmal(b))
return false;
return true;
} else {
if(n == 1) return (a[0] == 1);
if(n == 2) return (a[0] == a[1]);
for (int i=0; i<n; i++){
a[i] = a[i] - 1;
}
bool flag = true;
for (int i=0; i<n; i++){
if(a[i] != a[0]) {
flag = false;
break;
}
}
if(flag) return true;
int mn = *min_element(a.begin(), a.end());
int mx = *max_element(a.begin(), a.end());
if(mx - mn == 2){
int cntMax = 0;
for (int i=0; i<n; i++){
if(a[i] == mx) cntMax++;
}
if(cntMax == 1) return true;
}
return false;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> a(n);
long long sum = 0;
for (int i=0; i<n; i++){
cin >> a[i];
sum += a[i];
}
if(check(a)) cout<<"TAK";
else cout<<"NIE";
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 | #include <bits/stdc++.h> using namespace std; bool rosmal(vector<int>& a) { int m = a.size(); if(m == 0) return true; int kiedy = 0; while(kiedy+1 < m && a[kiedy] <= a[kiedy+1]) kiedy++; for (int i = kiedy; i < m-1; i++){ if(a[i] < a[i+1]) return false; } return true; } bool check(vector<int>& a) { int n = a.size(); if(n == 0) return true; vector<int> nz; for (int i=0; i<n; i++){ if(a[i] > 0) nz.push_back(i); } if(!(nz.size() == n)){ int m = nz.size(); int start = nz[0]; for (int i=0; i<m; i++){ int idx = (start + i) % n; if(a[idx] == 0) return false; } for (int i = m; i < n; i++){ int idx = (start + i) % n; if(a[idx] > 0) return false; } vector<int> b; for (int i=0; i<m; i++){ int idx = (start + i) % n; b.push_back(a[idx]-1); } if(!rosmal(b)) return false; return true; } else { if(n == 1) return (a[0] == 1); if(n == 2) return (a[0] == a[1]); for (int i=0; i<n; i++){ a[i] = a[i] - 1; } bool flag = true; for (int i=0; i<n; i++){ if(a[i] != a[0]) { flag = false; break; } } if(flag) return true; int mn = *min_element(a.begin(), a.end()); int mx = *max_element(a.begin(), a.end()); if(mx - mn == 2){ int cntMax = 0; for (int i=0; i<n; i++){ if(a[i] == mx) cntMax++; } if(cntMax == 1) return true; } return false; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ int n; cin >> n; vector<int> a(n); long long sum = 0; for (int i=0; i<n; i++){ cin >> a[i]; sum += a[i]; } if(check(a)) cout<<"TAK"; else cout<<"NIE"; cout<<'\n'; } return 0; } |
English