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 ...
随机推荐
- el-table翻页序号不从1开始(已解决)
法一:赋值方式(亲测有效) <el-table-column type="index" fixed="left" align="center&q ...
- H3C IGP与EGP
- H3C在设备上使用TFTP服务
- 关于对height:100%的研究
参考此链接: https://segmentfault.com/a/1190000012707337
- 2018.11.25 齐鲁工业大学ACM-ICPC迎新赛正式赛题解
整理人:周翔 A 约数个数(难) 解法1:苗学林 解法2:刘少瑞 解法3:刘凯 解法4:董海峥 B Alice And Bob(易) 解法1:周翔 解法2:苗学林 解法3:刘少瑞 C 黑白 ...
- codeforces 1167B Lost Numbers
传送门:https://codeforces.com/contest/1167/problem/B 题意: 交互题:现在你有6个数4, 8, 15, 16, 23, 42组成的某种组合,你可以询问系统 ...
- .Net Core解除文件上传大小限制
一共要修改两处地方: \Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc( ...
- 使用exp/imp 在oracle数据库间导数据
最近工作需要将oracle数据库的表数据导出到另一个oracle数据库表,找到了oracle 自带的命令行,并记录下导数据过程. 导数据过程分以下几步: 假设源数据库为A,目标数据库为B 1.在B上通 ...
- web应用中web.xml文件的解释
一.web.xml配置文件常用元素及其意义预览 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></di ...
- 13.python基础试题(二)
借鉴:https://www.cnblogs.com/shengyang17/p/8543712.html https://www.cnblogs.com/you-wei1/p/9693254.htm ...