C. Swap Adjacent Elements

探讨如何根据特定条件(包括允许交换的元素位置)对数组进行排序,以实现升序排列。文章提供了解决方案并附带示例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


抱歉,由于眼瞎,之前的代码被hack了,已更正。


题意:

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

思路:

根据冒泡排序可知,任何一个无序序列都可以通过相邻元素交换变成有序的。那么问题就简单了。
如果当前数字不能交换,则判断它是否等于其下标+1,;
否则判断找出这一段‘1’的右边界(‘0’或 '\0'),判断这一段区间内存储的数字是否在下标区间内。如果在区间外则输出no。将j的值赋给i。
可以定义一个minn,maxx记录区间内的最大值和最小值。事实上,为了节省时间,可以省略minn,因为已知左边界,所以在遍历数组时判断当前下标的数字是否小于左边界就可以了。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<iostream>
#include<queue>
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')
        {
            int maxx=0;
            for(j=i; ; j++)
            {
                if(a[j]<i+1)
                {
                    printf("NO\n");
                    exit(0);
                }
                maxx=max(maxx,a[j]);
                if(str[j]=='0' || str[j]==0)
                {
                    if(maxx>j+1)
                    {
                        printf("NO\n");
                        exit(0);
                    }
                    i=j;
                    break;
                }
            }
        }
        else if(a[i]!=i+1)
        {
            printf("NO\n");
            exit(0);
        }
    }
    printf("YES\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值