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} ...
随机推荐
- OV7670配置和调试小结
先上一下OV7670的框架图 OV7670常用寄存器设置说明 直接看OV7670 Implementation Guide (V1.0)等 资料我已经上传了 https://files.cnblogs ...
- Deque 双端队列 Stack 堆栈
1. peek 获取栈顶元素 pop 删除栈顶元素
- VC中明明已经添加了头文件却还提示未定义的问题
我在VS中编译程序遇到这个错误:error C3861: 'ReadDirectoryChangesW': identifier not found, even with argument-depen ...
- 20165304《JAVA程序设计》第四周学习总结
教材内容总结 第五章 子类与继承 1.子类声明中通常用关键字extend来定义一个子类(class 子类名 extend 父类名{}) 2.子类和父类在同一包中的继承性,继承的成员变量或方法的访问权限 ...
- JAVA版开源微信管家—JeeWx捷微3.1小程序版本发布,支持微信公众号,微信企业号,支付窗
支持小程序,JeeWx捷微3.1小程序版本发布^_^ JeeWx捷微V3.1--多触点小程序版本管理平台(支持微信公众号,微信企业号,支付窗) JeeWx捷微V3.1.0版本紧跟微信小程序更新,在原有 ...
- python-玉米(小米)商城作业
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Django--URL(路由层)
一.django 静态文件配置 在配置文件中settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR ...
- shell脚本运行springboot项目jar包
#!/bin/bash APP_NAME=AutomationGuide-0.0.1-SNAPSHOT.jar #使用说明,用来提示输入参数 usage() { echo "please e ...
- python流程控制while和if
流程控制 1.流程控制if if的4种语法 语法1: #结构形式 #if条件: # 代码1 # 代码2 # 代码3 # ... key_bak=123 key=int(input('key:')) ...
- VirtualAlloc申请进程空间
https://baike.baidu.com/item/VirtualAlloc 百度百科 https://msdn.microsoft.com/zh-cn/library/window ...