CF920C Swap Adjacent Elements 贪心】的更多相关文章

我也不知道该说啥,水就是了~ code: #include <bits/stdc++.h> #define N 300004 #define setIO(s) freopen(s".in","r",stdin) using namespace std; char S[N]; int a[N],s[N]; int main() { // setIO("input"); int i,j,n,flag=0; scanf("%d&q…
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have an array a consisting of n integers. Each integer from 1…
传送门:点我 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 pe…
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 a…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然l..r这一段连续的1可以把l..r+1变成有序的. 那么就把所有的连续1段变成有序的就好. 看看最后是不是升序即可. [代码] #include <bits/stdc++.h> using namespace std; const int N = 2e5; int n,a[N+10],b[N+10]; char s[N+10]; int main(){ #ifdef LOCAL_DEFINE freopen("…
题意 给你一个1-n的排列. 并给你一个字符串——其中用0和1表示对应数列中的位置上的值可不可以和后面相邻的数交换. 判断该数列能否在限制中交换为不降序数列. 思路 由于刚学了树状数组,一开始以为是用这样的数据结构去找有没有逆序. 事实上题目中的1~n并且每个数并不相同应该引起注意.关键就是这了. 下面是两种不同的思路: #include <cstdio> #include <iostream> #include <cstring> #include <strin…
题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. 思路 贪心 由于是排列,所以排序后第 \(i\) 个位置上的数就是 \(i\).所以当 \(a[0] \neq 0\) 时,把 \(a[0]\) 位置上的元素交换到相应位置.如果 \(a[0] = 0\),就找到第一个不在正确位置上的数,把它与第 \(0\) 个数交换,那么下一次又是第一种情况了,…
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 6 7 8 90:3 5 7 2 6 4 9 0 8 1 1:3 5 0 2 6 4 9 7 8 12:3 5 2 0 6 4 9 7 8 13:0 5 2 3 6 4 9 7 8 1 (如果0处在自己位置上,那么找一个不在自己位置上的数val与之交换)4:5 0 2 3 6 4 9 7 8 15…
传送门 看到要求"字典序最小"的方案,一个很直观的想法是按位贪心,那么我们需要check的就是当某一个数放在了第一个序列之后是否还存在方案. 假设当前两个序列的最大值和前缀最值数量分别为\(Mx_1 , Mx_2 , cnt_1 , cnt_2\),那么我们要求在剩下的数列中选出两个序列\(\{a\},\{b\}\)满足 \(Mx_1 < a_1 < a_2 < ... < a_{k_1}\) , \(Mx_2 < b_1 < b_2 < ..…
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the startin…