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 (≤) 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<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int pos[maxn];
int main(){
int n,ans = ;
scanf("%d",&n);
int left = n - ,num;
for(int i = ; i < n; i++){
scanf("%d",&num);
pos[num] = i;
if(num == i && num != ){
left--;
}
}
int k = ;
while(left > ){
if(pos[] == ){
while(k < n){
if(pos[k] != k){
swap(pos[],pos[k]);
ans++;
break;
}
k++;
}
}
if(pos[] != ){
swap(pos[],pos[pos[]]);
ans++;
left--;
}
}
printf("%d",ans);
return ;
}

10-排序6 Sort with Swap(0, i) (25 分)的更多相关文章

  1. PTA 10-排序6 Sort with Swap(0, i) (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

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

  3. 10-排序6 Sort with Swap(0, i) (25 分)

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

  4. 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 increasing order ...

  5. 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 increasing order ...

  6. A1067 Sort with Swap(0, i) (25 分)

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

  7. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

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

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

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

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

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

随机推荐

  1. 前端JS面试题

    题目如下: function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function ( ...

  2. js中的操作符

    写在前面 js语法 DOM对象(把body,div,p等节点树看成一个对象) BOM对象(把浏览器的地址栏历史记录DOM等装在一个对象) 浏览器是宿主,但js的宿主不限于浏览器,也可以是服务器,如no ...

  3. solr的copyFeild用法(改变各个feild的权重,修改打分结果)-注意!

    copyField的dest字段all本身有分析器处理:假设为mmseg4j name,title,description三个字段都复制到all字段上:其中title和description都是mms ...

  4. day35-hibernate映射 03-Hibernate持久态对象自动更新数据库

    持久态对象一个非常重要的能力:自动更新数据库. package cn.itcast.hibernate3.demo1; import static org.junit.Assert.*; import ...

  5. floyd路径记录

    #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #in ...

  6. Spring 第一天课程

    一. 面试题部分 1. 什么是IOC?什么是DI?他们有什么区别? 答案: IOC,即控制反转.是指将原来程序中自己创建实现类对象的控制权反转到IOC容器中. IOC的别名:依赖注入(DI),DI 是 ...

  7. Notepad++一键编译运行(Python、Java、C++)

    Python 需要事先安装Python配置好环境变量.建议使用Anaconda,方便. 在Notepad按F5,输入如下 cmd /k chdir /d $(CURRENT_DIRECTORY) &a ...

  8. C++面试笔记--面向对象

    说到面向对象,大家第一反应应该就是它的三大特性:封装性.继承性和多态性.那么我们先简单的了解一下这三大特性: (1)封装性:封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的 ...

  9. Windows下启动redis错误1067:进程意外中止

    已解决: 在redis-64.3.0.503文件夹下新建一个空文件夹,命名为logs,如下图所示: 最后成功了 开启服务:redis-server --service-start

  10. SQLServer存储引擎——04.数据

    4. SQL SERVER存储引擎之数据篇 (4.1)文件 (0)主数据文件.mdf初始文件大小至少为3MB,次要数据文件.ndf初始大小,同日志文件一样至少为512KB: (1)SQL SERVER ...