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. 【转】jQuery插件之ajaxFileUpload

    转自:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 说在前头,本文出自上面的作者,只是以前存的一些网址不见了,怕以后 ...

  2. Architecture Patterns

    This chapter provides guidelines for using architecture patterns. Introduction Patterns for system a ...

  3. VSCode隐藏node_modules目录

    使用VSCode,打开一个工程时,发现node_modules目录包含到工程中了,问题: settings.json配置如下,可以自己定制忽略的文件夹: search.exclude 用来忽略搜索的文 ...

  4. 树莓派打造对话机器人 Python(转)

    工具列表 1. **树莓派**(型号不要求,本人使用的是3B) 2. **usb麦克风**(某宝有卖,我就不打广告了) 用来录音 3. **音响或者喇叭**(某宝也有卖) 用来播放 以上就是需要的工具 ...

  5. com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction

    打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...

  6. CMD mysql 备份脚本

    创建.bat文件 echo. echo MySQL数据库备份脚本 echo ***************************** echo. echo 备份日期:%date% echo 备份时间 ...

  7. 发送ajax请求实现上传图片显示在网页上

    <?php // 1,通过超全局变量来获取files[上传的图片名称] $file = $_FILES["files"] // 2,在通过strrchr来获取图片的格式 $e ...

  8. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  9. How to Google

    程序员的基础生存技能 -- 关于搜索引擎的小贴士 如果票选近二十年最伟大的发明,我相信搜索引擎肯定会占据一个不容小觑的位置,它不单是一项发明,更是一项成就,最大程度消灭了信息的不平等.既然人人都可以接 ...

  10. JSP DAO(Model)

    示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...