题目链接:1067 Sort with Swap(0, i) (25 分)

题意

给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次。

思路

贪心

由于是排列,所以排序后第 \(i\) 个位置上的数就是 \(i\)。所以当 \(a[0] \neq 0\) 时,把 \(a[0]\) 位置上的元素交换到相应位置。如果 \(a[0] = 0\),就找到第一个不在正确位置上的数,把它与第 \(0\) 个数交换,那么下一次又是第一种情况了,也就是下一次交换可以换到正确的位置。

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10; int arr[maxn]; int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
} int c = 0, cnt = 0;
while (c < n) {
if (arr[0] != 0) {
swap(arr[0], arr[arr[0]]);
cnt++;
} else {
for (; c < n && arr[c] == c; ++c);
if (c == n) break;
swap(arr[0], arr[c]);
cnt++;
}
}
cout << cnt << endl;
return 0;
}

PTA 1067 Sort with Swap(0, i) (贪心)的更多相关文章

  1. PTA 1067 Sort with Swap(0, i) (25 分)(思维)

    传送门:点我 Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasin ...

  2. PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法

    贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例:   0 1 2 3 4 5 ...

  3. PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

    1067 Sort with Swap(0, i) (25 分)   Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...

  4. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

  5. 1067 Sort with Swap(0, i) (25 分)

    1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...

  6. PAT 1067. Sort with Swap(0,*)

    1067. Sort with Swap(0,*) (25)   Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...

  7. PTA(Advanced Level)1067.Sort with Swap(0, i)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  8. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

  9. 1067. Sort with Swap(0,*) (25)

    时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given any permutation of the num ...

随机推荐

  1. [集合Set]HashSet、LinkedHashSet TreeSet

    Set Set是不包含重复元素的集合.更正式地,集合不包含一对元素e1和e2,使得e1.equals(e2),并且最多一个空元素. 无索引,不可以重复,无序(存取不一致) Set接口除了继承自Coll ...

  2. 模块管理常规功能自己定义系统的设计与实现(15--进一步完好"省份"模块)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jfok/article/details/24737483 "省份"模块的进一步完 ...

  3. 使用JS增加标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 25、前端知识点--webpack篇之面试考点

    前端面试之webpack篇 https://blog.csdn.net/sinat_17775997/article/details/78122999 关于webpack的面试题 随着现代前端开发的复 ...

  5. 灵活的理解JavaScript中的this指向(一)

    this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确 ...

  6. 提升JAVA代码的好“味道”

    让代码性能更高 需要 Map 的主键和取值时,应该迭代 entrySet() 当循环中只需要 Map 的主键时,迭代 keySet() 是正确的.但是,当需要主键和取值时,迭代 entrySet() ...

  7. 在a标签中使用了onclick修改样式之后a:hover失效

    是因为优先级的原因造成,使用!important修改优先级. 如修改成: .button1:hover {            color: #FFF !important;            ...

  8. Linux--shell交互输入与循环语句--06

    一.交互输入 1.命令用法:read a b c   -> aa bb cc read命令同时可以定义多个变量值:而输入的内容默认以空格为分隔符,将值输入到对应的变量中:如果默认值输入过多,最后 ...

  9. 线上nginx 平滑添加新模块;如(--with-http_realip_module)

    nginx 添加模块1.查看当前nginx信息(配置文件路径,启动用户...) ps aux | grep nginx 2.查看当前nginx已启用的模块(记录模块信息,安装路径)./nginx -V ...

  10. html中的点击事件

    使用点击事件写一个计数器. onmouseup : 当鼠标按下,松开的时候触发事件. onmousedown: 当鼠标按下的时候触发事件.onMouseOver: 鼠标经过时触发 onMouseOut ...