#CODEFORCESP3191. 元素交换

    ID: 912 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>dfs and similargreedymathsortingstwo pointers*1400

元素交换

Swap Adjacent Elements

题面翻译

输入格式:

第一行一个整数 n (2≤n≤200000)

第二行 n个整数 a1,a2,⋯,an(1≤ai​≤200000) 。每个 1 到 n 间的整数正好出现一次。

第三行一个字符串,只包含 0 或者 1 。如果第 i个字符是1 ,你就可以把 ai与 ai+1交换。

输出格式:

如果可以排序,输出 YES,否则输出 NO。

感谢@小粉兔 提供的翻译

题目描述

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

For some indices i i ( 1<=i<=n1 1<=i<=n-1 ) it is possible to swap i i -th element with (i+1) (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 i -th element with (i+1) (i+1) -th (if the position is not forbidden).

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

输入格式

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

The second line contains n n integers a1 a_{1} , a2 a_{2} , ..., an a_{n} ( 1<=ai<=200000 1<=a_{i}<=200000 ) — the elements of the array. Each integer from 1 1 to n n appears exactly once.

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

输出格式

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.

样例 #1

样例输入 #1

6
1 2 5 3 4 6
01110

样例输出 #1

YES

样例 #2

样例输入 #2

6
1 2 5 3 4 6
01010

样例输出 #2

NO

提示

In the first example you may swap a3 a_{3} and a4 a_{4} , and then swap a4 a_{4} and a5 a_{5} .