You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.

For some indices i (1 ≤ i ≤ n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i + 1)-th (if the position is not forbidden).

Can you make this array sorted in ascending order performing some sequence of swapping operations?

Input

The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200000) — the elements of the array. Each integer from 1 to n appears exactly once.

The third line contains a string of n - 1 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i + 1)-th any number of times, otherwise it is forbidden to swap i-th element with (i + 1)-th.

Output

If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.

Example

Input
6
1 2 5 3 4 6
01110
Output
YES
Input
6
1 2 5 3 4 6
01010
Output
NO

Note

In the first example you may swap a3 and a4, and then swap a4 and a5.

因为n个元素是从1都n所以正常的顺序应该是第i个元素就是i所以如果第i个元素的值是i+k那么,肯定要把i+k从i通过对换移动到i+k位置,所以需要i到i+k-1位置都可以对换,只需要判断元素比下标值大的,或者元素比下标值小的,如果其中任何一方归位了,另一方也就归位了。一开始直接for循环判断,超时了,又注意到是区间记录,用了个树状数组。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath> using namespace std;
int s[],n,d;
char str[];
int digit(int t)
{
return t&-t;
}
void update(int x,int y)
{
while(x <= n)
{
s[x] += y;
x += digit(x);
}
}
int get(int x)
{
int ans = ;
while(x > )
{
ans += s[x];
x -= digit(x);
}
return ans;
}
int check()
{
int d = ;
for(int i = ;i < n;i ++)
{
d = get(i);
if(d && str[i - ] == '')return ;
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i = ;i <= n;i ++)
{
scanf("%d",&d);
if(d > i)
{
update(i,);
update(d,-);
}
}
scanf("%s",str);
if(check())cout<<"YES";
else cout<<"NO";
}

Swap Adjacent Elements的更多相关文章

  1. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  2. CodeForces - 920C Swap Adjacent Elements

    传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...

  3. 【Educational Codeforces Round 37 C】 Swap Adjacent Elements

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然l..r这一段连续的1可以把l..r+1变成有序的. 那么就把所有的连续1段变成有序的就好. 看看最后是不是升序即可. [代码] ...

  4. CF920C Swap Adjacent Elements 贪心

    我也不知道该说啥,水就是了~ code: #include <bits/stdc++.h> #define N 300004 #define setIO(s) freopen(s" ...

  5. [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

    In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...

  6. [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项

    In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...

  7. CF-920C-Swap Adjacent Elements 贪心

    题意 给你一个1-n的排列. 并给你一个字符串——其中用0和1表示对应数列中的位置上的值可不可以和后面相邻的数交换. 判断该数列能否在限制中交换为不降序数列. 思路 由于刚学了树状数组,一开始以为是用 ...

  8. 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. $git学习总结系列(2)——远程仓库

    本文主要介绍git本地仓库和GitHub远程仓库之间的交互和数据传输. 注:首先需要到github.com上注册一个账号. 1. 添加本地SSH Key到GitHub 要向GitHub远程仓库推送代码 ...

  2. win10系统修改Intel VT-x时进入不了BIOS问题

    一般电脑进入BIOS的方式都是在开机的时候不停的按F2或者F12,但是Win10系统由于支持快速启动,当win10系统快速启动的时候,按F12或者F2是没反应的,解决方式: 第一步:修改win10的启 ...

  3. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  4. BZOJ-1396: 识别子串

    后缀自动机+线段树 先建出\(sam\),统计一遍每个点的\(right\)集合大小\(siz\),对于\(siz=1\)的点\(x\),他所代表的子串只会出现一次,设\(y=fa[x]\),则这个点 ...

  5. awk实现替换字符串中指定位置之间的内容

    # 显示第xx行的第yy列的一个字符sed -n 'xx,xxp' file | awk '{print substr($0,yy,1);}' # 修改第xx行的第yy列的一个字符Chr,并保存为新文 ...

  6. 基于Spring mvc 的Websocket 开发

    1.Pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>sp ...

  7. 导出android真机上应用的apk文件

    1. 首先你的手机要开启调试模式 2. 终端输入命令行 (这个时候需要在手机端打开此应用.它的思路是抓取出当前窗口的包名.以下命令操作自己未亲自验证.) adb shell dumpsys windo ...

  8. Difference between stem and lemma

    lemma与stem的区别 Difference between stem and lemma 先从wikipedia上看看什么是stem,什么是lemma? Lemma(morphology):In ...

  9. ifconfig源代码分析

    一.ifconfig显示 [root@10g-host4 new]# ifconfig eth0 Link encap:Ethernet HWaddr 00:26:B9:4A:FC:EA inet a ...

  10. hdoj1006--Tick and Tick

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...