Swap Adjacent Elements CodeForces - 920C
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.
题意:
n个数字,为1到n这n个数字。
一个字符串s。如果s[i]为1,那么a[i]和a[i+1]可以交换任意次。
求这n个数字能否交换成升序
思路:
可以交换的部分一定可以交换成升序,主要是看不能交换的部分。你可以把能交换的部分排序,也可以判断不能交换部分是否已经是a[i] = i了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;char s[200005];
int a[200005];int main()
{int n;scanf("%d",&n);for(int i = 1;i <= n;i++)scanf("%d",&a[i]);scanf("%s",s + 1);int cnt = 0;for(int i = 1;i < n;i++){if(s[i] == '0'){sort(a + i - cnt,a + i - cnt + cnt + 1);cnt = 0;continue;}else cnt++;}sort(a + n - cnt,a + 1 + n);for(int i = 1;i < n;i++)if(a[i] > a[i + 1]){printf("NO\n");return 0;}printf("YES\n");return 0;
}
Swap Adjacent Elements CodeForces - 920C相关推荐
- 920C. Swap Adjacent Elements
You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this ...
- Replacing Elements (CodeForces - 1473A)
Replacing Elements You have an array a1,a2,-,an. All ai are positive integers. In one step you can c ...
- leetcode 777. Swap Adjacent in LR String | 777. 在LR字符串中交换相邻字符(双指针)
题目 https://leetcode.com/problems/swap-adjacent-in-lr-string/ 题解 本来以为是个带 visited 集合的 DFS,一看数据量,居然是 10 ...
- Codeforce - 920C- Swap Adjacent Elements 排序|思维
题意 输入n 再输入n个数为1~n的一种排列 再输入n-1个1或0,1表示该元素可以和后面的元素进行交换,0表示不能和后面的元素进行交换 让我们判断这个数组能否经过交换得到一个递增序列 分析 我们发现 ...
- Educational Codeforces Round 37 (Rated for Div. 2) 1
Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...
- Codeforces 题目合集+分类+代码 【Updating...】【361 in total】
961A - Tetris 模拟 ...
- Educational Codeforces Round 37 (Rated for Div. 2)
我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...
- codeforces 1367B - Even Array
B. Even Array time limit per test2 seconds memory limit per test256 megabytes inputstandard input ou ...
- [codeforces 339]D. Xenia and Bit Operations
[codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...
最新文章
- 在CentOS 7.7 x86_64上安装python3.7.7
- ASCII,Unicode和UTF-8终于找到一个能完全搞清楚的文章了
- angular 注入器配置_angular依赖注入
- matlab 把一个文件夹下的所有图片(可以包含在子文件夹下)考到指定文件夹下...
- 事实证明,OpenCV中对RGB图像数据的存储顺序是BGR,而且Scalar()的顺序也是B,G,R
- AI:Algorithmia《2020 state of enterprise machine learning—2020年企业机器学习状况》翻译与解读
- 洛谷 P3381 【模板】最小费用最大流
- PostgreSQL数据类型-枚举类型、几何类型、网络地址类型和其他数据类型
- [转]前端构建工具gulpjs的使用介绍及技巧
- DMAR(DMA remapping)与 IOMMU
- #1064 - You have an error in your SQL syntax; version for the right syntax to use near '' at line
- 优麒麟十年,聚生态之力压缩时间壁垒
- elasticsearch java对象 驼峰原则_2020年Java基础高频面试题汇总(1.4W字详细解析)...
- dos命令集--江南技术联盟
- DiskLruCache 源码分析
- 利用vcpkg编译InfluxDB-cxx库
- 使用api制作我的足迹地图
- 宏碁暗影骑士擎2022-重装系统-扬声器无声音
- postman报错500 Internal Server Error
- 联想Idealpad 710s - 13IKB 重装系统教程以及遇到的问题