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 ...
随机推荐
- H3C RIPv1的缺点
- P1033 沙茶会传染
题目描述 已知沙茶会传染,而且每一轮每一个沙茶都会传染给另外x个不是沙茶的人,让他们变成沙茶. 已知一开始人群中只有一只沙茶,请问n轮之后人群中会有多少沙茶? 输入格式 两个数 \(x(1 \le x ...
- springdata jpa使用Example快速实现动态查询
Example官方介绍 Query by Example (QBE) is a user-friendly querying technique with a simple interface. It ...
- 【t044】弗洛伊德
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 弗洛伊德是一个大牛!给一个有向图G,他有n个结点,现在请你求出对于他的每一对结点(x,y),从x出发走 ...
- 螺旋矩阵O(1)根据坐标求值
传送门 洛谷2239 •题意 从矩阵的左上角(第11行第11列)出发,初始时向右移动: 如果前方是未曾经过的格子,则继续前进,否则右转: 重复上述操作直至经过矩阵中所有格子. 根据经过顺序,在格子中依 ...
- Oracle如何分组排序并产生序号
SELECT C.ORG_SHORTNAME, B.USER_NAME, ROW_NUMBER () OVER ( PARTITION BY B.ORG_ID ORDER BY A.TOTAL_SCO ...
- ASP.NET WebForm Identity使用
环境 win10企业版x64+visual studio 2017+.net 4.5 step1 基本使用+邮件确认+密码重置 https://docs.microsoft.com/en-us/asp ...
- JMeter FTP测试计划
为了演示测试目的,我们将使用公共可用的FTP位置,可以使用它来测试文件的下载. 您可以使用市场上现有的任何可用的演示FTP位置.我们使用URL下的FTP位置: https://dlptest.com/ ...
- 超简单!pytorch入门教程(三):构造一个小型CNN
torch.nn只接受mini-batch的输入,也就是说我们输入的时候是必须是好几张图片同时输入. 例如:nn. Conv2d 允许输入4维的Tensor:n个样本 x n个色彩频道 x 高度 x ...
- 牛客国庆days赛 地铁
传送门:https://ac.nowcoder.com/acm/problem/52805 我佛了,还能跑边图啊!!! 跑边图不能用vector啦啦啦啦啦 具体也不难,就直接上代码了 #include ...