cf - 920 c 求能否实现交换
1 second
256 megabytes
standard input
standard output
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?
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.
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.
6
1 2 5 3 4 6
01110
YES
6
1 2 5 3 4 6
01010
NO
In the first example you may swap a3 and a4, and then swap a4 and a5.
题目分析 : 一串数字,还有一串字符串,为 1 的时候当前的数字可以和它下一位去交换,然后这么想,对于一个不在当前位置的数字,如果它大于当前的位置,那么它如果想要被交换到相应的位置,那么字符串的当前位置和要交换到的位置的前一个字符就应该都是 1 ,那么前缀和去处理字符串就可以了,还有就是只需要考虑数大于当前位置的情况,大的满足,小的一定成立。
代码示例 :
const int eps = 2e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long int pre[eps];
char s[eps];
int arr[eps]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n; cin >> n;
for(int i = 1; i <= n; i++){
scanf("%d", &pre[i]);
}
scanf("%s", s+1);
for(int i = 1; i < n; i++){
if (s[i] == '1') arr[i] = 1;
else arr[i] = 0;
arr[i] += arr[i-1];
}
//for(int i = 1; i < n; i++){
//printf("%d ", arr[i]);
//}
//printf("\n");
int sign = 0;
for(int i = 1; i < n; i++){
if (pre[i] > i) {
int f = arr[pre[i]-1] - arr[i-1];
if (f != pre[i]-i) {sign = 1; break;}
}
if (sign) break;
}
if (!sign) printf("YES\n");
else printf("NO\n");
return 0;
}
cf - 920 c 求能否实现交换的更多相关文章
- [CSL 的魔法][求排序最少交换次数]
链接:https://ac.nowcoder.com/acm/contest/551/E来源:牛客网题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b0,b1, ...
- CF 17E Palisection 求相交回文串个数
In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...
- CF 920
t1 随便乱搞 t2 随便乱搞 然后wa了三发,QAQ t3 随便乱搞 t4 邻接表+堆 对进出进行一个统计 然后时间到了...
- POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数
题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
- ACM_小明滚出去?(求逆序数)
小明滚出去? Time Limit: 2000/1000ms (Java/Others) Problem Description: 老师:“小明,写一个排序算法”: 小明: void mysort(i ...
- [cf 585 E] Marbles
(一道Div2E不会,我太难了) 题意: 给你一个长度为$n$的颜色序列$A$,每次操作可以选择两个相邻元素交换,求把序列交换成“相同颜色挨在一起”所需的最少操作数. 按颜色排序:设颜色$col$在序 ...
- [LeetCode] 801. Minimum Swaps To Make Sequences Increasing 最少交换使得序列递增
We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
随机推荐
- P1041 查找元素
题目描述 现在告诉你一个长度为 \(n\) 的有序数组 \(a_1, a_2, ..., a_n\) ,以及 \(q\) 次询问,每次询问会给你一个数 \(x\) ,对于每次询问,你需要确定在数组中是 ...
- linux 在 scull 中使用旗标
旗标机制给予 scull 一个工具, 可以在存取 scull_dev 数据结构时用来避免竞争情况. 但是正确使用这个工具是我们的责任. 正确使用加锁原语的关键是严密地指定要保护哪个 资源并且确认每个对 ...
- 5款顶尖Windows文件传输工具
5款顶尖Windows文件传输工具 英文原文: Drasko 日常工作中,公司里的系统管理员或其他岗位的员工都需要传递大量各种类型的文件和文档.其中一些可以通过 email 收发.但由于 email ...
- H3C 链路聚合的作用
- java.util.Date和jdk1.8新时间API比拼
旧的时间和日期的API的缺陷 Java 的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,而且都不是线程安全的. Date如果不格式化,打印出的日期可 ...
- 布尔&list与条件循环语句与trutle
布尔值与空值 布尔值: 一个布尔值只有True.False两种值 空值: 是python里一个特殊的值,用None表示.None不能理解为0.因为0是有意义的,而None是一个特殊值. list(列表 ...
- <QluOJ2018NewCode>约数个数
题目描述 p^q表示p的q次方,正整数M可以分解为M=(p1^a1)*(p2^a2)*(p3^a3)*……*(pn^an)的形式,其中p1,p2……pn为质数(大于1并且只能被1和自身整除的数叫做质数 ...
- 推荐:mysql锁 innodb下的记录锁,间隙锁,next-key锁
你需要知道的 之前我们介绍了排他锁,其实innodb下的记录锁(也叫行锁),间隙锁,next-key锁统统属于排他锁. 行锁 记录锁其实很好理解,对表中的记录加锁,叫做记录锁,简称行锁. 生活中的间隙 ...
- 关于MySQL中查询大数据量的情况下分页limit的性能优化
https://blog.csdn.net/weixin_37848710/article/details/80772725
- Java虚拟机-类文件结构
目录 类文件结构 Class类文件的结构 魔数与Class文件的版本 常量池 访问标志 类索引.父类索引和接口索引集合 字段表集合 方法表集合 属性表集合 完整结构描述 实例 源码 Class文件 分 ...