-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
75 lines (58 loc) · 1.5 KB
/
C.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main(){
fast;
ll n;
cin>>n;
ll arr[n];
for(ll i=0;i<n;i++)
cin>>arr[i];
string s;
cin>>s;
vector<ll> v[n];
for(ll i=0;i<n-1;i++){
if(s[i]=='1'){
v[i].push_back(i+1);
v[i+1].push_back(i);
}
}
ll vis[n] = {0};
for(ll i=0;i<n;i++){
if(!vis[i]){
vector<ll> indices;
vector<ll> values;
queue<ll> q;
q.push(i);
vis[i] = 1;
while(!q.empty()){
ll val = q.front();
q.pop();
indices.push_back(val);
values.push_back(arr[val]);
for(ll u: v[val]){
if(!vis[u]){
q.push(u);
vis[u] = 1;
}
}
}
sort(indices.begin(), indices.end());
sort(values.begin(), values.end());
ll sz=indices.size();
for(ll j=0;j<sz;j++)
arr[indices[j]] = values[j];
}
}
ll flag=0;
for(ll i=1;i<n;i++){
if(arr[i]<arr[i-1]){
flag = 1;
break;
}
}
cout<< ( flag ? "NO" : "YES" ) <<endl;
return 0;
}