#include #include #include #include using namespace std; int main(){ int t,m; int *result;//The test result array scanf("%d",&t);//Get the test case number result=(int*)malloc(sizeof(int)*t);//allocate memory for (int i = 0; i < t; i++) { result[i]=1;//intialise with 1 int n,*guest; string chain;//binary string scanf("%d",&n);// the number of hats if(n>15){ std::cout<<"invalid"; return 0; } guest=(int*)malloc(sizeof(int)*n);//allocate m+=n; int current;//current means the first hat number for (int j = 0; j < n; j++) { scanf("%d",&guest;[j]);//get the input of hat number } current=guest[0]; std::cin>>chain;//get the binary string for (int j = 0; j < n; j++) { if (chain[j]=='1') { //check if it goes with the current guest if (current==(j+1)) { // if it does then shift the next hat current=guest[j+1]; }else{ // if it doen't then put 0 for this test result[i]=0; } }else{ if (chain[current-1]!='1') { //distribute if the current hat is not necessary for the upcoming customer, required to be happy current=guest[j+1]; } } } free(guest); } if(m>1000){ std::cout<<"Invalid"; return 0; } for (int r = 0; r < t; r++) { std::cout<<(result[r]==0?"NO":"YES");//print the test case result } free(result); return 0; }