#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int,int> #define iii tuple<int,int,int> #define fi first #define se second #define endl '\n' #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n; int arr[1000005]; bool solve(int l,int r){ vector<int> v(r-l+1); rep(x,l,r+1) v[x-l]=arr[x]; v.pub(0); rep(x,0,sz(v)-1){ if (v[x]>v[x+1]) return false; v[x+1]-=v[x]; } return true; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); int TC; cin>>TC; while (TC--){ cin>>n; rep(x,1,n+1) cin>>arr[x]; int l=-1,r=-1; rep(x,1,n+1) if (arr[x]) r=x; rep(x,n+1,1) if (arr[x]) l=x; rep(x,l,r+1) arr[x]--; bool can=false; can|=solve(l,r); arr[l+1]--; can|=solve(l,r); arr[r-1]--; can|=solve(l,r); arr[l+1]++; can|=solve(l,r); if (can) cout<<"TAK"<<endl; else cout<<"NIE"<<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 | #include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int,int> #define iii tuple<int,int,int> #define fi first #define se second #define endl '\n' #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n; int arr[1000005]; bool solve(int l,int r){ vector<int> v(r-l+1); rep(x,l,r+1) v[x-l]=arr[x]; v.pub(0); rep(x,0,sz(v)-1){ if (v[x]>v[x+1]) return false; v[x+1]-=v[x]; } return true; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); int TC; cin>>TC; while (TC--){ cin>>n; rep(x,1,n+1) cin>>arr[x]; int l=-1,r=-1; rep(x,1,n+1) if (arr[x]) r=x; rep(x,n+1,1) if (arr[x]) l=x; rep(x,l,r+1) arr[x]--; bool can=false; can|=solve(l,r); arr[l+1]--; can|=solve(l,r); arr[r-1]--; can|=solve(l,r); arr[l+1]++; can|=solve(l,r); if (can) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } } |