Educational Codeforces Round 37 C. Swap Adjacent Elements 思维

C. Swap Adjacent Elements
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
6
1 2 5 3 4 6
01110
output
YES
input
Copy
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.

题意:给定一串数字,再给定一个二进制串,若第i位为1则表示第i位与第i+1位数字可以互换,且互换次数不限。问是否可以按要求将数字变换为第i位数字恰好为i。

思路:由冒泡排序的思想可知,对一串数字进行互换可以进行排序,故遍历二进制串中连续的1对应的数字[i,j],如果出现区间外的数字,则不能满足题意。

代码如下:

#include 
using namespace std;
const int maxn = 2e5+10;
int a[maxn];
char check[maxn];

int main()
{
    int n;
    while (~scanf("%d",&n)){
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        scanf("%s",check+1);
        int f=0;
        for (int i=1;i<=n;i++){
            if (check[i]=='0'){
                if (a[i]!=i){
                    f=1;
                    break;
                }
            }
            else{
                int j=i;
                while (check[j]=='1')
                    j++;
                for (int k=i;k<=j;k++){
                    if (a[k]j){
                        f=1;
                        break;
                    }
                }
                i=j;
                if (f==1)
                    break;
            }
        }
        if (f)
            printf("no\n");
        else
            printf("yes\n");
    }
    return 0;
}


你可能感兴趣的:(Educational Codeforces Round 37 C. Swap Adjacent Elements 思维)