传送门:点我

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 a1a2, ..., 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.

Examples

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.

题意:

给定1~n的序列(所有数刚好出现一次),然后再给01串,01串中如果是1,则可以交换当前位置和下一个位置的数字。问是否能还原为递增数组。

思路:

如果当前数的01串对应是0,且前一个位置01串也是0,且当前数字与下标不对应,肯定输出NO,因为没法交换

然后对可以交换的部分记录下最大最小值,然后记录下起始位置和终点位置,比较一下就行了。

比如说第一组数据

6
1 2 5 3 4 6
01110

01串第一个是0,对应上去是1,下标也是1,则可以。

然后从2~4都是可以交换的范围(即连续的1,然后后面第一个0)

保留下最大值是4,最小值是2,起始位置2,末尾位置4。一一对应了,就输出YES。

代码:

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int read(){
char c; bool op = ;
while((c = getchar()) < '' || c > '')
if(c == '-') op = ;
int res = c - '';
while((c = getchar()) >= '' && c <= '')
res = res * + c - '';
return op ? -res : res;
}
const int maxn = 1e6+;
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i <= n ; i++)
scanf("%d",&a[i]);
int Max = ,Min = n+;
int flag = ,ardMin;
getchar();
for(int i = ; i <= n - ; i++){
char c;
scanf("%c",&c);
if(flag == && c == ''){
ardMin = i;
}
if(c == '' && flag == ){
Max = max(Max,a[i]);
Min = min(Min,a[i]);
if(ardMin!= Min || Max != i){
return puts("NO"),;
}
Max = ;Min = n+;
flag = ;
}
else if(c == ''){
Max = max(Max,a[i]);
Min = min(Min,a[i]);
flag = ;
}
else if(c == ''&& a[i] != i){
return puts("NO"),;
}
}
return puts("YES"),;
}
/*
6
1 3 2 4 6 5
01000
*/

CodeForces - 920C 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. 【Educational Codeforces Round 37 C】 Swap Adjacent Elements

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

  3. Swap Adjacent Elements

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

  4. CF920C Swap Adjacent Elements 贪心

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

  5. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

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

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

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

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

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

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

  9. Codeforces 1215C. Swap Letters

    传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix} ...

随机推荐

  1. Jenkins配置中安装插件时提示“No such plugin: cloudbees-folder”

    第一次配置Jenkins时,执行下图中出现“No such plugin: cloudbees-folder”,这时应该是服务还没起完全,稍等等就好

  2. centos7.5安装VirtualBox

    centos7.5安装minikube时要求先安装VirtualBox 1.准备repo文件 [root@localhost yum.repos.d]# pwd /etc/yum.repos.d [r ...

  3. 使用STM32的USART的同步模式Synchronous调戏SPI【usart模拟spi理论】

    [原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/7064321.html 什么东西?? 我们先来看我们平常看到SPI的时序图(呵呵,要是忘记 ...

  4. sql server 查看对象最后修改时间

    sql server 查看对象最后修改时间,根据最后修改时间排序 存储过程 SELECT * FROM sys.all_objects WHERE  TYPE='P' ORDER BY modify_ ...

  5. PHP中汉字截取

    $len = 19; $text = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?"; echo strlen($text)<=$len ? $text ...

  6. inno setup 安装前判断进程是否存在,以及停止相应进程<转>

    打包的时候遇到了这样的需求:假似用户都是傻瓜                  式操作,如果更新安装程序的时候,之前的老程序还在运行这个时候如果你去提示让用户吧老程序手动退掉也不现实. 所以当遇到这种 ...

  7. The Google File System 中文版

    摘要 我们设计并实现了Google文件系统,一个面向分布式数据密集型应用的.可伸缩的分布式文件系统.虽然运行在廉价的日用硬件设备上,但是它依然了提供容错功能,为大量客户机提供了很高的总体性能. 虽然与 ...

  8. 1.Python基础知识小结:

    Python3下载地址:https://www.python.org/downloads/windows/ python3 windows安装参考地址: https://jingyan.baidu.c ...

  9. mysql索引小结——高性能mysql

    1.索引可以包含一个或者多个列的值,如果索引包含多个列的值,列的顺序很重要,mysql只能高效地使用索引的最左列前缀列. 2.索引是在存储引擎层而非服务器层实现的. 3.B-tree索引的限制: 如果 ...

  10. cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.

    第一种方案:  将  "http://java.sun.com/xml/ns/javaee"  换为  "http://java.sun.com/xml/ns/j2ee& ...