C. Swap Adjacent Elements

给定一个数组和一个01字符串,表示哪些位置的元素可以与其后一个元素交换。任务是判断是否能通过这些交换使数组升序排列。可以通过遍历找到每个可交换区间的最小值和最大值,然后再次遍历判断排序可行性。
摘要由CSDN通过智能技术生成
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 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.

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.

题意:

    给你一个数字序列和01字符串,若在字符串位置i为'1',i可以和i+1交换,问你能否通过交换,使得序列有序。

思路:

    序其实本质就是大小,不难想到排序,但我们不需要真正的排序,如一段区间是可交换的,我们只需要把区间的左端点设为这个区间的最小值,右端点设为这个区间的最大值,最后在遍历一次就行。

code:

#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
#include <vector>
#include<functional>
#define INF 200005
using namespace std;
typedef long long LL;
const int N = 200005;
int arr[N], ans[N], n, l, r, flag=0,nmax=-INF,nmin=INF;
char s[N];
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	scanf("%s", s + 1);
	s[n] = '0';
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == '1')
		{
			if(flag==0)
			{
				flag = 1;
				l = i;
			}
			nmax = max(nmax, arr[i]);
			nmin = min(nmin, arr[i]);
		}
		else
		{
			if (flag)
			{
				nmax = max(nmax, arr[i]);
				nmin = min(nmin, arr[i]);
				ans[l] = nmin;
				ans[i] = nmax;
				flag = 0;
				nmax = -INF, nmin = INF;
			}
			else
			{
				ans[i] = arr[i];
			}
		}
	}
	flag = 0;
	for (int i = 1; i < n; i++)
	{
		if (ans[i] != 0 && ans[i + 1] != 0)
		{
			if (ans[i] > ans[i + 1])
			{
				flag = 1;
				break;
			}
		}
	}
	if (flag)
		cout << "NO" << endl;
	else
		cout << "YES" << endl;
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值