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} ...
随机推荐
- springMVC源码学习之获取参数名
1.入口到参数处理调用流程 入口为spring-webmvc-4.3.18.RELEASE.jar中org.springframework.web.servlet.DispatcherServlet. ...
- php实现单点登录实例
1.准备两个虚拟域名 127.0.0.1 www.a.com127.0.0.1 www.b.com 2.在a的根目录下创建以下文件 1 //index.php 2 3 <?php 4 sessi ...
- shell脚本运行springboot项目jar包
#!/bin/bash APP_NAME=AutomationGuide-0.0.1-SNAPSHOT.jar #使用说明,用来提示输入参数 usage() { echo "please e ...
- 利用nginx搭建RTMP视频点播、直播、HLS服务器(转)
开发环境 Ubuntu 14.04 server nginx-1.8.1 nginx-rtmp-module nginx的服务器的搭建 安装nginx的依赖库 sudo apt-get update ...
- Windows系统封装总结
注:使用虚拟机或者实体机进行封装均可,实体机进行封装的成功率更高.虚拟机进行封装建议使用VMware,12版本.过高的版本容易造成封装失败 一. Windows 10系统封装 1 ...
- 钩子函数mounted:
1.钩子函数 钩子函数是Windows消息处理机制的一部分,通过设置“钩子”,应用程序可以在系统级对所有消息.事件进行过滤,访问在正常情况下无法访问的消息.钩子的本质是一段用以处理系统消息的程序,通过 ...
- KVM虚拟化技术(三)KVM环境预配
一.平台操作系统安装 选择合适的操作系统,此处选用CentOS 7 系统可最小化安装,也可标准安装: 如果要远程连接,建议安装VNC-Server 将防火墙配置可通信,SELINUX设为permiss ...
- JAVAWEB 一一 userweb1(原生,非servlet版)
创建数据库和表 首先,创建一个web项目 然后引入jar包 创建jsp页面 创建包 创建接口 实现类 详细内容 首先创建一个登陆页面 login.jsp <%@ page language=&q ...
- 限制ssh登录ip和系统用户
一般对于安全性要求比较高的系统,限制ssh登录的源ip地址和可以登录的系统账户是非常有必要的,ssh登录的源地址和可以登录的系统账户的设置在sshd的配置文件/etc/ssh/sshd_config中 ...
- ArcGIS案例学习笔记2_2_等高线生成DEM和三维景观动画
ArcGIS案例学习笔记2_2_等高线生成DEM和三维景观动画 计划时间:第二天下午 教程:Pdf/405 数据:ch9/ex3 方法: 1. 创建DEM SA工具箱/插值分析/地形转栅格 2. 生成 ...