CodeForces - 920C 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.
Examples
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.
题意:
给定1~n的序列(所有数刚好出现一次),然后再给01串,01串中如果是1,则可以交换当前位置和下一个位置的数字。问是否能还原为递增数组。
思路:
如果当前数的01串对应是0,且前一个位置01串也是0,且当前数字与下标不对应,肯定输出NO,因为没法交换
然后对可以交换的部分记录下最大最小值,然后记录下起始位置和终点位置,比较一下就行了。
比如说第一组数据
6
1 2 5 3 4 6
01110
01串第一个是0,对应上去是1,下标也是1,则可以。
然后从2~4都是可以交换的范围(即连续的1,然后后面第一个0)
保留下最大值是4,最小值是2,起始位置2,末尾位置4。一一对应了,就输出YES。
代码:
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int read(){
char c; bool op = ;
while((c = getchar()) < '' || c > '')
if(c == '-') op = ;
int res = c - '';
while((c = getchar()) >= '' && c <= '')
res = res * + c - '';
return op ? -res : res;
}
const int maxn = 1e6+;
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i <= n ; i++)
scanf("%d",&a[i]);
int Max = ,Min = n+;
int flag = ,ardMin;
getchar();
for(int i = ; i <= n - ; i++){
char c;
scanf("%c",&c);
if(flag == && c == ''){
ardMin = i;
}
if(c == '' && flag == ){
Max = max(Max,a[i]);
Min = min(Min,a[i]);
if(ardMin!= Min || Max != i){
return puts("NO"),;
}
Max = ;Min = n+;
flag = ;
}
else if(c == ''){
Max = max(Max,a[i]);
Min = min(Min,a[i]);
flag = ;
}
else if(c == ''&& a[i] != i){
return puts("NO"),;
}
}
return puts("YES"),;
}
/*
6
1 3 2 4 6 5
01000
*/
CodeForces - 920C 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 ...
- 【Educational Codeforces Round 37 C】 Swap Adjacent Elements
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然l..r这一段连续的1可以把l..r+1变成有序的. 那么就把所有的连续1段变成有序的就好. 看看最后是不是升序即可. [代码] ...
- Swap Adjacent Elements
You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this ...
- CF920C Swap Adjacent Elements 贪心
我也不知道该说啥,水就是了~ code: #include <bits/stdc++.h> #define N 300004 #define setIO(s) freopen(s" ...
- Codeforces Gym 100203G Good elements 暴力乱搞
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...
- [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表示对应数列中的位置上的值可不可以和后面相邻的数交换. 判断该数列能否在限制中交换为不降序数列. 思路 由于刚学了树状数组,一开始以为是用 ...
- Codeforces 1215C. Swap Letters
传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix} ...
随机推荐
- 关于存session,cookie还是数据库或者memcache的优劣,部分网上抄录
从效率考虑:cookie > memcache > 数据库cookie对服务器端负载没影响,如果加密.解密会多消耗一点点cpu.带宽倒是会消耗得多一点,同域名下的所有http reques ...
- 深度学习原理与框架-卷积网络细节-经典网络架构 1.AlexNet 2.VGG
1.AlexNet是2012年最早的第一代神经网络,整个神经网络的构架是8层的网络结构.网络刚开始使用11*11获得较大的感受野,随后使用5*5和3*3做特征的提取,最后使用3个全连接层做得分值得运算 ...
- class 方法
实例对象调用class方法时返回这个实例对象的isa指针,也就是对应的类对象: 类对象调用class方法时返回这个类对象本身. (注:如果想一直获得一个类的类对象,也就是isa指针,可以调用runti ...
- UIPanGestureRecognizer translateInView, locationInView
假设在panGesture的回调事件里已经拿到了panGestureRecognizer CGPoint point = [panGestureRecognizer locationInView:se ...
- The Google File System 中文版
摘要 我们设计并实现了Google文件系统,一个面向分布式数据密集型应用的.可伸缩的分布式文件系统.虽然运行在廉价的日用硬件设备上,但是它依然了提供容错功能,为大量客户机提供了很高的总体性能. 虽然与 ...
- 剑指offer例题——二维数组中的查找
//实现一个函数,将一个字符串的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Solutio ...
- linux 内核假死循环导致的问题
[, comm: -IFileSender Tainted: G B ENX -- ZTE Grantley/S1008 [:[<ffffffff810fb2cb>] [<fffff ...
- Structs复习 ActionMethod
action在执行的是时候 可以不执行excute方法 可以由自己制定 可以在action标签里指定 也可以在方法里动态指定 推荐使用后者 jar包 web.xml <?xml version ...
- ArcGIS案例学习笔记2_2_txtexcel空间可视化和空间插值
ArcGIS案例学习笔记2_2_txt/excel空间可视化和空间插值 计划时间:第二天下午 教程:pdf page=337 数据:chapter8/ex4 方法: 1.加载xy.txt和gdp.tx ...
- centos系统vsftpd的一些配置
一.检测FTP是否安装: 右击打开终端输入 rpm –q vsftpd 显示 package vsftpd is not installed 说明没有被安装 安装时输入命令:yum install v ...