Untitled

 avatar
unknown
c_cpp
10 months ago
1.6 kB
5
Indexable
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	    int n; cin>>n;
	    vector<int> arr(n); 
	    for(int i=0;i<n;i++) cin>>arr[i];
	    string str; cin>>str;
	    
	    vector<int> arrNew(arr.begin(), arr.end());
	    sort(arrNew.begin(), arrNew.end());
	    
	    bool northFoundSt= false, southFoundSt= false;
	    bool iteratingOnSorted= true;
	    
	    bool allSouth= true, allNorth= true;
	    for(int i=0;i<n;i++){
	        
	        if(str[i]=='N') {
	            if(iteratingOnSorted) northFoundSt= true;
	            allSouth= false;
	        }else {
	            if(iteratingOnSorted) southFoundSt= true;
	            allNorth= false;
	        }
	        
	        
	        if(arr[i] != arrNew[i]){
	            iteratingOnSorted= false;
	        }
	    }
	    
	   if(iteratingOnSorted){
	        cout<<0<<endl;
	        continue;
	    }
	    
	    
	    if(str[0] != str[n-1]){
	        cout<<1<<endl;
	        continue;
	    }
	    
	    if(allNorth || allSouth){
	        cout<<-1<<endl;
	        continue;
	    }
	    
	    
	    bool northFoundEd= false, southFoundEd= false;
	    for(int i=n-1;i>=0;i--){
	        if(str[i]=='N') northFoundEd= true;
	        else southFoundEd= true;
	        
	        if(arr[i] != arrNew[i]){
	            break;
	        }
	    }
	    
	    if((northFoundSt && southFoundEd)||(northFoundEd && southFoundSt)){
	        cout<<1<<endl;
	    }else{
	        cout<<2<<endl;
	    }
	    
	    
	}

}
Editor is loading...
Leave a Comment