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.

题目分析 : 一串数字,还有一串字符串,为 1 的时候当前的数字可以和它下一位去交换,然后这么想,对于一个不在当前位置的数字,如果它大于当前的位置,那么它如果想要被交换到相应的位置,那么字符串的当前位置和要交换到的位置的前一个字符就应该都是 1 ,那么前缀和去处理字符串就可以了,还有就是只需要考虑数大于当前位置的情况,大的满足,小的一定成立。

代码示例 :

const int eps = 2e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long int pre[eps];
char s[eps];
int arr[eps]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n; cin >> n;
for(int i = 1; i <= n; i++){
scanf("%d", &pre[i]);
}
scanf("%s", s+1);
for(int i = 1; i < n; i++){
if (s[i] == '1') arr[i] = 1;
else arr[i] = 0;
arr[i] += arr[i-1];
}
//for(int i = 1; i < n; i++){
//printf("%d ", arr[i]);
//}
//printf("\n");
int sign = 0;
for(int i = 1; i < n; i++){
if (pre[i] > i) {
int f = arr[pre[i]-1] - arr[i-1];
if (f != pre[i]-i) {sign = 1; break;}
}
if (sign) break;
}
if (!sign) printf("YES\n");
else printf("NO\n");
return 0;
}

cf - 920 c 求能否实现交换的更多相关文章

  1. [CSL 的魔法][求排序最少交换次数]

    链接:https://ac.nowcoder.com/acm/contest/551/E来源:牛客网题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b0,b1, ...

  2. CF 17E Palisection 求相交回文串个数

    In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...

  3. CF 920

    t1 随便乱搞 t2 随便乱搞 然后wa了三发,QAQ t3 随便乱搞 t4 邻接表+堆 对进出进行一个统计 然后时间到了...

  4. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

  5. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  6. ACM_小明滚出去?(求逆序数)

    小明滚出去? Time Limit: 2000/1000ms (Java/Others) Problem Description: 老师:“小明,写一个排序算法”: 小明: void mysort(i ...

  7. [cf 585 E] Marbles

    (一道Div2E不会,我太难了) 题意: 给你一个长度为$n$的颜色序列$A$,每次操作可以选择两个相邻元素交换,求把序列交换成“相同颜色挨在一起”所需的最少操作数. 按颜色排序:设颜色$col$在序 ...

  8. [LeetCode] 801. Minimum Swaps To Make Sequences Increasing 最少交换使得序列递增

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  9. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

随机推荐

  1. mysql 第一课 win7艰难的配置与删除注册表重新安装

    mysql 这个安装一定要慎重!慎重!慎重! 重要的事情说三遍.. 不会的一定要按步骤来~出错了真的很难受. 详细教程 -->  https://jingyan.baidu.com/album/ ...

  2. H3C RIP协议概述

  3. linux主次编号

    字符设备通过文件系统中的名子来存取. 那些名子称为文件系统的特殊文件, 或者设备文 件, 或者文件系统的简单结点; 惯例上它们位于 /dev 目录. 字符驱动的特殊文件由使用 ls -l 的输出的第一 ...

  4. 关于Character的digit,forDigit,getNumericValue方法的一点理解

    Character类是一个包装类. char这种数据类型是基于原始的Unicode编码的,储存一个char用16个bit,因此定义characters也是16位定长的实体集合. Unicode编码标准 ...

  5. java 反射实现框架功能

    框架与框架要解决的核心问题 我做房子卖给用户住,由用户自己安装门窗和空调,我做的房子就是框架,用户需要使用我的框架,把门窗插入进我提供的框架中.框架与工具类有区别,工具类被用户的类调用,而框架则是调用 ...

  6. linux平台依赖性

    每个电脑平台有其自己的特点, 内核设计者可以自由使用所有的特性来获得更好的性能. in the target object file ??? 不象应用程序开发者, 他们必须和预编译的库一起连接他们的代 ...

  7. P1038 间谍入侵

    题目描述 爱丽丝魔法王国成立10周年,于是决定矩形国庆大阅兵. 在国庆大阅兵期间,为了防止暗黑王国的间谍乔装成平民混入,需要对每一个进城的人做检测. 因为暗黑王国的人长得和爱丽丝魔法王国的人长得很像, ...

  8. javaScript通过URL获取参数

    // 函数方法 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=( ...

  9. 慕课网electron写音乐播放器教程,代码跟随教程变动(十)

    添加播放状态,首先是歌曲名称和时间 在index.html中添加 <div class="container fixed-bottom bg-white pb-4"> ...

  10. C\C++串口通信编程的一点技术记录

    新工作接的第一个活,要写一个配合设备调试的上位机程序. 除了MFC界面的部分,就是要处理几条命令. 串口通信部分代码借鉴的是这一篇文章:http://blog.sina.com.cn/s/blog_a ...