题目信息

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

时间限制150 ms

内存限制65536 kB

代码长度限制16000 B

Given any permutation of the numbers {0, 1, 2,…, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}

Swap(0, 3) => {4, 1, 2, 3, 0}

Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (<=10^5) followed by a permutation sequence of {0, 1, …, N-1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10 3 5 7 2 6 4 9 0 8 1

Sample Output:

9

解题思路

首位为0,找出第一个未排序的数的位置进行交换

首位非0。与该数应处的位置交换

AC代码

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a, b;
int main()
{
int n, t, c = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", &t);
a.push_back(t);
}
int cnt = 0;
while (c < n){ //序列排序未结束
if (0 == a[0]){ //首位为0,找出第一个未排序的数的位置
for (; c < n && c == a[c]; ++c)
continue;
if (c >= n) break;
swap(a[0], a[c]);
++cnt;
}else{ //首位非0。与应处于的位置交换
swap(a[0], a[a[0]]);
++cnt;
}
}
printf("%d\n", cnt);
return 0;
}

个人游戏推广:

《10云方》与方块来次消除大战!

1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise的更多相关文章

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

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

  2. PTA 1067 Sort with Swap(0, i) (贪心)

    题目链接:1067 Sort with Swap(0, i) (25 分) 题意 给定长度为 \(n\) 的排列,如果每次只能把某个数和第 \(0\) 个数交换,那么要使排列是升序的最少需要交换几次. ...

  3. 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 ...

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

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

  5. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)

    只对没有归位的数进行交换. 分两种情况: 如果0在最前面,那么随便拿一个没有归位的数和0交换位置. 如果0不在最前面,那么必然可以归位一个数字,将那个数字归位. 这样模拟一下即可. #include& ...

  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. pat1067. Sort with Swap(0,*) (25)

    1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Apache + Tomcat 负载均衡 session复制

    转自:http://blog.csdn.net/cssmhyl/article/details/8455400 http://snowolf.iteye.com/blog/743611 Apache  ...

  2. 简单的Queue

    不考虑好多东西,算法考试中用得到的Queue #include<iostream> using namespace std; const int MAX = 100; struct MyQ ...

  3. 源码高速定位工具-qwandry

    https://github.com/adamsanderson/qwandry qwandry 能高速定位到我们须要找到 库文件, 项目 的工具. Ruby中实现高速定位的方法有好多种.我知道的有三 ...

  4. 婚礼上的谎言(C++实现)

    #include<iostream> using namespace std; void main(void) { int a,b,c; char DD,EE,FF; for (a=1;a ...

  5. 读取excel模板填充数据 并合并相同文本单元格

    try             { string OutFileName = "北京市国控企业污染源废气在线比对监测数据审核表" + DateTime.Now.ToString(& ...

  6. WSL初体验

    Windows10 新增加了WSL子系统, 体验了一下感觉还不错...   开启对应的功能后, 在商店里选择安装Ubuntu就可以了. 迁移文件系统 WSL 的文件系统位于 C 盘,当安装的软件越来越 ...

  7. idea设置Template

    在eclipse里面经常会用到syso和main类似这样的内容,但是idea工具里面没有,可以通过 Editor ==> Live templates  ==> 1.首先创建一个自己的Te ...

  8. 11. Container With Most Water[M]盛最多水的容器

    题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...

  9. String or binary data would be truncated 异常解决办法 .

    原因:一般出现这个问题是因为数据库中的某个字段的长度小,而插入数据大解决:修改表结构,使表字段大小相同或大于要插入的数据

  10. C# 异步编程学习(一)

    异步 编程 可在 等待 某个 任务 完成时, 避免 线程 的 占用, 但要 想 正确地 实现 编程, 仍然 十分 伤脑筋. . NET Framework 中, 有三种 不同 的 模型 来 简化 异步 ...