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 ...
随机推荐
- qt的登录设置(转)
1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...
- PHP验证是否为图片格式文件
/** * 判断是否为图片格式(jpg/jpeg/gif/png)文件 * * @param string $filePath * @return bool|string */ function is ...
- CSS3 3D旋转下拉菜单
在线演示 本地下载
- 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...
- java Web 文件上传
注意:请求实体过大的问题,请修改Nginx服务器的大小(百度参考413 Request Entity Too Large 的解决方法)jsp:<input type="file&quo ...
- showModalDialog改进版,包括Chrome下的特殊处理
父页面: if(window.ActiveXObject){ //IE $("#choose_entp").click(function(){ ...
- inline-block和同级的text-align问题
https://www.cnblogs.com/qjqcs/p/5551640.html margin:0 auto:是设置块标签在父级中居中对齐,是一种对齐方式.所以对于display:inline ...
- Solr 报错:java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody
HBase协处理器中使用Solr时报错,如下 2018-07-11 17:06:14,054 INFO [LruBlockCacheStatsExecutor] hfile.LruBlockCache ...
- Spring初学之bean的生命周期
实体Bean: Car.java: package spring.beans.cycle; public class Car { private String name; private int pr ...
- Java中的算术运算符
算术运算符主要用于进行基本的算术运算,如加法.减法.乘法.除法等. Java 中常用的算术运算符: 其中,++ 和 -- 既可以出现在操作数的左边,也可以出现在右边,但结果是不同滴 例1: 运行结果: ...