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 Nnonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (≤) 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
 #include <iostream>
using namespace std;
int m = , N, nums[], flag = , index = ;
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
cin >> nums[i]; while (index<N)
{
while (nums[] != )
{
swap(nums[], nums[nums[]]);
++m;
}
for (; index < N; ++index)//使用index,不用每次从0开始遍历
{
if (index != nums[index])
{
swap(nums[], nums[index]);
++m;
break;
}
}
}
cout << m << endl;
return ;
}

PAT甲级——A1067 Sort with Swap(0, i)的更多相关文章

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

  2. A1067. Sort with Swap(0,*)

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

  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. A1067 Sort with Swap(0, i) (25 分)

    一.技术总结 题目要求是,只能使用0,进行交换位置,然后达到按序排列,所使用的最少交换次数 输入时,用数组记录好每个数字所在的位置. 然后使用for循环,查看i当前位置是否为该数字,核心是等待0回到自 ...

  5. PAT_A1067#Sort with Swap(0, i)

    Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...

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

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

  9. Pat1067:Sort with Swap(0,*)

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

随机推荐

  1. Logstash2.3.4趟坑之集成Redis哨兵模式

    最新在使用Lostash2.3.4收集数据的时候,在读取redis数据的时候,报了如下的一个异常: 异常如下 Pipeline aborted due to error {:exception=> ...

  2. split的用法

    split用法返回的是数组 使用split('')根据空格返回数组 使用split()返回一个完整的数组 使用split("",3)返回前三项,是单个的字母 不过要注意: 使用sp ...

  3. PHP算法之盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  4. Appium 微信 webview 的自动化技术

    Appium 微信 webview 的自动化技术   最近好多人问微信webview自动化的事情, 碰巧我也在追微信webview的自动化和性能分析方法. 先发出来一点我的进展给大家参考下. 此方法用 ...

  5. Altera设置Virtual Pin

    1,GUI方式 大家都知道的,assignment editor –> category –> logic options –> to –> virtual pin –> ...

  6. iOS开发之SceneKit框架--SCNView.h

    1.SCNView 在macOS中,SCNView是NSView的子类,在iOS和tvOS中,SCNView是UIView的子类.SCNView用于显示SceneKit的3D场景,而需要设置场景的相关 ...

  7. POJ-1260-Pearls-dp+理解题意

    In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jew ...

  8. FastJson使用方法

    FastJson是阿里的一款开源框架,用来快速实现Java的序列化和反序列化. 官方地址:https://github.com/alibaba/fastjson 使用方法演示: 下载jar包,使用ID ...

  9. 02_mybatis开发dao的方法

    MyBatis开发dao的方法 1. SqlSession使用范围 1.1 SqlSessionFactoryBuilder ​ 通过SqlSessionFactoryBuilder创建会话工厂Sql ...

  10. shell脚本,循环的记录

    ######################################################################### # File Name: showlogged.sh ...