Swap Adjacent Elements
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
6
1 2 5 3 4 6
01110
YES
6
1 2 5 3 4 6
01010
NO
Note
In the first example you may swap a3 and a4, and then swap a4 and a5.
因为n个元素是从1都n所以正常的顺序应该是第i个元素就是i所以如果第i个元素的值是i+k那么,肯定要把i+k从i通过对换移动到i+k位置,所以需要i到i+k-1位置都可以对换,只需要判断元素比下标值大的,或者元素比下标值小的,如果其中任何一方归位了,另一方也就归位了。一开始直接for循环判断,超时了,又注意到是区间记录,用了个树状数组。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath> using namespace std;
int s[],n,d;
char str[];
int digit(int t)
{
return t&-t;
}
void update(int x,int y)
{
while(x <= n)
{
s[x] += y;
x += digit(x);
}
}
int get(int x)
{
int ans = ;
while(x > )
{
ans += s[x];
x -= digit(x);
}
return ans;
}
int check()
{
int d = ;
for(int i = ;i < n;i ++)
{
d = get(i);
if(d && str[i - ] == '')return ;
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i = ;i <= n;i ++)
{
scanf("%d",&d);
if(d > i)
{
update(i,);
update(d,-);
}
}
scanf("%s",str);
if(check())cout<<"YES";
else cout<<"NO";
}
Swap Adjacent Elements的更多相关文章
- Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...
- CodeForces - 920C Swap Adjacent Elements
传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...
- 【Educational Codeforces Round 37 C】 Swap Adjacent Elements
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然l..r这一段连续的1可以把l..r+1变成有序的. 那么就把所有的连续1段变成有序的就好. 看看最后是不是升序即可. [代码] ...
- CF920C Swap Adjacent Elements 贪心
我也不知道该说啥,水就是了~ code: #include <bits/stdc++.h> #define N 300004 #define setIO(s) freopen(s" ...
- [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...
- [LeetCode] Swap Adjacent in LR String 交换LR字符串中的相邻项
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of ...
- CF-920C-Swap Adjacent Elements 贪心
题意 给你一个1-n的排列. 并给你一个字符串——其中用0和1表示对应数列中的位置上的值可不可以和后面相邻的数交换. 判断该数列能否在限制中交换为不降序数列. 思路 由于刚学了树状数组,一开始以为是用 ...
- 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
随机推荐
- Django 进阶篇之 CSRF,COOIKE,SESSION
1.CSRF(Cross Site Request Forgery, 跨站域请求伪造) CSRF 背景与介绍 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网 ...
- 寻找最大(小)的K个数
<<编程之美>>一书中提到了寻找最大的K个数的问题,问题可以简单描述为:在长度为N的数组中,寻找第K(K<N)个最大的数.问题的解法涉及到了很多排序算法,对我们理解和运用 ...
- HTTP协议—常见的HTTP响应状态码解析
常见的HTTP响应状态码解析 1XX Informational(信息性状态码) 2XX Success(成功状态码) 3XX Redirection(重定向状态码) 4XX Client Error ...
- 创建Android守护进程(底层服务)【转】
本文转载自:https://blog.csdn.net/myfriend0/article/details/80016739 创建Android守护进程(底层服务) 前言 Android底层服务,即运 ...
- 汽车AC键到底是干什么的?老司机告诉你
现在很多人都会开车,想我当初学车的时候一会就可以上手了,开车简单,但是很多细节方面的就是得慢慢学习的过程,比如说汽车的AC键,我相信很多车主,包括老司机都不知道到底有哪些作用,只知道开空调,其实它的用 ...
- excel比较筛选两列不一样的数据
在excel表中,罗列两列数据,用B列数据与A列比较,筛选出B列中哪些数据不同,并用红色标记出来. 首先选中B列.直接鼠标左键点击B列即可选中."开始"--->&qu ...
- windows下如何查看端口占用
1.查看端口使用情况netstat -aon(以3306为例) 2.根据3306端口号查看对应的进程号(进程号就是进程的唯一标识,根据这个进程号就能找到对应的应用) 3.根据进程号查询相应的应用占用端 ...
- Spring分布式事务
[如何实现XA式.非XA式Spring分布式事务] [http://www.importnew.com/15812.html] 在JavaWorld大会上,来自SpringSource的David S ...
- Deep Learning(Ian Goodfellow) — Chapter2 Linear Algebra
线性代数是机器学习的数学基础之一,这里总结一下深度学习花书线性代数一章中机器学习主要用到的知识,并不囊括所有线性代数知识. 2.1 基础概念 Scalars: 一个数: Vctors: 一列数: Ma ...
- hdu 5241 Friends(找规律?)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...