Swap Adjacent Elements CodeForces - 920C

Swap Adjacent Elements

CodeForces - 920C

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.

题意:

给你一串数字序列和一串0,1组成的字符串。如果数字序列中的元素所在下标在字符串中为‘1’,则可以和i+1交换。求是否可以得到递增的数字序列。

思路:

根据冒泡排序可知,任何一个无序序列都可以通过相邻元素交换变成有序的。那么问题就简单了。
如果当前数字不能交换,则判断它是否等于其下标+1,;
否则判断找出这一段‘1’的右边界(‘0’或 '\0'),判断这一段区间内存储的数字是否在下标区间内。如果在区间外则输出no。将j的值赋给i。


code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a[200005];
char str[200005];
int main(){
    scanf("%d",&n);
    memset(str,0,sizeof(str));
    for(int i = 0; i < n; i++)
        scanf("%d",&a[i]);
    scanf("%s",str);
    int i,j;
    for(i = 0; i < n; i++){
       if(str[i] == '1'){//如果是1可交换,一直往后到0第一个不可交换的数或字符串结束为止
          int maxx = 0;//记录可交换区间内最大数
          for(j = i; ; j++){
             if(a[j] < i+1){//小于左边界说明不在区间内不符合
                printf("NO\n");
                return 0;
             }
             maxx = max(maxx,a[j]);
             if(str[j] == '0' || str[j] == 0){
                if(maxx > j+1){//如果到了右边界,且区间内的最大值超出右边界不符合
                    printf("NO\n");
                    return 0;
                }
                i = j;
                break;
             }
          }
       }
       else if(a[i] != i+1){//如果是0不可交换,那么这个数必须等于下标+1,否则不对
          printf("NO\n");
          return 0;
       }
    }
    printf("YES\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值