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

题意

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

思路

贪心

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

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5 + 10;
  4. int arr[maxn];
  5. int main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(0);
  8. int n;
  9. cin >> n;
  10. for (int i = 0; i < n; ++i) {
  11. cin >> arr[i];
  12. }
  13. int c = 0, cnt = 0;
  14. while (c < n) {
  15. if (arr[0] != 0) {
  16. swap(arr[0], arr[arr[0]]);
  17. cnt++;
  18. } else {
  19. for (; c < n && arr[c] == c; ++c);
  20. if (c == n) break;
  21. swap(arr[0], arr[c]);
  22. cnt++;
  23. }
  24. }
  25. cout << cnt << endl;
  26. return 0;
  27. }

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. C/C++二维数组名和二级指针

    转载 :https://blog.csdn.net/wu_nan_nan/article/details/51741030  作者:吴一奇 1. 指针1.1 一个指针包含两方面:a) 地址值:b) 所 ...

  2. Acwing.835. Trie字符串统计(模板)

    维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...

  3. IDEA中Java目录结构

    IDEA中Java的目录结构 1.首先新建Project,选择Empty,新建空的项目 2.选择Module时候,需要选择JDK,JDK只需要选择到Java Home目录就可以了 3.创建好Modul ...

  4. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. 如何在Set集合中避免重复元素

    文章翻译自 Avoiding near-duplicates in sets, 作者Paul Hudson @twostraws是一名优秀的Swifter. 这是我第一次翻译,可能有翻译不到位的地方, ...

  6. 自定义、操作cookie

    /** * 读取所有cookie * 注意二.从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交.浏览器提交Cookie时只会提交name与value属性.maxAg ...

  7. openstack stein部署手册 8. neutron-api

    # 建立数据库用户及权限 create database neutron; grant all privileges on neutron.* to neutron@'localhost' ident ...

  8. hadoop HA架构

    什么是Hadoop? http://hadoop.apache.org/ 解决问题:·海量数据的存储 (HDFS)·海量数据的分析 (MapReduce)·资源管理调度 (YARN) 集群规划:(这里 ...

  9. APKMirror - 直接下载google play里的应用

    APKMirror - Free APK Downloads - Download Free Android APKs #APKPLZ https://www.apkmirror.com/

  10. [BZOJ] 最长距离

    问题描述 windy 有一块矩形土地,被分为 NM 块 11 的小格子. 有的格子含有障碍物.如果从格子 A 可以走到格子 B,那么两个格子的距离就为两个格子中心的欧几里德距离.如果从格子 A 不可以 ...